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/lstrings.h"
37 #include "support/lyxalgo.h" // sorted
41 #include <QToolButton>
46 static void initializeResources()
48 static bool initialized = false;
50 Q_INIT_RESOURCE(Resources);
63 using support::compare;
74 bool operator<(PngMap const & lhs, PngMap const & rhs)
76 return compare(lhs.key, rhs.key) < 0;
82 CompareKey(string const & name) : name_(name) {}
83 bool operator()(PngMap const & other) const { return other.key == name_; }
89 PngMap sorted_png_map[] = {
90 { "Bumpeq", "bumpeq2" },
93 { "Delta", "delta2" },
94 { "Downarrow", "downarrow2" },
95 { "Gamma", "gamma2" },
96 { "Lambda", "lambda2" },
97 { "Leftarrow", "leftarrow2" },
98 { "Leftrightarrow", "leftrightarrow2" },
99 { "Longleftarrow", "longleftarrow2" },
100 { "Longleftrightarrow", "longleftrightarrow2" },
101 { "Longrightarrow", "longrightarrow2" },
102 { "Omega", "omega2" },
106 { "Rightarrow", "rightarrow2" },
107 { "Sigma", "sigma2" },
108 { "Subset", "subset2" },
109 { "Supset", "supset2" },
110 { "Theta", "theta2" },
111 { "Uparrow", "uparrow2" },
112 { "Updownarrow", "updownarrow2" },
113 { "Upsilon", "upsilon2" },
114 { "Vdash", "vdash3" },
116 { "nLeftarrow", "nleftarrow2" },
117 { "nLeftrightarrow", "nleftrightarrow2" },
118 { "nRightarrow", "nrightarrow2" },
119 { "nVDash", "nvdash3" },
120 { "nvDash", "nvdash2" },
121 { "textrm \\AA", "textrm_AA"},
122 { "textrm \\O", "textrm_Oe"},
123 { "vDash", "vdash2" }
126 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
129 string const find_png(string const & name)
131 PngMap const * const begin = sorted_png_map;
132 PngMap const * const end = begin + nr_sorted_png_map;
133 BOOST_ASSERT(sorted(begin, end));
135 PngMap const * const it = std::find_if(begin, end, CompareKey(name));
139 png_name = it->value;
141 png_name = subst(name, "_", "underscore");
142 png_name = subst(png_name, ' ', '_');
144 // This way we can have "math-delim { }" on the toolbar.
145 png_name = subst(png_name, "(", "lparen");
146 png_name = subst(png_name, ")", "rparen");
147 png_name = subst(png_name, "[", "lbracket");
148 png_name = subst(png_name, "]", "rbracket");
149 png_name = subst(png_name, "{", "lbrace");
150 png_name = subst(png_name, "}", "rbrace");
151 png_name = subst(png_name, "|", "bars");
152 png_name = subst(png_name, ",", "thinspace");
153 png_name = subst(png_name, ":", "mediumspace");
154 png_name = subst(png_name, ";", "thickspace");
155 png_name = subst(png_name, "!", "negthinspace");
158 LYXERR(Debug::GUI) << "find_png(" << name << ")\n"
159 << "Looking for math PNG called \""
160 << png_name << '"' << std::endl;
167 /// return a icon for the given action
168 static QIcon getIcon(FuncRequest const & f, bool unknown)
170 initializeResources();
176 case LFUN_MATH_INSERT:
177 if (!f.argument().empty())
178 name1 = "math/" + find_png(to_utf8(f.argument()).substr(1));
180 case LFUN_MATH_DELIM:
181 case LFUN_MATH_BIGDELIM:
182 name1 = "math/" + find_png(to_utf8(f.argument()));
185 name2 = lyxaction.getActionName(f.action);
188 if (!f.argument().empty())
189 name1 = subst(name2 + ' ' + to_utf8(f.argument()), ' ', '_');
192 if (pm.load(":/images/" + toqstr(name1) + ".png"))
195 if (pm.load(":/images/" + toqstr(name2) + ".png"))
198 LYXERR(Debug::GUI) << "Cannot find icon for command \""
199 << lyxaction.getActionName(f.action)
200 << '(' << to_utf8(f.argument()) << ")\"" << endl;
202 pm.load(":/images/unknown.png");
208 static TextClass const & textClass(LyXView const & lv)
210 return lv.buffer()->params().getTextClass();
214 /////////////////////////////////////////////////////////////////////
218 /////////////////////////////////////////////////////////////////////
220 GuiLayoutBox::GuiLayoutBox(GuiViewBase & owner)
223 setSizeAdjustPolicy(QComboBox::AdjustToContents);
224 setFocusPolicy(Qt::ClickFocus);
225 setMinimumWidth(sizeHint().width());
226 setMaxVisibleItems(100);
228 QObject::connect(this, SIGNAL(activated(QString)),
229 this, SLOT(selected(QString)));
233 void GuiLayoutBox::set(docstring const & layout)
235 TextClass const & tc = textClass(owner_);
237 QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
240 for (; i < count(); ++i) {
241 if (name == itemText(i))
246 lyxerr << "Trying to select non existent layout type "
247 << fromqstr(name) << endl;
255 void GuiLayoutBox::updateContents()
257 TextClass const & tc = textClass(owner_);
259 setUpdatesEnabled(false);
262 TextClass::const_iterator it = tc.begin();
263 TextClass::const_iterator const end = tc.end();
264 for (; it != end; ++it) {
265 // ignore obsolete entries
266 addItem(toqstr(translateIfPossible((*it)->name())));
269 // needed to recalculate size hint
271 setMinimumWidth(sizeHint().width());
274 setUpdatesEnabled(true);
278 void GuiLayoutBox::selected(const QString & str)
281 TextClass const & tc = owner_.buffer()->params().getTextClass();
282 docstring const name = qstring_to_ucs4(str);
283 TextClass::const_iterator it = tc.begin();
284 TextClass::const_iterator const end = tc.end();
285 for (; it != end; ++it) {
286 docstring const & itname = (*it)->name();
287 if (translateIfPossible(itname) == name) {
288 FuncRequest const func(LFUN_LAYOUT, itname,
289 FuncRequest::TOOLBAR);
290 owner_.dispatch(func);
294 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
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 tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
400 connect(this, SIGNAL(iconSizeChanged(QSize)),
401 tb, SLOT(setIconSize(QSize)));
403 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
404 m->setWindowTitle(qt_(to_ascii(item.label_)));
405 m->setTearOffEnabled(true);
406 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
407 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
409 lyxerr << "Unknown toolbar " << item.name_ << endl;
412 ToolbarInfo::item_iterator it = tbinfo->items.begin();
413 ToolbarInfo::item_iterator const end = tbinfo->items.end();
414 for (; it != end; ++it)
415 if (!getStatus(it->func_).unknown())
416 m->add(addItem(*it));
421 case ToolbarItem::COMMAND: {
422 if (!getStatus(item.func_).unknown())
423 addAction(addItem(item));
432 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
434 // if tbinfo.state == auto *do not* set on/off
435 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
436 if (GuiToolbar::isVisible())
437 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
439 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
442 // no need to save it here.
443 Qt::ToolBarArea loc = owner_.toolBarArea(this);
445 if (loc == Qt::TopToolBarArea)
446 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
447 else if (loc == Qt::BottomToolBarArea)
448 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
449 else if (loc == Qt::RightToolBarArea)
450 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
451 else if (loc == Qt::LeftToolBarArea)
452 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
454 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
456 // save toolbar position. They are not used to restore toolbar position
457 // now because move(x,y) does not work for toolbar.
458 tbinfo.posx = pos().x();
459 tbinfo.posy = pos().y();
463 void GuiToolbar::updateContents()
465 // update visible toolbars only
468 // This is a speed bottleneck because this is called on every keypress
469 // and update calls getStatus, which copies the cursor at least two times
470 for (int i = 0; i < actions_.size(); ++i)
471 actions_[i]->update();
478 } // namespace frontend
481 #include "GuiToolbar_moc.cpp"