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 QIcon DynamicMenuButton::icon_textstyle_apply_;
66 QIcon DynamicMenuButton::icon_undo_;
67 QIcon DynamicMenuButton::icon_paste_;
70 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
71 : QToolBar(toqstr(tbinfo.gui_name), &owner), visibility_(0),
72 owner_(owner), command_buffer_(nullptr), tbinfo_(tbinfo), filled_(false),
75 setIconSize(owner.iconSize());
76 connect(&owner, SIGNAL(iconSizeChanged(QSize)), this,
77 SLOT(setIconSize(QSize)));
79 // This is used by QMainWindow::restoreState for proper main window state
81 setObjectName(toqstr(tbinfo.name));
86 void GuiToolbar::setVisible(bool visible)
88 // This is a hack to find out which toolbars have been restored by
89 // MainWindow::restoreState and which toolbars should be initialized
90 // by us (i.e., new toolbars)
92 // Record the actual visibility in toolbar state visibility_.
93 // This is useful to restore the visibility of toolbars when
94 // returning from full-screen. Therefore the recording is disabled
95 // while LyX is in full-screen state.
96 if (!owner_.isFullScreen()) {
98 visibility_ |= Toolbars::ON;
100 visibility_ &= ~Toolbars::ON;
102 QToolBar::setVisible(visible);
106 bool GuiToolbar::isRestored() const
112 void GuiToolbar::fill()
116 ToolbarInfo::item_iterator it = tbinfo_.items.begin();
117 ToolbarInfo::item_iterator end = tbinfo_.items.end();
118 for (; it != end; ++it)
124 void GuiToolbar::refill()
132 void GuiToolbar::showEvent(QShowEvent * ev)
139 void GuiToolbar::setVisibility(int visibility)
141 visibility_ = visibility;
145 Action * GuiToolbar::addItem(ToolbarItem const & item, bool menu)
147 QString text = toqstr(item.label);
148 QString tooltip = text;
149 // Get the keys bound to this action, but keep only the
151 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(*item.func);
152 if (!bindings.empty()) {
153 QString binding = toqstr(bindings.begin()->print(KeySequence::ForGui));
155 text += '\t' + binding;
157 tooltip += " [" + binding + "]";
160 Action * act = new Action(item.func, getIcon(*item.func, false), text,
162 if (item.type == ToolbarItem::BIDICOMMAND)
163 act->setRtlIcon(getIcon(*item.func, false, true));
165 actions_.append(act);
171 class PaletteButton : public QToolButton
175 ToolbarItem const & tbitem_;
178 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
179 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
181 QString const label = qt_(to_ascii(tbitem_.label));
185 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
186 this, SLOT(setIconSize(QSize)));
188 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name);
190 // use the icon of first action for the toolbar button
191 setIcon(getIcon(*tbinfo->items.begin()->func, true));
194 void mousePressEvent(QMouseEvent * e) override
197 QToolButton::mousePressEvent(e);
203 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name);
205 LYXERR0("Unknown toolbar " << tbitem_.name);
208 IconPalette * panel = new IconPalette(this);
209 QString const label = qt_(to_ascii(tbitem_.label));
210 panel->setWindowTitle(label);
211 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
212 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
213 ToolbarInfo::item_iterator it = tbinfo->items.begin();
214 ToolbarInfo::item_iterator const end = tbinfo->items.end();
215 for (; it != end; ++it)
216 if (!getStatus(*it->func).unknown())
217 panel->addButton(bar_->addItem(*it));
219 QToolButton::mousePressEvent(e);
226 MenuButtonBase::MenuButtonBase(GuiToolbar * bar, ToolbarItem const & item)
227 : QToolButton(bar), bar_(bar), tbitem_(item)
229 setPopupMode(QToolButton::InstantPopup);
230 QString const label = qt_(to_ascii(tbitem_.label));
234 QString const name = toqstr(tbitem_.name);
235 QStringList imagedirs;
236 imagedirs << "images/math/" << "images/";
237 for (int i = 0; i < imagedirs.size(); ++i) {
238 QString imagedir = imagedirs.at(i);
239 FileName const fname = imageLibFileSearch(imagedir, name, "svgz,png",
240 theGuiApp()->imageSearchMode());
241 if (fname.exists()) {
242 setIcon(QIcon(getPixmap(imagedir, name, "svgz,png")));
249 void MenuButtonBase::actionTriggered(QAction * action)
251 QToolButton::setDefaultAction(action);
252 setPopupMode(QToolButton::DelayedPopup);
256 StaticMenuButton::StaticMenuButton(
257 GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
258 : MenuButtonBase(bar, item)
261 connect(this, SIGNAL(triggered(QAction *)),
262 this, SLOT(actionTriggered(QAction *)));
263 connect(bar, SIGNAL(iconSizeChanged(QSize)),
264 this, SLOT(setIconSize(QSize)));
269 void StaticMenuButton::initialize()
271 QString const label = qt_(to_ascii(tbitem_.label));
272 ButtonMenu * m = new ButtonMenu(label, this);
273 m->setWindowTitle(label);
274 m->setTearOffEnabled(true);
275 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
276 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
277 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name);
279 LYXERR0("Unknown toolbar " << tbitem_.name);
282 ToolbarInfo::item_iterator it = tbinfo->items.begin();
283 ToolbarInfo::item_iterator const end = tbinfo->items.end();
284 for (; it != end; ++it)
285 if (!getStatus(*it->func).unknown())
286 m->add(bar_->addItem(*it, true));
291 void StaticMenuButton::updateTriggered()
296 bool enabled = false;
297 QList<QAction *> acts = menu()->actions();
298 for (auto const & act : acts)
299 if (act->isEnabled()) {
303 // Enable the MenuButton if at least one menu item is enabled
305 // If a disabled item is default, switch to InstantPopup
306 // (this can happen if a user selects e.g. DVI and then
307 // turns non-TeX fonts on)
308 if (defaultAction() && !defaultAction()->isEnabled())
309 setPopupMode(QToolButton::InstantPopup);
313 class DynamicMenuButton::Private
316 Private(Private const &);
317 void operator=(Private const &);
319 Private() : inset_(nullptr) {}
321 DocumentClassConstPtr text_class_;
323 InsetText const * inset_;
327 DynamicMenuButton::DynamicMenuButton(GuiToolbar * bar, ToolbarItem const & item)
328 : MenuButtonBase(bar, item), d(new Private())
334 DynamicMenuButton::~DynamicMenuButton()
340 void DynamicMenuButton::initialize()
342 QString const label = qt_(to_ascii(tbitem_.label));
343 ButtonMenu * m = new ButtonMenu(label, this);
344 m->setWindowTitle(label);
345 m->setTearOffEnabled(true);
346 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
347 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
348 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
349 this, SLOT(setIconSize(QSize)));
354 bool DynamicMenuButton::isMenuType(string const & s)
356 return s == "dynamic-custom-insets"
357 || s == "dynamic-char-styles"
358 || s == "textstyle-apply"
363 void DynamicMenuButton::resetIconCache()
365 icon_textstyle_apply_ = getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false);
366 icon_undo_ = getIcon(FuncRequest(LFUN_UNDO), false);
367 icon_paste_ = getIcon(FuncRequest(LFUN_PASTE), false);
371 void DynamicMenuButton::updateTriggered()
374 // the menu should exist by this point
375 // if not, we can at least avoid crashing in release mode
377 GuiView const & owner = bar_->owner();
378 BufferView const * bv = owner.currentBufferView();
380 string const & menutype = tbitem_.name;
381 if (menutype == "dynamic-custom-insets" || menutype == "dynamic-char-styles") {
385 setMinimumWidth(sizeHint().width());
386 d->text_class_.reset();
390 DocumentClassConstPtr text_class =
391 bv->buffer().params().documentClassPtr();
392 InsetText const * inset = &(bv->cursor().innerText()->inset());
393 // if the text class has changed, then we need to reload the menu
394 if (d->text_class_ != text_class) {
395 d->text_class_ = text_class;
396 // at the moment, we can just call loadFlexInsets, and it will
397 // handle both types. if there were more types of menus, then we
398 // might need to have other options.
401 // remember where we are
403 // note that enabling here might need to be more subtle if there
404 // were other kinds of menus.
405 setEnabled(!bv->buffer().isReadonly()
407 && inset->insetAllowed(FLEX_CODE));
408 } else if (menutype == "textstyle-apply") {
410 setPopupMode(QToolButton::MenuButtonPopup);
412 QToolButton::setIcon(icon_textstyle_apply_);
416 vector<docstring> ffList = bv->cursor().innerText()->getFreeFonts();
418 Action * default_act = nullptr;
419 for (auto const & f : ffList) {
420 FuncRequest func(LFUN_TEXTSTYLE_APPLY, convert<docstring>(i),
421 FuncRequest::TOOLBAR);
422 docstring const lb = char_type('&') + convert<docstring>(i)
423 + from_ascii(". ") + f ;
424 Action * act = new Action(func, QIcon(), toqstr(lb), toqstr(f), this);
426 // The most recent one is the default
431 // Add item to reset to defaults
432 Action * reset_act = new Action(FuncRequest(LFUN_FONT_DEFAULT, FuncRequest::TOOLBAR),
434 qt_("&Reset to default"),
435 qt_("Reset all font settings to their defaults"), this);
436 m->addAction(reset_act);
438 QToolButton::setDefaultAction(default_act);
439 QToolButton::setIcon(icon_textstyle_apply_);
440 setEnabled(lyx::getStatus(FuncRequest(LFUN_TEXTSTYLE_APPLY)).enabled()
441 || lyx::getStatus(FuncRequest(LFUN_FONT_DEFAULT)).enabled());
442 } else if (menutype == "paste") {
444 setPopupMode(QToolButton::MenuButtonPopup);
445 Action * default_action = new Action(FuncRequest(LFUN_PASTE),
447 qt_("Paste"), qt_("Paste"), this);
450 QToolButton::setDefaultAction(default_action);
453 docstring_list const sel = cap::availableSelections(&bv->buffer());
455 docstring_list::const_iterator cit = sel.begin();
456 docstring_list::const_iterator end = sel.end();
458 for (unsigned int index = 0; cit != end; ++cit, ++index) {
459 docstring const s = *cit;
460 FuncRequest func(LFUN_PASTE, convert<docstring>(index),
461 FuncRequest::TOOLBAR);
462 docstring const lb = char_type('&') + convert<docstring>(index)
463 + from_ascii(". ") + s ;
464 Action * act = new Action(func, QIcon(), toqstr(lb), toqstr(s), this);
467 QToolButton::setDefaultAction(default_action);
468 setEnabled(lyx::getStatus(FuncRequest(LFUN_PASTE)).enabled());
473 void DynamicMenuButton::loadFlexInsets()
477 string const & menutype = tbitem_.name;
479 if (menutype == "dynamic-custom-insets")
480 ftype = InsetLyXType::CUSTOM;
481 else if (menutype == "dynamic-char-styles")
482 ftype = InsetLyXType::CHARSTYLE;
484 // this should have been taken care of earlier
485 LASSERT(false, return);
488 TextClass::InsetLayouts const & inset_layouts =
489 d->text_class_->insetLayouts();
490 for (auto const & iit : inset_layouts) {
491 InsetLayout const & il = iit.second;
492 if (il.lyxtype() != ftype)
494 docstring const name = iit.first;
495 QString const loc_item = toqstr(translateIfPossible(
496 prefixIs(name, from_ascii("Flex:")) ?
497 name.substr(5) : name));
498 FuncRequest func(LFUN_FLEX_INSERT,
499 from_ascii("\"") + name + from_ascii("\""), FuncRequest::TOOLBAR);
501 new Action(func, getIcon(func, false), loc_item, loc_item, this);
507 void GuiToolbar::add(ToolbarItem const & item)
510 case ToolbarItem::SEPARATOR:
513 case ToolbarItem::LAYOUTS: {
514 LayoutBox * layout = owner_.getLayoutDialog();
515 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
516 layout, SLOT(setIconSize(QSize)));
517 QAction * action = addWidget(layout);
518 action->setVisible(true);
521 case ToolbarItem::MINIBUFFER:
522 command_buffer_ = new GuiCommandBuffer(&owner_);
523 addWidget(command_buffer_);
524 /// \todo find a Qt5 equivalent to setHorizontalStretchable(true);
525 //setHorizontalStretchable(true);
527 case ToolbarItem::TABLEINSERT: {
528 QToolButton * tb = new QToolButton;
529 tb->setCheckable(true);
530 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
531 QString const label = qt_(to_ascii(item.label));
532 tb->setToolTip(label);
533 tb->setStatusTip(label);
535 InsertTableWidget * iv = new InsertTableWidget(tb);
536 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
537 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
538 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
542 case ToolbarItem::ICONPALETTE:
543 addWidget(new PaletteButton(this, item));
545 case ToolbarItem::POPUPMENU: {
546 addWidget(new StaticMenuButton(this, item, false));
549 case ToolbarItem::STICKYPOPUPMENU: {
550 addWidget(new StaticMenuButton(this, item, true));
553 case ToolbarItem::DYNAMICMENU: {
554 // we only handle certain things
555 if (DynamicMenuButton::isMenuType(item.name))
556 addWidget(new DynamicMenuButton(this, item));
558 LYXERR0("Unknown dynamic menu type: " << item.name);
561 case ToolbarItem::BIDICOMMAND: {
562 if (!getStatus(*item.func).unknown())
563 addAction(addItem(item));
566 case ToolbarItem::COMMAND: {
567 if (!getStatus(*item.func).unknown())
568 addAction(addItem(item));
577 void GuiToolbar::update(int context)
579 if (visibility_ & Toolbars::AUTO) {
580 setVisible(visibility_ & context & Toolbars::ALLOWAUTO);
581 if (isVisible() && commandBuffer() && (context & Toolbars::MINIBUFFER_FOCUS))
582 commandBuffer()->setFocus();
585 // update visible toolbars only
589 // This is a speed bottleneck because this is called on every keypress
590 // and update calls getStatus, which copies the cursor at least two times
591 for (auto const & action : actions_)
594 LayoutBox * layout = owner_.getLayoutDialog();
596 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
603 QString GuiToolbar::sessionKey() const
605 return "views/" + QString::number(owner_.id()) + "/" + objectName();
609 void GuiToolbar::saveSession(QSettings & settings) const
611 settings.setValue(sessionKey() + "/visibility", visibility_);
612 settings.setValue(sessionKey() + "/movability", isMovable());
616 void GuiToolbar::restoreSession()
619 int const error_val = -1;
621 settings.value(sessionKey() + "/visibility", error_val).toInt();
622 if (visibility == error_val || visibility == 0) {
623 // This should not happen, but in case we use the defaults
624 LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
626 guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
628 setVisibility(visibility);
630 int movability = settings.value(sessionKey() + "/movability", true).toBool();
631 setMovable(movability);
635 bool GuiToolbar::isVisibilityOn() const
637 return visibility_ & Toolbars::ON;
641 void GuiToolbar::setState(string const state)
644 if (state == "auto") {
645 if (visibility_ & Toolbars::ALLOWAUTO) {
646 visibility_ |= Toolbars::AUTO;
648 newstate = _("auto");
650 owner_.message(bformat(_("Toolbar \"%1$s\" does not support state \"auto\""),
651 qstring_to_ucs4(windowTitle())));
653 if (visibility_ & Toolbars::AUTO)
654 visibility_ &= ~Toolbars::AUTO;
655 if (state == "off") {
658 } else if (state == "on") {
664 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
665 qstring_to_ucs4(windowTitle()), newstate));
669 void GuiToolbar::toggle()
671 if (visibility_ & Toolbars::ALLOWAUTO) {
672 if (!(visibility_ & Toolbars::AUTO) && !isVisibilityOn()) {
675 if (isVisibilityOn())
688 void GuiToolbar::movable(bool silent)
691 setMovable(!isMovable());
693 // manual update avoids bug in qt that the drag handle is not removed
694 // properly, e.g. in Windows
697 // silence for toggling of many toolbars for performance
701 state = _("movable");
703 state = _("immovable");
704 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
705 qstring_to_ucs4(windowTitle()), state));
709 } // namespace frontend
712 #include "moc_GuiToolbar.cpp"