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"
38 #include "support/lyxalgo.h" // sorted
42 #include <QToolButton>
51 using support::FileName;
52 using support::libFileSearch;
54 using support::compare;
59 static TextClass const & textClass(LyXView const & lv)
61 return lv.buffer()->params().getTextClass();
65 /////////////////////////////////////////////////////////////////////
69 /////////////////////////////////////////////////////////////////////
71 GuiLayoutBox::GuiLayoutBox(GuiViewBase & owner)
74 setSizeAdjustPolicy(QComboBox::AdjustToContents);
75 setFocusPolicy(Qt::ClickFocus);
76 setMinimumWidth(sizeHint().width());
77 setMaxVisibleItems(100);
79 QObject::connect(this, SIGNAL(activated(QString)),
80 this, SLOT(selected(QString)));
84 void GuiLayoutBox::set(docstring const & layout)
86 TextClass const & tc = textClass(owner_);
88 QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
91 for (; i < count(); ++i) {
92 if (name == itemText(i))
97 lyxerr << "Trying to select non existent layout type "
98 << fromqstr(name) << endl;
106 void GuiLayoutBox::updateContents()
108 TextClass const & tc = textClass(owner_);
110 setUpdatesEnabled(false);
113 TextClass::const_iterator it = tc.begin();
114 TextClass::const_iterator const end = tc.end();
115 for (; it != end; ++it) {
116 // ignore obsolete entries
117 if ((*it)->obsoleted_by().empty())
118 addItem(toqstr(translateIfPossible((*it)->name())));
121 // needed to recalculate size hint
123 setMinimumWidth(sizeHint().width());
126 setUpdatesEnabled(true);
130 void GuiLayoutBox::selected(const QString & str)
133 TextClass const & tc = owner_.buffer()->params().getTextClass();
134 docstring const name = qstring_to_ucs4(str);
135 TextClass::const_iterator it = tc.begin();
136 TextClass::const_iterator const end = tc.end();
137 for (; it != end; ++it) {
138 docstring const & itname = (*it)->name();
139 if (translateIfPossible(itname) == name) {
140 FuncRequest const func(LFUN_LAYOUT, itname,
141 FuncRequest::TOOLBAR);
142 owner_.dispatch(func);
146 lyxerr << "ERROR (layoutSelected): layout not found!"
152 /////////////////////////////////////////////////////////////////////
156 /////////////////////////////////////////////////////////////////////
159 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiViewBase & owner)
160 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
161 layout_(0), command_buffer_(0)
163 // give visual separation between adjacent toolbars
166 // TODO: save toolbar position
169 ToolbarInfo::item_iterator it = tbinfo.items.begin();
170 ToolbarInfo::item_iterator end = tbinfo.items.end();
171 for (; it != end; ++it)
176 Action * GuiToolbar::addItem(ToolbarItem const & item)
178 Action * act = new Action(owner_,
179 getIcon(item.func_, false).c_str(),
180 toqstr(item.label_), item.func_, toqstr(item.label_));
181 actions_.append(act);
186 void GuiToolbar::add(ToolbarItem const & item)
188 switch (item.type_) {
189 case ToolbarItem::SEPARATOR:
192 case ToolbarItem::LAYOUTS:
193 layout_ = new GuiLayoutBox(owner_);
196 case ToolbarItem::MINIBUFFER:
197 command_buffer_ = new GuiCommandBuffer(&owner_);
198 addWidget(command_buffer_);
199 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
200 //setHorizontalStretchable(true);
202 case ToolbarItem::TABLEINSERT: {
203 QToolButton * tb = new QToolButton;
204 tb->setCheckable(true);
205 tb->setIcon(QPixmap(toqstr(getIcon(FuncRequest(LFUN_TABULAR_INSERT)))));
206 tb->setToolTip(qt_(to_ascii(item.label_)));
207 tb->setStatusTip(qt_(to_ascii(item.label_)));
208 tb->setText(qt_(to_ascii(item.label_)));
209 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
210 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
211 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
212 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
216 case ToolbarItem::ICONPALETTE: {
217 QToolButton * tb = new QToolButton(this);
218 tb->setToolTip(qt_(to_ascii(item.label_)));
219 tb->setStatusTip(qt_(to_ascii(item.label_)));
220 tb->setText(qt_(to_ascii(item.label_)));
221 connect(this, SIGNAL(iconSizeChanged(QSize)),
222 tb, SLOT(setIconSize(QSize)));
223 IconPalette * panel = new IconPalette(tb);
224 panel->setWindowTitle(qt_(to_ascii(item.label_)));
225 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
226 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
228 lyxerr << "Unknown toolbar " << item.name_ << endl;
231 ToolbarInfo::item_iterator it = tbinfo->items.begin();
232 ToolbarInfo::item_iterator const end = tbinfo->items.end();
233 for (; it != end; ++it)
234 if (!getStatus(it->func_).unknown()) {
235 panel->addButton(addItem(*it));
236 // use the icon of first action for the toolbar button
237 if (it == tbinfo->items.begin())
238 tb->setIcon(QPixmap(getIcon(it->func_).c_str()));
240 tb->setCheckable(true);
241 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
242 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
246 case ToolbarItem::POPUPMENU: {
247 QToolButton * tb = new QToolButton;
248 tb->setPopupMode(QToolButton::InstantPopup);
249 tb->setToolTip(qt_(to_ascii(item.label_)));
250 tb->setStatusTip(qt_(to_ascii(item.label_)));
251 tb->setText(qt_(to_ascii(item.label_)));
252 FileName icon_path = libFileSearch("images/math", item.name_, "png");
253 tb->setIcon(QIcon(toqstr(icon_path.absFilename())));
254 connect(this, SIGNAL(iconSizeChanged(QSize)),
255 tb, SLOT(setIconSize(QSize)));
257 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
258 m->setWindowTitle(qt_(to_ascii(item.label_)));
259 m->setTearOffEnabled(true);
260 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
261 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
263 lyxerr << "Unknown toolbar " << item.name_ << endl;
266 ToolbarInfo::item_iterator it = tbinfo->items.begin();
267 ToolbarInfo::item_iterator const end = tbinfo->items.end();
268 for (; it != end; ++it)
269 if (!getStatus(it->func_).unknown())
270 m->add(addItem(*it));
275 case ToolbarItem::COMMAND: {
276 if (!getStatus(item.func_).unknown())
277 addAction(addItem(item));
286 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
288 // if tbinfo.state == auto *do not* set on/off
289 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
290 if (GuiToolbar::isVisible())
291 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
293 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
296 // no need to save it here.
297 Qt::ToolBarArea loc = owner_.toolBarArea(this);
299 if (loc == Qt::TopToolBarArea)
300 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
301 else if (loc == Qt::BottomToolBarArea)
302 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
303 else if (loc == Qt::RightToolBarArea)
304 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
305 else if (loc == Qt::LeftToolBarArea)
306 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
308 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
310 // save toolbar position. They are not used to restore toolbar position
311 // now because move(x,y) does not work for toolbar.
312 tbinfo.posx = pos().x();
313 tbinfo.posy = pos().y();
317 void GuiToolbar::updateContents()
319 // update visible toolbars only
322 // This is a speed bottleneck because this is called on every keypress
323 // and update calls getStatus, which copies the cursor at least two times
324 for (int i = 0; i < actions_.size(); ++i)
325 actions_[i]->update();
340 bool operator<(PngMap const & lhs, PngMap const & rhs)
342 return compare(lhs.key, rhs.key) < 0;
348 CompareKey(string const & name) : name_(name) {}
349 bool operator()(PngMap const & other) const { return other.key == name_; }
355 PngMap sorted_png_map[] = {
356 { "Bumpeq", "bumpeq2" },
359 { "Delta", "delta2" },
360 { "Downarrow", "downarrow2" },
361 { "Gamma", "gamma2" },
362 { "Lambda", "lambda2" },
363 { "Leftarrow", "leftarrow2" },
364 { "Leftrightarrow", "leftrightarrow2" },
365 { "Longleftarrow", "longleftarrow2" },
366 { "Longleftrightarrow", "longleftrightarrow2" },
367 { "Longrightarrow", "longrightarrow2" },
368 { "Omega", "omega2" },
372 { "Rightarrow", "rightarrow2" },
373 { "Sigma", "sigma2" },
374 { "Subset", "subset2" },
375 { "Supset", "supset2" },
376 { "Theta", "theta2" },
377 { "Uparrow", "uparrow2" },
378 { "Updownarrow", "updownarrow2" },
379 { "Upsilon", "upsilon2" },
380 { "Vdash", "vdash3" },
382 { "nLeftarrow", "nleftarrow2" },
383 { "nLeftrightarrow", "nleftrightarrow2" },
384 { "nRightarrow", "nrightarrow2" },
385 { "nVDash", "nvdash3" },
386 { "nvDash", "nvdash2" },
387 { "textrm \\AA", "textrm_AA"},
388 { "textrm \\O", "textrm_Oe"},
389 { "vDash", "vdash2" }
392 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
395 string const find_png(string const & name)
397 PngMap const * const begin = sorted_png_map;
398 PngMap const * const end = begin + nr_sorted_png_map;
399 BOOST_ASSERT(sorted(begin, end));
401 PngMap const * const it = std::find_if(begin, end, CompareKey(name));
405 png_name = it->value;
407 png_name = subst(name, "_", "underscore");
408 png_name = subst(png_name, ' ', '_');
410 // This way we can have "math-delim { }" on the toolbar.
411 png_name = subst(png_name, "(", "lparen");
412 png_name = subst(png_name, ")", "rparen");
413 png_name = subst(png_name, "[", "lbracket");
414 png_name = subst(png_name, "]", "rbracket");
415 png_name = subst(png_name, "{", "lbrace");
416 png_name = subst(png_name, "}", "rbrace");
417 png_name = subst(png_name, "|", "bars");
418 png_name = subst(png_name, ",", "thinspace");
419 png_name = subst(png_name, ":", "mediumspace");
420 png_name = subst(png_name, ";", "thickspace");
421 png_name = subst(png_name, "!", "negthinspace");
424 LYXERR(Debug::GUI) << "find_png(" << name << ")\n"
425 << "Looking for math PNG called \""
426 << png_name << '"' << std::endl;
428 return libFileSearch("images/math/", png_name, "png").absFilename();
434 string const getIcon(FuncRequest const & f, bool unknown)
439 case LFUN_MATH_INSERT:
440 if (!f.argument().empty())
441 fullname = find_png(to_utf8(f.argument()).substr(1));
443 case LFUN_MATH_DELIM:
444 case LFUN_MATH_BIGDELIM:
445 fullname = find_png(to_utf8(f.argument()));
448 string const name = lyxaction.getActionName(f.action);
449 string png_name = name;
451 if (!f.argument().empty())
452 png_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
454 fullname = libFileSearch("images", png_name, "png").absFilename();
456 if (fullname.empty()) {
457 // try without the argument
458 fullname = libFileSearch("images", name, "png").absFilename();
462 if (!fullname.empty()) {
463 LYXERR(Debug::GUI) << "Full icon name is `"
464 << fullname << '\'' << endl;
468 LYXERR(Debug::GUI) << "Cannot find icon for command \""
469 << lyxaction.getActionName(f.action)
470 << '(' << to_utf8(f.argument()) << ")\"" << endl;
472 return libFileSearch("images", "unknown", "png").absFilename();
478 } // namespace frontend
481 #include "GuiToolbar_moc.cpp"