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>
46 #include <boost/assert.hpp>
49 static void initializeResources()
51 static bool initialized = false;
53 Q_INIT_RESOURCE(Resources);
65 using support::libFileSearch;
67 using support::compare;
78 bool operator<(PngMap const & lhs, PngMap const & rhs)
80 return compare(lhs.key, rhs.key) < 0;
86 CompareKey(string const & name) : name_(name) {}
87 bool operator()(PngMap const & other) const { return other.key == name_; }
93 PngMap sorted_png_map[] = {
94 { "Bumpeq", "bumpeq2" },
97 { "Delta", "delta2" },
98 { "Downarrow", "downarrow2" },
99 { "Gamma", "gamma2" },
100 { "Lambda", "lambda2" },
101 { "Leftarrow", "leftarrow2" },
102 { "Leftrightarrow", "leftrightarrow2" },
103 { "Longleftarrow", "longleftarrow2" },
104 { "Longleftrightarrow", "longleftrightarrow2" },
105 { "Longrightarrow", "longrightarrow2" },
106 { "Omega", "omega2" },
110 { "Rightarrow", "rightarrow2" },
111 { "Sigma", "sigma2" },
112 { "Subset", "subset2" },
113 { "Supset", "supset2" },
114 { "Theta", "theta2" },
115 { "Uparrow", "uparrow2" },
116 { "Updownarrow", "updownarrow2" },
117 { "Upsilon", "upsilon2" },
118 { "Vdash", "vdash3" },
120 { "nLeftarrow", "nleftarrow2" },
121 { "nLeftrightarrow", "nleftrightarrow2" },
122 { "nRightarrow", "nrightarrow2" },
123 { "nVDash", "nvdash3" },
124 { "nvDash", "nvdash2" },
125 { "textrm \\AA", "textrm_AA"},
126 { "textrm \\O", "textrm_Oe"},
127 { "vDash", "vdash2" }
130 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
133 string const find_png(string const & name)
135 PngMap const * const begin = sorted_png_map;
136 PngMap const * const end = begin + nr_sorted_png_map;
137 BOOST_ASSERT(sorted(begin, end));
139 PngMap const * const it = std::find_if(begin, end, CompareKey(name));
143 png_name = it->value;
145 png_name = subst(name, "_", "underscore");
146 png_name = subst(png_name, ' ', '_');
148 // This way we can have "math-delim { }" on the toolbar.
149 png_name = subst(png_name, "(", "lparen");
150 png_name = subst(png_name, ")", "rparen");
151 png_name = subst(png_name, "[", "lbracket");
152 png_name = subst(png_name, "]", "rbracket");
153 png_name = subst(png_name, "{", "lbrace");
154 png_name = subst(png_name, "}", "rbrace");
155 png_name = subst(png_name, "|", "bars");
156 png_name = subst(png_name, ",", "thinspace");
157 png_name = subst(png_name, ":", "mediumspace");
158 png_name = subst(png_name, ";", "thickspace");
159 png_name = subst(png_name, "!", "negthinspace");
162 LYXERR(Debug::GUI) << "find_png(" << name << ")\n"
163 << "Looking for math PNG called \""
164 << png_name << '"' << std::endl;
171 /// return a icon for the given action
172 static QIcon getIcon(FuncRequest const & f, bool unknown)
174 initializeResources();
182 case LFUN_MATH_INSERT:
183 if (!f.argument().empty()) {
185 name1 = find_png(to_utf8(f.argument()).substr(1));
188 case LFUN_MATH_DELIM:
189 case LFUN_MATH_BIGDELIM:
191 name1 = find_png(to_utf8(f.argument()));
195 name1 = to_utf8(f.argument());
198 name2 = lyxaction.getActionName(f.action);
201 if (!f.argument().empty())
202 name1 = subst(name2 + ' ' + to_utf8(f.argument()), ' ', '_');
205 fullname = libFileSearch("images/" + path, name1, "png").absFilename();
206 if (pm.load(toqstr(fullname)))
209 fullname = libFileSearch("images/" + path, name2, "png").absFilename();
210 if (pm.load(toqstr(fullname)))
213 if (pm.load(":/images/" + toqstr(path + name1) + ".png"))
216 if (pm.load(":/images/" + toqstr(path + name2) + ".png"))
219 LYXERR(Debug::GUI) << "Cannot find icon for command \""
220 << lyxaction.getActionName(f.action)
221 << '(' << to_utf8(f.argument()) << ")\"" << endl;
223 pm.load(":/images/unknown.png");
229 static TextClass const & textClass(LyXView const & lv)
231 return lv.buffer()->params().getTextClass();
235 /////////////////////////////////////////////////////////////////////
239 /////////////////////////////////////////////////////////////////////
241 GuiLayoutBox::GuiLayoutBox(GuiViewBase & owner)
244 setSizeAdjustPolicy(QComboBox::AdjustToContents);
245 setFocusPolicy(Qt::ClickFocus);
246 setMinimumWidth(sizeHint().width());
247 setMaxVisibleItems(100);
249 QObject::connect(this, SIGNAL(activated(QString)),
250 this, SLOT(selected(QString)));
254 void GuiLayoutBox::set(docstring const & layout)
256 TextClass const & tc = textClass(owner_);
258 QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
261 for (; i < count(); ++i) {
262 if (name == itemText(i))
267 lyxerr << "Trying to select non existent layout type "
268 << fromqstr(name) << endl;
276 void GuiLayoutBox::updateContents()
278 TextClass const & tc = textClass(owner_);
280 setUpdatesEnabled(false);
283 TextClass::const_iterator it = tc.begin();
284 TextClass::const_iterator const end = tc.end();
285 for (; it != end; ++it) {
286 // ignore obsolete entries
287 addItem(toqstr(translateIfPossible((*it)->name())));
290 // needed to recalculate size hint
292 setMinimumWidth(sizeHint().width());
295 setUpdatesEnabled(true);
299 void GuiLayoutBox::selected(const QString & str)
302 TextClass const & tc = owner_.buffer()->params().getTextClass();
303 docstring const name = qstring_to_ucs4(str);
304 TextClass::const_iterator it = tc.begin();
305 TextClass::const_iterator const end = tc.end();
306 for (; it != end; ++it) {
307 docstring const & itname = (*it)->name();
308 if (translateIfPossible(itname) == name) {
309 FuncRequest const func(LFUN_LAYOUT, itname,
310 FuncRequest::TOOLBAR);
311 owner_.dispatch(func);
315 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
320 /////////////////////////////////////////////////////////////////////
324 /////////////////////////////////////////////////////////////////////
327 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiViewBase & owner)
328 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
329 layout_(0), command_buffer_(0)
331 // give visual separation between adjacent toolbars
334 // TODO: save toolbar position
337 ToolbarInfo::item_iterator it = tbinfo.items.begin();
338 ToolbarInfo::item_iterator end = tbinfo.items.end();
339 for (; it != end; ++it)
344 Action * GuiToolbar::addItem(ToolbarItem const & item)
346 Action * act = new Action(owner_,
347 getIcon(item.func_, false),
348 toqstr(item.label_), item.func_, toqstr(item.label_));
349 actions_.append(act);
354 void GuiToolbar::add(ToolbarItem const & item)
356 switch (item.type_) {
357 case ToolbarItem::SEPARATOR:
360 case ToolbarItem::LAYOUTS:
361 layout_ = new GuiLayoutBox(owner_);
364 case ToolbarItem::MINIBUFFER:
365 command_buffer_ = new GuiCommandBuffer(&owner_);
366 addWidget(command_buffer_);
367 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
368 //setHorizontalStretchable(true);
370 case ToolbarItem::TABLEINSERT: {
371 QToolButton * tb = new QToolButton;
372 tb->setCheckable(true);
373 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
374 tb->setToolTip(qt_(to_ascii(item.label_)));
375 tb->setStatusTip(qt_(to_ascii(item.label_)));
376 tb->setText(qt_(to_ascii(item.label_)));
377 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
378 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
379 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
380 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
384 case ToolbarItem::ICONPALETTE: {
385 QToolButton * tb = new QToolButton(this);
386 tb->setToolTip(qt_(to_ascii(item.label_)));
387 tb->setStatusTip(qt_(to_ascii(item.label_)));
388 tb->setText(qt_(to_ascii(item.label_)));
389 connect(this, SIGNAL(iconSizeChanged(QSize)),
390 tb, SLOT(setIconSize(QSize)));
391 IconPalette * panel = new IconPalette(tb);
392 panel->setWindowTitle(qt_(to_ascii(item.label_)));
393 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
394 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
396 lyxerr << "Unknown toolbar " << item.name_ << endl;
399 ToolbarInfo::item_iterator it = tbinfo->items.begin();
400 ToolbarInfo::item_iterator const end = tbinfo->items.end();
401 for (; it != end; ++it)
402 if (!getStatus(it->func_).unknown()) {
403 panel->addButton(addItem(*it));
404 // use the icon of first action for the toolbar button
405 if (it == tbinfo->items.begin())
406 tb->setIcon(getIcon(it->func_, true));
408 tb->setCheckable(true);
409 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
410 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
414 case ToolbarItem::POPUPMENU: {
415 QToolButton * tb = new QToolButton;
416 tb->setPopupMode(QToolButton::InstantPopup);
417 tb->setToolTip(qt_(to_ascii(item.label_)));
418 tb->setStatusTip(qt_(to_ascii(item.label_)));
419 tb->setText(qt_(to_ascii(item.label_)));
420 tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
421 connect(this, SIGNAL(iconSizeChanged(QSize)),
422 tb, SLOT(setIconSize(QSize)));
424 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
425 m->setWindowTitle(qt_(to_ascii(item.label_)));
426 m->setTearOffEnabled(true);
427 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
428 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
430 lyxerr << "Unknown toolbar " << item.name_ << endl;
433 ToolbarInfo::item_iterator it = tbinfo->items.begin();
434 ToolbarInfo::item_iterator const end = tbinfo->items.end();
435 for (; it != end; ++it)
436 if (!getStatus(it->func_).unknown())
437 m->add(addItem(*it));
442 case ToolbarItem::COMMAND: {
443 if (!getStatus(item.func_).unknown())
444 addAction(addItem(item));
453 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
455 // if tbinfo.state == auto *do not* set on/off
456 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
457 if (GuiToolbar::isVisible())
458 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
460 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
463 // no need to save it here.
464 Qt::ToolBarArea loc = owner_.toolBarArea(this);
466 if (loc == Qt::TopToolBarArea)
467 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
468 else if (loc == Qt::BottomToolBarArea)
469 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
470 else if (loc == Qt::RightToolBarArea)
471 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
472 else if (loc == Qt::LeftToolBarArea)
473 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
475 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
477 // save toolbar position. They are not used to restore toolbar position
478 // now because move(x,y) does not work for toolbar.
479 tbinfo.posx = pos().x();
480 tbinfo.posy = pos().y();
484 void GuiToolbar::updateContents()
486 // update visible toolbars only
489 // This is a speed bottleneck because this is called on every keypress
490 // and update calls getStatus, which copies the cursor at least two times
491 for (int i = 0; i < actions_.size(); ++i)
492 actions_[i]->update();
499 } // namespace frontend
502 #include "GuiToolbar_moc.cpp"