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 allowauto_(false), owner_(owner), command_buffer_(0),
57 tbinfo_(tbinfo), filled_(false)
59 setIconSize(owner.iconSize());
60 connect(&owner, SIGNAL(iconSizeChanged(QSize)), this,
61 SLOT(setIconSize(QSize)));
63 // Toolbar dragging is allowed.
65 // This is used by QMainWindow::restoreState for proper main window state
67 setObjectName(toqstr(tbinfo.name));
72 void GuiToolbar::fill()
76 ToolbarInfo::item_iterator it = tbinfo_.items.begin();
77 ToolbarInfo::item_iterator end = tbinfo_.items.end();
78 for (; it != end; ++it)
84 void GuiToolbar::showEvent(QShowEvent * ev)
91 void GuiToolbar::setVisibility(int visibility)
93 visibility_ = visibility;
94 allowauto_ = visibility_ >= Toolbars::MATH;
98 Action * GuiToolbar::addItem(ToolbarItem const & item)
100 QString text = toqstr(item.label_);
101 // Get the keys bound to this action, but keep only the
103 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(item.func_);
105 text += " [" + toqstr(bindings.begin()->print(KeySequence::ForGui)) + "]";
107 Action * act = new Action(&owner_, getIcon(item.func_, false),
108 text, item.func_, text, this);
109 actions_.append(act);
115 class PaletteButton : public QToolButton
119 ToolbarItem const & tbitem_;
122 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
123 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
125 QString const label = qt_(to_ascii(tbitem_.label_));
129 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
130 this, SLOT(setIconSize(QSize)));
132 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
134 // use the icon of first action for the toolbar button
135 setIcon(getIcon(tbinfo->items.begin()->func_, true));
138 void mousePressEvent(QMouseEvent * e)
141 QToolButton::mousePressEvent(e);
147 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
149 LYXERR0("Unknown toolbar " << tbitem_.name_);
152 IconPalette * panel = new IconPalette(this);
153 QString const label = qt_(to_ascii(tbitem_.label_));
154 panel->setWindowTitle(label);
155 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
156 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
157 ToolbarInfo::item_iterator it = tbinfo->items.begin();
158 ToolbarInfo::item_iterator const end = tbinfo->items.end();
159 for (; it != end; ++it)
160 if (!getStatus(it->func_).unknown())
161 panel->addButton(bar_->addItem(*it));
163 QToolButton::mousePressEvent(e);
170 MenuButton::MenuButton(GuiToolbar * bar, ToolbarItem const & item, bool const sticky)
171 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
173 setPopupMode(QToolButton::InstantPopup);
174 QString const label = qt_(to_ascii(tbitem_.label_));
178 QString const name = toqstr(tbitem_.name_);
179 FileName const fname = libFileSearch("images/math/", name, "png");
181 setIcon(QIcon(getPixmap("images/math/", name, "png")));
183 setIcon(QIcon(getPixmap("images/", name, "png")));
185 connect(this, SIGNAL(triggered(QAction *)),
186 this, SLOT(actionTriggered(QAction *)));
187 connect(bar, SIGNAL(iconSizeChanged(QSize)),
188 this, SLOT(setIconSize(QSize)));
191 void MenuButton::mousePressEvent(QMouseEvent * e)
194 QToolButton::mousePressEvent(e);
200 QString const label = qt_(to_ascii(tbitem_.label_));
201 ButtonMenu * m = new ButtonMenu(label, this);
202 m->setWindowTitle(label);
203 m->setTearOffEnabled(true);
204 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
205 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
207 LYXERR0("Unknown toolbar " << tbitem_.name_);
210 ToolbarInfo::item_iterator it = tbinfo->items.begin();
211 ToolbarInfo::item_iterator const end = tbinfo->items.end();
212 for (; it != end; ++it)
213 if (!getStatus(it->func_).unknown())
214 m->add(bar_->addItem(*it));
217 QToolButton::mousePressEvent(e);
221 void MenuButton::actionTriggered(QAction * action)
223 QToolButton::setDefaultAction(action);
224 setPopupMode(QToolButton::DelayedPopup);
228 void GuiToolbar::add(ToolbarItem const & item)
230 switch (item.type_) {
231 case ToolbarItem::SEPARATOR:
234 case ToolbarItem::LAYOUTS: {
235 LayoutBox * layout = owner_.getLayoutDialog();
236 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
237 layout, SLOT(setIconSize(QSize)));
238 QAction * action = addWidget(layout);
239 action->setVisible(true);
242 case ToolbarItem::MINIBUFFER:
243 command_buffer_ = new GuiCommandBuffer(&owner_);
244 addWidget(command_buffer_);
245 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
246 //setHorizontalStretchable(true);
248 case ToolbarItem::TABLEINSERT: {
249 QToolButton * tb = new QToolButton;
250 tb->setCheckable(true);
251 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
252 QString const label = qt_(to_ascii(item.label_));
253 tb->setToolTip(label);
254 tb->setStatusTip(label);
256 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
257 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
258 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
259 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
263 case ToolbarItem::ICONPALETTE:
264 addWidget(new PaletteButton(this, item));
267 case ToolbarItem::POPUPMENU: {
268 addWidget(new MenuButton(this, item, false));
271 case ToolbarItem::STICKYPOPUPMENU: {
272 addWidget(new MenuButton(this, item, true));
275 case ToolbarItem::COMMAND: {
276 if (!getStatus(item.func_).unknown())
277 addAction(addItem(item));
286 void GuiToolbar::update(bool in_math, bool in_table, bool in_review,
287 bool in_mathmacrotemplate)
289 if (visibility_ & Toolbars::AUTO) {
290 bool show_it = (in_math && (visibility_ & Toolbars::MATH))
291 || (in_table && (visibility_ & Toolbars::TABLE))
292 || (in_review && (visibility_ & Toolbars::REVIEW))
293 || (in_mathmacrotemplate && (visibility_ & Toolbars::MATHMACROTEMPLATE));
297 // update visible toolbars only
301 // This is a speed bottleneck because this is called on every keypress
302 // and update calls getStatus, which copies the cursor at least two times
303 for (int i = 0; i < actions_.size(); ++i)
304 actions_[i]->update();
306 LayoutBox * layout = owner_.getLayoutDialog();
308 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
315 QString GuiToolbar::sessionKey() const
317 return "views/" + QString::number(owner_.id()) + "/" + objectName();
321 void GuiToolbar::saveSession() const
324 settings.setValue(sessionKey() + "/visibility", visibility_);
328 void GuiToolbar::restoreSession()
331 setVisibility(settings.value(sessionKey() + "/visibility").toInt());
335 void GuiToolbar::toggle()
339 if (!(visibility_ & Toolbars::AUTO)) {
340 visibility_ |= Toolbars::AUTO;
344 visibility_ &= ~Toolbars::AUTO;
363 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
364 qstring_to_ucs4(windowTitle()), state));
367 } // namespace frontend
370 #include "moc_GuiToolbar.cpp"