2 * \file qt/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"
22 #include "BufferParams.h"
23 #include "BufferView.h"
25 #include "CutAndPaste.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "GuiApplication.h"
29 #include "GuiCommandBuffer.h"
31 #include "IconPalette.h"
32 #include "InsertTableWidget.h"
34 #include "LayoutBox.h"
37 #include "qt_helpers.h"
40 #include "TextClass.h"
43 #include "insets/InsetText.h"
45 #include "support/convert.h"
46 #include "support/debug.h"
47 #include "support/docstring_list.h"
48 #include "support/gettext.h"
49 #include "support/lstrings.h"
55 #include <QToolButton>
57 #include "support/lassert.h"
60 using namespace lyx::support;
65 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
66 : QToolBar(toqstr(tbinfo.gui_name), &owner), visibility_(0),
67 owner_(owner), command_buffer_(0), tbinfo_(tbinfo), filled_(false),
70 setIconSize(owner.iconSize());
71 connect(&owner, SIGNAL(iconSizeChanged(QSize)), this,
72 SLOT(setIconSize(QSize)));
74 // This is used by QMainWindow::restoreState for proper main window state
76 setObjectName(toqstr(tbinfo.name));
81 void GuiToolbar::setVisible(bool visible)
83 // This is a hack to find out which toolbars have been restored by
84 // MainWindow::restoreState and which toolbars should be initialized
85 // by us (i.e., new toolbars)
87 QToolBar::setVisible(visible);
91 bool GuiToolbar::isRestored() const
97 void GuiToolbar::fill()
101 ToolbarInfo::item_iterator it = tbinfo_.items.begin();
102 ToolbarInfo::item_iterator end = tbinfo_.items.end();
103 for (; it != end; ++it)
109 void GuiToolbar::showEvent(QShowEvent * ev)
116 void GuiToolbar::setVisibility(int visibility)
118 visibility_ = visibility;
122 Action * GuiToolbar::addItem(ToolbarItem const & item)
124 QString text = toqstr(item.label_);
125 // Get the keys bound to this action, but keep only the
127 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(*item.func_);
128 if (!bindings.empty())
129 text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
131 Action * act = new Action(item.func_, getIcon(*item.func_, false), text,
133 actions_.append(act);
139 class PaletteButton : public QToolButton
143 ToolbarItem const & tbitem_;
146 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
147 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
149 QString const label = qt_(to_ascii(tbitem_.label_));
153 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
154 this, SLOT(setIconSize(QSize)));
156 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
158 // use the icon of first action for the toolbar button
159 setIcon(getIcon(*tbinfo->items.begin()->func_, true));
162 void mousePressEvent(QMouseEvent * e)
165 QToolButton::mousePressEvent(e);
171 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
173 LYXERR0("Unknown toolbar " << tbitem_.name_);
176 IconPalette * panel = new IconPalette(this);
177 QString const label = qt_(to_ascii(tbitem_.label_));
178 panel->setWindowTitle(label);
179 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
180 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
181 ToolbarInfo::item_iterator it = tbinfo->items.begin();
182 ToolbarInfo::item_iterator const end = tbinfo->items.end();
183 for (; it != end; ++it)
184 if (!getStatus(*it->func_).unknown())
185 panel->addButton(bar_->addItem(*it));
187 QToolButton::mousePressEvent(e);
194 MenuButtonBase::MenuButtonBase(GuiToolbar * bar, ToolbarItem const & item)
195 : QToolButton(bar), bar_(bar), tbitem_(item)
197 setPopupMode(QToolButton::InstantPopup);
198 QString const label = qt_(to_ascii(tbitem_.label_));
202 QString const name = toqstr(tbitem_.name_);
203 QStringList imagedirs;
204 imagedirs << "images/math/" << "images/";
205 for (int i = 0; i < imagedirs.size(); ++i) {
206 QString imagedir = imagedirs.at(i);
207 FileName const fname = imageLibFileSearch(imagedir, name, "svgz,png",
208 theGuiApp()->imageSearchMode());
209 if (fname.exists()) {
210 setIcon(QIcon(getPixmap(imagedir, name, "svgz,png")));
217 void MenuButtonBase::actionTriggered(QAction * action)
219 QToolButton::setDefaultAction(action);
220 setPopupMode(QToolButton::DelayedPopup);
224 StaticMenuButton::StaticMenuButton(
225 GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
226 : MenuButtonBase(bar, item)
229 connect(this, SIGNAL(triggered(QAction *)),
230 this, SLOT(actionTriggered(QAction *)));
231 connect(bar, SIGNAL(iconSizeChanged(QSize)),
232 this, SLOT(setIconSize(QSize)));
237 void StaticMenuButton::initialize()
239 QString const label = qt_(to_ascii(tbitem_.label_));
240 ButtonMenu * m = new ButtonMenu(label, this);
241 m->setWindowTitle(label);
242 m->setTearOffEnabled(true);
243 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
244 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
245 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
247 LYXERR0("Unknown toolbar " << tbitem_.name_);
250 ToolbarInfo::item_iterator it = tbinfo->items.begin();
251 ToolbarInfo::item_iterator const end = tbinfo->items.end();
252 for (; it != end; ++it)
253 if (!getStatus(*it->func_).unknown())
254 m->add(bar_->addItem(*it));
259 void StaticMenuButton::updateTriggered()
264 bool enabled = false;
265 QList<QAction *> acts = menu()->actions();
266 for (int i = 0; i < acts.size(); ++i)
267 if (acts[i]->isEnabled()) {
271 // Enable the MenuButton if at least one menu item is enabled
273 // If a disabled item is default, switch to InstantPopup
274 // (this can happen if a user selects e.g. DVI and then
275 // turns non-TeX fonts on)
276 if (defaultAction() && !defaultAction()->isEnabled())
277 setPopupMode(QToolButton::InstantPopup);
281 class DynamicMenuButton::Private
284 Private(Private const &);
285 void operator=(Private const &);
287 Private() : inset_(0) {}
289 DocumentClassConstPtr text_class_;
291 InsetText const * inset_;
295 DynamicMenuButton::DynamicMenuButton(GuiToolbar * bar, ToolbarItem const & item)
296 : MenuButtonBase(bar, item), d(new Private())
302 DynamicMenuButton::~DynamicMenuButton()
308 void DynamicMenuButton::initialize()
310 QString const label = qt_(to_ascii(tbitem_.label_));
311 ButtonMenu * m = new ButtonMenu(label, this);
312 m->setWindowTitle(label);
313 m->setTearOffEnabled(true);
314 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
315 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
316 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
317 this, SLOT(setIconSize(QSize)));
322 bool DynamicMenuButton::isMenuType(string const & s)
324 return s == "dynamic-custom-insets"
325 || s == "dynamic-char-styles"
326 || s == "textstyle-apply"
331 void DynamicMenuButton::updateTriggered()
334 // the menu should exist by this point
335 // if not, we can at least avoid crashing in release mode
337 GuiView const & owner = bar_->owner();
338 BufferView const * bv = owner.currentBufferView();
340 string const & menutype = tbitem_.name_;
341 if (menutype == "dynamic-custom-insets" || menutype == "dynamic-char-styles") {
345 setMinimumWidth(sizeHint().width());
346 d->text_class_.reset();
350 DocumentClassConstPtr text_class =
351 bv->buffer().params().documentClassPtr();
352 InsetText const * inset = &(bv->cursor().innerText()->inset());
353 // if the text class has changed, then we need to reload the menu
354 if (d->text_class_ != text_class) {
355 d->text_class_ = text_class;
356 // at the moment, we can just call loadFlexInsets, and it will
357 // handle both types. if there were more types of menus, then we
358 // might need to have other options.
361 // remember where we are
363 // note that enabling here might need to be more subtle if there
364 // were other kinds of menus.
365 setEnabled(!bv->buffer().isReadonly()
367 && inset->insetAllowed(FLEX_CODE));
368 } else if (menutype == "textstyle-apply") {
370 setPopupMode(QToolButton::MenuButtonPopup);
372 QToolButton::setIcon(getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false));
376 vector<docstring> ffList = bv->cursor().innerText()->getFreeFonts();
378 Action * default_act = nullptr;
379 for (auto const & f : ffList) {
380 FuncRequest func(LFUN_TEXTSTYLE_APPLY, convert<docstring>(i),
381 FuncRequest::TOOLBAR);
382 docstring const lb = char_type('&') + convert<docstring>(i)
383 + from_ascii(". ") + f ;
384 Action * act = new Action(func, QIcon(), toqstr(lb), toqstr(f), this);
386 // The most recent one is the default
391 // Add item to reset to defaults
392 Action * reset_act = new Action(FuncRequest(LFUN_FONT_DEFAULT, FuncRequest::TOOLBAR),
393 getIcon(FuncRequest(LFUN_UNDO), false),
394 qt_("&Reset to default"),
395 qt_("Reset all font settings to their defaults"), this);
396 m->addAction(reset_act);
398 QToolButton::setDefaultAction(default_act);
399 QToolButton::setIcon(getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false));
400 setEnabled(lyx::getStatus(FuncRequest(LFUN_TEXTSTYLE_APPLY)).enabled()
401 || lyx::getStatus(FuncRequest(LFUN_FONT_DEFAULT)).enabled());
402 } else if (menutype == "paste") {
404 setPopupMode(QToolButton::MenuButtonPopup);
405 Action * default_action = new Action(FuncRequest(LFUN_PASTE),
406 getIcon(FuncRequest(LFUN_PASTE), false),
407 qt_("Paste"), qt_("Paste"), this);
410 QToolButton::setDefaultAction(default_action);
413 docstring_list const sel = cap::availableSelections(&bv->buffer());
415 docstring_list::const_iterator cit = sel.begin();
416 docstring_list::const_iterator end = sel.end();
418 for (unsigned int index = 0; cit != end; ++cit, ++index) {
419 docstring const s = *cit;
420 FuncRequest func(LFUN_PASTE, convert<docstring>(index),
421 FuncRequest::TOOLBAR);
422 docstring const lb = char_type('&') + convert<docstring>(index)
423 + from_ascii(". ") + s ;
424 Action * act = new Action(func, QIcon(), toqstr(lb), toqstr(s), this);
427 QToolButton::setDefaultAction(default_action);
428 setEnabled(lyx::getStatus(FuncRequest(LFUN_PASTE)).enabled());
433 void DynamicMenuButton::loadFlexInsets()
437 string const & menutype = tbitem_.name_;
438 InsetLayout::InsetLyXType ftype;
439 if (menutype == "dynamic-custom-insets")
440 ftype = InsetLayout::CUSTOM;
441 else if (menutype == "dynamic-char-styles")
442 ftype = InsetLayout::CHARSTYLE;
444 // this should have been taken care of earlier
445 LASSERT(false, return);
448 TextClass::InsetLayouts const & inset_layouts =
449 d->text_class_->insetLayouts();
450 for (auto const & iit : inset_layouts) {
451 InsetLayout const & il = iit.second;
452 if (il.lyxtype() != ftype)
454 docstring const name = iit.first;
455 QString const loc_item = toqstr(translateIfPossible(
456 prefixIs(name, from_ascii("Flex:")) ?
457 name.substr(5) : name));
458 FuncRequest func(LFUN_FLEX_INSERT,
459 from_ascii("\"") + name + from_ascii("\""), FuncRequest::TOOLBAR);
461 new Action(func, getIcon(func, false), loc_item, loc_item, this);
467 void GuiToolbar::add(ToolbarItem const & item)
469 switch (item.type_) {
470 case ToolbarItem::SEPARATOR:
473 case ToolbarItem::LAYOUTS: {
474 LayoutBox * layout = owner_.getLayoutDialog();
475 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
476 layout, SLOT(setIconSize(QSize)));
477 QAction * action = addWidget(layout);
478 action->setVisible(true);
481 case ToolbarItem::MINIBUFFER:
482 command_buffer_ = new GuiCommandBuffer(&owner_);
483 addWidget(command_buffer_);
484 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
485 //setHorizontalStretchable(true);
487 case ToolbarItem::TABLEINSERT: {
488 QToolButton * tb = new QToolButton;
489 tb->setCheckable(true);
490 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
491 QString const label = qt_(to_ascii(item.label_));
492 tb->setToolTip(label);
493 tb->setStatusTip(label);
495 InsertTableWidget * iv = new InsertTableWidget(tb);
496 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
497 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
498 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
502 case ToolbarItem::ICONPALETTE:
503 addWidget(new PaletteButton(this, item));
505 case ToolbarItem::POPUPMENU: {
506 addWidget(new StaticMenuButton(this, item, false));
509 case ToolbarItem::STICKYPOPUPMENU: {
510 addWidget(new StaticMenuButton(this, item, true));
513 case ToolbarItem::DYNAMICMENU: {
514 // we only handle certain things
515 if (DynamicMenuButton::isMenuType(item.name_))
516 addWidget(new DynamicMenuButton(this, item));
518 LYXERR0("Unknown dynamic menu type: " << item.name_);
521 case ToolbarItem::COMMAND: {
522 if (!getStatus(*item.func_).unknown())
523 addAction(addItem(item));
532 void GuiToolbar::update(int context)
534 if (visibility_ & Toolbars::AUTO) {
535 setVisible(visibility_ & context & Toolbars::ALLOWAUTO);
536 if (isVisible() && commandBuffer() && (context & Toolbars::MINIBUFFER_FOCUS))
537 commandBuffer()->setFocus();
540 // update visible toolbars only
544 // This is a speed bottleneck because this is called on every keypress
545 // and update calls getStatus, which copies the cursor at least two times
546 for (int i = 0; i < actions_.size(); ++i)
547 actions_[i]->update();
549 LayoutBox * layout = owner_.getLayoutDialog();
551 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
558 QString GuiToolbar::sessionKey() const
560 return "views/" + QString::number(owner_.id()) + "/" + objectName();
564 void GuiToolbar::saveSession(QSettings & settings) const
566 settings.setValue(sessionKey() + "/visibility", visibility_);
567 settings.setValue(sessionKey() + "/movability", isMovable());
571 void GuiToolbar::restoreSession()
574 int const error_val = -1;
576 settings.value(sessionKey() + "/visibility", error_val).toInt();
577 if (visibility == error_val || visibility == 0) {
578 // This should not happen, but in case we use the defaults
579 LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
581 guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
583 setVisibility(visibility);
585 int movability = settings.value(sessionKey() + "/movability", true).toBool();
586 setMovable(movability);
590 void GuiToolbar::toggle()
593 if (visibility_ & Toolbars::ALLOWAUTO) {
594 if (!(visibility_ & Toolbars::AUTO)) {
595 visibility_ |= Toolbars::AUTO;
599 visibility_ &= ~Toolbars::AUTO;
618 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
619 qstring_to_ucs4(windowTitle()), state));
622 void GuiToolbar::movable(bool silent)
625 setMovable(!isMovable());
627 // manual update avoids bug in qt that the drag handle is not removed
628 // properly, e.g. in Windows
631 // silence for toggling of many toolbars for performance
635 state = _("movable");
637 state = _("immovable");
638 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
639 qstring_to_ucs4(windowTitle()), state));
643 } // namespace frontend
646 #include "moc_GuiToolbar.cpp"