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"
43 #include <QToolButton>
45 #include "support/lassert.h"
48 using namespace lyx::support;
53 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
54 : QToolBar(toqstr(tbinfo.gui_name), &owner), visibility_(0),
55 allowauto_(false), owner_(owner), layout_(0), command_buffer_(0),
56 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;
93 allowauto_ = visibility_ >= Toolbars::MATH;
97 Action * GuiToolbar::addItem(ToolbarItem const & item)
99 QString text = toqstr(item.label_);
100 // Get the keys bound to this action, but keep only the
102 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(item.func_);
104 text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
106 Action * act = new Action(&owner_, getIcon(item.func_, false),
107 text, item.func_, text, this);
108 actions_.append(act);
114 class PaletteButton : public QToolButton
118 ToolbarItem const & tbitem_;
121 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
122 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
124 QString const label = qt_(to_ascii(tbitem_.label_));
128 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
129 this, SLOT(setIconSize(QSize)));
131 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
133 // use the icon of first action for the toolbar button
134 setIcon(getIcon(tbinfo->items.begin()->func_, true));
137 void mousePressEvent(QMouseEvent * e)
140 QToolButton::mousePressEvent(e);
146 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
148 LYXERR0("Unknown toolbar " << tbitem_.name_);
151 IconPalette * panel = new IconPalette(this);
152 QString const label = qt_(to_ascii(tbitem_.label_));
153 panel->setWindowTitle(label);
154 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
155 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
156 ToolbarInfo::item_iterator it = tbinfo->items.begin();
157 ToolbarInfo::item_iterator const end = tbinfo->items.end();
158 for (; it != end; ++it)
159 if (!getStatus(it->func_).unknown())
160 panel->addButton(bar_->addItem(*it));
162 QToolButton::mousePressEvent(e);
169 MenuButton::MenuButton(GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
170 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
172 setPopupMode(QToolButton::InstantPopup);
173 QString const label = qt_(to_ascii(tbitem_.label_));
177 setIcon(QIcon(getPixmap("images/math/", toqstr(tbitem_.name_), "png")));
179 connect(this, SIGNAL(triggered(QAction *)),
180 this, SLOT(actionTriggered(QAction *)));
181 connect(bar, SIGNAL(iconSizeChanged(QSize)),
182 this, SLOT(setIconSize(QSize)));
185 void MenuButton::mousePressEvent(QMouseEvent * e)
188 QToolButton::mousePressEvent(e);
194 QString const label = qt_(to_ascii(tbitem_.label_));
195 ButtonMenu * m = new ButtonMenu(label, this);
196 m->setWindowTitle(label);
197 m->setTearOffEnabled(true);
198 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
199 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
201 LYXERR0("Unknown toolbar " << tbitem_.name_);
204 ToolbarInfo::item_iterator it = tbinfo->items.begin();
205 ToolbarInfo::item_iterator const end = tbinfo->items.end();
206 for (; it != end; ++it)
207 if (!getStatus(it->func_).unknown())
208 m->add(bar_->addItem(*it));
211 QToolButton::mousePressEvent(e);
215 void MenuButton::actionTriggered(QAction * action)
217 QToolButton::setDefaultAction(action);
218 setPopupMode(QToolButton::DelayedPopup);
222 void GuiToolbar::add(ToolbarItem const & item)
224 switch (item.type_) {
225 case ToolbarItem::SEPARATOR:
228 case ToolbarItem::LAYOUTS:
229 layout_ = new LayoutBox(this, owner_);
232 case ToolbarItem::MINIBUFFER:
233 command_buffer_ = new GuiCommandBuffer(&owner_);
234 addWidget(command_buffer_);
235 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
236 //setHorizontalStretchable(true);
238 case ToolbarItem::TABLEINSERT: {
239 QToolButton * tb = new QToolButton;
240 tb->setCheckable(true);
241 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
242 QString const label = qt_(to_ascii(item.label_));
243 tb->setToolTip(label);
244 tb->setStatusTip(label);
246 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
247 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
248 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
249 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
253 case ToolbarItem::ICONPALETTE:
254 addWidget(new PaletteButton(this, item));
257 case ToolbarItem::POPUPMENU: {
258 addWidget(new MenuButton(this, item, false));
261 case ToolbarItem::STICKYPOPUPMENU: {
262 addWidget(new MenuButton(this, item, true));
265 case ToolbarItem::COMMAND: {
266 if (!getStatus(item.func_).unknown())
267 addAction(addItem(item));
276 void GuiToolbar::update(bool in_math, bool in_table, bool in_review,
277 bool in_mathmacrotemplate)
279 if (visibility_ & Toolbars::AUTO) {
280 bool show_it = (in_math && (visibility_ & Toolbars::MATH))
281 || (in_table && (visibility_ & Toolbars::TABLE))
282 || (in_review && (visibility_ & Toolbars::REVIEW))
283 || (in_mathmacrotemplate && (visibility_ & Toolbars::MATHMACROTEMPLATE));
287 // update visible toolbars only
291 // This is a speed bottleneck because this is called on every keypress
292 // and update calls getStatus, which copies the cursor at least two times
293 for (int i = 0; i < actions_.size(); ++i)
294 actions_[i]->update();
297 layout_->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
304 QString GuiToolbar::sessionKey() const
306 return "views/" + QString::number(owner_.id()) + "/" + objectName();
310 void GuiToolbar::saveSession() const
313 settings.setValue(sessionKey() + "/visibility", visibility_);
317 void GuiToolbar::restoreSession()
320 setVisibility(settings.value(sessionKey() + "/visibility").toInt());
324 void GuiToolbar::toggle()
328 if (!(visibility_ & Toolbars::AUTO)) {
329 visibility_ |= Toolbars::AUTO;
333 visibility_ &= ~Toolbars::AUTO;
352 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
353 qstring_to_ucs4(windowTitle()), state));
356 } // namespace frontend
359 #include "moc_GuiToolbar.cpp"