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 // 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::showEvent(QShowEvent * ev)
126 void GuiToolbar::setVisibility(int visibility)
128 visibility_ = visibility;
132 Action * GuiToolbar::addItem(ToolbarItem const & item)
134 QString text = toqstr(item.label);
135 // Get the keys bound to this action, but keep only the
137 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(*item.func);
138 if (!bindings.empty())
139 text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
141 Action * act = new Action(item.func, getIcon(*item.func, false), text,
143 if (item.type == ToolbarItem::BIDICOMMAND)
144 act->setRtlIcon(getIcon(*item.func, false, true));
146 actions_.append(act);
152 class PaletteButton : public QToolButton
156 ToolbarItem const & tbitem_;
159 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
160 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
162 QString const label = qt_(to_ascii(tbitem_.label));
166 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
167 this, SLOT(setIconSize(QSize)));
169 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name);
171 // use the icon of first action for the toolbar button
172 setIcon(getIcon(*tbinfo->items.begin()->func, true));
175 void mousePressEvent(QMouseEvent * e) override
178 QToolButton::mousePressEvent(e);
184 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name);
186 LYXERR0("Unknown toolbar " << tbitem_.name);
189 IconPalette * panel = new IconPalette(this);
190 QString const label = qt_(to_ascii(tbitem_.label));
191 panel->setWindowTitle(label);
192 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
193 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
194 ToolbarInfo::item_iterator it = tbinfo->items.begin();
195 ToolbarInfo::item_iterator const end = tbinfo->items.end();
196 for (; it != end; ++it)
197 if (!getStatus(*it->func).unknown())
198 panel->addButton(bar_->addItem(*it));
200 QToolButton::mousePressEvent(e);
207 MenuButtonBase::MenuButtonBase(GuiToolbar * bar, ToolbarItem const & item)
208 : QToolButton(bar), bar_(bar), tbitem_(item)
210 setPopupMode(QToolButton::InstantPopup);
211 QString const label = qt_(to_ascii(tbitem_.label));
215 QString const name = toqstr(tbitem_.name);
216 QStringList imagedirs;
217 imagedirs << "images/math/" << "images/";
218 for (int i = 0; i < imagedirs.size(); ++i) {
219 QString imagedir = imagedirs.at(i);
220 FileName const fname = imageLibFileSearch(imagedir, name, "svgz,png",
221 theGuiApp()->imageSearchMode());
222 if (fname.exists()) {
223 setIcon(QIcon(getPixmap(imagedir, name, "svgz,png")));
230 void MenuButtonBase::actionTriggered(QAction * action)
232 QToolButton::setDefaultAction(action);
233 setPopupMode(QToolButton::DelayedPopup);
237 StaticMenuButton::StaticMenuButton(
238 GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
239 : MenuButtonBase(bar, item)
242 connect(this, SIGNAL(triggered(QAction *)),
243 this, SLOT(actionTriggered(QAction *)));
244 connect(bar, SIGNAL(iconSizeChanged(QSize)),
245 this, SLOT(setIconSize(QSize)));
250 void StaticMenuButton::initialize()
252 QString const label = qt_(to_ascii(tbitem_.label));
253 ButtonMenu * m = new ButtonMenu(label, this);
254 m->setWindowTitle(label);
255 m->setTearOffEnabled(true);
256 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
257 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
258 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name);
260 LYXERR0("Unknown toolbar " << tbitem_.name);
263 ToolbarInfo::item_iterator it = tbinfo->items.begin();
264 ToolbarInfo::item_iterator const end = tbinfo->items.end();
265 for (; it != end; ++it)
266 if (!getStatus(*it->func).unknown())
267 m->add(bar_->addItem(*it));
272 void StaticMenuButton::updateTriggered()
277 bool enabled = false;
278 QList<QAction *> acts = menu()->actions();
279 for (int i = 0; i < acts.size(); ++i)
280 if (acts[i]->isEnabled()) {
284 // Enable the MenuButton if at least one menu item is enabled
286 // If a disabled item is default, switch to InstantPopup
287 // (this can happen if a user selects e.g. DVI and then
288 // turns non-TeX fonts on)
289 if (defaultAction() && !defaultAction()->isEnabled())
290 setPopupMode(QToolButton::InstantPopup);
294 class DynamicMenuButton::Private
297 Private(Private const &);
298 void operator=(Private const &);
300 Private() : inset_(0) {}
302 DocumentClassConstPtr text_class_;
304 InsetText const * inset_;
308 DynamicMenuButton::DynamicMenuButton(GuiToolbar * bar, ToolbarItem const & item)
309 : MenuButtonBase(bar, item), d(new Private())
315 DynamicMenuButton::~DynamicMenuButton()
321 void DynamicMenuButton::initialize()
323 QString const label = qt_(to_ascii(tbitem_.label));
324 ButtonMenu * m = new ButtonMenu(label, this);
325 m->setWindowTitle(label);
326 m->setTearOffEnabled(true);
327 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
328 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
329 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
330 this, SLOT(setIconSize(QSize)));
335 bool DynamicMenuButton::isMenuType(string const & s)
337 return s == "dynamic-custom-insets"
338 || s == "dynamic-char-styles"
339 || s == "textstyle-apply"
344 void DynamicMenuButton::updateTriggered()
347 // the menu should exist by this point
348 // if not, we can at least avoid crashing in release mode
350 GuiView const & owner = bar_->owner();
351 BufferView const * bv = owner.currentBufferView();
353 string const & menutype = tbitem_.name;
354 if (menutype == "dynamic-custom-insets" || menutype == "dynamic-char-styles") {
358 setMinimumWidth(sizeHint().width());
359 d->text_class_.reset();
363 DocumentClassConstPtr text_class =
364 bv->buffer().params().documentClassPtr();
365 InsetText const * inset = &(bv->cursor().innerText()->inset());
366 // if the text class has changed, then we need to reload the menu
367 if (d->text_class_ != text_class) {
368 d->text_class_ = text_class;
369 // at the moment, we can just call loadFlexInsets, and it will
370 // handle both types. if there were more types of menus, then we
371 // might need to have other options.
374 // remember where we are
376 // note that enabling here might need to be more subtle if there
377 // were other kinds of menus.
378 setEnabled(!bv->buffer().isReadonly()
380 && inset->insetAllowed(FLEX_CODE));
381 } else if (menutype == "textstyle-apply") {
383 setPopupMode(QToolButton::MenuButtonPopup);
385 QToolButton::setIcon(getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false));
389 vector<docstring> ffList = bv->cursor().innerText()->getFreeFonts();
391 Action * default_act = nullptr;
392 for (auto const & f : ffList) {
393 FuncRequest func(LFUN_TEXTSTYLE_APPLY, convert<docstring>(i),
394 FuncRequest::TOOLBAR);
395 docstring const lb = char_type('&') + convert<docstring>(i)
396 + from_ascii(". ") + f ;
397 Action * act = new Action(func, QIcon(), toqstr(lb), toqstr(f), this);
399 // The most recent one is the default
404 // Add item to reset to defaults
405 Action * reset_act = new Action(FuncRequest(LFUN_FONT_DEFAULT, FuncRequest::TOOLBAR),
406 getIcon(FuncRequest(LFUN_UNDO), false),
407 qt_("&Reset to default"),
408 qt_("Reset all font settings to their defaults"), this);
409 m->addAction(reset_act);
411 QToolButton::setDefaultAction(default_act);
412 QToolButton::setIcon(getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false));
413 setEnabled(lyx::getStatus(FuncRequest(LFUN_TEXTSTYLE_APPLY)).enabled()
414 || lyx::getStatus(FuncRequest(LFUN_FONT_DEFAULT)).enabled());
415 } else if (menutype == "paste") {
417 setPopupMode(QToolButton::MenuButtonPopup);
418 Action * default_action = new Action(FuncRequest(LFUN_PASTE),
419 getIcon(FuncRequest(LFUN_PASTE), false),
420 qt_("Paste"), qt_("Paste"), this);
423 QToolButton::setDefaultAction(default_action);
426 docstring_list const sel = cap::availableSelections(&bv->buffer());
428 docstring_list::const_iterator cit = sel.begin();
429 docstring_list::const_iterator end = sel.end();
431 for (unsigned int index = 0; cit != end; ++cit, ++index) {
432 docstring const s = *cit;
433 FuncRequest func(LFUN_PASTE, convert<docstring>(index),
434 FuncRequest::TOOLBAR);
435 docstring const lb = char_type('&') + convert<docstring>(index)
436 + from_ascii(". ") + s ;
437 Action * act = new Action(func, QIcon(), toqstr(lb), toqstr(s), this);
440 QToolButton::setDefaultAction(default_action);
441 setEnabled(lyx::getStatus(FuncRequest(LFUN_PASTE)).enabled());
446 void DynamicMenuButton::loadFlexInsets()
450 string const & menutype = tbitem_.name;
451 InsetLayout::InsetLyXType ftype;
452 if (menutype == "dynamic-custom-insets")
453 ftype = InsetLayout::CUSTOM;
454 else if (menutype == "dynamic-char-styles")
455 ftype = InsetLayout::CHARSTYLE;
457 // this should have been taken care of earlier
458 LASSERT(false, return);
461 TextClass::InsetLayouts const & inset_layouts =
462 d->text_class_->insetLayouts();
463 for (auto const & iit : inset_layouts) {
464 InsetLayout const & il = iit.second;
465 if (il.lyxtype() != ftype)
467 docstring const name = iit.first;
468 QString const loc_item = toqstr(translateIfPossible(
469 prefixIs(name, from_ascii("Flex:")) ?
470 name.substr(5) : name));
471 FuncRequest func(LFUN_FLEX_INSERT,
472 from_ascii("\"") + name + from_ascii("\""), FuncRequest::TOOLBAR);
474 new Action(func, getIcon(func, false), loc_item, loc_item, this);
480 void GuiToolbar::add(ToolbarItem const & item)
483 case ToolbarItem::SEPARATOR:
486 case ToolbarItem::LAYOUTS: {
487 LayoutBox * layout = owner_.getLayoutDialog();
488 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
489 layout, SLOT(setIconSize(QSize)));
490 QAction * action = addWidget(layout);
491 action->setVisible(true);
494 case ToolbarItem::MINIBUFFER:
495 command_buffer_ = new GuiCommandBuffer(&owner_);
496 addWidget(command_buffer_);
497 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
498 //setHorizontalStretchable(true);
500 case ToolbarItem::TABLEINSERT: {
501 QToolButton * tb = new QToolButton;
502 tb->setCheckable(true);
503 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
504 QString const label = qt_(to_ascii(item.label));
505 tb->setToolTip(label);
506 tb->setStatusTip(label);
508 InsertTableWidget * iv = new InsertTableWidget(tb);
509 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
510 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
511 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
515 case ToolbarItem::ICONPALETTE:
516 addWidget(new PaletteButton(this, item));
518 case ToolbarItem::POPUPMENU: {
519 addWidget(new StaticMenuButton(this, item, false));
522 case ToolbarItem::STICKYPOPUPMENU: {
523 addWidget(new StaticMenuButton(this, item, true));
526 case ToolbarItem::DYNAMICMENU: {
527 // we only handle certain things
528 if (DynamicMenuButton::isMenuType(item.name))
529 addWidget(new DynamicMenuButton(this, item));
531 LYXERR0("Unknown dynamic menu type: " << item.name);
534 case ToolbarItem::BIDICOMMAND: {
535 if (!getStatus(*item.func).unknown())
536 addAction(addItem(item));
539 case ToolbarItem::COMMAND: {
540 if (!getStatus(*item.func).unknown())
541 addAction(addItem(item));
550 void GuiToolbar::update(int context)
552 if (visibility_ & Toolbars::AUTO) {
553 setVisible(visibility_ & context & Toolbars::ALLOWAUTO);
554 if (isVisible() && commandBuffer() && (context & Toolbars::MINIBUFFER_FOCUS))
555 commandBuffer()->setFocus();
558 // update visible toolbars only
562 // This is a speed bottleneck because this is called on every keypress
563 // and update calls getStatus, which copies the cursor at least two times
564 for (int i = 0; i < actions_.size(); ++i)
565 actions_[i]->update();
567 LayoutBox * layout = owner_.getLayoutDialog();
569 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
576 QString GuiToolbar::sessionKey() const
578 return "views/" + QString::number(owner_.id()) + "/" + objectName();
582 void GuiToolbar::saveSession(QSettings & settings) const
584 settings.setValue(sessionKey() + "/visibility", visibility_);
585 settings.setValue(sessionKey() + "/movability", isMovable());
589 void GuiToolbar::restoreSession()
592 int const error_val = -1;
594 settings.value(sessionKey() + "/visibility", error_val).toInt();
595 if (visibility == error_val || visibility == 0) {
596 // This should not happen, but in case we use the defaults
597 LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
599 guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
601 setVisibility(visibility);
603 int movability = settings.value(sessionKey() + "/movability", true).toBool();
604 setMovable(movability);
608 bool GuiToolbar::isVisibiltyOn() const
610 return visibility_ & Toolbars::ON;
614 void GuiToolbar::toggle()
617 if (visibility_ & Toolbars::ALLOWAUTO) {
618 if (!(visibility_ & Toolbars::AUTO)) {
619 visibility_ |= Toolbars::AUTO;
623 visibility_ &= ~Toolbars::AUTO;
642 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
643 qstring_to_ucs4(windowTitle()), state));
646 void GuiToolbar::movable(bool silent)
649 setMovable(!isMovable());
651 // manual update avoids bug in qt that the drag handle is not removed
652 // properly, e.g. in Windows
655 // silence for toggling of many toolbars for performance
659 state = _("movable");
661 state = _("immovable");
662 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
663 qstring_to_ucs4(windowTitle()), state));
667 } // namespace frontend
670 #include "moc_GuiToolbar.cpp"