[LyX/master] Use customizable zoom context menu

Juergen Spitzmueller spitz at lyx.org
Sun Mar 14 15:36:51 UTC 2021


commit 1bf53d47a5b8956497a9f7435b8c59584391bc96
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Sun Mar 14 16:41:44 2021 +0100

    Use customizable zoom context menu
    
    Based on a proposal by Daniel (#12187)
---
 lib/ui/stdcontext.inc        |    9 ++++++
 src/frontends/qt/GuiView.cpp |   57 ++++++++------------------------------
 src/frontends/qt/GuiView.h   |   24 +---------------
 src/frontends/qt/Menus.cpp   |   62 +++++++++++++++++++++++++++++++++++++++--
 4 files changed, 81 insertions(+), 71 deletions(-)

diff --git a/lib/ui/stdcontext.inc b/lib/ui/stdcontext.inc
index 115d58b..83e6d9a 100644
--- a/lib/ui/stdcontext.inc
+++ b/lib/ui/stdcontext.inc
@@ -723,4 +723,13 @@ Menuset
 		Item "Giant-sized Icons" "icon-size giant"
 	End
 
+#
+# Zoom context menu
+#
+	Menu "context-zoom"
+		ZoomOptions
+		Separator
+		Item "Show Zoom Slider|S" "ui-toggle zoomslider"
+	End
+
 End
diff --git a/src/frontends/qt/GuiView.cpp b/src/frontends/qt/GuiView.cpp
index f392efe..b34cb2d 100644
--- a/src/frontends/qt/GuiView.cpp
+++ b/src/frontends/qt/GuiView.cpp
@@ -684,28 +684,8 @@ GuiView::GuiView(int id)
 	zoom_value_->setEnabled(currentBufferView());
 	zoom_value_->setContextMenuPolicy(Qt::CustomContextMenu);
 
-	ZoomMenu * zoom_menu = new ZoomMenu(statusBar());
-	act_zoom_default_ = new QAction(toqstr(bformat(_("&Reset to default (%1$d%)"),
-						       lyxrc.defaultZoom)), this);
-	act_zoom_in_ = new QAction(qt_("Zoom &in"), this);
-	act_zoom_out_ = new QAction(qt_("Zoom &out"), this);
-	act_zoom_show_ = new QAction(qt_("Show zoom slider"), this);
-	act_zoom_show_->setCheckable(true);
-	zoom_menu->addAction(act_zoom_default_);
-	zoom_menu->addAction(act_zoom_in_);
-	zoom_menu->addAction(act_zoom_out_);
-	zoom_menu->addAction(act_zoom_show_);
-	enableZoomOptions();
-	connect(act_zoom_default_, SIGNAL(triggered()),
-			this, SLOT(resetDefaultZoom()));
-	connect(act_zoom_in_, SIGNAL(triggered()),
-			this, SLOT(zoomInPressed()));
-	connect(act_zoom_out_, SIGNAL(triggered()),
-			this, SLOT(zoomOutPressed()));
-	connect(act_zoom_show_, SIGNAL(triggered()),
-			this, SLOT(toogleZoomSlider()));
 	connect(zoom_value_, SIGNAL(customContextMenuRequested(QPoint)),
-		zoom_menu, SLOT(showMenu(QPoint)));
+		this, SLOT(showZoomContextMenu()));
 
 	int const iconheight = max(int(d.normalIconSize), fm.height());
 	QSize const iconsize(iconheight, iconheight);
@@ -804,15 +784,6 @@ void GuiView::checkCancelBackground()
 }
 
 
-void GuiView::enableZoomOptions()
-{
-	act_zoom_default_->setEnabled(zoom_slider_->value() != lyxrc.defaultZoom);
-	FuncStatus status;
-	act_zoom_in_->setEnabled(getStatus(FuncRequest(LFUN_BUFFER_ZOOM_IN), status));
-	act_zoom_out_->setEnabled(getStatus(FuncRequest(LFUN_BUFFER_ZOOM_OUT), status));
-}
-
-
 void GuiView::zoomSliderMoved(int value)
 {
 	DispatchResult dr;
@@ -826,7 +797,6 @@ void GuiView::zoomValueChanged(int value)
 {
 	if (value != lyxrc.currentZoom)
 		zoomSliderMoved(value);
-	enableZoomOptions();
 }
 
 
@@ -846,17 +816,12 @@ void GuiView::zoomOutPressed()
 }
 
 
-void GuiView::toogleZoomSlider()
+void GuiView::showZoomContextMenu()
 {
-	DispatchResult dr;
-	dispatch(FuncRequest(LFUN_UI_TOGGLE, "zoomslider"), dr);
-}
-
-
-void GuiView::resetDefaultZoom()
-{
-	zoomValueChanged(lyxrc.defaultZoom);
-	enableZoomOptions();
+	QMenu * menu = guiApp->menus().menu(toqstr("context-zoom"), * this);
+	if (!menu)
+		return;
+	menu->exec(QCursor::pos());
 }
 
 
@@ -1008,7 +973,6 @@ bool GuiView::restoreLayout()
 
 	bool const show_zoom_slider = settings.value("zoom_slider_visible", true).toBool();
 	zoom_slider_->setVisible(show_zoom_slider);
-	act_zoom_show_->setChecked(show_zoom_slider);
 	zoom_in_->setVisible(show_zoom_slider);
 	zoom_out_->setVisible(show_zoom_slider);
 
@@ -2363,7 +2327,10 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
 	case LFUN_UI_TOGGLE:
 		if (cmd.argument() == "zoomslider") {
 			enable = doc_buffer;
-			flag.setOnOff(zoom_slider_->isVisible());
+			// Test to avoid crash if called before zoom_slider_ is initialized
+			// FIXME: can probably be done better
+			if (enable)
+				flag.setOnOff(zoom_slider_->isVisible());
 		} else
 			flag.setOnOff(isFullScreen());
 		break;
@@ -2482,7 +2449,8 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
 				bformat(_("Zoom level cannot be less than %1$d%."), zoom_min_);
 			flag.message(msg);
 			enable = false;
-		}
+		} else if (cmd.argument().empty() && lyxrc.currentZoom == lyxrc.defaultZoom)
+			enable = false;
 		else
 			enable = doc_buffer;
 		break;
@@ -4870,7 +4838,6 @@ bool GuiView::lfunUiToggle(string const & ui_component)
 		zoom_slider_->setVisible(!zoom_slider_->isVisible());
 		zoom_in_->setVisible(zoom_slider_->isVisible());
 		zoom_out_->setVisible(zoom_slider_->isVisible());
-		act_zoom_show_->setChecked(zoom_slider_->isVisible());
 	} else if (ui_component == "frame") {
 		int const l = contentsMargins().left();
 
diff --git a/src/frontends/qt/GuiView.h b/src/frontends/qt/GuiView.h
index eda1f5a..47f5e40 100644
--- a/src/frontends/qt/GuiView.h
+++ b/src/frontends/qt/GuiView.h
@@ -253,9 +253,7 @@ private Q_SLOTS:
 	///
 	void zoomOutPressed();
 	///
-	void resetDefaultZoom();
-	///
-	void toogleZoomSlider();
+	void showZoomContextMenu();
 	///
 	void on_currentWorkAreaChanged(GuiWorkArea *);
 	///
@@ -480,8 +478,6 @@ private:
 	void dispatchToBufferView(FuncRequest const & cmd, DispatchResult & dr);
 	///
 	void showMessage();
-	///
-	void enableZoomOptions();
 
 	/// This view ID.
 	int id_;
@@ -513,14 +509,6 @@ private:
 	QPushButton * zoom_in_;
 	/// Zoom out ("-") Button
 	QPushButton * zoom_out_;
-	/// Set zoom to default
-	QAction * act_zoom_default_;
-	/// Zoom in menu action
-	QAction * act_zoom_in_;
-	/// Zoom out menu action
-	QAction * act_zoom_out_;
-	/// Show zoom slider
-	QAction * act_zoom_show_;
 
 	/// The rate from which the actual zoom value is calculated
 	/// from the default zoom pref
@@ -546,16 +534,6 @@ public Q_SLOTS:
 	void showMenu(QPoint const &) { exec(QCursor::pos()); }
 };
 
-class ZoomMenu : public QMenu
-{
-	Q_OBJECT
-public:
-	explicit ZoomMenu(QWidget *) {};
-
-public Q_SLOTS:
-	void showMenu(QPoint const &) { exec(QCursor::pos()); }
-};
-
 } // namespace frontend
 } // namespace lyx
 
diff --git a/src/frontends/qt/Menus.cpp b/src/frontends/qt/Menus.cpp
index 3b7bde0..8f6464f 100644
--- a/src/frontends/qt/Menus.cpp
+++ b/src/frontends/qt/Menus.cpp
@@ -197,7 +197,9 @@ public:
 		/** Commands to separate environments (context menu version). */
 		EnvironmentSeparatorsContext,
 		/** This is the list of quotation marks available */
-		SwitchQuotes
+		SwitchQuotes,
+		/** Options in the Zoom menu **/
+		ZoomOptions
 	};
 
 	explicit MenuItem(Kind kind) : kind_(kind), optional_(false) {}
@@ -374,6 +376,7 @@ public:
 	void expandCaptions(Buffer const * buf, bool switchcap = false);
 	void expandEnvironmentSeparators(BufferView const *, bool contextmenu = false);
 	void expandQuotes(BufferView const *);
+	void expandZoomOptions(BufferView const *);
 	///
 	ItemList items_;
 	///
@@ -489,7 +492,8 @@ void MenuDefinition::read(Lexer & lex)
 		md_switchcaptions,
 		md_env_separators,
 		md_env_separatorscontext,
-		md_switchquotes
+		md_switchquotes,
+		md_zoomoptions
 	};
 
 	LexerKeyword menutags[] = {
@@ -530,7 +534,8 @@ void MenuDefinition::read(Lexer & lex)
 		{ "toc", md_toc },
 		{ "toolbars", md_toolbars },
 		{ "updateformats", md_updateformats },
-		{ "viewformats", md_viewformats }
+		{ "viewformats", md_viewformats },
+		{ "zoomoptions", md_zoomoptions }
 	};
 
 	lex.pushTable(menutags);
@@ -687,6 +692,10 @@ void MenuDefinition::read(Lexer & lex)
 			add(MenuItem(MenuItem::SwitchQuotes));
 			break;
 
+		case md_zoomoptions:
+			add(MenuItem(MenuItem::ZoomOptions));
+			break;
+
 		case md_optsubmenu:
 		case md_submenu: {
 			lex.next(true);
@@ -1815,6 +1824,49 @@ void MenuDefinition::expandCaptions(Buffer const * buf, bool switchcap)
 }
 
 
+void MenuDefinition::expandZoomOptions(BufferView const * bv)
+{
+	if (!bv)
+		return;
+
+	add(MenuItem(MenuItem::Command,
+		     toqstr(bformat(_("Reset to Default (%1$d%)|R"),
+				    lyxrc.defaultZoom)),
+		     FuncRequest(LFUN_BUFFER_ZOOM)));
+	add(MenuItem(MenuItem::Separator));
+	add(MenuItem(MenuItem::Command, qt_("Zoom In|I"),
+		     FuncRequest(LFUN_BUFFER_ZOOM_IN)));
+	add(MenuItem(MenuItem::Command, qt_("Zoom Out|O"),
+		     FuncRequest(LFUN_BUFFER_ZOOM_OUT)));
+	add(MenuItem(MenuItem::Separator));
+	// Offer some fractional values of the default
+	int z = lyxrc.defaultZoom * 1.75;
+	add(MenuItem(MenuItem::Command,
+		     toqstr(bformat(_("[[ZOOM]]%1$d%"), z)),
+		     FuncRequest(LFUN_BUFFER_ZOOM, convert<string>(z))));
+	z = lyxrc.defaultZoom * 1.5;
+	add(MenuItem(MenuItem::Command,
+		     toqstr(bformat(_("[[ZOOM]]%1$d%"), z)),
+		     FuncRequest(LFUN_BUFFER_ZOOM, convert<string>(z))));
+	z = lyxrc.defaultZoom * 1.25;
+	add(MenuItem(MenuItem::Command,
+		     toqstr(bformat(_("[[ZOOM]]%1$d%"), z)),
+		     FuncRequest(LFUN_BUFFER_ZOOM, convert<string>(z))));
+	z = lyxrc.defaultZoom * 0.75;
+	add(MenuItem(MenuItem::Command,
+		     toqstr(bformat(_("[[ZOOM]]%1$d%"), z)),
+		     FuncRequest(LFUN_BUFFER_ZOOM, convert<string>(z))));
+	z = lyxrc.defaultZoom * 0.5;
+	add(MenuItem(MenuItem::Command,
+		     toqstr(bformat(_("[[ZOOM]]%1$d%"), z)),
+		     FuncRequest(LFUN_BUFFER_ZOOM, convert<string>(z))));
+	z = lyxrc.defaultZoom * 0.25;
+	add(MenuItem(MenuItem::Command,
+		     toqstr(bformat(_("[[ZOOM]]%1$d%"), z)),
+		     FuncRequest(LFUN_BUFFER_ZOOM, convert<string>(z))));
+}
+
+
 void MenuDefinition::expandQuotes(BufferView const * bv)
 {
 	if (!bv)
@@ -2432,6 +2484,10 @@ void Menus::Impl::expand(MenuDefinition const & frommenu,
 			tomenu.expandQuotes(bv);
 			break;
 
+		case MenuItem::ZoomOptions:
+			tomenu.expandZoomOptions(bv);
+			break;
+
 		case MenuItem::Submenu: {
 			MenuItem item(*cit);
 			item.setSubmenu(MenuDefinition(cit->submenuname()));


More information about the lyx-cvs mailing list