[LyX/master] Show toolbar context menu only when clicking on the toolbar

Jean-Marc Lasgouttes lasgouttes at lyx.org
Wed Sep 16 09:06:37 UTC 2020


commit f25253f89ac121b1981fe26f0b90af9e2bbbe849
Author: Daniel Ramoeller <d.lyx at web.de>
Date:   Tue Sep 15 08:04:16 2020 +0200

    Show toolbar context menu only when clicking on the toolbar
    
    Previsouly it was on the whole application window, except on disabled toolbar
    buttons.
    
    Fix for bug #11949
---
 src/frontends/qt/GuiToolbar.cpp |   22 ++++++++++++++++++++++
 src/frontends/qt/GuiToolbar.h   |    2 ++
 src/frontends/qt/GuiView.cpp    |   12 ------------
 src/frontends/qt/GuiView.h      |    3 ---
 4 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/src/frontends/qt/GuiToolbar.cpp b/src/frontends/qt/GuiToolbar.cpp
index f58069b..e80dc48 100644
--- a/src/frontends/qt/GuiToolbar.cpp
+++ b/src/frontends/qt/GuiToolbar.cpp
@@ -34,6 +34,7 @@
 #include "LayoutBox.h"
 #include "LyX.h"
 #include "LyXRC.h"
+#include "Menus.h"
 #include "qt_helpers.h"
 #include "Session.h"
 #include "Text.h"
@@ -70,6 +71,9 @@ GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
 	setIconSize(owner.iconSize());
 	connect(&owner, SIGNAL(iconSizeChanged(QSize)), this,
 		SLOT(setIconSize(QSize)));
+	// install toolbar filter for context menu including disabled buttons
+	setContextMenuPolicy(Qt::PreventContextMenu);
+	QCoreApplication::instance()->installEventFilter(this);
 
 	// This is used by QMainWindow::restoreState for proper main window state
 	// restoration.
@@ -123,6 +127,24 @@ void GuiToolbar::showEvent(QShowEvent * ev)
 }
 
 
+bool GuiToolbar::eventFilter(QObject * obj, QEvent * ev)
+{
+	if (obj == this || obj->parent() == this) {
+		if (ev->type() == QEvent::ContextMenu) {
+			QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(ev);
+			QMenu * menu = guiApp->menus().menu(toqstr("context-toolbars"), owner_);
+			menu->exec(mouseEvent->globalPos());
+			return true;
+		} else {
+			return false;
+		}
+	} else {
+		// pass the event on to the parent class
+		return QToolBar::eventFilter(obj, ev);
+	}
+}
+
+
 void GuiToolbar::setVisibility(int visibility)
 {
 	visibility_ = visibility;
diff --git a/src/frontends/qt/GuiToolbar.h b/src/frontends/qt/GuiToolbar.h
index eccd05a..bd4ef8f 100644
--- a/src/frontends/qt/GuiToolbar.h
+++ b/src/frontends/qt/GuiToolbar.h
@@ -177,6 +177,8 @@ private:
 	void fill();
 	///
 	void showEvent(QShowEvent *);
+	///
+	bool eventFilter(QObject * obj, QEvent * ev);
 
 	///
 	QList<Action *> actions_;
diff --git a/src/frontends/qt/GuiView.cpp b/src/frontends/qt/GuiView.cpp
index 948deb9..7dd6dbb 100644
--- a/src/frontends/qt/GuiView.cpp
+++ b/src/frontends/qt/GuiView.cpp
@@ -677,11 +677,6 @@ GuiView::GuiView(int id)
 	connect(this, SIGNAL(triggerShowDialog(QString const &, QString const &, Inset *)),
 		SLOT(doShowDialog(QString const &, QString const &, Inset *)));
 
-	// set custom application bars context menu, e.g. tool bar and menu bar
-	setContextMenuPolicy(Qt::CustomContextMenu);
-	connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
-		SLOT(toolBarPopup(const QPoint &)));
-
 	// Forbid too small unresizable window because it can happen
 	// with some window manager under X11.
 	setMinimumSize(300, 200);
@@ -3788,13 +3783,6 @@ bool GuiView::goToFileRow(string const & argument)
 }
 
 
-void GuiView::toolBarPopup(const QPoint & /*pos*/)
-{
-	QMenu * menu = guiApp->menus().menu(toqstr("context-toolbars"), * this);
-	menu->exec(QCursor::pos());
-}
-
-
 template<class T>
 Buffer::ExportStatus GuiView::GuiViewPrivate::runAndDestroy(const T& func,
 		Buffer const * orig, Buffer * clone, string const & format)
diff --git a/src/frontends/qt/GuiView.h b/src/frontends/qt/GuiView.h
index d611b39..1ad534a 100644
--- a/src/frontends/qt/GuiView.h
+++ b/src/frontends/qt/GuiView.h
@@ -260,9 +260,6 @@ private Q_SLOTS:
 	void updateStatusBarMessage(QString const & str);
 	void clearMessageText();
 
-	///
-	void toolBarPopup(const QPoint &pos);
-
 private:
 	/// Open given child document in current buffer directory.
 	void openChildDocument(std::string const & filename);


More information about the lyx-cvs mailing list