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 "TextClass.h"
27 #include "ToolbarBackend.h"
30 #include "GuiCommandBuffer.h"
31 #include "GuiToolbar.h"
32 #include "LyXAction.h"
34 #include "qt_helpers.h"
35 #include "InsertTableWidget.h"
38 #include "support/filetools.h"
39 #include "support/lstrings.h"
40 #include "support/lyxalgo.h" // sorted
44 #include <QToolButton>
48 #include <boost/assert.hpp>
51 static void initializeResources()
53 static bool initialized = false;
55 Q_INIT_RESOURCE(Resources);
67 using support::libFileSearch;
69 using support::compare;
80 bool operator<(PngMap const & lhs, PngMap const & rhs)
82 return compare(lhs.key, rhs.key) < 0;
88 CompareKey(string const & name) : name_(name) {}
89 bool operator()(PngMap const & other) const { return other.key == name_; }
95 PngMap sorted_png_map[] = {
96 { "Bumpeq", "bumpeq2" },
99 { "Delta", "delta2" },
100 { "Downarrow", "downarrow2" },
101 { "Gamma", "gamma2" },
102 { "Lambda", "lambda2" },
103 { "Leftarrow", "leftarrow2" },
104 { "Leftrightarrow", "leftrightarrow2" },
105 { "Longleftarrow", "longleftarrow2" },
106 { "Longleftrightarrow", "longleftrightarrow2" },
107 { "Longrightarrow", "longrightarrow2" },
108 { "Omega", "omega2" },
112 { "Rightarrow", "rightarrow2" },
113 { "Sigma", "sigma2" },
114 { "Subset", "subset2" },
115 { "Supset", "supset2" },
116 { "Theta", "theta2" },
117 { "Uparrow", "uparrow2" },
118 { "Updownarrow", "updownarrow2" },
119 { "Upsilon", "upsilon2" },
120 { "Vdash", "vdash3" },
122 { "nLeftarrow", "nleftarrow2" },
123 { "nLeftrightarrow", "nleftrightarrow2" },
124 { "nRightarrow", "nrightarrow2" },
125 { "nVDash", "nvdash3" },
126 { "nvDash", "nvdash2" },
127 { "textrm \\AA", "textrm_AA"},
128 { "textrm \\O", "textrm_Oe"},
129 { "vDash", "vdash2" }
132 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
135 string const find_png(string const & name)
137 PngMap const * const begin = sorted_png_map;
138 PngMap const * const end = begin + nr_sorted_png_map;
139 BOOST_ASSERT(sorted(begin, end));
141 PngMap const * const it = std::find_if(begin, end, CompareKey(name));
145 png_name = it->value;
147 png_name = subst(name, "_", "underscore");
148 png_name = subst(png_name, ' ', '_');
150 // This way we can have "math-delim { }" on the toolbar.
151 png_name = subst(png_name, "(", "lparen");
152 png_name = subst(png_name, ")", "rparen");
153 png_name = subst(png_name, "[", "lbracket");
154 png_name = subst(png_name, "]", "rbracket");
155 png_name = subst(png_name, "{", "lbrace");
156 png_name = subst(png_name, "}", "rbrace");
157 png_name = subst(png_name, "|", "bars");
158 png_name = subst(png_name, ",", "thinspace");
159 png_name = subst(png_name, ":", "mediumspace");
160 png_name = subst(png_name, ";", "thickspace");
161 png_name = subst(png_name, "!", "negthinspace");
164 LYXERR(Debug::GUI, "find_png(" << name << ")\n"
165 << "Looking for math PNG called \"" << png_name << '"');
172 /// return a icon for the given action
173 static QIcon getIcon(FuncRequest const & f, bool unknown)
175 initializeResources();
183 case LFUN_MATH_INSERT:
184 if (!f.argument().empty()) {
186 name1 = find_png(to_utf8(f.argument()).substr(1));
189 case LFUN_MATH_DELIM:
190 case LFUN_MATH_BIGDELIM:
192 name1 = find_png(to_utf8(f.argument()));
196 name1 = to_utf8(f.argument());
199 name2 = lyxaction.getActionName(f.action);
202 if (!f.argument().empty())
203 name1 = subst(name2 + ' ' + to_utf8(f.argument()), ' ', '_');
206 fullname = libFileSearch("images/" + path, name1, "png").absFilename();
207 if (pm.load(toqstr(fullname)))
210 fullname = libFileSearch("images/" + path, name2, "png").absFilename();
211 if (pm.load(toqstr(fullname)))
214 if (pm.load(":/images/" + toqstr(path + name1) + ".png"))
217 if (pm.load(":/images/" + toqstr(path + name2) + ".png"))
220 LYXERR(Debug::GUI, "Cannot find icon for command \""
221 << lyxaction.getActionName(f.action)
222 << '(' << to_utf8(f.argument()) << ")\"");
224 pm.load(":/images/unknown.png");
230 static TextClass const & textClass(LyXView const & lv)
232 return lv.buffer()->params().getTextClass();
236 /////////////////////////////////////////////////////////////////////
240 /////////////////////////////////////////////////////////////////////
242 GuiLayoutBox::GuiLayoutBox(GuiView & owner)
245 setSizeAdjustPolicy(QComboBox::AdjustToContents);
246 setFocusPolicy(Qt::ClickFocus);
247 setMinimumWidth(sizeHint().width());
248 setMaxVisibleItems(100);
250 QObject::connect(this, SIGNAL(activated(QString)),
251 this, SLOT(selected(QString)));
255 void GuiLayoutBox::set(docstring const & layout)
257 TextClass const & tc = textClass(owner_);
259 QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
262 for (; i < count(); ++i) {
263 if (name == itemText(i))
268 lyxerr << "Trying to select non existent layout type "
269 << fromqstr(name) << endl;
277 void GuiLayoutBox::addItemSort(QString const & item, bool sorted)
279 int const end = count();
280 if (!sorted || end < 2 || item[0].category() != QChar::Letter_Uppercase) {
285 // Let the default one be at the beginning
287 for (setCurrentIndex(i); currentText() < item;) {
288 // e.g. --Separator--
289 if (currentText()[0].category() != QChar::Letter_Uppercase)
300 void GuiLayoutBox::updateContents()
302 TextClass const & tc = textClass(owner_);
304 setUpdatesEnabled(false);
307 TextClass::const_iterator it = tc.begin();
308 TextClass::const_iterator const end = tc.end();
309 for (; it != end; ++it) {
310 // ignore obsolete entries
311 addItemSort(toqstr(translateIfPossible((*it)->name())), lyxrc.sort_layouts);
316 // needed to recalculate size hint
318 setMinimumWidth(sizeHint().width());
321 setUpdatesEnabled(true);
325 void GuiLayoutBox::selected(const QString & str)
328 TextClass const & tc = owner_.buffer()->params().getTextClass();
329 docstring const name = qstring_to_ucs4(str);
330 TextClass::const_iterator it = tc.begin();
331 TextClass::const_iterator const end = tc.end();
332 for (; it != end; ++it) {
333 docstring const & itname = (*it)->name();
334 if (translateIfPossible(itname) == name) {
335 FuncRequest const func(LFUN_LAYOUT, itname,
336 FuncRequest::TOOLBAR);
337 owner_.dispatch(func);
341 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
346 /////////////////////////////////////////////////////////////////////
350 /////////////////////////////////////////////////////////////////////
353 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
354 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
355 layout_(0), command_buffer_(0)
357 // give visual separation between adjacent toolbars
360 // TODO: save toolbar position
363 ToolbarInfo::item_iterator it = tbinfo.items.begin();
364 ToolbarInfo::item_iterator end = tbinfo.items.end();
365 for (; it != end; ++it)
370 Action * GuiToolbar::addItem(ToolbarItem const & item)
372 Action * act = new Action(owner_,
373 getIcon(item.func_, false),
374 toqstr(item.label_), item.func_, toqstr(item.label_));
375 actions_.append(act);
380 void GuiToolbar::add(ToolbarItem const & item)
382 switch (item.type_) {
383 case ToolbarItem::SEPARATOR:
386 case ToolbarItem::LAYOUTS:
387 layout_ = new GuiLayoutBox(owner_);
390 case ToolbarItem::MINIBUFFER:
391 command_buffer_ = new GuiCommandBuffer(&owner_);
392 addWidget(command_buffer_);
393 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
394 //setHorizontalStretchable(true);
396 case ToolbarItem::TABLEINSERT: {
397 QToolButton * tb = new QToolButton;
398 tb->setCheckable(true);
399 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
400 tb->setToolTip(qt_(to_ascii(item.label_)));
401 tb->setStatusTip(qt_(to_ascii(item.label_)));
402 tb->setText(qt_(to_ascii(item.label_)));
403 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
404 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
405 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
406 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
410 case ToolbarItem::ICONPALETTE: {
411 QToolButton * tb = new QToolButton(this);
412 tb->setToolTip(qt_(to_ascii(item.label_)));
413 tb->setStatusTip(qt_(to_ascii(item.label_)));
414 tb->setText(qt_(to_ascii(item.label_)));
415 connect(this, SIGNAL(iconSizeChanged(QSize)),
416 tb, SLOT(setIconSize(QSize)));
417 IconPalette * panel = new IconPalette(tb);
418 panel->setWindowTitle(qt_(to_ascii(item.label_)));
419 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
420 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
422 lyxerr << "Unknown toolbar " << item.name_ << endl;
425 ToolbarInfo::item_iterator it = tbinfo->items.begin();
426 ToolbarInfo::item_iterator const end = tbinfo->items.end();
427 for (; it != end; ++it)
428 if (!getStatus(it->func_).unknown()) {
429 panel->addButton(addItem(*it));
430 // use the icon of first action for the toolbar button
431 if (it == tbinfo->items.begin())
432 tb->setIcon(getIcon(it->func_, true));
434 tb->setCheckable(true);
435 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
436 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
440 case ToolbarItem::POPUPMENU: {
441 QToolButton * tb = new QToolButton;
442 tb->setPopupMode(QToolButton::InstantPopup);
443 tb->setToolTip(qt_(to_ascii(item.label_)));
444 tb->setStatusTip(qt_(to_ascii(item.label_)));
445 tb->setText(qt_(to_ascii(item.label_)));
446 tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
447 connect(this, SIGNAL(iconSizeChanged(QSize)),
448 tb, SLOT(setIconSize(QSize)));
450 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
451 m->setWindowTitle(qt_(to_ascii(item.label_)));
452 m->setTearOffEnabled(true);
453 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
454 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
456 lyxerr << "Unknown toolbar " << item.name_ << endl;
459 ToolbarInfo::item_iterator it = tbinfo->items.begin();
460 ToolbarInfo::item_iterator const end = tbinfo->items.end();
461 for (; it != end; ++it)
462 if (!getStatus(it->func_).unknown())
463 m->add(addItem(*it));
468 case ToolbarItem::COMMAND: {
469 if (!getStatus(item.func_).unknown())
470 addAction(addItem(item));
479 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
481 // if tbinfo.state == auto *do not* set on/off
482 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
483 if (GuiToolbar::isVisible())
484 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
486 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
489 // no need to save it here.
490 Qt::ToolBarArea loc = owner_.toolBarArea(this);
492 if (loc == Qt::TopToolBarArea)
493 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
494 else if (loc == Qt::BottomToolBarArea)
495 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
496 else if (loc == Qt::RightToolBarArea)
497 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
498 else if (loc == Qt::LeftToolBarArea)
499 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
501 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
503 // save toolbar position. They are not used to restore toolbar position
504 // now because move(x,y) does not work for toolbar.
505 tbinfo.posx = pos().x();
506 tbinfo.posy = pos().y();
510 void GuiToolbar::updateContents()
512 // update visible toolbars only
515 // This is a speed bottleneck because this is called on every keypress
516 // and update calls getStatus, which copies the cursor at least two times
517 for (int i = 0; i < actions_.size(); ++i)
518 actions_[i]->update();
525 } // namespace frontend
528 #include "GuiToolbar_moc.cpp"