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"
36 #include "support/debug.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
44 #include <QToolButton>
46 #include "support/lassert.h"
49 using namespace lyx::support;
54 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
55 : QToolBar(toqstr(tbinfo.gui_name), &owner), visibility_(0),
56 owner_(owner), command_buffer_(0), tbinfo_(tbinfo), filled_(false)
58 setIconSize(owner.iconSize());
59 connect(&owner, SIGNAL(iconSizeChanged(QSize)), this,
60 SLOT(setIconSize(QSize)));
62 // Toolbar dragging is allowed.
64 // This is used by QMainWindow::restoreState for proper main window state
66 setObjectName(toqstr(tbinfo.name));
71 void GuiToolbar::fill()
75 ToolbarInfo::item_iterator it = tbinfo_.items.begin();
76 ToolbarInfo::item_iterator end = tbinfo_.items.end();
77 for (; it != end; ++it)
83 void GuiToolbar::showEvent(QShowEvent * ev)
90 void GuiToolbar::setVisibility(int visibility)
92 visibility_ = visibility;
96 Action * GuiToolbar::addItem(ToolbarItem const & item)
98 QString text = toqstr(item.label_);
99 // Get the keys bound to this action, but keep only the
101 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(item.func_);
103 text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
105 Action * act = new Action(&owner_, getIcon(item.func_, false),
106 text, item.func_, text, this);
107 actions_.append(act);
113 class PaletteButton : public QToolButton
117 ToolbarItem const & tbitem_;
120 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
121 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
123 QString const label = qt_(to_ascii(tbitem_.label_));
127 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
128 this, SLOT(setIconSize(QSize)));
130 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
132 // use the icon of first action for the toolbar button
133 setIcon(getIcon(tbinfo->items.begin()->func_, true));
136 void mousePressEvent(QMouseEvent * e)
139 QToolButton::mousePressEvent(e);
145 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
147 LYXERR0("Unknown toolbar " << tbitem_.name_);
150 IconPalette * panel = new IconPalette(this);
151 QString const label = qt_(to_ascii(tbitem_.label_));
152 panel->setWindowTitle(label);
153 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
154 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
155 ToolbarInfo::item_iterator it = tbinfo->items.begin();
156 ToolbarInfo::item_iterator const end = tbinfo->items.end();
157 for (; it != end; ++it)
158 if (!getStatus(it->func_).unknown())
159 panel->addButton(bar_->addItem(*it));
161 QToolButton::mousePressEvent(e);
168 MenuButton::MenuButton(GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
169 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
171 setPopupMode(QToolButton::InstantPopup);
172 QString const label = qt_(to_ascii(tbitem_.label_));
176 QString const name = toqstr(tbitem_.name_);
177 QString imagedir = "images/math/";
178 FileName const fname = imageLibFileSearch(imagedir, name, "png");
179 if (fname.exists()) {
180 setIcon(QIcon(getPixmap(imagedir, name, "png")));
182 imagedir = "images/";
183 imageLibFileSearch(imagedir, name, "png");
184 setIcon(QIcon(getPixmap(imagedir, name, "png")));
187 connect(this, SIGNAL(triggered(QAction *)),
188 this, SLOT(actionTriggered(QAction *)));
189 connect(bar, SIGNAL(iconSizeChanged(QSize)),
190 this, SLOT(setIconSize(QSize)));
193 void MenuButton::mousePressEvent(QMouseEvent * e)
196 QToolButton::mousePressEvent(e);
202 QString const label = qt_(to_ascii(tbitem_.label_));
203 ButtonMenu * m = new ButtonMenu(label, this);
204 m->setWindowTitle(label);
205 m->setTearOffEnabled(true);
206 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
207 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
208 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
210 LYXERR0("Unknown toolbar " << tbitem_.name_);
213 ToolbarInfo::item_iterator it = tbinfo->items.begin();
214 ToolbarInfo::item_iterator const end = tbinfo->items.end();
215 for (; it != end; ++it)
216 if (!getStatus(it->func_).unknown())
217 m->add(bar_->addItem(*it));
220 QToolButton::mousePressEvent(e);
224 void MenuButton::actionTriggered(QAction * action)
226 QToolButton::setDefaultAction(action);
227 setPopupMode(QToolButton::DelayedPopup);
231 void MenuButton::updateTriggered()
236 bool enabled = false;
237 QList<QAction *> acts = menu()->actions();
238 for (int i = 0; i < acts.size(); ++i)
239 if (acts[i]->isEnabled()) {
243 // Enable the MenuButton if at least one menu item is enabled
245 // If a disabled item is default, switch to InstantPopup
246 // (this can happen if a user selects e.g. DVI and then
247 // turns non-TeX fonts on)
248 if (defaultAction() && !defaultAction()->isEnabled())
249 setPopupMode(QToolButton::InstantPopup);
253 void GuiToolbar::add(ToolbarItem const & item)
255 switch (item.type_) {
256 case ToolbarItem::SEPARATOR:
259 case ToolbarItem::LAYOUTS: {
260 LayoutBox * layout = owner_.getLayoutDialog();
261 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
262 layout, SLOT(setIconSize(QSize)));
263 QAction * action = addWidget(layout);
264 action->setVisible(true);
267 case ToolbarItem::MINIBUFFER:
268 command_buffer_ = new GuiCommandBuffer(&owner_);
269 addWidget(command_buffer_);
270 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
271 //setHorizontalStretchable(true);
273 case ToolbarItem::TABLEINSERT: {
274 QToolButton * tb = new QToolButton;
275 tb->setCheckable(true);
276 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
277 QString const label = qt_(to_ascii(item.label_));
278 tb->setToolTip(label);
279 tb->setStatusTip(label);
281 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
282 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
283 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
284 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
288 case ToolbarItem::ICONPALETTE:
289 addWidget(new PaletteButton(this, item));
292 case ToolbarItem::POPUPMENU: {
293 addWidget(new MenuButton(this, item, false));
296 case ToolbarItem::STICKYPOPUPMENU: {
297 addWidget(new MenuButton(this, item, true));
300 case ToolbarItem::COMMAND: {
301 if (!getStatus(item.func_).unknown())
302 addAction(addItem(item));
311 void GuiToolbar::update(bool in_math, bool in_table, bool in_review,
312 bool in_mathmacrotemplate)
314 if (visibility_ & Toolbars::AUTO) {
315 bool show_it = (in_math && (visibility_ & Toolbars::MATH))
316 || (in_table && (visibility_ & Toolbars::TABLE))
317 || (in_review && (visibility_ & Toolbars::REVIEW))
318 || (in_mathmacrotemplate && (visibility_ & Toolbars::MATHMACROTEMPLATE));
322 // update visible toolbars only
326 // This is a speed bottleneck because this is called on every keypress
327 // and update calls getStatus, which copies the cursor at least two times
328 for (int i = 0; i < actions_.size(); ++i)
329 actions_[i]->update();
331 LayoutBox * layout = owner_.getLayoutDialog();
333 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
340 QString GuiToolbar::sessionKey() const
342 return "views/" + QString::number(owner_.id()) + "/" + objectName();
346 void GuiToolbar::saveSession() const
349 settings.setValue(sessionKey() + "/visibility", visibility_);
353 void GuiToolbar::restoreSession()
356 int const error_val = -1;
358 settings.value(sessionKey() + "/visibility", error_val).toInt();
359 if (visibility == error_val || visibility == 0) {
360 // This should not happen, but in case we use the defaults
361 LYXERR0("Session settings could not be found! Defaults are used instead.");
363 guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
365 setVisibility(visibility);
369 void GuiToolbar::toggle()
372 if (visibility_ & Toolbars::ALLOWAUTO) {
373 if (!(visibility_ & Toolbars::AUTO)) {
374 visibility_ |= Toolbars::AUTO;
378 visibility_ &= ~Toolbars::AUTO;
397 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
398 qstring_to_ucs4(windowTitle()), state));
401 } // namespace frontend
404 #include "moc_GuiToolbar.cpp"