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 QStringList imagedirs;
178 imagedirs << "images/math/" << "images/";
179 for (int i = 0; i < imagedirs.size(); ++i) {
180 QString imagedir = imagedirs.at(i);
181 FileName const fname = imageLibFileSearch(imagedir, name, "png");
182 if (fname.exists()) {
183 setIcon(QIcon(getPixmap(imagedir, name, "png")));
188 connect(this, SIGNAL(triggered(QAction *)),
189 this, SLOT(actionTriggered(QAction *)));
190 connect(bar, SIGNAL(iconSizeChanged(QSize)),
191 this, SLOT(setIconSize(QSize)));
194 void MenuButton::mousePressEvent(QMouseEvent * e)
197 QToolButton::mousePressEvent(e);
203 QString const label = qt_(to_ascii(tbitem_.label_));
204 ButtonMenu * m = new ButtonMenu(label, this);
205 m->setWindowTitle(label);
206 m->setTearOffEnabled(true);
207 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
208 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
209 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
211 LYXERR0("Unknown toolbar " << tbitem_.name_);
214 ToolbarInfo::item_iterator it = tbinfo->items.begin();
215 ToolbarInfo::item_iterator const end = tbinfo->items.end();
216 for (; it != end; ++it)
217 if (!getStatus(it->func_).unknown())
218 m->add(bar_->addItem(*it));
221 QToolButton::mousePressEvent(e);
225 void MenuButton::actionTriggered(QAction * action)
227 QToolButton::setDefaultAction(action);
228 setPopupMode(QToolButton::DelayedPopup);
232 void MenuButton::updateTriggered()
237 bool enabled = false;
238 QList<QAction *> acts = menu()->actions();
239 for (int i = 0; i < acts.size(); ++i)
240 if (acts[i]->isEnabled()) {
244 // Enable the MenuButton if at least one menu item is enabled
246 // If a disabled item is default, switch to InstantPopup
247 // (this can happen if a user selects e.g. DVI and then
248 // turns non-TeX fonts on)
249 if (defaultAction() && !defaultAction()->isEnabled())
250 setPopupMode(QToolButton::InstantPopup);
254 void GuiToolbar::add(ToolbarItem const & item)
256 switch (item.type_) {
257 case ToolbarItem::SEPARATOR:
260 case ToolbarItem::LAYOUTS: {
261 LayoutBox * layout = owner_.getLayoutDialog();
262 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
263 layout, SLOT(setIconSize(QSize)));
264 QAction * action = addWidget(layout);
265 action->setVisible(true);
268 case ToolbarItem::MINIBUFFER:
269 command_buffer_ = new GuiCommandBuffer(&owner_);
270 addWidget(command_buffer_);
271 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
272 //setHorizontalStretchable(true);
274 case ToolbarItem::TABLEINSERT: {
275 QToolButton * tb = new QToolButton;
276 tb->setCheckable(true);
277 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
278 QString const label = qt_(to_ascii(item.label_));
279 tb->setToolTip(label);
280 tb->setStatusTip(label);
282 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
283 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
284 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
285 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
289 case ToolbarItem::ICONPALETTE:
290 addWidget(new PaletteButton(this, item));
293 case ToolbarItem::POPUPMENU: {
294 addWidget(new MenuButton(this, item, false));
297 case ToolbarItem::STICKYPOPUPMENU: {
298 addWidget(new MenuButton(this, item, true));
301 case ToolbarItem::COMMAND: {
302 if (!getStatus(item.func_).unknown())
303 addAction(addItem(item));
312 void GuiToolbar::update(bool in_math, bool in_table, bool in_review,
313 bool in_mathmacrotemplate, bool in_ipa)
315 if (visibility_ & Toolbars::AUTO) {
316 bool show_it = (in_math && (visibility_ & Toolbars::MATH))
317 || (in_table && (visibility_ & Toolbars::TABLE))
318 || (in_review && (visibility_ & Toolbars::REVIEW))
319 || (in_mathmacrotemplate && (visibility_ & Toolbars::MATHMACROTEMPLATE))
320 || (in_ipa && (visibility_ & Toolbars::IPA));
324 // update visible toolbars only
328 // This is a speed bottleneck because this is called on every keypress
329 // and update calls getStatus, which copies the cursor at least two times
330 for (int i = 0; i < actions_.size(); ++i)
331 actions_[i]->update();
333 LayoutBox * layout = owner_.getLayoutDialog();
335 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
342 QString GuiToolbar::sessionKey() const
344 return "views/" + QString::number(owner_.id()) + "/" + objectName();
348 void GuiToolbar::saveSession() const
351 settings.setValue(sessionKey() + "/visibility", visibility_);
355 void GuiToolbar::restoreSession()
358 int const error_val = -1;
360 settings.value(sessionKey() + "/visibility", error_val).toInt();
361 if (visibility == error_val || visibility == 0) {
362 // This should not happen, but in case we use the defaults
363 LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
365 guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
367 setVisibility(visibility);
371 void GuiToolbar::toggle()
374 if (visibility_ & Toolbars::ALLOWAUTO) {
375 if (!(visibility_ & Toolbars::AUTO)) {
376 visibility_ |= Toolbars::AUTO;
380 visibility_ &= ~Toolbars::AUTO;
399 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
400 qstring_to_ucs4(windowTitle()), state));
403 } // namespace frontend
406 #include "moc_GuiToolbar.cpp"