]> git.lyx.org Git - features.git/commitdiff
Use customizable zoom context menu
authorJuergen Spitzmueller <spitz@lyx.org>
Sun, 14 Mar 2021 15:41:44 +0000 (16:41 +0100)
committerJuergen Spitzmueller <spitz@lyx.org>
Sun, 14 Mar 2021 15:41:44 +0000 (16:41 +0100)
Based on a proposal by Daniel (#12187)

lib/ui/stdcontext.inc
src/frontends/qt/GuiView.cpp
src/frontends/qt/GuiView.h
src/frontends/qt/Menus.cpp

index 115d58b4b6243f5b97e2f67dd0515a3c1956bdb4..83e6d9a33ba4b1a993ec24aeb6cb326e2adc5829 100644 (file)
@@ -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
index f392efe0b88fcb92b778fa84c7080eb26c963e35..b34cb2d7991484c0746231e5145b23fe46160673 100644 (file)
@@ -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();
 
index eda1f5a339feb3a0d1064e645792b564bc11c32b..47f5e4046e2a89248b60f9a90116ca23077048a0 100644 (file)
@@ -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
 
index 3b7bde076e458e0fa30a2a85d3b143a506afcf49..8f6464f569957bef295bce6f413df9ffebaf743f 100644 (file)
@@ -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()));