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"
21 #include "GuiApplication.h"
22 #include "GuiCommandBuffer.h"
24 #include "IconPalette.h"
25 #include "InsertTableWidget.h"
26 #include "LayoutBox.h"
27 #include "qt_helpers.h"
30 #include "FuncRequest.h"
31 #include "FuncStatus.h"
37 #include "support/debug.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
45 #include <QToolButton>
47 #include "support/lassert.h"
50 using namespace lyx::support;
55 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
56 : QToolBar(toqstr(tbinfo.gui_name), &owner), visibility_(0),
57 owner_(owner), command_buffer_(0), tbinfo_(tbinfo), filled_(false),
60 setIconSize(owner.iconSize());
61 connect(&owner, SIGNAL(iconSizeChanged(QSize)), this,
62 SLOT(setIconSize(QSize)));
64 // Toolbar dragging is allowed.
66 // This is used by QMainWindow::restoreState for proper main window state
68 setObjectName(toqstr(tbinfo.name));
73 void GuiToolbar::setVisible(bool visible)
75 // This is a hack to find out which toolbars have been restored by
76 // MainWindow::restoreState and which toolbars should be initialized
77 // by us (i.e., new toolbars)
79 QToolBar::setVisible(visible);
83 bool GuiToolbar::isRestored() const
89 void GuiToolbar::fill()
93 ToolbarInfo::item_iterator it = tbinfo_.items.begin();
94 ToolbarInfo::item_iterator end = tbinfo_.items.end();
95 for (; it != end; ++it)
101 void GuiToolbar::showEvent(QShowEvent * ev)
108 void GuiToolbar::setVisibility(int visibility)
110 visibility_ = visibility;
114 Action * GuiToolbar::addItem(ToolbarItem const & item)
116 QString text = toqstr(item.label_);
117 // Get the keys bound to this action, but keep only the
119 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(item.func_);
120 if (!bindings.empty())
121 text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
123 Action * act = new Action(getIcon(item.func_, false),
124 text, item.func_, text, this);
125 actions_.append(act);
131 class PaletteButton : public QToolButton
135 ToolbarItem const & tbitem_;
138 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
139 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
141 QString const label = qt_(to_ascii(tbitem_.label_));
145 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
146 this, SLOT(setIconSize(QSize)));
148 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
150 // use the icon of first action for the toolbar button
151 setIcon(getIcon(tbinfo->items.begin()->func_, true));
154 void mousePressEvent(QMouseEvent * e)
157 QToolButton::mousePressEvent(e);
163 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
165 LYXERR0("Unknown toolbar " << tbitem_.name_);
168 IconPalette * panel = new IconPalette(this);
169 QString const label = qt_(to_ascii(tbitem_.label_));
170 panel->setWindowTitle(label);
171 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
172 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
173 ToolbarInfo::item_iterator it = tbinfo->items.begin();
174 ToolbarInfo::item_iterator const end = tbinfo->items.end();
175 for (; it != end; ++it)
176 if (!getStatus(it->func_).unknown())
177 panel->addButton(bar_->addItem(*it));
179 QToolButton::mousePressEvent(e);
186 MenuButton::MenuButton(GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
187 : QToolButton(bar), bar_(bar), tbitem_(item)
189 setPopupMode(QToolButton::InstantPopup);
190 QString const label = qt_(to_ascii(tbitem_.label_));
194 QString const name = toqstr(tbitem_.name_);
195 QStringList imagedirs;
196 imagedirs << "images/math/" << "images/";
197 for (int i = 0; i < imagedirs.size(); ++i) {
198 QString imagedir = imagedirs.at(i);
199 FileName const fname = imageLibFileSearch(imagedir, name, "svgz,png",
200 theGuiApp()->imageSearchMode());
201 if (fname.exists()) {
202 setIcon(QIcon(getPixmap(imagedir, name, "svgz,png")));
207 connect(this, SIGNAL(triggered(QAction *)),
208 this, SLOT(actionTriggered(QAction *)));
209 connect(bar, SIGNAL(iconSizeChanged(QSize)),
210 this, SLOT(setIconSize(QSize)));
215 void MenuButton::initialize()
217 QString const label = qt_(to_ascii(tbitem_.label_));
218 ButtonMenu * m = new ButtonMenu(label, this);
219 m->setWindowTitle(label);
220 m->setTearOffEnabled(true);
221 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
222 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
223 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
225 LYXERR0("Unknown toolbar " << tbitem_.name_);
228 ToolbarInfo::item_iterator it = tbinfo->items.begin();
229 ToolbarInfo::item_iterator const end = tbinfo->items.end();
230 for (; it != end; ++it)
231 if (!getStatus(it->func_).unknown())
232 m->add(bar_->addItem(*it));
237 void MenuButton::actionTriggered(QAction * action)
239 QToolButton::setDefaultAction(action);
240 setPopupMode(QToolButton::DelayedPopup);
244 void MenuButton::updateTriggered()
249 bool enabled = false;
250 QList<QAction *> acts = menu()->actions();
251 for (int i = 0; i < acts.size(); ++i)
252 if (acts[i]->isEnabled()) {
256 // Enable the MenuButton if at least one menu item is enabled
258 // If a disabled item is default, switch to InstantPopup
259 // (this can happen if a user selects e.g. DVI and then
260 // turns non-TeX fonts on)
261 if (defaultAction() && !defaultAction()->isEnabled())
262 setPopupMode(QToolButton::InstantPopup);
266 void GuiToolbar::add(ToolbarItem const & item)
268 switch (item.type_) {
269 case ToolbarItem::SEPARATOR:
272 case ToolbarItem::LAYOUTS: {
273 LayoutBox * layout = owner_.getLayoutDialog();
274 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
275 layout, SLOT(setIconSize(QSize)));
276 QAction * action = addWidget(layout);
277 action->setVisible(true);
280 case ToolbarItem::MINIBUFFER:
281 command_buffer_ = new GuiCommandBuffer(&owner_);
282 addWidget(command_buffer_);
283 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
284 //setHorizontalStretchable(true);
286 case ToolbarItem::TABLEINSERT: {
287 QToolButton * tb = new QToolButton;
288 tb->setCheckable(true);
289 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
290 QString const label = qt_(to_ascii(item.label_));
291 tb->setToolTip(label);
292 tb->setStatusTip(label);
294 InsertTableWidget * iv = new InsertTableWidget(tb);
295 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
296 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
297 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
301 case ToolbarItem::ICONPALETTE:
302 addWidget(new PaletteButton(this, item));
305 case ToolbarItem::POPUPMENU: {
306 addWidget(new MenuButton(this, item, false));
309 case ToolbarItem::STICKYPOPUPMENU: {
310 addWidget(new MenuButton(this, item, true));
313 case ToolbarItem::COMMAND: {
314 if (!getStatus(item.func_).unknown())
315 addAction(addItem(item));
324 void GuiToolbar::update(int context)
326 if (visibility_ & Toolbars::AUTO) {
327 bool const was_visible = isVisible();
328 setVisible(visibility_ & context & Toolbars::ALLOWAUTO);
329 if (isVisible() && !was_visible && commandBuffer())
330 commandBuffer()->setFocus();
333 // update visible toolbars only
337 // This is a speed bottleneck because this is called on every keypress
338 // and update calls getStatus, which copies the cursor at least two times
339 for (int i = 0; i < actions_.size(); ++i)
340 actions_[i]->update();
342 LayoutBox * layout = owner_.getLayoutDialog();
344 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
351 QString GuiToolbar::sessionKey() const
353 return "views/" + QString::number(owner_.id()) + "/" + objectName();
357 void GuiToolbar::saveSession() const
360 settings.setValue(sessionKey() + "/visibility", visibility_);
364 void GuiToolbar::restoreSession()
367 int const error_val = -1;
369 settings.value(sessionKey() + "/visibility", error_val).toInt();
370 if (visibility == error_val || visibility == 0) {
371 // This should not happen, but in case we use the defaults
372 LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
374 guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
376 setVisibility(visibility);
380 void GuiToolbar::toggle()
383 if (visibility_ & Toolbars::ALLOWAUTO) {
384 if (!(visibility_ & Toolbars::AUTO)) {
385 visibility_ |= Toolbars::AUTO;
389 visibility_ &= ~Toolbars::AUTO;
408 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
409 qstring_to_ucs4(windowTitle()), state));
412 } // namespace frontend
415 #include "moc_GuiToolbar.cpp"