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 Abdelrazak Younes
12 * Full author contact details are available in file CREDITS.
18 #include "BufferParams.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
23 #include "IconPalette.h"
26 #include "ToolbarBackend.h"
29 #include "GuiCommandBuffer.h"
30 #include "GuiToolbar.h"
31 #include "LyXAction.h"
33 #include "qt_helpers.h"
34 #include "InsertTableWidget.h"
36 #include "support/filetools.h"
37 #include "support/lstrings.h"
39 #include "controllers/ControlMath.h"
43 #include <QToolButton>
51 using support::FileName;
52 using support::libFileSearch;
57 static TextClass const & textClass(LyXView const & lv)
59 return lv.buffer()->params().getTextClass();
63 /////////////////////////////////////////////////////////////////////
67 /////////////////////////////////////////////////////////////////////
69 GuiLayoutBox::GuiLayoutBox(QToolBar * toolbar, GuiViewBase & 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(QString)),
79 this, SLOT(selected(QString)));
81 toolbar->addWidget(combo_);
85 void GuiLayoutBox::set(docstring const & layout)
87 TextClass const & tc = textClass(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 GuiLayoutBox::updateContents()
109 TextClass const & tc = textClass(owner_);
111 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(toqstr(translateIfPossible((*it)->name())));
122 // needed to recalculate size hint
124 combo_->setMinimumWidth(combo_->sizeHint().width());
127 combo_->setUpdatesEnabled(true);
132 void GuiLayoutBox::clear()
138 void GuiLayoutBox::open()
144 void GuiLayoutBox::setEnabled(bool enable)
146 // Workaround for Qt bug where setEnabled(true) closes
148 if (enable != combo_->isEnabled())
149 combo_->setEnabled(enable);
154 // FIXME: put that in frontends/LayoutBox class.
155 void layoutSelected(LyXView & lv, docstring const & name)
157 TextClass const & tc = lv.buffer()->params().getTextClass();
159 TextClass::const_iterator it = tc.begin();
160 TextClass::const_iterator const end = tc.end();
161 for (; it != end; ++it) {
162 docstring const & itname = (*it)->name();
163 if (translateIfPossible(itname) == name) {
164 FuncRequest const func(LFUN_LAYOUT, itname,
165 FuncRequest::TOOLBAR);
170 lyxerr << "ERROR (layoutSelected): layout not found!"
176 void GuiLayoutBox::selected(const QString & str)
180 layoutSelected(owner_, qstring_to_ucs4(str));
185 /////////////////////////////////////////////////////////////////////
189 /////////////////////////////////////////////////////////////////////
192 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiViewBase & owner)
193 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
194 layout_(0), command_buffer_(0)
196 // give visual separation between adjacent toolbars
199 // TODO: save toolbar position
202 ToolbarInfo::item_iterator it = tbinfo.items.begin();
203 ToolbarInfo::item_iterator end = tbinfo.items.end();
204 for (; it != end; ++it)
209 void GuiToolbar::focusCommandBuffer()
212 command_buffer_->setFocus();
216 Action * GuiToolbar::addItem(ToolbarItem const & item)
218 Action * act = new Action(owner_,
219 getIcon(item.func_, false).c_str(),
220 toqstr(item.label_), item.func_, toqstr(item.label_));
221 actions_.append(act);
226 void GuiToolbar::add(ToolbarItem const & item)
228 switch (item.type_) {
229 case ToolbarItem::SEPARATOR:
232 case ToolbarItem::LAYOUTS:
233 layout_ = new GuiLayoutBox(this, owner_);
235 case ToolbarItem::MINIBUFFER:
236 command_buffer_ = new GuiCommandBuffer(&owner_);
237 addWidget(command_buffer_);
238 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
239 //setHorizontalStretchable(true);
241 case ToolbarItem::TABLEINSERT: {
242 QToolButton * tb = new QToolButton;
243 tb->setCheckable(true);
244 tb->setIcon(QPixmap(toqstr(getIcon(FuncRequest(LFUN_TABULAR_INSERT)))));
245 tb->setToolTip(qt_(to_ascii(item.label_)));
246 tb->setStatusTip(qt_(to_ascii(item.label_)));
247 tb->setText(qt_(to_ascii(item.label_)));
248 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
249 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
250 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
251 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
255 case ToolbarItem::ICONPALETTE: {
256 QToolButton * tb = new QToolButton(this);
257 tb->setToolTip(qt_(to_ascii(item.label_)));
258 tb->setStatusTip(qt_(to_ascii(item.label_)));
259 tb->setText(qt_(to_ascii(item.label_)));
260 connect(this, SIGNAL(iconSizeChanged(QSize)),
261 tb, SLOT(setIconSize(QSize)));
262 IconPalette * panel = new IconPalette(tb);
263 panel->setWindowTitle(qt_(to_ascii(item.label_)));
264 connect(this, SIGNAL(updated()), panel, 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 panel->addButton(addItem(*it));
275 // use the icon of first action for the toolbar button
276 if (it == tbinfo->items.begin())
277 tb->setIcon(QPixmap(getIcon(it->func_).c_str()));
279 tb->setCheckable(true);
280 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
281 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
285 case ToolbarItem::POPUPMENU: {
286 QToolButton * tb = new QToolButton;
287 tb->setPopupMode(QToolButton::InstantPopup);
288 tb->setToolTip(qt_(to_ascii(item.label_)));
289 tb->setStatusTip(qt_(to_ascii(item.label_)));
290 tb->setText(qt_(to_ascii(item.label_)));
291 FileName icon_path = libFileSearch("images/math", item.name_, "png");
292 tb->setIcon(QIcon(toqstr(icon_path.absFilename())));
293 connect(this, SIGNAL(iconSizeChanged(QSize)),
294 tb, SLOT(setIconSize(QSize)));
296 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
297 m->setWindowTitle(qt_(to_ascii(item.label_)));
298 m->setTearOffEnabled(true);
299 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
300 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
302 lyxerr << "Unknown toolbar " << item.name_ << endl;
305 ToolbarInfo::item_iterator it = tbinfo->items.begin();
306 ToolbarInfo::item_iterator const end = tbinfo->items.end();
307 for (; it != end; ++it)
308 if (!getStatus(it->func_).unknown())
309 m->add(addItem(*it));
314 case ToolbarItem::COMMAND: {
315 if (!getStatus(item.func_).unknown())
316 addAction(addItem(item));
325 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
327 // if tbinfo.state == auto *do not* set on/off
328 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
329 if (GuiToolbar::isVisible())
330 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
332 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
335 // no need to save it here.
336 Qt::ToolBarArea loc = owner_.toolBarArea(this);
338 if (loc == Qt::TopToolBarArea)
339 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
340 else if (loc == Qt::BottomToolBarArea)
341 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
342 else if (loc == Qt::RightToolBarArea)
343 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
344 else if (loc == Qt::LeftToolBarArea)
345 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
347 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
349 // save toolbar position. They are not used to restore toolbar position
350 // now because move(x,y) does not work for toolbar.
351 tbinfo.posx = pos().x();
352 tbinfo.posy = pos().y();
356 void GuiToolbar::updateContents()
358 // update visible toolbars only
361 // This is a speed bottleneck because this is called on every keypress
362 // and update calls getStatus, which copies the cursor at least two times
363 for (int i = 0; i < actions_.size(); ++i)
364 actions_[i]->update();
371 string const getIcon(FuncRequest const & f, bool unknown)
373 using frontend::find_png;
378 case LFUN_MATH_INSERT:
379 if (!f.argument().empty())
380 fullname = find_png(to_utf8(f.argument()).substr(1));
382 case LFUN_MATH_DELIM:
383 case LFUN_MATH_BIGDELIM:
384 fullname = find_png(to_utf8(f.argument()));
387 string const name = lyxaction.getActionName(f.action);
388 string png_name = name;
390 if (!f.argument().empty())
391 png_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
393 fullname = libFileSearch("images", png_name, "png").absFilename();
395 if (fullname.empty()) {
396 // try without the argument
397 fullname = libFileSearch("images", name, "png").absFilename();
401 if (!fullname.empty()) {
402 LYXERR(Debug::GUI) << "Full icon name is `"
403 << fullname << '\'' << endl;
407 LYXERR(Debug::GUI) << "Cannot find icon for command \""
408 << lyxaction.getActionName(f.action)
409 << '(' << to_utf8(f.argument()) << ")\"" << endl;
411 return libFileSearch("images", "unknown", "png").absFilename();
417 } // namespace frontend
420 #include "GuiToolbar_moc.cpp"