]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiToolbar.cpp
Speed up exit time
[lyx.git] / src / frontends / qt4 / GuiToolbar.cpp
1 /**
2  * \file qt4/GuiToolbar.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author Stefan Schimanski
11  * \author Abdelrazak Younes
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "GuiToolbar.h"
19
20 #include "Action.h"
21 #include "GuiApplication.h"
22 #include "GuiCommandBuffer.h"
23 #include "GuiView.h"
24 #include "IconPalette.h"
25 #include "InsertTableWidget.h"
26 #include "LayoutBox.h"
27 #include "qt_helpers.h"
28 #include "Toolbars.h"
29
30 #include "FuncRequest.h"
31 #include "FuncStatus.h"
32 #include "KeyMap.h"
33 #include "LyX.h"
34 #include "LyXRC.h"
35 #include "Session.h"
36
37 #include "support/debug.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40
41 #include <QSettings>
42 #include <QShowEvent>
43 #include <QString>
44 #include <QToolBar>
45 #include <QToolButton>
46
47 #include "support/lassert.h"
48
49 using namespace std;
50 using namespace lyx::support;
51
52 namespace lyx {
53 namespace frontend {
54
55 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
56         : QToolBar(toqstr(tbinfo.gui_name), &owner), visibility_(0),
57           owner_(owner), command_buffer_(0), tbinfo_(tbinfo), filled_(false),
58           restored_(false)
59 {
60         setIconSize(owner.iconSize());
61         connect(&owner, SIGNAL(iconSizeChanged(QSize)), this,
62                 SLOT(setIconSize(QSize)));
63
64         // Toolbar dragging is allowed.
65         setMovable(true);
66         // This is used by QMainWindow::restoreState for proper main window state
67         // restauration.
68         setObjectName(toqstr(tbinfo.name));
69         restoreSession();
70 }
71
72
73 void GuiToolbar::setVisible(bool visible)
74 {
75         // This is a hack to find out which toolbars have been restored by
76         // MainWindow::restoreState and which toolbars should be initialized 
77         // by us (i.e., new toolbars)
78         restored_ = true;
79         QToolBar::setVisible(visible);
80 }
81
82
83 bool GuiToolbar::isRestored() const
84 {
85         return restored_;
86 }
87
88
89 void GuiToolbar::fill()
90 {
91         if (filled_)
92                 return;
93         ToolbarInfo::item_iterator it = tbinfo_.items.begin();
94         ToolbarInfo::item_iterator end = tbinfo_.items.end();
95         for (; it != end; ++it)
96                 add(*it);       
97         filled_ = true;
98 }
99
100
101 void GuiToolbar::showEvent(QShowEvent * ev)
102 {
103         fill();
104         ev->accept();
105 }
106
107
108 void GuiToolbar::setVisibility(int visibility)
109 {
110         visibility_ = visibility;
111 }
112
113
114 Action * GuiToolbar::addItem(ToolbarItem const & item)
115 {
116         QString text = toqstr(item.label_);
117         // Get the keys bound to this action, but keep only the
118         // first one later
119         KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(item.func_);
120         if (!bindings.empty())
121                 text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
122
123         Action * act = new Action(getIcon(item.func_, false),
124                                   text, item.func_, text, this);
125         actions_.append(act);
126         return act;
127 }
128
129 namespace {
130
131 class PaletteButton : public QToolButton
132 {
133 private:
134         GuiToolbar * bar_;
135         ToolbarItem const & tbitem_;
136         bool initialized_;
137 public:
138         PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
139                 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
140         {
141                 QString const label = qt_(to_ascii(tbitem_.label_));
142                 setToolTip(label);
143                 setStatusTip(label);
144                 setText(label);
145                 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
146                         this, SLOT(setIconSize(QSize)));
147                 setCheckable(true);
148                 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
149                 if (tbinfo)
150                         // use the icon of first action for the toolbar button
151                         setIcon(getIcon(tbinfo->items.begin()->func_, true));
152         }
153
154         void mousePressEvent(QMouseEvent * e)
155         {
156                 if (initialized_) {
157                         QToolButton::mousePressEvent(e);
158                         return;
159                 }
160
161                 initialized_ = true;
162
163                 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
164                 if (!tbinfo) {
165                         LYXERR0("Unknown toolbar " << tbitem_.name_);
166                         return;
167                 }
168                 IconPalette * panel = new IconPalette(this);
169                 QString const label = qt_(to_ascii(tbitem_.label_));
170                 panel->setWindowTitle(label);
171                 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
172                 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
173                 ToolbarInfo::item_iterator it = tbinfo->items.begin();
174                 ToolbarInfo::item_iterator const end = tbinfo->items.end();
175                 for (; it != end; ++it)
176                         if (!getStatus(it->func_).unknown())
177                                 panel->addButton(bar_->addItem(*it));
178
179                 QToolButton::mousePressEvent(e);
180         }
181 };
182
183 }
184
185
186 MenuButton::MenuButton(GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
187         : QToolButton(bar), bar_(bar), tbitem_(item)
188 {
189         setPopupMode(QToolButton::InstantPopup);
190         QString const label = qt_(to_ascii(tbitem_.label_));
191         setToolTip(label);
192         setStatusTip(label);
193         setText(label);
194         QString const name = toqstr(tbitem_.name_);
195         QStringList imagedirs;
196         imagedirs << "images/math/" << "images/";
197         for (int i = 0; i < imagedirs.size(); ++i) {
198                 QString imagedir = imagedirs.at(i);
199                 FileName const fname = imageLibFileSearch(imagedir, name, "svgz,png",
200                         theGuiApp()->imageSearchMode());
201                 if (fname.exists()) {
202                         setIcon(QIcon(getPixmap(imagedir, name, "svgz,png")));
203                         break;
204                 }
205         }
206         if (sticky)
207                 connect(this, SIGNAL(triggered(QAction *)),
208                         this, SLOT(actionTriggered(QAction *)));
209         connect(bar, SIGNAL(iconSizeChanged(QSize)),
210                 this, SLOT(setIconSize(QSize)));
211         initialize();
212 }
213
214
215 void MenuButton::initialize()
216 {
217         QString const label = qt_(to_ascii(tbitem_.label_));
218         ButtonMenu * m = new ButtonMenu(label, this);
219         m->setWindowTitle(label);
220         m->setTearOffEnabled(true);
221         connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
222         connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
223         ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
224         if (!tbinfo) {
225                 LYXERR0("Unknown toolbar " << tbitem_.name_);
226                 return;
227         }
228         ToolbarInfo::item_iterator it = tbinfo->items.begin();
229         ToolbarInfo::item_iterator const end = tbinfo->items.end();
230         for (; it != end; ++it)
231                 if (!getStatus(it->func_).unknown())
232                         m->add(bar_->addItem(*it));
233         setMenu(m);
234 }
235
236
237 void MenuButton::actionTriggered(QAction * action)
238 {
239         QToolButton::setDefaultAction(action);
240         setPopupMode(QToolButton::DelayedPopup);
241 }
242
243
244 void MenuButton::updateTriggered()
245 {
246         if (!menu())
247                 return;
248
249         bool enabled = false;
250         QList<QAction *> acts = menu()->actions();
251         for (int i = 0; i < acts.size(); ++i)
252                 if (acts[i]->isEnabled()) {
253                         enabled = true;
254                         break;
255                 }
256         // Enable the MenuButton if at least one menu item is enabled
257         setEnabled(enabled);
258         // If a disabled item is default, switch to InstantPopup
259         // (this can happen if a user selects e.g. DVI and then
260         // turns non-TeX fonts on)
261         if (defaultAction() && !defaultAction()->isEnabled())
262                 setPopupMode(QToolButton::InstantPopup);
263 }
264
265
266 void GuiToolbar::add(ToolbarItem const & item)
267 {
268         switch (item.type_) {
269         case ToolbarItem::SEPARATOR:
270                 addSeparator();
271                 break;
272         case ToolbarItem::LAYOUTS: {
273                 LayoutBox * layout = owner_.getLayoutDialog();
274                 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
275                         layout, SLOT(setIconSize(QSize)));
276                 QAction * action = addWidget(layout);
277                 action->setVisible(true);
278                 break;
279         }
280         case ToolbarItem::MINIBUFFER:
281                 command_buffer_ = new GuiCommandBuffer(&owner_);
282                 addWidget(command_buffer_);
283                 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
284                 //setHorizontalStretchable(true);
285                 break;
286         case ToolbarItem::TABLEINSERT: {
287                 QToolButton * tb = new QToolButton;
288                 tb->setCheckable(true);
289                 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
290                 QString const label = qt_(to_ascii(item.label_));
291                 tb->setToolTip(label);
292                 tb->setStatusTip(label);
293                 tb->setText(label);
294                 InsertTableWidget * iv = new InsertTableWidget(tb);
295                 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
296                 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
297                 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
298                 addWidget(tb);
299                 break;
300                 }
301         case ToolbarItem::ICONPALETTE:
302                 addWidget(new PaletteButton(this, item));
303                 break;
304
305         case ToolbarItem::POPUPMENU: {
306                 addWidget(new MenuButton(this, item, false));
307                 break;
308                 }
309         case ToolbarItem::STICKYPOPUPMENU: {
310                 addWidget(new MenuButton(this, item, true));
311                 break;
312                 }
313         case ToolbarItem::COMMAND: {
314                 if (!getStatus(item.func_).unknown())
315                         addAction(addItem(item));
316                 break;
317                 }
318         default:
319                 break;
320         }
321 }
322
323
324 void GuiToolbar::update(int context)
325 {
326         if (visibility_ & Toolbars::AUTO) {
327                 setVisible(visibility_ & context & Toolbars::ALLOWAUTO);
328                 if (isVisible() && commandBuffer() && (context & Toolbars::MINIBUFFER_FOCUS))
329                         commandBuffer()->setFocus();
330         }
331
332         // update visible toolbars only
333         if (!isVisible())
334                 return;
335
336         // This is a speed bottleneck because this is called on every keypress
337         // and update calls getStatus, which copies the cursor at least two times
338         for (int i = 0; i < actions_.size(); ++i)
339                 actions_[i]->update();
340
341         LayoutBox * layout = owner_.getLayoutDialog();
342         if (layout)
343                 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
344
345         // emit signal
346         updated();
347 }
348
349
350 QString GuiToolbar::sessionKey() const
351 {
352         return "views/" + QString::number(owner_.id()) + "/" + objectName();
353 }
354
355
356 void GuiToolbar::saveSession(QSettings & settings) const
357 {
358         settings.setValue(sessionKey() + "/visibility", visibility_);
359 }
360
361
362 void GuiToolbar::restoreSession()
363 {
364         QSettings settings;
365         int const error_val = -1;
366         int visibility =
367                 settings.value(sessionKey() + "/visibility", error_val).toInt();
368         if (visibility == error_val || visibility == 0) {
369                 // This should not happen, but in case we use the defaults
370                 LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
371                 visibility = 
372                         guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
373         }
374         setVisibility(visibility);
375 }
376
377
378 void GuiToolbar::toggle()
379 {
380         docstring state;
381         if (visibility_ & Toolbars::ALLOWAUTO) {
382                 if (!(visibility_ & Toolbars::AUTO)) {
383                         visibility_ |= Toolbars::AUTO;
384                         hide();
385                         state = _("auto");
386                 } else {
387                         visibility_ &= ~Toolbars::AUTO;
388                         if (isVisible()) {
389                                 hide();
390                                 state = _("off");
391                         } else {
392                                 show();
393                                 state = _("on");
394                         }
395                 }
396         } else {
397                 if (isVisible()) {
398                         hide();
399                         state = _("off");
400                 } else {
401                         show();
402                         state = _("on");
403                 }
404         }
405
406         owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
407                 qstring_to_ucs4(windowTitle()), state));
408 }
409
410 } // namespace frontend
411 } // namespace lyx
412
413 #include "moc_GuiToolbar.cpp"