[LyX/master] GuiRefs: add keyboard actions alike GuiCitation

Juergen Spitzmueller <[email protected]> Wed, 29 Jul 2026 06:16:43 +0000
Newsgroups gmane.editors.lyx.cvs
Message-ID <[email protected]>
commit 005cc195d0936a6bd086e61d2c812c9a63ea38ac
Author: Juergen Spitzmueller <[email protected]>
Date:   Wed Jul 29 08:16:06 2026 +0200

    GuiRefs: add keyboard actions alike GuiCitation
---
 src/frontends/qt/GuiRef.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++
 src/frontends/qt/GuiRef.h   |  4 +++
 2 files changed, 92 insertions(+)

diff --git a/src/frontends/qt/GuiRef.cpp b/src/frontends/qt/GuiRef.cpp
index a69cb3e7a8..93b7a16ef8 100644
--- a/src/frontends/qt/GuiRef.cpp
+++ b/src/frontends/qt/GuiRef.cpp
@@ -147,6 +147,9 @@ GuiRef::GuiRef(GuiView & lv)
 	setFocusProxy(filter_);
 	refsTW->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
 	selectedLV->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
+
+	refsTW->installEventFilter(this);
+	selectedLV->installEventFilter(this);
 }
 
 
@@ -1050,6 +1053,91 @@ void GuiRef::resetFilter()
 }
 
 
+bool GuiRef::eventFilter(QObject * obj, QEvent * event)
+{
+	QEvent::Type etype = event->type();
+	if (obj == refsTW) {
+		if (etype == QEvent::KeyPress) {
+			QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
+			int const keyPressed = keyEvent->key();
+			Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
+			// Enter key without modifier will add current item.
+			// Ctrl-Enter will add it and close the dialog.
+			// This is designed to work both with the main enter key
+			// and the one on the numeric keypad.
+			if (keyPressed == Qt::Key_Enter || keyPressed == Qt::Key_Return) {
+				if (!keyModifiers ||
+				    keyModifiers == Qt::ControlModifier ||
+				    keyModifiers == Qt::KeypadModifier  ||
+				    keyModifiers == (Qt::ControlModifier
+						     | Qt::KeypadModifier)) {
+					if (addPB->isEnabled()) {
+						addClicked();
+					}
+					if (keyModifiers)
+						slotOK(); //signal
+				}
+				event->accept();
+				return true;
+			}
+			// Right will move to selected widget
+			else if (keyPressed == Qt::Key_Right) {
+				QModelIndex const idx = refsTW->currentIndex();
+				if (refsTW->model()->hasChildren(idx)) { // skip for headers
+					return false;
+				}
+				focusAndHighlight(selectedLV);
+				event->accept();
+				return true;
+			}
+		}
+	} else if (obj == selectedLV) {
+		if (etype == QEvent::KeyPress) {
+			QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
+			int const keyPressed = keyEvent->key();
+			Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
+			// Delete or backspace key will delete current item
+			// ... with control modifier will clear the list
+			if (keyPressed == Qt::Key_Delete || keyPressed == Qt::Key_Backspace) {
+				if (keyModifiers == Qt::NoModifier && deletePB->isEnabled()) {
+					deleteClicked();
+					updateClicked();
+				} else if (keyModifiers == Qt::ControlModifier) {
+					selectedLV->model()->removeRows(0, selectedLV->model()->rowCount());
+					updateClicked();
+				} else
+					return QObject::eventFilter(obj, event);
+			}
+			// Ctrl-Up activates upPB
+			else if (keyPressed == Qt::Key_Up) {
+				if (keyModifiers == Qt::ControlModifier) {
+					if (upPB->isEnabled())
+						upClicked();
+					event->accept();
+					return true;
+				}
+			}
+			// Ctrl-Down activates downPB
+			else if (keyPressed == Qt::Key_Down) {
+				if (keyModifiers == Qt::ControlModifier) {
+					if (downPB->isEnabled())
+						downClicked();
+					event->accept();
+					return true;
+				}
+			}
+			// Left key moves to refs widget
+			else if (keyPressed == Qt::Key_Left) {
+				focusAndHighlight(refsTW);
+				event->accept();
+				return true;
+			}
+		}
+	}
+	return QObject::eventFilter(obj, event);
+}
+
+
 bool GuiRef::initialiseParams(std::string const & sdata)
 {
 	InsetCommand::string2params(sdata, params_);
diff --git a/src/frontends/qt/GuiRef.h b/src/frontends/qt/GuiRef.h
index 631b06fac6..e0fd41d018 100644
--- a/src/frontends/qt/GuiRef.h
+++ b/src/frontends/qt/GuiRef.h
@@ -59,6 +59,10 @@ private Q_SLOTS:
 	void downClicked();
 	void typeChanged();
 
+protected Q_SLOTS:
+	///
+	bool eventFilter(QObject *, QEvent *) override;
+
 private:
 	///
 	bool isBufferDependent() const override { return true; }
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs