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;
68 bool operator<(PngMap const & lhs, PngMap const & rhs)
70 return compare(lhs.key, rhs.key) < 0;
76 CompareKey(string const & name) : name_(name) {}
77 bool operator()(PngMap const & other) const { return other.key == name_; }
83 PngMap sorted_png_map[] = {
84 { "Bumpeq", "bumpeq2" },
87 { "Delta", "delta2" },
88 { "Downarrow", "downarrow2" },
89 { "Gamma", "gamma2" },
90 { "Lambda", "lambda2" },
91 { "Leftarrow", "leftarrow2" },
92 { "Leftrightarrow", "leftrightarrow2" },
93 { "Longleftarrow", "longleftarrow2" },
94 { "Longleftrightarrow", "longleftrightarrow2" },
95 { "Longrightarrow", "longrightarrow2" },
96 { "Omega", "omega2" },
100 { "Rightarrow", "rightarrow2" },
101 { "Sigma", "sigma2" },
102 { "Subset", "subset2" },
103 { "Supset", "supset2" },
104 { "Theta", "theta2" },
105 { "Uparrow", "uparrow2" },
106 { "Updownarrow", "updownarrow2" },
107 { "Upsilon", "upsilon2" },
108 { "Vdash", "vdash3" },
110 { "nLeftarrow", "nleftarrow2" },
111 { "nLeftrightarrow", "nleftrightarrow2" },
112 { "nRightarrow", "nrightarrow2" },
113 { "nVDash", "nvdash3" },
114 { "nvDash", "nvdash2" },
115 { "textrm \\AA", "textrm_AA"},
116 { "textrm \\O", "textrm_Oe"},
117 { "vDash", "vdash2" }
120 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
123 string const find_png(string const & name)
125 PngMap const * const begin = sorted_png_map;
126 PngMap const * const end = begin + nr_sorted_png_map;
127 BOOST_ASSERT(sorted(begin, end));
129 PngMap const * const it = std::find_if(begin, end, CompareKey(name));
133 png_name = it->value;
135 png_name = subst(name, "_", "underscore");
136 png_name = subst(png_name, ' ', '_');
138 // This way we can have "math-delim { }" on the toolbar.
139 png_name = subst(png_name, "(", "lparen");
140 png_name = subst(png_name, ")", "rparen");
141 png_name = subst(png_name, "[", "lbracket");
142 png_name = subst(png_name, "]", "rbracket");
143 png_name = subst(png_name, "{", "lbrace");
144 png_name = subst(png_name, "}", "rbrace");
145 png_name = subst(png_name, "|", "bars");
146 png_name = subst(png_name, ",", "thinspace");
147 png_name = subst(png_name, ":", "mediumspace");
148 png_name = subst(png_name, ";", "thickspace");
149 png_name = subst(png_name, "!", "negthinspace");
152 LYXERR(Debug::GUI) << "find_png(" << name << ")\n"
153 << "Looking for math PNG called \""
154 << png_name << '"' << std::endl;
156 return libFileSearch("images/math/", png_name, "png").absFilename();
162 /// return a icon for the given action
163 static QIcon getIcon(FuncRequest const & f, bool unknown)
168 case LFUN_MATH_INSERT:
169 if (!f.argument().empty())
170 fullname = find_png(to_utf8(f.argument()).substr(1));
172 case LFUN_MATH_DELIM:
173 case LFUN_MATH_BIGDELIM:
174 fullname = find_png(to_utf8(f.argument()));
177 string const name = lyxaction.getActionName(f.action);
178 string png_name = name;
180 if (!f.argument().empty())
181 png_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
183 fullname = libFileSearch("images", png_name, "png").absFilename();
185 if (fullname.empty()) {
186 // try without the argument
187 fullname = libFileSearch("images", name, "png").absFilename();
191 if (!fullname.empty()) {
192 LYXERR(Debug::GUI) << "Full icon name is `"
193 << fullname << '\'' << endl;
194 return QIcon(toqstr(fullname));
197 LYXERR(Debug::GUI) << "Cannot find icon for command \""
198 << lyxaction.getActionName(f.action)
199 << '(' << to_utf8(f.argument()) << ")\"" << endl;
201 return QIcon(toqstr(libFileSearch("images", "unknown", "png").absFilename()));
206 static TextClass const & textClass(LyXView const & lv)
208 return lv.buffer()->params().getTextClass();
212 /////////////////////////////////////////////////////////////////////
216 /////////////////////////////////////////////////////////////////////
218 GuiLayoutBox::GuiLayoutBox(GuiViewBase & owner)
221 setSizeAdjustPolicy(QComboBox::AdjustToContents);
222 setFocusPolicy(Qt::ClickFocus);
223 setMinimumWidth(sizeHint().width());
224 setMaxVisibleItems(100);
226 QObject::connect(this, SIGNAL(activated(QString)),
227 this, SLOT(selected(QString)));
231 void GuiLayoutBox::set(docstring const & layout)
233 TextClass const & tc = textClass(owner_);
235 QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
238 for (; i < count(); ++i) {
239 if (name == itemText(i))
244 lyxerr << "Trying to select non existent layout type "
245 << fromqstr(name) << endl;
253 void GuiLayoutBox::updateContents()
255 TextClass const & tc = textClass(owner_);
257 setUpdatesEnabled(false);
260 TextClass::const_iterator it = tc.begin();
261 TextClass::const_iterator const end = tc.end();
262 for (; it != end; ++it) {
263 // ignore obsolete entries
264 if ((*it)->obsoleted_by().empty())
265 addItem(toqstr(translateIfPossible((*it)->name())));
268 // needed to recalculate size hint
270 setMinimumWidth(sizeHint().width());
273 setUpdatesEnabled(true);
277 void GuiLayoutBox::selected(const QString & str)
280 TextClass const & tc = owner_.buffer()->params().getTextClass();
281 docstring const name = qstring_to_ucs4(str);
282 TextClass::const_iterator it = tc.begin();
283 TextClass::const_iterator const end = tc.end();
284 for (; it != end; ++it) {
285 docstring const & itname = (*it)->name();
286 if (translateIfPossible(itname) == name) {
287 FuncRequest const func(LFUN_LAYOUT, itname,
288 FuncRequest::TOOLBAR);
289 owner_.dispatch(func);
293 lyxerr << "ERROR (layoutSelected): layout not found!"
299 /////////////////////////////////////////////////////////////////////
303 /////////////////////////////////////////////////////////////////////
306 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiViewBase & owner)
307 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
308 layout_(0), command_buffer_(0)
310 // give visual separation between adjacent toolbars
313 // TODO: save toolbar position
316 ToolbarInfo::item_iterator it = tbinfo.items.begin();
317 ToolbarInfo::item_iterator end = tbinfo.items.end();
318 for (; it != end; ++it)
323 Action * GuiToolbar::addItem(ToolbarItem const & item)
325 Action * act = new Action(owner_,
326 getIcon(item.func_, false),
327 toqstr(item.label_), item.func_, toqstr(item.label_));
328 actions_.append(act);
333 void GuiToolbar::add(ToolbarItem const & item)
335 switch (item.type_) {
336 case ToolbarItem::SEPARATOR:
339 case ToolbarItem::LAYOUTS:
340 layout_ = new GuiLayoutBox(owner_);
343 case ToolbarItem::MINIBUFFER:
344 command_buffer_ = new GuiCommandBuffer(&owner_);
345 addWidget(command_buffer_);
346 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
347 //setHorizontalStretchable(true);
349 case ToolbarItem::TABLEINSERT: {
350 QToolButton * tb = new QToolButton;
351 tb->setCheckable(true);
352 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
353 tb->setToolTip(qt_(to_ascii(item.label_)));
354 tb->setStatusTip(qt_(to_ascii(item.label_)));
355 tb->setText(qt_(to_ascii(item.label_)));
356 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
357 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
358 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
359 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
363 case ToolbarItem::ICONPALETTE: {
364 QToolButton * tb = new QToolButton(this);
365 tb->setToolTip(qt_(to_ascii(item.label_)));
366 tb->setStatusTip(qt_(to_ascii(item.label_)));
367 tb->setText(qt_(to_ascii(item.label_)));
368 connect(this, SIGNAL(iconSizeChanged(QSize)),
369 tb, SLOT(setIconSize(QSize)));
370 IconPalette * panel = new IconPalette(tb);
371 panel->setWindowTitle(qt_(to_ascii(item.label_)));
372 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
373 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
375 lyxerr << "Unknown toolbar " << item.name_ << endl;
378 ToolbarInfo::item_iterator it = tbinfo->items.begin();
379 ToolbarInfo::item_iterator const end = tbinfo->items.end();
380 for (; it != end; ++it)
381 if (!getStatus(it->func_).unknown()) {
382 panel->addButton(addItem(*it));
383 // use the icon of first action for the toolbar button
384 if (it == tbinfo->items.begin())
385 tb->setIcon(getIcon(it->func_, true));
387 tb->setCheckable(true);
388 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
389 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
393 case ToolbarItem::POPUPMENU: {
394 QToolButton * tb = new QToolButton;
395 tb->setPopupMode(QToolButton::InstantPopup);
396 tb->setToolTip(qt_(to_ascii(item.label_)));
397 tb->setStatusTip(qt_(to_ascii(item.label_)));
398 tb->setText(qt_(to_ascii(item.label_)));
399 FileName icon_path = libFileSearch("images/math", item.name_, "png");
400 tb->setIcon(QIcon(toqstr(icon_path.absFilename())));
401 connect(this, SIGNAL(iconSizeChanged(QSize)),
402 tb, SLOT(setIconSize(QSize)));
404 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
405 m->setWindowTitle(qt_(to_ascii(item.label_)));
406 m->setTearOffEnabled(true);
407 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
408 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
410 lyxerr << "Unknown toolbar " << item.name_ << endl;
413 ToolbarInfo::item_iterator it = tbinfo->items.begin();
414 ToolbarInfo::item_iterator const end = tbinfo->items.end();
415 for (; it != end; ++it)
416 if (!getStatus(it->func_).unknown())
417 m->add(addItem(*it));
422 case ToolbarItem::COMMAND: {
423 if (!getStatus(item.func_).unknown())
424 addAction(addItem(item));
433 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
435 // if tbinfo.state == auto *do not* set on/off
436 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
437 if (GuiToolbar::isVisible())
438 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
440 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
443 // no need to save it here.
444 Qt::ToolBarArea loc = owner_.toolBarArea(this);
446 if (loc == Qt::TopToolBarArea)
447 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
448 else if (loc == Qt::BottomToolBarArea)
449 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
450 else if (loc == Qt::RightToolBarArea)
451 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
452 else if (loc == Qt::LeftToolBarArea)
453 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
455 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
457 // save toolbar position. They are not used to restore toolbar position
458 // now because move(x,y) does not work for toolbar.
459 tbinfo.posx = pos().x();
460 tbinfo.posy = pos().y();
464 void GuiToolbar::updateContents()
466 // update visible toolbars only
469 // This is a speed bottleneck because this is called on every keypress
470 // and update calls getStatus, which copies the cursor at least two times
471 for (int i = 0; i < actions_.size(); ++i)
472 actions_[i]->update();
480 } // namespace frontend
483 #include "GuiToolbar_moc.cpp"