]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt/GuiToolbar.cpp
Implement sane UI for switching tristate toolbars (#6364)
[lyx.git] / src / frontends / qt / GuiToolbar.cpp
index f58069bde3b82ab8fc25c1b9ec699b169a5f243e..5600f4a74f7212d78d6e25e3495ee00048311755 100644 (file)
@@ -64,7 +64,7 @@ namespace frontend {
 
 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
        : QToolBar(toqstr(tbinfo.gui_name), &owner), visibility_(0),
-         owner_(owner), command_buffer_(0), tbinfo_(tbinfo), filled_(false),
+         owner_(owner), command_buffer_(nullptr), tbinfo_(tbinfo), filled_(false),
          restored_(false)
 {
        setIconSize(owner.iconSize());
@@ -172,7 +172,7 @@ public:
                        setIcon(getIcon(*tbinfo->items.begin()->func, true));
        }
 
-       void mousePressEvent(QMouseEvent * e)
+       void mousePressEvent(QMouseEvent * e) override
        {
                if (initialized_) {
                        QToolButton::mousePressEvent(e);
@@ -276,8 +276,8 @@ void StaticMenuButton::updateTriggered()
 
        bool enabled = false;
        QList<QAction *> acts = menu()->actions();
-       for (int i = 0; i < acts.size(); ++i)
-               if (acts[i]->isEnabled()) {
+       for (auto const & act : acts)
+               if (act->isEnabled()) {
                        enabled = true;
                        break;
                }
@@ -297,7 +297,7 @@ class DynamicMenuButton::Private
        Private(Private const &);
        void operator=(Private const &);
 public:
-       Private() : inset_(0) {}
+       Private() : inset_(nullptr) {}
        ///
        DocumentClassConstPtr text_class_;
        ///
@@ -448,11 +448,11 @@ void DynamicMenuButton::loadFlexInsets()
        QMenu * m = menu();
        m->clear();
        string const & menutype = tbitem_.name;
-       InsetLayout::InsetLyXType ftype;
+       InsetLyXType ftype;
        if (menutype == "dynamic-custom-insets")
-               ftype = InsetLayout::CUSTOM;
+               ftype = InsetLyXType::CUSTOM;
        else if (menutype == "dynamic-char-styles")
-               ftype = InsetLayout::CHARSTYLE;
+               ftype = InsetLyXType::CHARSTYLE;
        else {
                // this should have been taken care of earlier
                LASSERT(false, return);
@@ -561,8 +561,8 @@ void GuiToolbar::update(int context)
 
        // This is a speed bottleneck because this is called on every keypress
        // and update calls getStatus, which copies the cursor at least two times
-       for (int i = 0; i < actions_.size(); ++i)
-               actions_[i]->update();
+       for (auto const & action : actions_)
+               action->update();
 
        LayoutBox * layout = owner_.getLayoutDialog();
        if (layout)
@@ -611,36 +611,51 @@ bool GuiToolbar::isVisibiltyOn() const
 }
 
 
-void GuiToolbar::toggle()
+void GuiToolbar::setState(string const state)
 {
-       docstring state;
-       if (visibility_ & Toolbars::ALLOWAUTO) {
-               if (!(visibility_ & Toolbars::AUTO)) {
+       docstring newstate;
+       if (state == "auto") {
+               if (visibility_ & Toolbars::ALLOWAUTO) {
                        visibility_ |= Toolbars::AUTO;
                        hide();
-                       state = _("auto");
-               } else {
-                       visibility_ &= ~Toolbars::AUTO;
-                       if (isVisible()) {
-                               hide();
-                               state = _("off");
-                       } else {
-                               show();
-                               state = _("on");
-                       }
-               }
+                       newstate = _("auto");
+               } else
+                       owner_.message(bformat(_("Toolbar \"%1$s\" does not support state \"auto\""),
+                               qstring_to_ucs4(windowTitle())));
        } else {
-               if (isVisible()) {
+               if (visibility_ & Toolbars::AUTO)
+                       visibility_ &= ~Toolbars::AUTO;
+               if (state == "off") {
                        hide();
-                       state = _("off");
-               } else {
+                       newstate = _("off");
+               } else if (state == "on") {
                        show();
-                       state = _("on");
+                       newstate = _("on");
                }
        }
 
        owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
-               qstring_to_ucs4(windowTitle()), state));
+               qstring_to_ucs4(windowTitle()), newstate));
+}
+
+
+void GuiToolbar::toggle()
+{
+       if (visibility_ & Toolbars::ALLOWAUTO) {
+               if (!(visibility_ & Toolbars::AUTO) && !isVisibiltyOn()) {
+                       setState("auto");
+               } else {
+                       if (isVisibiltyOn())
+                               setState("off");
+                       else
+                               setState("on");
+               }
+       } else {
+               if (isVisible())
+                       setState("off");
+               else
+                       setState("on");
+       }
 }
 
 void GuiToolbar::movable(bool silent)