2 * \file qt4/QLToolbar.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 Abdelrazak Younes
12 * Full author contact details are available in file CREDITS.
18 #include "BufferParams.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
24 #include "IconPalette.h"
27 #include "QLToolbar.h"
28 #include "LyXAction.h"
30 #include "qt_helpers.h"
31 #include "InsertTableWidget.h"
32 #include "support/filetools.h"
33 #include "support/lstrings.h"
34 #include "controllers/ControlMath.h"
35 #include "ToolbarBackend.h"
39 #include <QToolButton>
47 using support::FileName;
48 using support::libFileSearch;
52 using support::libFileSearch;
59 TextClass const & getTextClass(LyXView const & lv)
61 return lv.buffer()->params().getTextClass();
68 QLayoutBox::QLayoutBox(QToolBar * toolbar, GuiView & owner)
71 combo_ = new QComboBox;
72 combo_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
73 combo_->setFocusPolicy(Qt::ClickFocus);
74 combo_->setMinimumWidth(combo_->sizeHint().width());
75 combo_->setMaxVisibleItems(100);
77 QObject::connect(combo_, SIGNAL(activated(const QString &)),
78 this, SLOT(selected(const QString &)));
80 toolbar->addWidget(combo_);
84 void QLayoutBox::set(string const & layout)
86 TextClass const & tc = getTextClass(owner_);
88 QString const & name = qt_(tc[layout]->name());
91 for (; i < combo_->count(); ++i) {
92 if (name == combo_->itemText(i))
96 if (i == combo_->count()) {
97 lyxerr << "Trying to select non existent layout type "
98 << fromqstr(name) << endl;
102 combo_->setCurrentIndex(i);
106 void QLayoutBox::update()
108 TextClass const & tc = getTextClass(owner_);
110 combo_->setUpdatesEnabled(false);
114 TextClass::const_iterator it = tc.begin();
115 TextClass::const_iterator const end = tc.end();
116 for (; it != end; ++it) {
117 // ignore obsolete entries
118 if ((*it)->obsoleted_by().empty())
119 combo_->addItem(qt_((*it)->name()));
122 // needed to recalculate size hint
124 combo_->setMinimumWidth(combo_->sizeHint().width());
127 combo_->setUpdatesEnabled(true);
132 void QLayoutBox::clear()
138 void QLayoutBox::open()
144 void QLayoutBox::setEnabled(bool enable)
146 // Workaround for Qt bug where setEnabled(true) closes
148 if (enable != combo_->isEnabled())
149 combo_->setEnabled(enable);
153 void QLayoutBox::selected(const QString & str)
155 string const sel = fromqstr(str);
159 layoutSelected(owner_, sel);
163 QLToolbar::QLToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
164 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner)
166 // give visual separation between adjacent toolbars
169 // TODO: save toolbar position
172 ToolbarInfo::item_iterator it = tbinfo.items.begin();
173 ToolbarInfo::item_iterator end = tbinfo.items.end();
174 for (; it != end; ++it)
179 void QLToolbar::add(ToolbarItem const & item)
181 switch (item.type_) {
182 case ToolbarItem::SEPARATOR:
185 case ToolbarItem::LAYOUTS:
186 layout_.reset(new QLayoutBox(this, owner_));
188 case ToolbarItem::MINIBUFFER:
189 owner_.addCommandBuffer(this);
190 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
191 //setHorizontalStretchable(true);
193 case ToolbarItem::TABLEINSERT: {
194 QToolButton * tb = new QToolButton;
195 tb->setCheckable(true);
196 tb->setIcon(QPixmap(toqstr(getIcon(FuncRequest(LFUN_TABULAR_INSERT)))));
197 tb->setToolTip(qt_(to_ascii(item.label_)));
198 tb->setStatusTip(qt_(to_ascii(item.label_)));
199 tb->setText(qt_(to_ascii(item.label_)));
200 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
201 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
202 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
203 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
207 case ToolbarItem::ICONPALETTE: {
208 QToolButton * tb = new QToolButton(this);
209 tb->setToolTip(qt_(to_ascii(item.label_)));
210 tb->setStatusTip(qt_(to_ascii(item.label_)));
211 tb->setText(qt_(to_ascii(item.label_)));
212 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
213 tb, SLOT(setIconSize(const QSize &)));
214 IconPalette * panel = new IconPalette(tb);
215 panel->setWindowTitle(qt_(to_ascii(item.label_)));
216 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
217 ToolbarInfo const & tbinfo = toolbarbackend.getToolbar(item.name_);
218 ToolbarInfo::item_iterator it = tbinfo.items.begin();
219 ToolbarInfo::item_iterator const end = tbinfo.items.end();
220 for (; it != end; ++it)
221 if (!getStatus(it->func_).unknown()) {
222 Action * action = new Action(owner_,
227 panel->addButton(action);
228 ActionVector.push_back(action);
229 // use the icon of first action for the toolbar button
230 if (it == tbinfo.items.begin())
231 tb->setIcon(QPixmap(getIcon(it->func_).c_str()));
233 tb->setCheckable(true);
234 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
235 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
239 case ToolbarItem::POPUPMENU: {
240 QToolButton * tb = new QToolButton;
241 tb->setPopupMode(QToolButton::InstantPopup);
242 tb->setToolTip(qt_(to_ascii(item.label_)));
243 tb->setStatusTip(qt_(to_ascii(item.label_)));
244 tb->setText(qt_(to_ascii(item.label_)));
245 FileName icon_path = libFileSearch("images/math", item.name_, "xpm");
246 tb->setIcon(QIcon(toqstr(icon_path.absFilename())));
247 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
248 tb, SLOT(setIconSize(const QSize &)));
250 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
251 m->setWindowTitle(qt_(to_ascii(item.label_)));
252 m->setTearOffEnabled(true);
253 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
254 ToolbarInfo const & tbinfo = toolbarbackend.getToolbar(item.name_);
255 ToolbarInfo::item_iterator it = tbinfo.items.begin();
256 ToolbarInfo::item_iterator const end = tbinfo.items.end();
257 for (; it != end; ++it)
258 if (!getStatus(it->func_).unknown()) {
259 Action * action = new Action(owner_,
260 getIcon(it->func_, false),
265 ActionVector.push_back(action);
271 case ToolbarItem::COMMAND: {
272 if (getStatus(item.func_).unknown())
275 Action * action = new Action(owner_,
281 ActionVector.push_back(action);
290 void QLToolbar::hide(bool)
296 void QLToolbar::show(bool)
302 bool QLToolbar::isVisible() const
304 return QToolBar::isVisible();
308 void QLToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
310 // if tbinfo.state == auto *do not* set on/off
311 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
312 if (QLToolbar::isVisible())
313 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
315 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
318 // no need to save it here.
319 Qt::ToolBarArea loc = owner_.toolBarArea(this);
321 if (loc == Qt::TopToolBarArea)
322 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
323 else if (loc == Qt::BottomToolBarArea)
324 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
325 else if (loc == Qt::RightToolBarArea)
326 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
327 else if (loc == Qt::LeftToolBarArea)
328 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
330 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
332 // save toolbar position. They are not used to restore toolbar position
333 // now because move(x,y) does not work for toolbar.
334 tbinfo.posx = pos().x();
335 tbinfo.posy = pos().y();
339 void QLToolbar::update()
341 // This is a speed bottleneck because this is called on every keypress
342 // and update calls getStatus, which copies the cursor at least two times
343 for (size_t i = 0; i < ActionVector.size(); ++i)
344 ActionVector[i]->update();
351 string const getIcon(FuncRequest const & f, bool unknown)
353 using frontend::find_xpm;
358 case LFUN_MATH_INSERT:
359 if (!f.argument().empty())
360 fullname = find_xpm(to_utf8(f.argument()).substr(1));
362 case LFUN_MATH_DELIM:
363 case LFUN_MATH_BIGDELIM:
364 fullname = find_xpm(to_utf8(f.argument()));
367 string const name = lyxaction.getActionName(f.action);
368 string xpm_name(name);
370 if (!f.argument().empty())
371 xpm_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
373 fullname = libFileSearch("images", xpm_name, "xpm").absFilename();
375 if (fullname.empty()) {
376 // try without the argument
377 fullname = libFileSearch("images", name, "xpm").absFilename();
381 if (!fullname.empty()) {
382 LYXERR(Debug::GUI) << "Full icon name is `"
383 << fullname << '\'' << endl;
387 LYXERR(Debug::GUI) << "Cannot find icon for command \""
388 << lyxaction.getActionName(f.action)
389 << '(' << to_utf8(f.argument()) << ")\"" << endl;
391 return libFileSearch("images", "unknown", "xpm").absFilename();
397 } // namespace frontend
400 #include "QLToolbar_moc.cpp"