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 "QCommandBuffer.h"
28 #include "QLToolbar.h"
29 #include "LyXAction.h"
31 #include "qt_helpers.h"
32 #include "InsertTableWidget.h"
33 #include "support/filetools.h"
34 #include "support/lstrings.h"
35 #include "controllers/ControlMath.h"
36 #include "ToolbarBackend.h"
40 #include <QToolButton>
48 using support::FileName;
49 using support::libFileSearch;
53 using support::libFileSearch;
60 TextClass const & getTextClass(LyXView const & lv)
62 return lv.buffer()->params().getTextClass();
69 QLayoutBox::QLayoutBox(QToolBar * toolbar, GuiView & owner)
72 combo_ = new QComboBox;
73 combo_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
74 combo_->setFocusPolicy(Qt::ClickFocus);
75 combo_->setMinimumWidth(combo_->sizeHint().width());
76 combo_->setMaxVisibleItems(100);
78 QObject::connect(combo_, SIGNAL(activated(const QString &)),
79 this, SLOT(selected(const QString &)));
81 toolbar->addWidget(combo_);
85 void QLayoutBox::set(docstring const & layout)
87 TextClass const & tc = getTextClass(owner_);
89 QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
92 for (; i < combo_->count(); ++i) {
93 if (name == combo_->itemText(i))
97 if (i == combo_->count()) {
98 lyxerr << "Trying to select non existent layout type "
99 << fromqstr(name) << endl;
103 combo_->setCurrentIndex(i);
107 void QLayoutBox::update()
109 TextClass const & tc = getTextClass(owner_);
111 combo_->setUpdatesEnabled(false);
115 TextClass::const_iterator it = tc.begin();
116 TextClass::const_iterator const end = tc.end();
117 for (; it != end; ++it) {
118 // ignore obsolete entries
119 if ((*it)->obsoleted_by().empty())
120 combo_->addItem(toqstr(translateIfPossible((*it)->name())));
123 // needed to recalculate size hint
125 combo_->setMinimumWidth(combo_->sizeHint().width());
128 combo_->setUpdatesEnabled(true);
133 void QLayoutBox::clear()
139 void QLayoutBox::open()
145 void QLayoutBox::setEnabled(bool enable)
147 // Workaround for Qt bug where setEnabled(true) closes
149 if (enable != combo_->isEnabled())
150 combo_->setEnabled(enable);
154 void QLayoutBox::selected(const QString & str)
158 layoutSelected(owner_, qstring_to_ucs4(str));
162 QLToolbar::QLToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
163 : QToolBar(qt_(tbinfo.gui_name), &owner), command_buffer_(0), owner_(owner)
165 // give visual separation between adjacent toolbars
168 // TODO: save toolbar position
171 ToolbarInfo::item_iterator it = tbinfo.items.begin();
172 ToolbarInfo::item_iterator end = tbinfo.items.end();
173 for (; it != end; ++it)
178 void QLToolbar::focusCommandBuffer()
181 command_buffer_->setFocus();
185 void QLToolbar::add(ToolbarItem const & item)
187 switch (item.type_) {
188 case ToolbarItem::SEPARATOR:
191 case ToolbarItem::LAYOUTS:
192 layout_.reset(new QLayoutBox(this, owner_));
194 case ToolbarItem::MINIBUFFER:
195 command_buffer_ = new QCommandBuffer(&owner_);
196 addWidget(command_buffer_);
197 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
198 //setHorizontalStretchable(true);
200 case ToolbarItem::TABLEINSERT: {
201 QToolButton * tb = new QToolButton;
202 tb->setCheckable(true);
203 tb->setIcon(QPixmap(toqstr(getIcon(FuncRequest(LFUN_TABULAR_INSERT)))));
204 tb->setToolTip(qt_(to_ascii(item.label_)));
205 tb->setStatusTip(qt_(to_ascii(item.label_)));
206 tb->setText(qt_(to_ascii(item.label_)));
207 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
208 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
209 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
210 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
214 case ToolbarItem::ICONPALETTE: {
215 QToolButton * tb = new QToolButton(this);
216 tb->setToolTip(qt_(to_ascii(item.label_)));
217 tb->setStatusTip(qt_(to_ascii(item.label_)));
218 tb->setText(qt_(to_ascii(item.label_)));
219 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
220 tb, SLOT(setIconSize(const QSize &)));
221 IconPalette * panel = new IconPalette(tb);
222 panel->setWindowTitle(qt_(to_ascii(item.label_)));
223 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
224 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
226 lyxerr << "Unknown toolbar " << item.name_ << endl;
229 ToolbarInfo::item_iterator it = tbinfo->items.begin();
230 ToolbarInfo::item_iterator const end = tbinfo->items.end();
231 for (; it != end; ++it)
232 if (!getStatus(it->func_).unknown()) {
233 Action * action = new Action(owner_,
238 panel->addButton(action);
239 ActionVector.push_back(action);
240 // use the icon of first action for the toolbar button
241 if (it == tbinfo->items.begin())
242 tb->setIcon(QPixmap(getIcon(it->func_).c_str()));
244 tb->setCheckable(true);
245 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
246 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
250 case ToolbarItem::POPUPMENU: {
251 QToolButton * tb = new QToolButton;
252 tb->setPopupMode(QToolButton::InstantPopup);
253 tb->setToolTip(qt_(to_ascii(item.label_)));
254 tb->setStatusTip(qt_(to_ascii(item.label_)));
255 tb->setText(qt_(to_ascii(item.label_)));
256 FileName icon_path = libFileSearch("images/math", item.name_, "xpm");
257 tb->setIcon(QIcon(toqstr(icon_path.absFilename())));
258 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
259 tb, SLOT(setIconSize(const QSize &)));
261 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
262 m->setWindowTitle(qt_(to_ascii(item.label_)));
263 m->setTearOffEnabled(true);
264 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
265 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
267 lyxerr << "Unknown toolbar " << item.name_ << endl;
270 ToolbarInfo::item_iterator it = tbinfo->items.begin();
271 ToolbarInfo::item_iterator const end = tbinfo->items.end();
272 for (; it != end; ++it)
273 if (!getStatus(it->func_).unknown()) {
274 Action * action = new Action(owner_,
275 getIcon(it->func_, false),
280 ActionVector.push_back(action);
286 case ToolbarItem::COMMAND: {
287 if (getStatus(item.func_).unknown())
290 Action * action = new Action(owner_,
296 ActionVector.push_back(action);
305 void QLToolbar::hide(bool)
311 void QLToolbar::show(bool)
317 bool QLToolbar::isVisible() const
319 return QToolBar::isVisible();
323 void QLToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
325 // if tbinfo.state == auto *do not* set on/off
326 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
327 if (QLToolbar::isVisible())
328 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
330 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
333 // no need to save it here.
334 Qt::ToolBarArea loc = owner_.toolBarArea(this);
336 if (loc == Qt::TopToolBarArea)
337 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
338 else if (loc == Qt::BottomToolBarArea)
339 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
340 else if (loc == Qt::RightToolBarArea)
341 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
342 else if (loc == Qt::LeftToolBarArea)
343 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
345 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
347 // save toolbar position. They are not used to restore toolbar position
348 // now because move(x,y) does not work for toolbar.
349 tbinfo.posx = pos().x();
350 tbinfo.posy = pos().y();
354 void QLToolbar::update()
356 // This is a speed bottleneck because this is called on every keypress
357 // and update calls getStatus, which copies the cursor at least two times
358 for (size_t i = 0; i < ActionVector.size(); ++i)
359 ActionVector[i]->update();
366 string const getIcon(FuncRequest const & f, bool unknown)
368 using frontend::find_xpm;
373 case LFUN_MATH_INSERT:
374 if (!f.argument().empty())
375 fullname = find_xpm(to_utf8(f.argument()).substr(1));
377 case LFUN_MATH_DELIM:
378 case LFUN_MATH_BIGDELIM:
379 fullname = find_xpm(to_utf8(f.argument()));
382 string const name = lyxaction.getActionName(f.action);
383 string xpm_name(name);
385 if (!f.argument().empty())
386 xpm_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
388 fullname = libFileSearch("images", xpm_name, "xpm").absFilename();
390 if (fullname.empty()) {
391 // try without the argument
392 fullname = libFileSearch("images", name, "xpm").absFilename();
396 if (!fullname.empty()) {
397 LYXERR(Debug::GUI) << "Full icon name is `"
398 << fullname << '\'' << endl;
402 LYXERR(Debug::GUI) << "Cannot find icon for command \""
403 << lyxaction.getActionName(f.action)
404 << '(' << to_utf8(f.argument()) << ")\"" << endl;
406 return libFileSearch("images", "unknown", "xpm").absFilename();
412 } // namespace frontend
415 #include "QLToolbar_moc.cpp"