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)
147 QString text = toqstr(item.label);
148 // Get the keys bound to this action, but keep only the
150 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(*item.func);
151 if (!bindings.empty())
152 text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
154 Action * act = new Action(item.func, getIcon(*item.func, false), text,
156 if (item.type == ToolbarItem::BIDICOMMAND)
157 act->setRtlIcon(getIcon(*item.func, false, true));
159 actions_.append(act);
165 class PaletteButton : public QToolButton
169 ToolbarItem const & tbitem_;
172 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
173 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
175 QString const label = qt_(to_ascii(tbitem_.label));
179 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
180 this, SLOT(setIconSize(QSize)));
182 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name);
184 // use the icon of first action for the toolbar button
185 setIcon(getIcon(*tbinfo->items.begin()->func, true));
188 void mousePressEvent(QMouseEvent * e) override
191 QToolButton::mousePressEvent(e);
197 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name);
199 LYXERR0("Unknown toolbar " << tbitem_.name);
202 IconPalette * panel = new IconPalette(this);
203 QString const label = qt_(to_ascii(tbitem_.label));
204 panel->setWindowTitle(label);
205 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
206 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
207 ToolbarInfo::item_iterator it = tbinfo->items.begin();
208 ToolbarInfo::item_iterator const end = tbinfo->items.end();
209 for (; it != end; ++it)
210 if (!getStatus(*it->func).unknown())
211 panel->addButton(bar_->addItem(*it));
213 QToolButton::mousePressEvent(e);
220 MenuButtonBase::MenuButtonBase(GuiToolbar * bar, ToolbarItem const & item)
221 : QToolButton(bar), bar_(bar), tbitem_(item)
223 setPopupMode(QToolButton::InstantPopup);
224 QString const label = qt_(to_ascii(tbitem_.label));
228 QString const name = toqstr(tbitem_.name);
229 QStringList imagedirs;
230 imagedirs << "images/math/" << "images/";
231 for (int i = 0; i < imagedirs.size(); ++i) {
232 QString imagedir = imagedirs.at(i);
233 FileName const fname = imageLibFileSearch(imagedir, name, "svgz,png",
234 theGuiApp()->imageSearchMode());
235 if (fname.exists()) {
236 setIcon(QIcon(getPixmap(imagedir, name, "svgz,png")));
243 void MenuButtonBase::actionTriggered(QAction * action)
245 QToolButton::setDefaultAction(action);
246 setPopupMode(QToolButton::DelayedPopup);
250 StaticMenuButton::StaticMenuButton(
251 GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
252 : MenuButtonBase(bar, item)
255 connect(this, SIGNAL(triggered(QAction *)),
256 this, SLOT(actionTriggered(QAction *)));
257 connect(bar, SIGNAL(iconSizeChanged(QSize)),
258 this, SLOT(setIconSize(QSize)));
263 void StaticMenuButton::initialize()
265 QString const label = qt_(to_ascii(tbitem_.label));
266 ButtonMenu * m = new ButtonMenu(label, this);
267 m->setWindowTitle(label);
268 m->setTearOffEnabled(true);
269 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
270 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
271 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name);
273 LYXERR0("Unknown toolbar " << tbitem_.name);
276 ToolbarInfo::item_iterator it = tbinfo->items.begin();
277 ToolbarInfo::item_iterator const end = tbinfo->items.end();
278 for (; it != end; ++it)
279 if (!getStatus(*it->func).unknown())
280 m->add(bar_->addItem(*it));
285 void StaticMenuButton::updateTriggered()
290 bool enabled = false;
291 QList<QAction *> acts = menu()->actions();
292 for (auto const & act : acts)
293 if (act->isEnabled()) {
297 // Enable the MenuButton if at least one menu item is enabled
299 // If a disabled item is default, switch to InstantPopup
300 // (this can happen if a user selects e.g. DVI and then
301 // turns non-TeX fonts on)
302 if (defaultAction() && !defaultAction()->isEnabled())
303 setPopupMode(QToolButton::InstantPopup);
307 class DynamicMenuButton::Private
310 Private(Private const &);
311 void operator=(Private const &);
313 Private() : inset_(nullptr) {}
315 DocumentClassConstPtr text_class_;
317 InsetText const * inset_;
321 DynamicMenuButton::DynamicMenuButton(GuiToolbar * bar, ToolbarItem const & item)
322 : MenuButtonBase(bar, item), d(new Private())
328 DynamicMenuButton::~DynamicMenuButton()
334 void DynamicMenuButton::initialize()
336 QString const label = qt_(to_ascii(tbitem_.label));
337 ButtonMenu * m = new ButtonMenu(label, this);
338 m->setWindowTitle(label);
339 m->setTearOffEnabled(true);
340 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
341 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
342 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
343 this, SLOT(setIconSize(QSize)));
348 bool DynamicMenuButton::isMenuType(string const & s)
350 return s == "dynamic-custom-insets"
351 || s == "dynamic-char-styles"
352 || s == "textstyle-apply"
357 void DynamicMenuButton::resetIconCache()
359 icon_textstyle_apply_ = getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false);
360 icon_undo_ = getIcon(FuncRequest(LFUN_UNDO), false);
361 icon_paste_ = getIcon(FuncRequest(LFUN_PASTE), false);
365 void DynamicMenuButton::updateTriggered()
368 // the menu should exist by this point
369 // if not, we can at least avoid crashing in release mode
371 GuiView const & owner = bar_->owner();
372 BufferView const * bv = owner.currentBufferView();
374 string const & menutype = tbitem_.name;
375 if (menutype == "dynamic-custom-insets" || menutype == "dynamic-char-styles") {
379 setMinimumWidth(sizeHint().width());
380 d->text_class_.reset();
384 DocumentClassConstPtr text_class =
385 bv->buffer().params().documentClassPtr();
386 InsetText const * inset = &(bv->cursor().innerText()->inset());
387 // if the text class has changed, then we need to reload the menu
388 if (d->text_class_ != text_class) {
389 d->text_class_ = text_class;
390 // at the moment, we can just call loadFlexInsets, and it will
391 // handle both types. if there were more types of menus, then we
392 // might need to have other options.
395 // remember where we are
397 // note that enabling here might need to be more subtle if there
398 // were other kinds of menus.
399 setEnabled(!bv->buffer().isReadonly()
401 && inset->insetAllowed(FLEX_CODE));
402 } else if (menutype == "textstyle-apply") {
404 setPopupMode(QToolButton::MenuButtonPopup);
406 QToolButton::setIcon(icon_textstyle_apply_);
410 vector<docstring> ffList = bv->cursor().innerText()->getFreeFonts();
412 Action * default_act = nullptr;
413 for (auto const & f : ffList) {
414 FuncRequest func(LFUN_TEXTSTYLE_APPLY, convert<docstring>(i),
415 FuncRequest::TOOLBAR);
416 docstring const lb = char_type('&') + convert<docstring>(i)
417 + from_ascii(". ") + f ;
418 Action * act = new Action(func, QIcon(), toqstr(lb), toqstr(f), this);
420 // The most recent one is the default
425 // Add item to reset to defaults
426 Action * reset_act = new Action(FuncRequest(LFUN_FONT_DEFAULT, FuncRequest::TOOLBAR),
428 qt_("&Reset to default"),
429 qt_("Reset all font settings to their defaults"), this);
430 m->addAction(reset_act);
432 QToolButton::setDefaultAction(default_act);
433 QToolButton::setIcon(icon_textstyle_apply_);
434 setEnabled(lyx::getStatus(FuncRequest(LFUN_TEXTSTYLE_APPLY)).enabled()
435 || lyx::getStatus(FuncRequest(LFUN_FONT_DEFAULT)).enabled());
436 } else if (menutype == "paste") {
438 setPopupMode(QToolButton::MenuButtonPopup);
439 Action * default_action = new Action(FuncRequest(LFUN_PASTE),
441 qt_("Paste"), qt_("Paste"), this);
444 QToolButton::setDefaultAction(default_action);
447 docstring_list const sel = cap::availableSelections(&bv->buffer());
449 docstring_list::const_iterator cit = sel.begin();
450 docstring_list::const_iterator end = sel.end();
452 for (unsigned int index = 0; cit != end; ++cit, ++index) {
453 docstring const s = *cit;
454 FuncRequest func(LFUN_PASTE, convert<docstring>(index),
455 FuncRequest::TOOLBAR);
456 docstring const lb = char_type('&') + convert<docstring>(index)
457 + from_ascii(". ") + s ;
458 Action * act = new Action(func, QIcon(), toqstr(lb), toqstr(s), this);
461 QToolButton::setDefaultAction(default_action);
462 setEnabled(lyx::getStatus(FuncRequest(LFUN_PASTE)).enabled());
467 void DynamicMenuButton::loadFlexInsets()
471 string const & menutype = tbitem_.name;
473 if (menutype == "dynamic-custom-insets")
474 ftype = InsetLyXType::CUSTOM;
475 else if (menutype == "dynamic-char-styles")
476 ftype = InsetLyXType::CHARSTYLE;
478 // this should have been taken care of earlier
479 LASSERT(false, return);
482 TextClass::InsetLayouts const & inset_layouts =
483 d->text_class_->insetLayouts();
484 for (auto const & iit : inset_layouts) {
485 InsetLayout const & il = iit.second;
486 if (il.lyxtype() != ftype)
488 docstring const name = iit.first;
489 QString const loc_item = toqstr(translateIfPossible(
490 prefixIs(name, from_ascii("Flex:")) ?
491 name.substr(5) : name));
492 FuncRequest func(LFUN_FLEX_INSERT,
493 from_ascii("\"") + name + from_ascii("\""), FuncRequest::TOOLBAR);
495 new Action(func, getIcon(func, false), loc_item, loc_item, this);
501 void GuiToolbar::add(ToolbarItem const & item)
504 case ToolbarItem::SEPARATOR:
507 case ToolbarItem::LAYOUTS: {
508 LayoutBox * layout = owner_.getLayoutDialog();
509 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
510 layout, SLOT(setIconSize(QSize)));
511 QAction * action = addWidget(layout);
512 action->setVisible(true);
515 case ToolbarItem::MINIBUFFER:
516 command_buffer_ = new GuiCommandBuffer(&owner_);
517 addWidget(command_buffer_);
518 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
519 //setHorizontalStretchable(true);
521 case ToolbarItem::TABLEINSERT: {
522 QToolButton * tb = new QToolButton;
523 tb->setCheckable(true);
524 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
525 QString const label = qt_(to_ascii(item.label));
526 tb->setToolTip(label);
527 tb->setStatusTip(label);
529 InsertTableWidget * iv = new InsertTableWidget(tb);
530 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
531 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
532 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
536 case ToolbarItem::ICONPALETTE:
537 addWidget(new PaletteButton(this, item));
539 case ToolbarItem::POPUPMENU: {
540 addWidget(new StaticMenuButton(this, item, false));
543 case ToolbarItem::STICKYPOPUPMENU: {
544 addWidget(new StaticMenuButton(this, item, true));
547 case ToolbarItem::DYNAMICMENU: {
548 // we only handle certain things
549 if (DynamicMenuButton::isMenuType(item.name))
550 addWidget(new DynamicMenuButton(this, item));
552 LYXERR0("Unknown dynamic menu type: " << item.name);
555 case ToolbarItem::BIDICOMMAND: {
556 if (!getStatus(*item.func).unknown())
557 addAction(addItem(item));
560 case ToolbarItem::COMMAND: {
561 if (!getStatus(*item.func).unknown())
562 addAction(addItem(item));
571 void GuiToolbar::update(int context)
573 if (visibility_ & Toolbars::AUTO) {
574 setVisible(visibility_ & context & Toolbars::ALLOWAUTO);
575 if (isVisible() && commandBuffer() && (context & Toolbars::MINIBUFFER_FOCUS))
576 commandBuffer()->setFocus();
579 // update visible toolbars only
583 // This is a speed bottleneck because this is called on every keypress
584 // and update calls getStatus, which copies the cursor at least two times
585 for (auto const & action : actions_)
588 LayoutBox * layout = owner_.getLayoutDialog();
590 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
597 QString GuiToolbar::sessionKey() const
599 return "views/" + QString::number(owner_.id()) + "/" + objectName();
603 void GuiToolbar::saveSession(QSettings & settings) const
605 settings.setValue(sessionKey() + "/visibility", visibility_);
606 settings.setValue(sessionKey() + "/movability", isMovable());
610 void GuiToolbar::restoreSession()
613 int const error_val = -1;
615 settings.value(sessionKey() + "/visibility", error_val).toInt();
616 if (visibility == error_val || visibility == 0) {
617 // This should not happen, but in case we use the defaults
618 LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
620 guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
622 setVisibility(visibility);
624 int movability = settings.value(sessionKey() + "/movability", true).toBool();
625 setMovable(movability);
629 bool GuiToolbar::isVisibilityOn() const
631 return visibility_ & Toolbars::ON;
635 void GuiToolbar::setState(string const state)
638 if (state == "auto") {
639 if (visibility_ & Toolbars::ALLOWAUTO) {
640 visibility_ |= Toolbars::AUTO;
642 newstate = _("auto");
644 owner_.message(bformat(_("Toolbar \"%1$s\" does not support state \"auto\""),
645 qstring_to_ucs4(windowTitle())));
647 if (visibility_ & Toolbars::AUTO)
648 visibility_ &= ~Toolbars::AUTO;
649 if (state == "off") {
652 } else if (state == "on") {
658 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
659 qstring_to_ucs4(windowTitle()), newstate));
663 void GuiToolbar::toggle()
665 if (visibility_ & Toolbars::ALLOWAUTO) {
666 if (!(visibility_ & Toolbars::AUTO) && !isVisibilityOn()) {
669 if (isVisibilityOn())
682 void GuiToolbar::movable(bool silent)
685 setMovable(!isMovable());
687 // manual update avoids bug in qt that the drag handle is not removed
688 // properly, e.g. in Windows
691 // silence for toggling of many toolbars for performance
695 state = _("movable");
697 state = _("immovable");
698 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
699 qstring_to_ucs4(windowTitle()), state));
703 } // namespace frontend
706 #include "moc_GuiToolbar.cpp"