]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiToolbar.cpp
On Linux show in crash message box the backtrace
[lyx.git] / src / frontends / qt4 / GuiToolbar.cpp
index 80be9d3b5d36a80c1653130ee65a521dd4e30507..a80accdddc11a3cb6b04f5bd32da4d7f8e5577ee 100644 (file)
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "KeyMap.h"
-#include "LyXFunc.h"
+#include "LyX.h"
 #include "LyXRC.h"
+#include "Session.h"
 
 #include "support/debug.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
 
 #include <QSettings>
+#include <QShowEvent>
 #include <QString>
 #include <QToolBar>
 #include <QToolButton>
@@ -52,8 +54,8 @@ namespace frontend {
 
 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
        : QToolBar(toqstr(tbinfo.gui_name), &owner), visibility_(0),
-         allowauto_(false), owner_(owner), layout_(0), 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,
@@ -68,6 +70,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_)
@@ -90,7 +108,6 @@ void GuiToolbar::showEvent(QShowEvent * ev)
 void GuiToolbar::setVisibility(int visibility)
 {
        visibility_ = visibility;
-       allowauto_ = visibility_ >= Toolbars::MATH;
 }
 
 
@@ -100,11 +117,11 @@ Action * GuiToolbar::addItem(ToolbarItem const & item)
        // Get the keys bound to this action, but keep only the
        // first one later
        KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(item.func_);
-       if (bindings.size())
+       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(getIcon(item.func_, false),
+                                 text, item.func_, text, this);
        actions_.append(act);
        return act;
 }
@@ -167,35 +184,41 @@ 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_));
        setToolTip(label);
        setStatusTip(label);
        setText(label);
-       setIcon(QIcon(getPixmap("images/math/", toqstr(tbitem_.name_), "png")));
+       QString const name = toqstr(tbitem_.name_);
+       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, "png");
+               if (fname.exists()) {
+                       setIcon(QIcon(getPixmap(imagedir, name, "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_);
@@ -207,8 +230,6 @@ void MenuButton::mousePressEvent(QMouseEvent * e)
                if (!getStatus(it->func_).unknown())
                        m->add(bar_->addItem(*it));
        setMenu(m);
-
-       QToolButton::mousePressEvent(e);
 }
 
 
@@ -219,16 +240,42 @@ 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_) {
        case ToolbarItem::SEPARATOR:
                addSeparator();
                break;
-       case ToolbarItem::LAYOUTS:
-               layout_ = new LayoutBox(this, owner_);
-               addWidget(layout_);
+       case ToolbarItem::LAYOUTS: {
+               LayoutBox * layout = owner_.getLayoutDialog();
+               QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
+                       layout, SLOT(setIconSize(QSize)));
+               QAction * action = addWidget(layout);
+               action->setVisible(true);
                break;
+       }
        case ToolbarItem::MINIBUFFER:
                command_buffer_ = new GuiCommandBuffer(&owner_);
                addWidget(command_buffer_);
@@ -243,7 +290,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()));
@@ -274,13 +321,14 @@ void GuiToolbar::add(ToolbarItem const & item)
 
 
 void GuiToolbar::update(bool in_math, bool in_table, bool in_review, 
-       bool in_mathmacrotemplate)
+       bool in_mathmacrotemplate, bool in_ipa)
 {
        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));
+                       || (in_mathmacrotemplate && (visibility_ & Toolbars::MATHMACROTEMPLATE))
+                       || (in_ipa && (visibility_ & Toolbars::IPA));
                setVisible(show_it);
        }
 
@@ -293,8 +341,9 @@ void GuiToolbar::update(bool in_math, bool in_table, bool in_review,
        for (int i = 0; i < actions_.size(); ++i)
                actions_[i]->update();
 
-       if (layout_)
-               layout_->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
+       LayoutBox * layout = owner_.getLayoutDialog();
+       if (layout)
+               layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
 
        // emit signal
        updated();
@@ -317,14 +366,23 @@ void GuiToolbar::saveSession() const
 void GuiToolbar::restoreSession()
 {
        QSettings settings;
-       setVisibility(settings.value(sessionKey() + "/visibility").toInt());
+       int const error_val = -1;
+       int visibility =
+               settings.value(sessionKey() + "/visibility", error_val).toInt();
+       if (visibility == error_val || visibility == 0) {
+               // This should not happen, but in case we use the defaults
+               LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
+               visibility = 
+                       guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
+       }
+       setVisibility(visibility);
 }
 
 
 void GuiToolbar::toggle()
 {
        docstring state;
-       if (allowauto_) {
+       if (visibility_ & Toolbars::ALLOWAUTO) {
                if (!(visibility_ & Toolbars::AUTO)) {
                        visibility_ |= Toolbars::AUTO;
                        hide();