]> git.lyx.org Git - features.git/blobdiff - src/frontends/qt/GuiToolbar.cpp
Fix broken Apple speller interface
[features.git] / src / frontends / qt / GuiToolbar.cpp
index e80dc4887c1d6cd2e49e42dec036ec456bab024a..51699c98a8741a2f2e451464f3e501b9cda49a5a 100644 (file)
@@ -34,7 +34,6 @@
 #include "LayoutBox.h"
 #include "LyX.h"
 #include "LyXRC.h"
-#include "Menus.h"
 #include "qt_helpers.h"
 #include "Session.h"
 #include "Text.h"
@@ -63,17 +62,19 @@ using namespace lyx::support;
 namespace lyx {
 namespace frontend {
 
+QIcon DynamicMenuButton::icon_textstyle_apply_;
+QIcon DynamicMenuButton::icon_undo_;
+QIcon DynamicMenuButton::icon_paste_;
+
+
 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());
        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.
@@ -120,28 +121,18 @@ void GuiToolbar::fill()
 }
 
 
-void GuiToolbar::showEvent(QShowEvent * ev)
+void GuiToolbar::refill()
 {
+       filled_ = false;
+       clear();
        fill();
-       ev->accept();
 }
 
 
-bool GuiToolbar::eventFilter(QObject * obj, QEvent * ev)
+void GuiToolbar::showEvent(QShowEvent * 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);
-       }
+       fill();
+       ev->accept();
 }
 
 
@@ -151,17 +142,23 @@ void GuiToolbar::setVisibility(int visibility)
 }
 
 
-Action * GuiToolbar::addItem(ToolbarItem const & item)
+Action * GuiToolbar::addItem(ToolbarItem const & item, bool menu)
 {
        QString text = toqstr(item.label);
+       QString tooltip = text;
        // Get the keys bound to this action, but keep only the
        // first one later
        KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(*item.func);
-       if (!bindings.empty())
-               text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
+       if (!bindings.empty()) {
+               QString binding = toqstr(bindings.begin()->print(KeySequence::ForGui));
+               if (menu)
+                       text += '\t' + binding;
+               else
+                       tooltip += " [" + binding + "]";
+       }
 
        Action * act = new Action(item.func, getIcon(*item.func, false), text,
-                                                         text, this);
+                                 tooltip, this);
        if (item.type == ToolbarItem::BIDICOMMAND)
                act->setRtlIcon(getIcon(*item.func, false, true));
 
@@ -194,7 +191,7 @@ public:
                        setIcon(getIcon(*tbinfo->items.begin()->func, true));
        }
 
-       void mousePressEvent(QMouseEvent * e)
+       void mousePressEvent(QMouseEvent * e) override
        {
                if (initialized_) {
                        QToolButton::mousePressEvent(e);
@@ -286,7 +283,7 @@ void StaticMenuButton::initialize()
        ToolbarInfo::item_iterator const end = tbinfo->items.end();
        for (; it != end; ++it)
                if (!getStatus(*it->func).unknown())
-                       m->add(bar_->addItem(*it));
+                       m->add(bar_->addItem(*it, true));
        setMenu(m);
 }
 
@@ -298,8 +295,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;
                }
@@ -319,7 +316,7 @@ class DynamicMenuButton::Private
        Private(Private const &);
        void operator=(Private const &);
 public:
-       Private() : inset_(0) {}
+       Private() : inset_(nullptr) {}
        ///
        DocumentClassConstPtr text_class_;
        ///
@@ -363,6 +360,14 @@ bool DynamicMenuButton::isMenuType(string const & s)
 }
 
 
+void DynamicMenuButton::resetIconCache()
+{
+       icon_textstyle_apply_ = getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false);
+       icon_undo_ =  getIcon(FuncRequest(LFUN_UNDO), false);
+       icon_paste_ = getIcon(FuncRequest(LFUN_PASTE), false);
+}
+
+
 void DynamicMenuButton::updateTriggered()
 {
        QMenu * m = menu();
@@ -404,7 +409,7 @@ void DynamicMenuButton::updateTriggered()
                m->clear();
                setPopupMode(QToolButton::MenuButtonPopup);
                if (!bv) {
-                       QToolButton::setIcon(getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false));
+                       QToolButton::setIcon(icon_textstyle_apply_);
                        setEnabled(false);
                        return;
                }
@@ -425,20 +430,20 @@ void DynamicMenuButton::updateTriggered()
                }
                // Add item to reset to defaults
                Action * reset_act = new Action(FuncRequest(LFUN_FONT_DEFAULT, FuncRequest::TOOLBAR),
-                                               getIcon(FuncRequest(LFUN_UNDO), false),
+                                               icon_undo_,
                                                qt_("&Reset to default"),
                                                qt_("Reset all font settings to their defaults"), this);
                m->addAction(reset_act);
                if (default_act)
                        QToolButton::setDefaultAction(default_act);
-               QToolButton::setIcon(getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false));
+               QToolButton::setIcon(icon_textstyle_apply_);
                setEnabled(lyx::getStatus(FuncRequest(LFUN_TEXTSTYLE_APPLY)).enabled()
                           || lyx::getStatus(FuncRequest(LFUN_FONT_DEFAULT)).enabled());
        } else if (menutype == "paste") {
                m->clear();
                setPopupMode(QToolButton::MenuButtonPopup);
                Action * default_action = new Action(FuncRequest(LFUN_PASTE),
-                                                    getIcon(FuncRequest(LFUN_PASTE), false),
+                                                    icon_paste_,
                                                     qt_("Paste"), qt_("Paste"), this);
                if (!bv) {
                        setEnabled(false);
@@ -470,11 +475,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);
@@ -583,8 +588,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)
@@ -627,42 +632,57 @@ void GuiToolbar::restoreSession()
 }
 
 
-bool GuiToolbar::isVisibiltyOn() const
+bool GuiToolbar::isVisibilityOn() const
 {
        return visibility_ & Toolbars::ON;
 }
 
 
-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) && !isVisibilityOn()) {
+                       setState("auto");
+               } else {
+                       if (isVisibilityOn())
+                               setState("off");
+                       else
+                               setState("on");
+               }
+       } else {
+               if (isVisible())
+                       setState("off");
+               else
+                       setState("on");
+       }
 }
 
 void GuiToolbar::movable(bool silent)