2 * \file qt4/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();
342 setMinimumWidth(sizeHint().width());
343 d->text_class_.reset();
348 string const & menutype = tbitem_.name_;
349 if (menutype == "dynamic-custom-insets" || menutype == "dynamic-char-styles") {
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 vector<docstring> ffList = bv->cursor().innerText()->getFreeFonts();
372 Action * default_act = nullptr;
373 for (auto const & f : ffList) {
374 FuncRequest func(LFUN_TEXTSTYLE_APPLY, convert<docstring>(i),
375 FuncRequest::TOOLBAR);
376 docstring const lb = char_type('&') + convert<docstring>(i)
377 + from_ascii(". ") + f ;
378 Action * act = new Action(func, QIcon(), toqstr(lb), toqstr(f), this);
380 // The most recent one is the default
385 // Add item to reset to defaults
386 Action * reset_act = new Action(FuncRequest(LFUN_FONT_DEFAULT, FuncRequest::TOOLBAR),
387 getIcon(FuncRequest(LFUN_UNDO), false),
388 qt_("&Reset to default"),
389 qt_("Reset all font settings to their defaults"), this);
390 m->addAction(reset_act);
392 QToolButton::setDefaultAction(default_act);
394 QToolButton::setIcon(getIcon(FuncRequest(LFUN_TEXTSTYLE_APPLY), false));
396 setPopupMode(QToolButton::MenuButtonPopup);
397 setEnabled(lyx::getStatus(FuncRequest(LFUN_TEXTSTYLE_APPLY)).enabled()
398 || lyx::getStatus(FuncRequest(LFUN_FONT_DEFAULT)).enabled());
399 } else if (menutype == "paste") {
401 docstring_list const sel = cap::availableSelections(&bv->buffer());
403 docstring_list::const_iterator cit = sel.begin();
404 docstring_list::const_iterator end = sel.end();
406 for (unsigned int index = 0; cit != end; ++cit, ++index) {
407 docstring const s = *cit;
408 FuncRequest func(LFUN_PASTE, convert<docstring>(index),
409 FuncRequest::TOOLBAR);
410 docstring const lb = char_type('&') + convert<docstring>(index)
411 + from_ascii(". ") + s ;
412 Action * act = new Action(func, QIcon(), toqstr(lb), toqstr(s), this);
415 Action * default_action = new Action(FuncRequest(LFUN_PASTE),
416 getIcon(FuncRequest(LFUN_PASTE), false),
417 qt_("Paste"), qt_("Paste"), this);
418 QToolButton::setDefaultAction(default_action);
419 setPopupMode(QToolButton::MenuButtonPopup);
420 setEnabled(lyx::getStatus(FuncRequest(LFUN_PASTE)).enabled());
425 void DynamicMenuButton::loadFlexInsets()
429 string const & menutype = tbitem_.name_;
430 InsetLayout::InsetLyXType ftype;
431 if (menutype == "dynamic-custom-insets")
432 ftype = InsetLayout::CUSTOM;
433 else if (menutype == "dynamic-char-styles")
434 ftype = InsetLayout::CHARSTYLE;
436 // this should have been taken care of earlier
437 LASSERT(false, return);
440 TextClass::InsetLayouts const & inset_layouts =
441 d->text_class_->insetLayouts();
442 for (auto const & iit : inset_layouts) {
443 InsetLayout const & il = iit.second;
444 if (il.lyxtype() != ftype)
446 docstring const name = iit.first;
447 QString const loc_item = toqstr(translateIfPossible(
448 prefixIs(name, from_ascii("Flex:")) ?
449 name.substr(5) : name));
450 FuncRequest func(LFUN_FLEX_INSERT,
451 from_ascii("\"") + name + from_ascii("\""), FuncRequest::TOOLBAR);
453 new Action(func, getIcon(func, false), loc_item, loc_item, this);
459 void GuiToolbar::add(ToolbarItem const & item)
461 switch (item.type_) {
462 case ToolbarItem::SEPARATOR:
465 case ToolbarItem::LAYOUTS: {
466 LayoutBox * layout = owner_.getLayoutDialog();
467 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
468 layout, SLOT(setIconSize(QSize)));
469 QAction * action = addWidget(layout);
470 action->setVisible(true);
473 case ToolbarItem::MINIBUFFER:
474 command_buffer_ = new GuiCommandBuffer(&owner_);
475 addWidget(command_buffer_);
476 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
477 //setHorizontalStretchable(true);
479 case ToolbarItem::TABLEINSERT: {
480 QToolButton * tb = new QToolButton;
481 tb->setCheckable(true);
482 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
483 QString const label = qt_(to_ascii(item.label_));
484 tb->setToolTip(label);
485 tb->setStatusTip(label);
487 InsertTableWidget * iv = new InsertTableWidget(tb);
488 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
489 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
490 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
494 case ToolbarItem::ICONPALETTE:
495 addWidget(new PaletteButton(this, item));
497 case ToolbarItem::POPUPMENU: {
498 addWidget(new StaticMenuButton(this, item, false));
501 case ToolbarItem::STICKYPOPUPMENU: {
502 addWidget(new StaticMenuButton(this, item, true));
505 case ToolbarItem::DYNAMICMENU: {
506 // we only handle certain things
507 if (DynamicMenuButton::isMenuType(item.name_))
508 addWidget(new DynamicMenuButton(this, item));
510 LYXERR0("Unknown dynamic menu type: " << item.name_);
513 case ToolbarItem::COMMAND: {
514 if (!getStatus(*item.func_).unknown())
515 addAction(addItem(item));
524 void GuiToolbar::update(int context)
526 if (visibility_ & Toolbars::AUTO) {
527 setVisible(visibility_ & context & Toolbars::ALLOWAUTO);
528 if (isVisible() && commandBuffer() && (context & Toolbars::MINIBUFFER_FOCUS))
529 commandBuffer()->setFocus();
532 // update visible toolbars only
536 // This is a speed bottleneck because this is called on every keypress
537 // and update calls getStatus, which copies the cursor at least two times
538 for (int i = 0; i < actions_.size(); ++i)
539 actions_[i]->update();
541 LayoutBox * layout = owner_.getLayoutDialog();
543 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
550 QString GuiToolbar::sessionKey() const
552 return "views/" + QString::number(owner_.id()) + "/" + objectName();
556 void GuiToolbar::saveSession(QSettings & settings) const
558 settings.setValue(sessionKey() + "/visibility", visibility_);
559 settings.setValue(sessionKey() + "/movability", isMovable());
563 void GuiToolbar::restoreSession()
566 int const error_val = -1;
568 settings.value(sessionKey() + "/visibility", error_val).toInt();
569 if (visibility == error_val || visibility == 0) {
570 // This should not happen, but in case we use the defaults
571 LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
573 guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
575 setVisibility(visibility);
577 int movability = settings.value(sessionKey() + "/movability", true).toBool();
578 setMovable(movability);
582 void GuiToolbar::toggle()
585 if (visibility_ & Toolbars::ALLOWAUTO) {
586 if (!(visibility_ & Toolbars::AUTO)) {
587 visibility_ |= Toolbars::AUTO;
591 visibility_ &= ~Toolbars::AUTO;
610 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
611 qstring_to_ucs4(windowTitle()), state));
614 void GuiToolbar::movable(bool silent)
617 setMovable(!isMovable());
619 // manual update avoids bug in qt that the drag handle is not removed
620 // properly, e.g. in Windows
623 // silence for toggling of many toolbars for performance
627 state = _("movable");
629 state = _("immovable");
630 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
631 qstring_to_ucs4(windowTitle()), state));
635 } // namespace frontend
638 #include "moc_GuiToolbar.cpp"