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 "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "GuiApplication.h"
28 #include "GuiCommandBuffer.h"
30 #include "IconPalette.h"
31 #include "InsertTableWidget.h"
33 #include "LayoutBox.h"
36 #include "qt_helpers.h"
39 #include "TextClass.h"
42 #include "insets/InsetText.h"
44 #include "support/debug.h"
45 #include "support/gettext.h"
46 #include "support/lstrings.h"
52 #include <QToolButton>
54 #include "support/lassert.h"
57 using namespace lyx::support;
62 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
63 : QToolBar(toqstr(tbinfo.gui_name), &owner), visibility_(0),
64 owner_(owner), command_buffer_(0), tbinfo_(tbinfo), filled_(false),
67 setIconSize(owner.iconSize());
68 connect(&owner, SIGNAL(iconSizeChanged(QSize)), this,
69 SLOT(setIconSize(QSize)));
71 // This is used by QMainWindow::restoreState for proper main window state
73 setObjectName(toqstr(tbinfo.name));
78 void GuiToolbar::setVisible(bool visible)
80 // This is a hack to find out which toolbars have been restored by
81 // MainWindow::restoreState and which toolbars should be initialized
82 // by us (i.e., new toolbars)
84 QToolBar::setVisible(visible);
88 bool GuiToolbar::isRestored() const
94 void GuiToolbar::fill()
98 ToolbarInfo::item_iterator it = tbinfo_.items.begin();
99 ToolbarInfo::item_iterator end = tbinfo_.items.end();
100 for (; it != end; ++it)
106 void GuiToolbar::showEvent(QShowEvent * ev)
113 void GuiToolbar::setVisibility(int visibility)
115 visibility_ = visibility;
119 Action * GuiToolbar::addItem(ToolbarItem const & item)
121 QString text = toqstr(item.label_);
122 // Get the keys bound to this action, but keep only the
124 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(*item.func_);
125 if (!bindings.empty())
126 text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
128 Action * act = new Action(item.func_, getIcon(*item.func_, false), text,
130 actions_.append(act);
136 class PaletteButton : public QToolButton
140 ToolbarItem const & tbitem_;
143 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
144 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
146 QString const label = qt_(to_ascii(tbitem_.label_));
150 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
151 this, SLOT(setIconSize(QSize)));
153 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
155 // use the icon of first action for the toolbar button
156 setIcon(getIcon(*tbinfo->items.begin()->func_, true));
159 void mousePressEvent(QMouseEvent * e)
162 QToolButton::mousePressEvent(e);
168 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
170 LYXERR0("Unknown toolbar " << tbitem_.name_);
173 IconPalette * panel = new IconPalette(this);
174 QString const label = qt_(to_ascii(tbitem_.label_));
175 panel->setWindowTitle(label);
176 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
177 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
178 ToolbarInfo::item_iterator it = tbinfo->items.begin();
179 ToolbarInfo::item_iterator const end = tbinfo->items.end();
180 for (; it != end; ++it)
181 if (!getStatus(*it->func_).unknown())
182 panel->addButton(bar_->addItem(*it));
184 QToolButton::mousePressEvent(e);
191 MenuButtonBase::MenuButtonBase(GuiToolbar * bar, ToolbarItem const & item)
192 : QToolButton(bar), bar_(bar), tbitem_(item)
194 setPopupMode(QToolButton::InstantPopup);
195 QString const label = qt_(to_ascii(tbitem_.label_));
199 QString const name = toqstr(tbitem_.name_);
200 QStringList imagedirs;
201 imagedirs << "images/math/" << "images/";
202 for (int i = 0; i < imagedirs.size(); ++i) {
203 QString imagedir = imagedirs.at(i);
204 FileName const fname = imageLibFileSearch(imagedir, name, "svgz,png",
205 theGuiApp()->imageSearchMode());
206 if (fname.exists()) {
207 setIcon(QIcon(getPixmap(imagedir, name, "svgz,png")));
214 void MenuButtonBase::actionTriggered(QAction * action)
216 QToolButton::setDefaultAction(action);
217 setPopupMode(QToolButton::DelayedPopup);
221 StaticMenuButton::StaticMenuButton(
222 GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
223 : MenuButtonBase(bar, item)
226 connect(this, SIGNAL(triggered(QAction *)),
227 this, SLOT(actionTriggered(QAction *)));
228 connect(bar, SIGNAL(iconSizeChanged(QSize)),
229 this, SLOT(setIconSize(QSize)));
234 void StaticMenuButton::initialize()
236 QString const label = qt_(to_ascii(tbitem_.label_));
237 ButtonMenu * m = new ButtonMenu(label, this);
238 m->setWindowTitle(label);
239 m->setTearOffEnabled(true);
240 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
241 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
242 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
244 LYXERR0("Unknown toolbar " << tbitem_.name_);
247 ToolbarInfo::item_iterator it = tbinfo->items.begin();
248 ToolbarInfo::item_iterator const end = tbinfo->items.end();
249 for (; it != end; ++it)
250 if (!getStatus(*it->func_).unknown())
251 m->add(bar_->addItem(*it));
256 void StaticMenuButton::updateTriggered()
261 bool enabled = false;
262 QList<QAction *> acts = menu()->actions();
263 for (int i = 0; i < acts.size(); ++i)
264 if (acts[i]->isEnabled()) {
268 // Enable the MenuButton if at least one menu item is enabled
270 // If a disabled item is default, switch to InstantPopup
271 // (this can happen if a user selects e.g. DVI and then
272 // turns non-TeX fonts on)
273 if (defaultAction() && !defaultAction()->isEnabled())
274 setPopupMode(QToolButton::InstantPopup);
278 class DynamicMenuButton::Private
281 Private(Private const &);
282 void operator=(Private const &);
284 Private() : inset_(0) {}
286 DocumentClassConstPtr text_class_;
288 InsetText const * inset_;
292 DynamicMenuButton::DynamicMenuButton(GuiToolbar * bar, ToolbarItem const & item)
293 : MenuButtonBase(bar, item), d(new Private())
299 DynamicMenuButton::~DynamicMenuButton()
305 void DynamicMenuButton::initialize()
307 QString const label = qt_(to_ascii(tbitem_.label_));
308 ButtonMenu * m = new ButtonMenu(label, this);
309 m->setWindowTitle(label);
310 m->setTearOffEnabled(true);
311 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
312 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
313 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
314 this, SLOT(setIconSize(QSize)));
319 bool DynamicMenuButton::isMenuType(string const & s)
321 return s == "dynamic-custom-insets" ||
322 s == "dynamic-char-styles";
326 void DynamicMenuButton::updateTriggered()
329 // the menu should exist by this point
330 // if not, we can at least avoid crashing in release mode
332 GuiView const & owner = bar_->owner();
333 BufferView const * bv = owner.currentBufferView();
337 setMinimumWidth(sizeHint().width());
338 d->text_class_.reset();
343 DocumentClassConstPtr text_class =
344 bv->buffer().params().documentClassPtr();
345 InsetText const * inset = &(bv->cursor().innerText()->inset());
346 // if the text class has changed, then we need to reload the menu
347 if (d->text_class_ != text_class) {
348 d->text_class_ = text_class;
349 // at the moment, we can just call loadFlexInsets, and it will
350 // handle both types. if there were more types of menus, then we
351 // might need to have other options.
354 // remember where we are
356 // note that enabling here might need to be more subtle if there
357 // were other kinds of menus.
358 setEnabled(!bv->buffer().isReadonly() &&
360 inset->insetAllowed(FLEX_CODE));
364 void DynamicMenuButton::loadFlexInsets()
368 string const & menutype = tbitem_.name_;
369 InsetLayout::InsetLyXType ftype;
370 if (menutype == "dynamic-custom-insets")
371 ftype = InsetLayout::CUSTOM;
372 else if (menutype == "dynamic-char-styles")
373 ftype = InsetLayout::CHARSTYLE;
375 // this should have been taken care of earlier
376 LASSERT(false, return);
379 TextClass::InsetLayouts const & inset_layouts =
380 d->text_class_->insetLayouts();
381 for (auto const & iit : inset_layouts) {
382 InsetLayout const & il = iit.second;
383 if (il.lyxtype() != ftype)
385 docstring const name = iit.first;
386 QString const loc_item = toqstr(translateIfPossible(
387 prefixIs(name, from_ascii("Flex:")) ?
388 name.substr(5) : name));
389 FuncRequest func(LFUN_FLEX_INSERT,
390 from_ascii("\"") + name + from_ascii("\""), FuncRequest::TOOLBAR);
392 new Action(func, getIcon(func, false), loc_item, loc_item, this);
398 void GuiToolbar::add(ToolbarItem const & item)
400 switch (item.type_) {
401 case ToolbarItem::SEPARATOR:
404 case ToolbarItem::LAYOUTS: {
405 LayoutBox * layout = owner_.getLayoutDialog();
406 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
407 layout, SLOT(setIconSize(QSize)));
408 QAction * action = addWidget(layout);
409 action->setVisible(true);
412 case ToolbarItem::MINIBUFFER:
413 command_buffer_ = new GuiCommandBuffer(&owner_);
414 addWidget(command_buffer_);
415 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
416 //setHorizontalStretchable(true);
418 case ToolbarItem::TABLEINSERT: {
419 QToolButton * tb = new QToolButton;
420 tb->setCheckable(true);
421 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
422 QString const label = qt_(to_ascii(item.label_));
423 tb->setToolTip(label);
424 tb->setStatusTip(label);
426 InsertTableWidget * iv = new InsertTableWidget(tb);
427 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
428 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
429 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
433 case ToolbarItem::ICONPALETTE:
434 addWidget(new PaletteButton(this, item));
436 case ToolbarItem::POPUPMENU: {
437 addWidget(new StaticMenuButton(this, item, false));
440 case ToolbarItem::STICKYPOPUPMENU: {
441 addWidget(new StaticMenuButton(this, item, true));
444 case ToolbarItem::DYNAMICMENU: {
445 // we only handle certain things
446 if (DynamicMenuButton::isMenuType(item.name_))
447 addWidget(new DynamicMenuButton(this, item));
449 LYXERR0("Unknown dynamic menu type: " << item.name_);
452 case ToolbarItem::COMMAND: {
453 if (!getStatus(*item.func_).unknown())
454 addAction(addItem(item));
463 void GuiToolbar::update(int context)
465 if (visibility_ & Toolbars::AUTO) {
466 setVisible(visibility_ & context & Toolbars::ALLOWAUTO);
467 if (isVisible() && commandBuffer() && (context & Toolbars::MINIBUFFER_FOCUS))
468 commandBuffer()->setFocus();
471 // update visible toolbars only
475 // This is a speed bottleneck because this is called on every keypress
476 // and update calls getStatus, which copies the cursor at least two times
477 for (int i = 0; i < actions_.size(); ++i)
478 actions_[i]->update();
480 LayoutBox * layout = owner_.getLayoutDialog();
482 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
489 QString GuiToolbar::sessionKey() const
491 return "views/" + QString::number(owner_.id()) + "/" + objectName();
495 void GuiToolbar::saveSession(QSettings & settings) const
497 settings.setValue(sessionKey() + "/visibility", visibility_);
498 settings.setValue(sessionKey() + "/movability", isMovable());
502 void GuiToolbar::restoreSession()
505 int const error_val = -1;
507 settings.value(sessionKey() + "/visibility", error_val).toInt();
508 if (visibility == error_val || visibility == 0) {
509 // This should not happen, but in case we use the defaults
510 LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
512 guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
514 setVisibility(visibility);
516 int movability = settings.value(sessionKey() + "/movability", true).toBool();
517 setMovable(movability);
521 void GuiToolbar::toggle()
524 if (visibility_ & Toolbars::ALLOWAUTO) {
525 if (!(visibility_ & Toolbars::AUTO)) {
526 visibility_ |= Toolbars::AUTO;
530 visibility_ &= ~Toolbars::AUTO;
549 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
550 qstring_to_ucs4(windowTitle()), state));
553 void GuiToolbar::movable(bool silent)
556 setMovable(!isMovable());
558 // manual update avoids bug in qt that the drag handle is not removed
559 // properly, e.g. in Windows
562 // silence for toggling of many toolbars for performance
566 state = _("movable");
568 state = _("immovable");
569 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
570 qstring_to_ucs4(windowTitle()), state));
574 } // namespace frontend
577 #include "moc_GuiToolbar.cpp"