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_(nullptr), 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 // Record the actual visibility in toolbar state visibility_.
88 // This is useful to restore the visibility of toolbars when
89 // returning from full-screen. Therefore the recording is disabled
90 // while LyX is in full-screen state.
91 if (!owner_.isFullScreen()) {
93 visibility_ |= Toolbars::ON;
95 visibility_ &= ~Toolbars::ON;
97 QToolBar::setVisible(visible);
101 bool GuiToolbar::isRestored() const
107 void GuiToolbar::fill()
111 ToolbarInfo::item_iterator it = tbinfo_.items.begin();
112 ToolbarInfo::item_iterator end = tbinfo_.items.end();
113 for (; it != end; ++it)
119 void GuiToolbar::refill()
127 void GuiToolbar::showEvent(QShowEvent * ev)
134 void GuiToolbar::setVisibility(int visibility)
136 visibility_ = visibility;
140 Action * GuiToolbar::addItem(ToolbarItem const & item)
142 QString text = toqstr(item.label);
143 // Get the keys bound to this action, but keep only the
145 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(*item.func);
146 if (!bindings.empty())
147 text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
149 Action * act = new Action(item.func, getIcon(*item.func, false), text,
151 if (item.type == ToolbarItem::BIDICOMMAND)
152 act->setRtlIcon(getIcon(*item.func, false, true));
154 actions_.append(act);
160 class PaletteButton : public QToolButton
164 ToolbarItem const & tbitem_;
167 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
168 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
170 QString const label = qt_(to_ascii(tbitem_.label));
174 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
175 this, SLOT(setIconSize(QSize)));
177 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name);
179 // use the icon of first action for the toolbar button
180 setIcon(getIcon(*tbinfo->items.begin()->func, true));
183 void mousePressEvent(QMouseEvent * e) override
186 QToolButton::mousePressEvent(e);
192 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name);
194 LYXERR0("Unknown toolbar " << tbitem_.name);
197 IconPalette * panel = new IconPalette(this);
198 QString const label = qt_(to_ascii(tbitem_.label));
199 panel->setWindowTitle(label);
200 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
201 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
202 ToolbarInfo::item_iterator it = tbinfo->items.begin();
203 ToolbarInfo::item_iterator const end = tbinfo->items.end();
204 for (; it != end; ++it)
205 if (!getStatus(*it->func).unknown())
206 panel->addButton(bar_->addItem(*it));
208 QToolButton::mousePressEvent(e);
215 MenuButtonBase::MenuButtonBase(GuiToolbar * bar, ToolbarItem const & item)
216 : QToolButton(bar), bar_(bar), tbitem_(item)
218 setPopupMode(QToolButton::InstantPopup);
219 QString const label = qt_(to_ascii(tbitem_.label));
223 QString const name = toqstr(tbitem_.name);
224 QStringList imagedirs;
225 imagedirs << "images/math/" << "images/";
226 for (int i = 0; i < imagedirs.size(); ++i) {
227 QString imagedir = imagedirs.at(i);
228 FileName const fname = imageLibFileSearch(imagedir, name, "svgz,png",
229 theGuiApp()->imageSearchMode());
230 if (fname.exists()) {
231 setIcon(QIcon(getPixmap(imagedir, name, "svgz,png")));
238 void MenuButtonBase::actionTriggered(QAction * action)
240 QToolButton::setDefaultAction(action);
241 setPopupMode(QToolButton::DelayedPopup);
245 StaticMenuButton::StaticMenuButton(
246 GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
247 : MenuButtonBase(bar, item)
250 connect(this, SIGNAL(triggered(QAction *)),
251 this, SLOT(actionTriggered(QAction *)));
252 connect(bar, SIGNAL(iconSizeChanged(QSize)),
253 this, SLOT(setIconSize(QSize)));
258 void StaticMenuButton::initialize()
260 QString const label = qt_(to_ascii(tbitem_.label));
261 ButtonMenu * m = new ButtonMenu(label, this);
262 m->setWindowTitle(label);
263 m->setTearOffEnabled(true);
264 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
265 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
266 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name);
268 LYXERR0("Unknown toolbar " << tbitem_.name);
271 ToolbarInfo::item_iterator it = tbinfo->items.begin();
272 ToolbarInfo::item_iterator const end = tbinfo->items.end();
273 for (; it != end; ++it)
274 if (!getStatus(*it->func).unknown())
275 m->add(bar_->addItem(*it));
280 void StaticMenuButton::updateTriggered()
285 bool enabled = false;
286 QList<QAction *> acts = menu()->actions();
287 for (auto const & act : acts)
288 if (act->isEnabled()) {
292 // Enable the MenuButton if at least one menu item is enabled
294 // If a disabled item is default, switch to InstantPopup
295 // (this can happen if a user selects e.g. DVI and then
296 // turns non-TeX fonts on)
297 if (defaultAction() && !defaultAction()->isEnabled())
298 setPopupMode(QToolButton::InstantPopup);
302 class DynamicMenuButton::Private
305 Private(Private const &);
306 void operator=(Private const &);
308 Private() : inset_(nullptr) {}
310 DocumentClassConstPtr text_class_;
312 InsetText const * inset_;
316 DynamicMenuButton::DynamicMenuButton(GuiToolbar * bar, ToolbarItem const & item)
317 : MenuButtonBase(bar, item), d(new Private())
323 DynamicMenuButton::~DynamicMenuButton()
329 void DynamicMenuButton::initialize()
331 QString const label = qt_(to_ascii(tbitem_.label));
332 ButtonMenu * m = new ButtonMenu(label, this);
333 m->setWindowTitle(label);
334 m->setTearOffEnabled(true);
335 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
336 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
337 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
338 this, SLOT(setIconSize(QSize)));
343 bool DynamicMenuButton::isMenuType(string const & s)
345 return s == "dynamic-custom-insets"
346 || s == "dynamic-char-styles"
347 || s == "textstyle-apply"
352 void DynamicMenuButton::updateTriggered()
355 // the menu should exist by this point
356 // if not, we can at least avoid crashing in release mode
358 GuiView const & owner = bar_->owner();
359 BufferView const * bv = owner.currentBufferView();
361 string const & menutype = tbitem_.name;
362 if (menutype == "dynamic-custom-insets" || menutype == "dynamic-char-styles") {
366 setMinimumWidth(sizeHint().width());
367 d->text_class_.reset();
371 DocumentClassConstPtr text_class =
372 bv->buffer().params().documentClassPtr();
373 InsetText const * inset = &(bv->cursor().innerText()->inset());
374 // if the text class has changed, then we need to reload the menu
375 if (d->text_class_ != text_class) {
376 d->text_class_ = text_class;
377 // at the moment, we can just call loadFlexInsets, and it will
378 // handle both types. if there were more types of menus, then we
379 // might need to have other options.
382 // remember where we are
384 // note that enabling here might need to be more subtle if there
385 // were other kinds of menus.
386 setEnabled(!bv->buffer().isReadonly()
388 && inset->insetAllowed(FLEX_CODE));
389 } else if (menutype == "textstyle-apply") {
391 setPopupMode(QToolButton::MenuButtonPopup);
393 QToolButton::setIcon(getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false));
397 vector<docstring> ffList = bv->cursor().innerText()->getFreeFonts();
399 Action * default_act = nullptr;
400 for (auto const & f : ffList) {
401 FuncRequest func(LFUN_TEXTSTYLE_APPLY, convert<docstring>(i),
402 FuncRequest::TOOLBAR);
403 docstring const lb = char_type('&') + convert<docstring>(i)
404 + from_ascii(". ") + f ;
405 Action * act = new Action(func, QIcon(), toqstr(lb), toqstr(f), this);
407 // The most recent one is the default
412 // Add item to reset to defaults
413 Action * reset_act = new Action(FuncRequest(LFUN_FONT_DEFAULT, FuncRequest::TOOLBAR),
414 getIcon(FuncRequest(LFUN_UNDO), false),
415 qt_("&Reset to default"),
416 qt_("Reset all font settings to their defaults"), this);
417 m->addAction(reset_act);
419 QToolButton::setDefaultAction(default_act);
420 QToolButton::setIcon(getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false));
421 setEnabled(lyx::getStatus(FuncRequest(LFUN_TEXTSTYLE_APPLY)).enabled()
422 || lyx::getStatus(FuncRequest(LFUN_FONT_DEFAULT)).enabled());
423 } else if (menutype == "paste") {
425 setPopupMode(QToolButton::MenuButtonPopup);
426 Action * default_action = new Action(FuncRequest(LFUN_PASTE),
427 getIcon(FuncRequest(LFUN_PASTE), false),
428 qt_("Paste"), qt_("Paste"), this);
431 QToolButton::setDefaultAction(default_action);
434 docstring_list const sel = cap::availableSelections(&bv->buffer());
436 docstring_list::const_iterator cit = sel.begin();
437 docstring_list::const_iterator end = sel.end();
439 for (unsigned int index = 0; cit != end; ++cit, ++index) {
440 docstring const s = *cit;
441 FuncRequest func(LFUN_PASTE, convert<docstring>(index),
442 FuncRequest::TOOLBAR);
443 docstring const lb = char_type('&') + convert<docstring>(index)
444 + from_ascii(". ") + s ;
445 Action * act = new Action(func, QIcon(), toqstr(lb), toqstr(s), this);
448 QToolButton::setDefaultAction(default_action);
449 setEnabled(lyx::getStatus(FuncRequest(LFUN_PASTE)).enabled());
454 void DynamicMenuButton::loadFlexInsets()
458 string const & menutype = tbitem_.name;
460 if (menutype == "dynamic-custom-insets")
461 ftype = InsetLyXType::CUSTOM;
462 else if (menutype == "dynamic-char-styles")
463 ftype = InsetLyXType::CHARSTYLE;
465 // this should have been taken care of earlier
466 LASSERT(false, return);
469 TextClass::InsetLayouts const & inset_layouts =
470 d->text_class_->insetLayouts();
471 for (auto const & iit : inset_layouts) {
472 InsetLayout const & il = iit.second;
473 if (il.lyxtype() != ftype)
475 docstring const name = iit.first;
476 QString const loc_item = toqstr(translateIfPossible(
477 prefixIs(name, from_ascii("Flex:")) ?
478 name.substr(5) : name));
479 FuncRequest func(LFUN_FLEX_INSERT,
480 from_ascii("\"") + name + from_ascii("\""), FuncRequest::TOOLBAR);
482 new Action(func, getIcon(func, false), loc_item, loc_item, this);
488 void GuiToolbar::add(ToolbarItem const & item)
491 case ToolbarItem::SEPARATOR:
494 case ToolbarItem::LAYOUTS: {
495 LayoutBox * layout = owner_.getLayoutDialog();
496 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
497 layout, SLOT(setIconSize(QSize)));
498 QAction * action = addWidget(layout);
499 action->setVisible(true);
502 case ToolbarItem::MINIBUFFER:
503 command_buffer_ = new GuiCommandBuffer(&owner_);
504 addWidget(command_buffer_);
505 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
506 //setHorizontalStretchable(true);
508 case ToolbarItem::TABLEINSERT: {
509 QToolButton * tb = new QToolButton;
510 tb->setCheckable(true);
511 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
512 QString const label = qt_(to_ascii(item.label));
513 tb->setToolTip(label);
514 tb->setStatusTip(label);
516 InsertTableWidget * iv = new InsertTableWidget(tb);
517 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
518 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
519 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
523 case ToolbarItem::ICONPALETTE:
524 addWidget(new PaletteButton(this, item));
526 case ToolbarItem::POPUPMENU: {
527 addWidget(new StaticMenuButton(this, item, false));
530 case ToolbarItem::STICKYPOPUPMENU: {
531 addWidget(new StaticMenuButton(this, item, true));
534 case ToolbarItem::DYNAMICMENU: {
535 // we only handle certain things
536 if (DynamicMenuButton::isMenuType(item.name))
537 addWidget(new DynamicMenuButton(this, item));
539 LYXERR0("Unknown dynamic menu type: " << item.name);
542 case ToolbarItem::BIDICOMMAND: {
543 if (!getStatus(*item.func).unknown())
544 addAction(addItem(item));
547 case ToolbarItem::COMMAND: {
548 if (!getStatus(*item.func).unknown())
549 addAction(addItem(item));
558 void GuiToolbar::update(int context)
560 if (visibility_ & Toolbars::AUTO) {
561 setVisible(visibility_ & context & Toolbars::ALLOWAUTO);
562 if (isVisible() && commandBuffer() && (context & Toolbars::MINIBUFFER_FOCUS))
563 commandBuffer()->setFocus();
566 // update visible toolbars only
570 // This is a speed bottleneck because this is called on every keypress
571 // and update calls getStatus, which copies the cursor at least two times
572 for (auto const & action : actions_)
575 LayoutBox * layout = owner_.getLayoutDialog();
577 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
584 QString GuiToolbar::sessionKey() const
586 return "views/" + QString::number(owner_.id()) + "/" + objectName();
590 void GuiToolbar::saveSession(QSettings & settings) const
592 settings.setValue(sessionKey() + "/visibility", visibility_);
593 settings.setValue(sessionKey() + "/movability", isMovable());
597 void GuiToolbar::restoreSession()
600 int const error_val = -1;
602 settings.value(sessionKey() + "/visibility", error_val).toInt();
603 if (visibility == error_val || visibility == 0) {
604 // This should not happen, but in case we use the defaults
605 LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
607 guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
609 setVisibility(visibility);
611 int movability = settings.value(sessionKey() + "/movability", true).toBool();
612 setMovable(movability);
616 bool GuiToolbar::isVisibiltyOn() const
618 return visibility_ & Toolbars::ON;
622 void GuiToolbar::setState(string const state)
625 if (state == "auto") {
626 if (visibility_ & Toolbars::ALLOWAUTO) {
627 visibility_ |= Toolbars::AUTO;
629 newstate = _("auto");
631 owner_.message(bformat(_("Toolbar \"%1$s\" does not support state \"auto\""),
632 qstring_to_ucs4(windowTitle())));
634 if (visibility_ & Toolbars::AUTO)
635 visibility_ &= ~Toolbars::AUTO;
636 if (state == "off") {
639 } else if (state == "on") {
645 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
646 qstring_to_ucs4(windowTitle()), newstate));
650 void GuiToolbar::toggle()
652 if (visibility_ & Toolbars::ALLOWAUTO) {
653 if (!(visibility_ & Toolbars::AUTO) && !isVisibiltyOn()) {
669 void GuiToolbar::movable(bool silent)
672 setMovable(!isMovable());
674 // manual update avoids bug in qt that the drag handle is not removed
675 // properly, e.g. in Windows
678 // silence for toggling of many toolbars for performance
682 state = _("movable");
684 state = _("immovable");
685 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
686 qstring_to_ucs4(windowTitle()), state));
690 } // namespace frontend
693 #include "moc_GuiToolbar.cpp"