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_);
102 if (!bindings.empty())
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)
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)));
196 void MenuButton::initialize()
198 QString const label = qt_(to_ascii(tbitem_.label_));
199 ButtonMenu * m = new ButtonMenu(label, this);
200 m->setWindowTitle(label);
201 m->setTearOffEnabled(true);
202 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
203 connect(bar_, SIGNAL(updated()), this, SLOT(updateTriggered()));
204 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
206 LYXERR0("Unknown toolbar " << tbitem_.name_);
209 ToolbarInfo::item_iterator it = tbinfo->items.begin();
210 ToolbarInfo::item_iterator const end = tbinfo->items.end();
211 for (; it != end; ++it)
212 if (!getStatus(it->func_).unknown())
213 m->add(bar_->addItem(*it));
218 void MenuButton::actionTriggered(QAction * action)
220 QToolButton::setDefaultAction(action);
221 setPopupMode(QToolButton::DelayedPopup);
225 void MenuButton::updateTriggered()
230 bool enabled = false;
231 QList<QAction *> acts = menu()->actions();
232 for (int i = 0; i < acts.size(); ++i)
233 if (acts[i]->isEnabled()) {
237 // Enable the MenuButton if at least one menu item is enabled
239 // If a disabled item is default, switch to InstantPopup
240 // (this can happen if a user selects e.g. DVI and then
241 // turns non-TeX fonts on)
242 if (defaultAction() && !defaultAction()->isEnabled())
243 setPopupMode(QToolButton::InstantPopup);
247 void GuiToolbar::add(ToolbarItem const & item)
249 switch (item.type_) {
250 case ToolbarItem::SEPARATOR:
253 case ToolbarItem::LAYOUTS: {
254 LayoutBox * layout = owner_.getLayoutDialog();
255 QObject::connect(this, SIGNAL(iconSizeChanged(QSize)),
256 layout, SLOT(setIconSize(QSize)));
257 QAction * action = addWidget(layout);
258 action->setVisible(true);
261 case ToolbarItem::MINIBUFFER:
262 command_buffer_ = new GuiCommandBuffer(&owner_);
263 addWidget(command_buffer_);
264 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
265 //setHorizontalStretchable(true);
267 case ToolbarItem::TABLEINSERT: {
268 QToolButton * tb = new QToolButton;
269 tb->setCheckable(true);
270 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
271 QString const label = qt_(to_ascii(item.label_));
272 tb->setToolTip(label);
273 tb->setStatusTip(label);
275 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
276 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
277 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
278 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
282 case ToolbarItem::ICONPALETTE:
283 addWidget(new PaletteButton(this, item));
286 case ToolbarItem::POPUPMENU: {
287 addWidget(new MenuButton(this, item, false));
290 case ToolbarItem::STICKYPOPUPMENU: {
291 addWidget(new MenuButton(this, item, true));
294 case ToolbarItem::COMMAND: {
295 if (!getStatus(item.func_).unknown())
296 addAction(addItem(item));
305 void GuiToolbar::update(bool in_math, bool in_table, bool in_review,
306 bool in_mathmacrotemplate, bool in_ipa)
308 if (visibility_ & Toolbars::AUTO) {
309 bool show_it = (in_math && (visibility_ & Toolbars::MATH))
310 || (in_table && (visibility_ & Toolbars::TABLE))
311 || (in_review && (visibility_ & Toolbars::REVIEW))
312 || (in_mathmacrotemplate && (visibility_ & Toolbars::MATHMACROTEMPLATE))
313 || (in_ipa && (visibility_ & Toolbars::IPA));
317 // update visible toolbars only
321 // This is a speed bottleneck because this is called on every keypress
322 // and update calls getStatus, which copies the cursor at least two times
323 for (int i = 0; i < actions_.size(); ++i)
324 actions_[i]->update();
326 LayoutBox * layout = owner_.getLayoutDialog();
328 layout->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
335 QString GuiToolbar::sessionKey() const
337 return "views/" + QString::number(owner_.id()) + "/" + objectName();
341 void GuiToolbar::saveSession() const
344 settings.setValue(sessionKey() + "/visibility", visibility_);
348 void GuiToolbar::restoreSession()
351 int const error_val = -1;
353 settings.value(sessionKey() + "/visibility", error_val).toInt();
354 if (visibility == error_val || visibility == 0) {
355 // This should not happen, but in case we use the defaults
356 LYXERR(Debug::GUI, "Session settings could not be found! Defaults are used instead.");
358 guiApp->toolbars().defaultVisibility(fromqstr(objectName()));
360 setVisibility(visibility);
364 void GuiToolbar::toggle()
367 if (visibility_ & Toolbars::ALLOWAUTO) {
368 if (!(visibility_ & Toolbars::AUTO)) {
369 visibility_ |= Toolbars::AUTO;
373 visibility_ &= ~Toolbars::AUTO;
392 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
393 qstring_to_ucs4(windowTitle()), state));
396 } // namespace frontend
399 #include "moc_GuiToolbar.cpp"