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.
6 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
10 * \author Stefan Schimanski
11 * \author Abdelrazak Younes
13 * Full author contact details are available in file CREDITS.
18 #include "GuiToolbar.h"
21 #include "GuiApplication.h"
22 #include "GuiCommandBuffer.h"
24 #include "IconPalette.h"
25 #include "InsertTableWidget.h"
26 #include "LayoutBox.h"
27 #include "qt_helpers.h"
30 #include "FuncRequest.h"
31 #include "FuncStatus.h"
37 #include "support/debug.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
45 #include <QToolButton>
47 #include "support/lassert.h"
50 using namespace lyx::support;
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),
60 setIconSize(owner.iconSize());
61 connect(&owner, SIGNAL(iconSizeChanged(QSize)), this,
62 SLOT(setIconSize(QSize)));
64 // Toolbar dragging is allowed.
66 // This is used by QMainWindow::restoreState for proper main window state
68 setObjectName(toqstr(tbinfo.name));
73 void GuiToolbar::setVisible(bool visible)
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)
79 QToolBar::setVisible(visible);
83 bool GuiToolbar::isRestored() const
89 void GuiToolbar::fill()
93 ToolbarInfo::item_iterator it = tbinfo_.items.begin();
94 ToolbarInfo::item_iterator end = tbinfo_.items.end();
95 for (; it != end; ++it)
101 void GuiToolbar::showEvent(QShowEvent * ev)
108 void GuiToolbar::setVisibility(int visibility)
110 visibility_ = visibility;
114 Action * GuiToolbar::addItem(ToolbarItem const & item)
116 QString text = toqstr(item.label_);
117 // Get the keys bound to this action, but keep only the
119 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(item.func_);
120 if (!bindings.empty())
121 text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
123 Action * act = new Action(getIcon(item.func_, false),
124 text, item.func_, text, this);
125 actions_.append(act);
131 class PaletteButton : public QToolButton
135 ToolbarItem const & tbitem_;
138 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
139 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
141 QString const label = qt_(to_ascii(tbitem_.label_));
145 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
146 this, SLOT(setIconSize(QSize)));
148 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
150 // use the icon of first action for the toolbar button
151 setIcon(getIcon(tbinfo->items.begin()->func_, true));
154 void mousePressEvent(QMouseEvent * e)
157 QToolButton::mousePressEvent(e);
163 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
165 LYXERR0("Unknown toolbar " << tbitem_.name_);
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));
179 QToolButton::mousePressEvent(e);
186 MenuButton::MenuButton(GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
187 : QToolButton(bar), bar_(bar), tbitem_(item)
189 setPopupMode(QToolButton::InstantPopup);
190 QString const label = qt_(to_ascii(tbitem_.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, "png");
200 if (fname.exists()) {
201 setIcon(QIcon(getPixmap(imagedir, name, "png")));
206 connect(this, SIGNAL(triggered(QAction *)),
207 this, SLOT(actionTriggered(QAction *)));
208 connect(bar, SIGNAL(iconSizeChanged(QSize)),
209 this, SLOT(setIconSize(QSize)));
214 void MenuButton::initialize()
216 QString const label = qt_(to_ascii(tbitem_.label_));
217 ButtonMenu * m = new ButtonMenu(label, this);
218 m->setWindowTitle(label);
219 m->setTearOffEnabled(true);
220 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
221 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
222 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
224 LYXERR0("Unknown toolbar " << tbitem_.name_);
227 ToolbarInfo::item_iterator it = tbinfo->items.begin();
228 ToolbarInfo::item_iterator const end = tbinfo->items.end();
229 for (; it != end; ++it)
230 if (!getStatus(it->func_).unknown())
231 m->add(bar_->addItem(*it));
236 void MenuButton::actionTriggered(QAction * action)
238 QToolButton::setDefaultAction(action);
239 setPopupMode(QToolButton::DelayedPopup);
243 void MenuButton::updateTriggered()
248 bool enabled = false;
249 QList<QAction *> acts = menu()->actions();
250 for (int i = 0; i < acts.size(); ++i)
251 if (acts[i]->isEnabled()) {
255 // Enable the MenuButton if at least one menu item is enabled
257 // If a disabled item is default, switch to InstantPopup
258 // (this can happen if a user selects e.g. DVI and then
259 // turns non-TeX fonts on)
260 if (defaultAction() && !defaultAction()->isEnabled())
261 setPopupMode(QToolButton::InstantPopup);
265 void GuiToolbar::add(ToolbarItem const & item)
267 switch (item.type_) {
268 case ToolbarItem::SEPARATOR:
271 case ToolbarItem::LAYOUTS: {
272 LayoutBox * layout = owner_.getLayoutDialog();
273 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
274 layout, SLOT(setIconSize(QSize)));
275 QAction * action = addWidget(layout);
276 action->setVisible(true);
279 case ToolbarItem::MINIBUFFER:
280 command_buffer_ = new GuiCommandBuffer(&owner_);
281 addWidget(command_buffer_);
282 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
283 //setHorizontalStretchable(true);
285 case ToolbarItem::TABLEINSERT: {
286 QToolButton * tb = new QToolButton;
287 tb->setCheckable(true);
288 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
289 QString const label = qt_(to_ascii(item.label_));
290 tb->setToolTip(label);
291 tb->setStatusTip(label);
293 InsertTableWidget * iv = new InsertTableWidget(tb);
294 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
295 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
296 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
300 case ToolbarItem::ICONPALETTE:
301 addWidget(new PaletteButton(this, item));
304 case ToolbarItem::POPUPMENU: {
305 addWidget(new MenuButton(this, item, false));
308 case ToolbarItem::STICKYPOPUPMENU: {
309 addWidget(new MenuButton(this, item, true));
312 case ToolbarItem::COMMAND: {
313 if (!getStatus(item.func_).unknown())
314 addAction(addItem(item));
323 void GuiToolbar::update(bool in_math, bool in_table, bool in_review,
324 bool in_mathmacrotemplate, bool in_ipa)
326 if (visibility_ & Toolbars::AUTO) {
327 bool show_it = (in_math && (visibility_ & Toolbars::MATH))
328 || (in_table && (visibility_ & Toolbars::TABLE))
329 || (in_review && (visibility_ & Toolbars::REVIEW))
330 || (in_mathmacrotemplate && (visibility_ & Toolbars::MATHMACROTEMPLATE))
331 || (in_ipa && (visibility_ & Toolbars::IPA));
335 // update visible toolbars only
339 // This is a speed bottleneck because this is called on every keypress
340 // and update calls getStatus, which copies the cursor at least two times
341 for (int i = 0; i < actions_.size(); ++i)
342 actions_[i]->update();
344 LayoutBox * layout = owner_.getLayoutDialog();
346 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
353 QString GuiToolbar::sessionKey() const
355 return "views/" + QString::number(owner_.id()) + "/" + objectName();
359 void GuiToolbar::saveSession() const
362 settings.setValue(sessionKey() + "/visibility", visibility_);
366 void GuiToolbar::restoreSession()
369 int const error_val = -1;
371 settings.value(sessionKey() + "/visibility", error_val).toInt();
372 if (visibility == error_val || visibility == 0) {
373 // This should not happen, but in case we use the defaults
374 LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
376 guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
378 setVisibility(visibility);
382 void GuiToolbar::toggle()
385 if (visibility_ & Toolbars::ALLOWAUTO) {
386 if (!(visibility_ & Toolbars::AUTO)) {
387 visibility_ |= Toolbars::AUTO;
391 visibility_ &= ~Toolbars::AUTO;
410 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
411 qstring_to_ucs4(windowTitle()), state));
414 } // namespace frontend
417 #include "moc_GuiToolbar.cpp"