]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiToolbar.cpp
Properly track the lifetime of signals2::slots (#8261)
[lyx.git] / src / frontends / qt4 / GuiToolbar.cpp
index bea9b551b8b13d12610616572038dc673c66e7d8..bc720d5edd55f0263541832603f57307e9696265 100644 (file)
@@ -32,6 +32,7 @@
 #include "KeyMap.h"
 #include "LyX.h"
 #include "LyXRC.h"
+#include "Session.h"
 
 #include "support/debug.h"
 #include "support/gettext.h"
@@ -53,14 +54,13 @@ 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_(0), tbinfo_(tbinfo), filled_(false),
+         restored_(false)
 {
        setIconSize(owner.iconSize());
        connect(&owner, SIGNAL(iconSizeChanged(QSize)), this,
                SLOT(setIconSize(QSize)));
 
-       // Toolbar dragging is allowed.
-       setMovable(true);
        // This is used by QMainWindow::restoreState for proper main window state
        // restauration.
        setObjectName(toqstr(tbinfo.name));
@@ -68,6 +68,22 @@ GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
 }
 
 
+void GuiToolbar::setVisible(bool visible)
+{
+       // This is a hack to find out which toolbars have been restored by
+       // MainWindow::restoreState and which toolbars should be initialized 
+       // by us (i.e., new toolbars)
+       restored_ = true;
+       QToolBar::setVisible(visible);
+}
+
+
+bool GuiToolbar::isRestored() const
+{
+       return restored_;
+}
+
+
 void GuiToolbar::fill()
 {
        if (filled_)
@@ -98,12 +114,12 @@ Action * GuiToolbar::addItem(ToolbarItem const & item)
        QString text = toqstr(item.label_);
        // Get the keys bound to this action, but keep only the
        // first one later
-       KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(item.func_);
-       if (bindings.size())
+       KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(*item.func_);
+       if (!bindings.empty())
                text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
 
-       Action * act = new Action(&owner_, getIcon(item.func_, false),
-               text, item.func_, text, this);
+       Action * act = new Action(item.func_, getIcon(*item.func_, false), text,
+                                 text, this);
        actions_.append(act);
        return act;
 }
@@ -130,7 +146,7 @@ public:
                ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
                if (tbinfo)
                        // use the icon of first action for the toolbar button
-                       setIcon(getIcon(tbinfo->items.begin()->func_, true));
+                       setIcon(getIcon(*tbinfo->items.begin()->func_, true));
        }
 
        void mousePressEvent(QMouseEvent * e)
@@ -155,7 +171,7 @@ public:
                ToolbarInfo::item_iterator it = tbinfo->items.begin();
                ToolbarInfo::item_iterator const end = tbinfo->items.end();
                for (; it != end; ++it)
-                       if (!getStatus(it->func_).unknown())
+                       if (!getStatus(*it->func_).unknown())
                                panel->addButton(bar_->addItem(*it));
 
                QToolButton::mousePressEvent(e);
@@ -166,7 +182,7 @@ public:
 
 
 MenuButton::MenuButton(GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
-       : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
+       : QToolButton(bar), bar_(bar), tbitem_(item)
 {
        setPopupMode(QToolButton::InstantPopup);
        QString const label = qt_(to_ascii(tbitem_.label_));
@@ -174,32 +190,34 @@ MenuButton::MenuButton(GuiToolbar * bar, ToolbarItem const & item, bool const st
        setStatusTip(label);
        setText(label);
        QString const name = toqstr(tbitem_.name_);
-       FileName const fname = libFileSearch("images/math/", name, "png");
-       if (fname.exists())
-           setIcon(QIcon(getPixmap("images/math/", name, "png")));
-       else
-           setIcon(QIcon(getPixmap("images/", name, "png")));
+       QStringList imagedirs;
+       imagedirs << "images/math/" << "images/";
+       for (int i = 0; i < imagedirs.size(); ++i) {
+               QString imagedir = imagedirs.at(i);
+               FileName const fname = imageLibFileSearch(imagedir, name, "svgz,png",
+                       theGuiApp()->imageSearchMode());
+               if (fname.exists()) {
+                       setIcon(QIcon(getPixmap(imagedir, name, "svgz,png")));
+                       break;
+               }
+       }
        if (sticky)
                connect(this, SIGNAL(triggered(QAction *)),
                        this, SLOT(actionTriggered(QAction *)));
        connect(bar, SIGNAL(iconSizeChanged(QSize)),
                this, SLOT(setIconSize(QSize)));
+       initialize();
 }
 
-void MenuButton::mousePressEvent(QMouseEvent * e)
-{
-       if (initialized_) {
-               QToolButton::mousePressEvent(e);
-               return;
-       }
-
-       initialized_ = true;
 
+void MenuButton::initialize()
+{
        QString const label = qt_(to_ascii(tbitem_.label_));
        ButtonMenu * m = new ButtonMenu(label, this);
        m->setWindowTitle(label);
        m->setTearOffEnabled(true);
        connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
+       connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
        ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
        if (!tbinfo) {
                LYXERR0("Unknown toolbar " << tbitem_.name_);
@@ -208,11 +226,9 @@ void MenuButton::mousePressEvent(QMouseEvent * e)
        ToolbarInfo::item_iterator it = tbinfo->items.begin();
        ToolbarInfo::item_iterator const end = tbinfo->items.end();
        for (; it != end; ++it)
-               if (!getStatus(it->func_).unknown())
+               if (!getStatus(*it->func_).unknown())
                        m->add(bar_->addItem(*it));
        setMenu(m);
-
-       QToolButton::mousePressEvent(e);
 }
 
 
@@ -223,6 +239,28 @@ void MenuButton::actionTriggered(QAction * action)
 }
 
 
+void MenuButton::updateTriggered()
+{
+       if (!menu())
+               return;
+
+       bool enabled = false;
+       QList<QAction *> acts = menu()->actions();
+       for (int i = 0; i < acts.size(); ++i)
+               if (acts[i]->isEnabled()) {
+                       enabled = true;
+                       break;
+               }
+       // Enable the MenuButton if at least one menu item is enabled
+       setEnabled(enabled);
+       // If a disabled item is default, switch to InstantPopup
+       // (this can happen if a user selects e.g. DVI and then
+       // turns non-TeX fonts on)
+       if (defaultAction() && !defaultAction()->isEnabled())
+               setPopupMode(QToolButton::InstantPopup);
+}
+
+
 void GuiToolbar::add(ToolbarItem const & item)
 {
        switch (item.type_) {
@@ -251,7 +289,7 @@ void GuiToolbar::add(ToolbarItem const & item)
                tb->setToolTip(label);
                tb->setStatusTip(label);
                tb->setText(label);
-               InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
+               InsertTableWidget * iv = new InsertTableWidget(tb);
                connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
                connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
                connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
@@ -271,7 +309,7 @@ void GuiToolbar::add(ToolbarItem const & item)
                break;
                }
        case ToolbarItem::COMMAND: {
-               if (!getStatus(item.func_).unknown())
+               if (!getStatus(*item.func_).unknown())
                        addAction(addItem(item));
                break;
                }
@@ -281,15 +319,12 @@ void GuiToolbar::add(ToolbarItem const & item)
 }
 
 
-void GuiToolbar::update(bool in_math, bool in_table, bool in_review, 
-       bool in_mathmacrotemplate)
+void GuiToolbar::update(int context)
 {
        if (visibility_ & Toolbars::AUTO) {
-               bool show_it = (in_math && (visibility_ & Toolbars::MATH))
-                       || (in_table && (visibility_ & Toolbars::TABLE))
-                       || (in_review && (visibility_ & Toolbars::REVIEW))
-                       || (in_mathmacrotemplate && (visibility_ & Toolbars::MATHMACROTEMPLATE));
-               setVisible(show_it);
+               setVisible(visibility_ & context & Toolbars::ALLOWAUTO);
+               if (isVisible() && commandBuffer() && (context & Toolbars::MINIBUFFER_FOCUS))
+                       commandBuffer()->setFocus();
        }
 
        // update visible toolbars only
@@ -320,6 +355,7 @@ void GuiToolbar::saveSession() const
 {
        QSettings settings;
        settings.setValue(sessionKey() + "/visibility", visibility_);
+       settings.setValue(sessionKey() + "/movability", isMovable());
 }
 
 
@@ -331,12 +367,14 @@ void GuiToolbar::restoreSession()
                settings.value(sessionKey() + "/visibility", error_val).toInt();
        if (visibility == error_val || visibility == 0) {
                // This should not happen, but in case we use the defaults
-               LYXERR0("Session settings could not be found!. "
-                       "Defaults are used instead");
+               LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
                visibility = 
                        guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
        }
        setVisibility(visibility);
+
+       int movability = settings.value(sessionKey() + "/movability", true).toBool();
+       setMovable(movability);
 }
 
 
@@ -372,6 +410,27 @@ void GuiToolbar::toggle()
                qstring_to_ucs4(windowTitle()), state));
 }
 
+void GuiToolbar::movable(bool silent)
+{
+       // toggle movability
+       setMovable(!isMovable());
+
+       // manual update avoids bug in qt that the drag handle is not removed
+       // properly, e.g. in Windows
+       Q_EMIT update();
+
+       // silence for toggling of many toolbars for performance
+       if (!silent) {
+               docstring state;
+               if (isMovable())
+                       state = _("movable");
+               else
+                       state = _("immovable");
+               owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
+                       qstring_to_ucs4(windowTitle()), state));
+       }
+}
+
 } // namespace frontend
 } // namespace lyx