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"
37 #include "support/filetools.h"
38 #include "support/lstrings.h"
39 #include "support/lyxalgo.h" // sorted
43 #include <QToolButton>
47 #include <boost/assert.hpp>
50 static void initializeResources()
52 static bool initialized = false;
54 Q_INIT_RESOURCE(Resources);
66 using support::libFileSearch;
68 using support::compare;
79 bool operator<(PngMap const & lhs, PngMap const & rhs)
81 return compare(lhs.key, rhs.key) < 0;
87 CompareKey(string const & name) : name_(name) {}
88 bool operator()(PngMap const & other) const { return other.key == name_; }
94 PngMap sorted_png_map[] = {
95 { "Bumpeq", "bumpeq2" },
98 { "Delta", "delta2" },
99 { "Downarrow", "downarrow2" },
100 { "Gamma", "gamma2" },
101 { "Lambda", "lambda2" },
102 { "Leftarrow", "leftarrow2" },
103 { "Leftrightarrow", "leftrightarrow2" },
104 { "Longleftarrow", "longleftarrow2" },
105 { "Longleftrightarrow", "longleftrightarrow2" },
106 { "Longrightarrow", "longrightarrow2" },
107 { "Omega", "omega2" },
111 { "Rightarrow", "rightarrow2" },
112 { "Sigma", "sigma2" },
113 { "Subset", "subset2" },
114 { "Supset", "supset2" },
115 { "Theta", "theta2" },
116 { "Uparrow", "uparrow2" },
117 { "Updownarrow", "updownarrow2" },
118 { "Upsilon", "upsilon2" },
119 { "Vdash", "vdash3" },
121 { "nLeftarrow", "nleftarrow2" },
122 { "nLeftrightarrow", "nleftrightarrow2" },
123 { "nRightarrow", "nrightarrow2" },
124 { "nVDash", "nvdash3" },
125 { "nvDash", "nvdash2" },
126 { "textrm \\AA", "textrm_AA"},
127 { "textrm \\O", "textrm_Oe"},
128 { "vDash", "vdash2" }
131 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
134 string const find_png(string const & name)
136 PngMap const * const begin = sorted_png_map;
137 PngMap const * const end = begin + nr_sorted_png_map;
138 BOOST_ASSERT(sorted(begin, end));
140 PngMap const * const it = std::find_if(begin, end, CompareKey(name));
144 png_name = it->value;
146 png_name = subst(name, "_", "underscore");
147 png_name = subst(png_name, ' ', '_');
149 // This way we can have "math-delim { }" on the toolbar.
150 png_name = subst(png_name, "(", "lparen");
151 png_name = subst(png_name, ")", "rparen");
152 png_name = subst(png_name, "[", "lbracket");
153 png_name = subst(png_name, "]", "rbracket");
154 png_name = subst(png_name, "{", "lbrace");
155 png_name = subst(png_name, "}", "rbrace");
156 png_name = subst(png_name, "|", "bars");
157 png_name = subst(png_name, ",", "thinspace");
158 png_name = subst(png_name, ":", "mediumspace");
159 png_name = subst(png_name, ";", "thickspace");
160 png_name = subst(png_name, "!", "negthinspace");
163 LYXERR(Debug::GUI) << "find_png(" << name << ")\n"
164 << "Looking for math PNG called \""
165 << png_name << '"' << std::endl;
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()) << ")\"" << endl;
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::updateContents()
279 TextClass const & tc = textClass(owner_);
281 setUpdatesEnabled(false);
284 TextClass::const_iterator it = tc.begin();
285 TextClass::const_iterator const end = tc.end();
286 for (; it != end; ++it) {
287 // ignore obsolete entries
288 addItem(toqstr(translateIfPossible((*it)->name())));
291 // needed to recalculate size hint
293 setMinimumWidth(sizeHint().width());
296 setUpdatesEnabled(true);
300 void GuiLayoutBox::selected(const QString & str)
303 TextClass const & tc = owner_.buffer()->params().getTextClass();
304 docstring const name = qstring_to_ucs4(str);
305 TextClass::const_iterator it = tc.begin();
306 TextClass::const_iterator const end = tc.end();
307 for (; it != end; ++it) {
308 docstring const & itname = (*it)->name();
309 if (translateIfPossible(itname) == name) {
310 FuncRequest const func(LFUN_LAYOUT, itname,
311 FuncRequest::TOOLBAR);
312 owner_.dispatch(func);
316 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
321 /////////////////////////////////////////////////////////////////////
325 /////////////////////////////////////////////////////////////////////
328 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
329 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
330 layout_(0), command_buffer_(0)
332 // give visual separation between adjacent toolbars
335 // TODO: save toolbar position
338 ToolbarInfo::item_iterator it = tbinfo.items.begin();
339 ToolbarInfo::item_iterator end = tbinfo.items.end();
340 for (; it != end; ++it)
345 Action * GuiToolbar::addItem(ToolbarItem const & item)
347 Action * act = new Action(owner_,
348 getIcon(item.func_, false),
349 toqstr(item.label_), item.func_, toqstr(item.label_));
350 actions_.append(act);
355 void GuiToolbar::add(ToolbarItem const & item)
357 switch (item.type_) {
358 case ToolbarItem::SEPARATOR:
361 case ToolbarItem::LAYOUTS:
362 layout_ = new GuiLayoutBox(owner_);
365 case ToolbarItem::MINIBUFFER:
366 command_buffer_ = new GuiCommandBuffer(&owner_);
367 addWidget(command_buffer_);
368 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
369 //setHorizontalStretchable(true);
371 case ToolbarItem::TABLEINSERT: {
372 QToolButton * tb = new QToolButton;
373 tb->setCheckable(true);
374 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
375 tb->setToolTip(qt_(to_ascii(item.label_)));
376 tb->setStatusTip(qt_(to_ascii(item.label_)));
377 tb->setText(qt_(to_ascii(item.label_)));
378 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
379 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
380 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
381 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
385 case ToolbarItem::ICONPALETTE: {
386 QToolButton * tb = new QToolButton(this);
387 tb->setToolTip(qt_(to_ascii(item.label_)));
388 tb->setStatusTip(qt_(to_ascii(item.label_)));
389 tb->setText(qt_(to_ascii(item.label_)));
390 connect(this, SIGNAL(iconSizeChanged(QSize)),
391 tb, SLOT(setIconSize(QSize)));
392 IconPalette * panel = new IconPalette(tb);
393 panel->setWindowTitle(qt_(to_ascii(item.label_)));
394 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
395 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
397 lyxerr << "Unknown toolbar " << item.name_ << endl;
400 ToolbarInfo::item_iterator it = tbinfo->items.begin();
401 ToolbarInfo::item_iterator const end = tbinfo->items.end();
402 for (; it != end; ++it)
403 if (!getStatus(it->func_).unknown()) {
404 panel->addButton(addItem(*it));
405 // use the icon of first action for the toolbar button
406 if (it == tbinfo->items.begin())
407 tb->setIcon(getIcon(it->func_, true));
409 tb->setCheckable(true);
410 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
411 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
415 case ToolbarItem::POPUPMENU: {
416 QToolButton * tb = new QToolButton;
417 tb->setPopupMode(QToolButton::InstantPopup);
418 tb->setToolTip(qt_(to_ascii(item.label_)));
419 tb->setStatusTip(qt_(to_ascii(item.label_)));
420 tb->setText(qt_(to_ascii(item.label_)));
421 tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
422 connect(this, SIGNAL(iconSizeChanged(QSize)),
423 tb, SLOT(setIconSize(QSize)));
425 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
426 m->setWindowTitle(qt_(to_ascii(item.label_)));
427 m->setTearOffEnabled(true);
428 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
429 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
431 lyxerr << "Unknown toolbar " << item.name_ << endl;
434 ToolbarInfo::item_iterator it = tbinfo->items.begin();
435 ToolbarInfo::item_iterator const end = tbinfo->items.end();
436 for (; it != end; ++it)
437 if (!getStatus(it->func_).unknown())
438 m->add(addItem(*it));
443 case ToolbarItem::COMMAND: {
444 if (!getStatus(item.func_).unknown())
445 addAction(addItem(item));
454 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
456 // if tbinfo.state == auto *do not* set on/off
457 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
458 if (GuiToolbar::isVisible())
459 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
461 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
464 // no need to save it here.
465 Qt::ToolBarArea loc = owner_.toolBarArea(this);
467 if (loc == Qt::TopToolBarArea)
468 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
469 else if (loc == Qt::BottomToolBarArea)
470 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
471 else if (loc == Qt::RightToolBarArea)
472 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
473 else if (loc == Qt::LeftToolBarArea)
474 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
476 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
478 // save toolbar position. They are not used to restore toolbar position
479 // now because move(x,y) does not work for toolbar.
480 tbinfo.posx = pos().x();
481 tbinfo.posy = pos().y();
485 void GuiToolbar::updateContents()
487 // update visible toolbars only
490 // This is a speed bottleneck because this is called on every keypress
491 // and update calls getStatus, which copies the cursor at least two times
492 for (int i = 0; i < actions_.size(); ++i)
493 actions_[i]->update();
500 } // namespace frontend
503 #include "GuiToolbar_moc.cpp"