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>
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(string const & layout)
87 TextClass const & tc = getTextClass(owner_);
89 QString const & name = qt_(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(qt_((*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)
156 string const sel = fromqstr(str);
160 layoutSelected(owner_, sel);
164 QLToolbar::QLToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
165 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner)
167 // give visual separation between adjacent toolbars
170 // TODO: save toolbar position
173 ToolbarInfo::item_iterator it = tbinfo.items.begin();
174 ToolbarInfo::item_iterator end = tbinfo.items.end();
175 for (; it != end; ++it)
180 void QLToolbar::add(ToolbarItem const & item)
182 switch (item.type_) {
183 case ToolbarItem::SEPARATOR:
186 case ToolbarItem::LAYOUTS:
187 layout_.reset(new QLayoutBox(this, owner_));
189 case ToolbarItem::MINIBUFFER:
190 owner_.addCommandBuffer(this);
191 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
192 //setHorizontalStretchable(true);
194 case ToolbarItem::TABLEINSERT: {
195 QToolButton * tb = new QToolButton;
196 tb->setCheckable(true);
197 tb->setIcon(QPixmap(toqstr(getIcon(FuncRequest(LFUN_TABULAR_INSERT)))));
198 tb->setToolTip(qt_(to_ascii(item.label_)));
199 tb->setStatusTip(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->setCheckable(true);
210 tb->setToolTip(qt_(to_ascii(item.label_)));
211 tb->setStatusTip(qt_(to_ascii(item.label_)));
212 tb->setText(qt_(to_ascii(item.label_)));
213 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
214 tb, SLOT(setIconSize(const QSize &)));
216 IconPalette * panel = new IconPalette(tb);
217 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
218 ToolbarInfo const & tbinfo = toolbarbackend.getToolbar(item.name_);
219 ToolbarInfo::item_iterator it = tbinfo.items.begin();
220 ToolbarInfo::item_iterator const end = tbinfo.items.end();
221 for (; it != end; ++it)
222 if (!getStatus(it->func_).unknown()) {
223 Action * action = new Action(owner_,
228 panel->addButton(action);
229 ActionVector.push_back(action);
230 // use the icon of first action for the toolbar button
231 if (it == tbinfo.items.begin())
232 tb->setIcon(QPixmap(getIcon(it->func_).c_str()));
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 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
252 ToolbarInfo const & tbinfo = toolbarbackend.getToolbar(item.name_);
253 ToolbarInfo::item_iterator it = tbinfo.items.begin();
254 ToolbarInfo::item_iterator const end = tbinfo.items.end();
255 for (; it != end; ++it)
256 if (!getStatus(it->func_).unknown()) {
257 Action * action = new Action(owner_,
258 getIcon(it->func_, false),
263 ActionVector.push_back(action);
269 case ToolbarItem::COMMAND: {
270 if (getStatus(item.func_).unknown())
273 Action * action = new Action(owner_,
279 ActionVector.push_back(action);
288 void QLToolbar::hide(bool)
294 void QLToolbar::show(bool)
300 void QLToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
302 // if tbinfo.state == auto *do not* set on/off
303 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
304 if (QLToolbar::isVisible())
305 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
307 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
310 // no need to save it here.
311 Qt::ToolBarArea loc = owner_.toolBarArea(this);
313 if (loc == Qt::TopToolBarArea)
314 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
315 else if (loc == Qt::BottomToolBarArea)
316 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
317 else if (loc == Qt::RightToolBarArea)
318 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
319 else if (loc == Qt::LeftToolBarArea)
320 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
322 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
324 // save toolbar position. They are not used to restore toolbar position
325 // now because move(x,y) does not work for toolbar.
326 tbinfo.posx = pos().x();
327 tbinfo.posy = pos().y();
331 void QLToolbar::update()
333 // This is a speed bottleneck because this is called on every keypress
334 // and update calls getStatus, which copies the cursor at least two times
335 for (size_t i = 0; i < ActionVector.size(); ++i)
336 ActionVector[i]->update();
343 string const getIcon(FuncRequest const & f, bool unknown)
345 using frontend::find_xpm;
350 case LFUN_MATH_INSERT:
351 if (!f.argument().empty())
352 fullname = find_xpm(to_utf8(f.argument()).substr(1));
354 case LFUN_MATH_DELIM:
355 case LFUN_MATH_BIGDELIM:
356 fullname = find_xpm(to_utf8(f.argument()));
359 string const name = lyxaction.getActionName(f.action);
360 string xpm_name(name);
362 if (!f.argument().empty())
363 xpm_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
365 fullname = libFileSearch("images", xpm_name, "xpm").absFilename();
367 if (fullname.empty()) {
368 // try without the argument
369 fullname = libFileSearch("images", name, "xpm").absFilename();
373 if (!fullname.empty()) {
374 LYXERR(Debug::GUI) << "Full icon name is `"
375 << fullname << '\'' << endl;
379 LYXERR(Debug::GUI) << "Cannot find icon for command \""
380 << lyxaction.getActionName(f.action)
381 << '(' << to_utf8(f.argument()) << ")\"" << endl;
383 return libFileSearch("images", "unknown", "xpm").absFilename();
389 } // namespace frontend
392 #include "QLToolbar_moc.cpp"