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>
47 static void initializeResources()
49 static bool initialized = false;
51 Q_INIT_RESOURCE(Resources);
63 using support::libFileSearch;
65 using support::compare;
76 bool operator<(PngMap const & lhs, PngMap const & rhs)
78 return compare(lhs.key, rhs.key) < 0;
84 CompareKey(string const & name) : name_(name) {}
85 bool operator()(PngMap const & other) const { return other.key == name_; }
91 PngMap sorted_png_map[] = {
92 { "Bumpeq", "bumpeq2" },
95 { "Delta", "delta2" },
96 { "Downarrow", "downarrow2" },
97 { "Gamma", "gamma2" },
98 { "Lambda", "lambda2" },
99 { "Leftarrow", "leftarrow2" },
100 { "Leftrightarrow", "leftrightarrow2" },
101 { "Longleftarrow", "longleftarrow2" },
102 { "Longleftrightarrow", "longleftrightarrow2" },
103 { "Longrightarrow", "longrightarrow2" },
104 { "Omega", "omega2" },
108 { "Rightarrow", "rightarrow2" },
109 { "Sigma", "sigma2" },
110 { "Subset", "subset2" },
111 { "Supset", "supset2" },
112 { "Theta", "theta2" },
113 { "Uparrow", "uparrow2" },
114 { "Updownarrow", "updownarrow2" },
115 { "Upsilon", "upsilon2" },
116 { "Vdash", "vdash3" },
118 { "nLeftarrow", "nleftarrow2" },
119 { "nLeftrightarrow", "nleftrightarrow2" },
120 { "nRightarrow", "nrightarrow2" },
121 { "nVDash", "nvdash3" },
122 { "nvDash", "nvdash2" },
123 { "textrm \\AA", "textrm_AA"},
124 { "textrm \\O", "textrm_Oe"},
125 { "vDash", "vdash2" }
128 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
131 string const find_png(string const & name)
133 PngMap const * const begin = sorted_png_map;
134 PngMap const * const end = begin + nr_sorted_png_map;
135 BOOST_ASSERT(sorted(begin, end));
137 PngMap const * const it = std::find_if(begin, end, CompareKey(name));
141 png_name = it->value;
143 png_name = subst(name, "_", "underscore");
144 png_name = subst(png_name, ' ', '_');
146 // This way we can have "math-delim { }" on the toolbar.
147 png_name = subst(png_name, "(", "lparen");
148 png_name = subst(png_name, ")", "rparen");
149 png_name = subst(png_name, "[", "lbracket");
150 png_name = subst(png_name, "]", "rbracket");
151 png_name = subst(png_name, "{", "lbrace");
152 png_name = subst(png_name, "}", "rbrace");
153 png_name = subst(png_name, "|", "bars");
154 png_name = subst(png_name, ",", "thinspace");
155 png_name = subst(png_name, ":", "mediumspace");
156 png_name = subst(png_name, ";", "thickspace");
157 png_name = subst(png_name, "!", "negthinspace");
160 LYXERR(Debug::GUI) << "find_png(" << name << ")\n"
161 << "Looking for math PNG called \""
162 << png_name << '"' << std::endl;
169 /// return a icon for the given action
170 static QIcon getIcon(FuncRequest const & f, bool unknown)
172 initializeResources();
180 case LFUN_MATH_INSERT:
181 if (!f.argument().empty()) {
183 name1 = find_png(to_utf8(f.argument()).substr(1));
186 case LFUN_MATH_DELIM:
187 case LFUN_MATH_BIGDELIM:
189 name1 = find_png(to_utf8(f.argument()));
193 name1 = to_utf8(f.argument());
196 name2 = lyxaction.getActionName(f.action);
199 if (!f.argument().empty())
200 name1 = subst(name2 + ' ' + to_utf8(f.argument()), ' ', '_');
203 fullname = libFileSearch("images/" + path, name1, "png").absFilename();
204 if (pm.load(toqstr(fullname)))
207 fullname = libFileSearch("images/" + path, name2, "png").absFilename();
208 if (pm.load(toqstr(fullname)))
211 if (pm.load(":/images/" + toqstr(path + name1) + ".png"))
214 if (pm.load(":/images/" + toqstr(path + name2) + ".png"))
217 LYXERR(Debug::GUI) << "Cannot find icon for command \""
218 << lyxaction.getActionName(f.action)
219 << '(' << to_utf8(f.argument()) << ")\"" << endl;
221 pm.load(":/images/unknown.png");
227 static TextClass const & textClass(LyXView const & lv)
229 return lv.buffer()->params().getTextClass();
233 /////////////////////////////////////////////////////////////////////
237 /////////////////////////////////////////////////////////////////////
239 GuiLayoutBox::GuiLayoutBox(GuiViewBase & owner)
242 setSizeAdjustPolicy(QComboBox::AdjustToContents);
243 setFocusPolicy(Qt::ClickFocus);
244 setMinimumWidth(sizeHint().width());
245 setMaxVisibleItems(100);
247 QObject::connect(this, SIGNAL(activated(QString)),
248 this, SLOT(selected(QString)));
252 void GuiLayoutBox::set(docstring const & layout)
254 TextClass const & tc = textClass(owner_);
256 QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
259 for (; i < count(); ++i) {
260 if (name == itemText(i))
265 lyxerr << "Trying to select non existent layout type "
266 << fromqstr(name) << endl;
274 void GuiLayoutBox::updateContents()
276 TextClass const & tc = textClass(owner_);
278 setUpdatesEnabled(false);
281 TextClass::const_iterator it = tc.begin();
282 TextClass::const_iterator const end = tc.end();
283 for (; it != end; ++it) {
284 // ignore obsolete entries
285 addItem(toqstr(translateIfPossible((*it)->name())));
288 // needed to recalculate size hint
290 setMinimumWidth(sizeHint().width());
293 setUpdatesEnabled(true);
297 void GuiLayoutBox::selected(const QString & str)
300 TextClass const & tc = owner_.buffer()->params().getTextClass();
301 docstring const name = qstring_to_ucs4(str);
302 TextClass::const_iterator it = tc.begin();
303 TextClass::const_iterator const end = tc.end();
304 for (; it != end; ++it) {
305 docstring const & itname = (*it)->name();
306 if (translateIfPossible(itname) == name) {
307 FuncRequest const func(LFUN_LAYOUT, itname,
308 FuncRequest::TOOLBAR);
309 owner_.dispatch(func);
313 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
318 /////////////////////////////////////////////////////////////////////
322 /////////////////////////////////////////////////////////////////////
325 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiViewBase & owner)
326 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
327 layout_(0), command_buffer_(0)
329 // give visual separation between adjacent toolbars
332 // TODO: save toolbar position
335 ToolbarInfo::item_iterator it = tbinfo.items.begin();
336 ToolbarInfo::item_iterator end = tbinfo.items.end();
337 for (; it != end; ++it)
342 Action * GuiToolbar::addItem(ToolbarItem const & item)
344 Action * act = new Action(owner_,
345 getIcon(item.func_, false),
346 toqstr(item.label_), item.func_, toqstr(item.label_));
347 actions_.append(act);
352 void GuiToolbar::add(ToolbarItem const & item)
354 switch (item.type_) {
355 case ToolbarItem::SEPARATOR:
358 case ToolbarItem::LAYOUTS:
359 layout_ = new GuiLayoutBox(owner_);
362 case ToolbarItem::MINIBUFFER:
363 command_buffer_ = new GuiCommandBuffer(&owner_);
364 addWidget(command_buffer_);
365 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
366 //setHorizontalStretchable(true);
368 case ToolbarItem::TABLEINSERT: {
369 QToolButton * tb = new QToolButton;
370 tb->setCheckable(true);
371 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
372 tb->setToolTip(qt_(to_ascii(item.label_)));
373 tb->setStatusTip(qt_(to_ascii(item.label_)));
374 tb->setText(qt_(to_ascii(item.label_)));
375 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
376 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
377 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
378 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
382 case ToolbarItem::ICONPALETTE: {
383 QToolButton * tb = new QToolButton(this);
384 tb->setToolTip(qt_(to_ascii(item.label_)));
385 tb->setStatusTip(qt_(to_ascii(item.label_)));
386 tb->setText(qt_(to_ascii(item.label_)));
387 connect(this, SIGNAL(iconSizeChanged(QSize)),
388 tb, SLOT(setIconSize(QSize)));
389 IconPalette * panel = new IconPalette(tb);
390 panel->setWindowTitle(qt_(to_ascii(item.label_)));
391 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
392 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
394 lyxerr << "Unknown toolbar " << item.name_ << endl;
397 ToolbarInfo::item_iterator it = tbinfo->items.begin();
398 ToolbarInfo::item_iterator const end = tbinfo->items.end();
399 for (; it != end; ++it)
400 if (!getStatus(it->func_).unknown()) {
401 panel->addButton(addItem(*it));
402 // use the icon of first action for the toolbar button
403 if (it == tbinfo->items.begin())
404 tb->setIcon(getIcon(it->func_, true));
406 tb->setCheckable(true);
407 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
408 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
412 case ToolbarItem::POPUPMENU: {
413 QToolButton * tb = new QToolButton;
414 tb->setPopupMode(QToolButton::InstantPopup);
415 tb->setToolTip(qt_(to_ascii(item.label_)));
416 tb->setStatusTip(qt_(to_ascii(item.label_)));
417 tb->setText(qt_(to_ascii(item.label_)));
418 tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
419 connect(this, SIGNAL(iconSizeChanged(QSize)),
420 tb, SLOT(setIconSize(QSize)));
422 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
423 m->setWindowTitle(qt_(to_ascii(item.label_)));
424 m->setTearOffEnabled(true);
425 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
426 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
428 lyxerr << "Unknown toolbar " << item.name_ << endl;
431 ToolbarInfo::item_iterator it = tbinfo->items.begin();
432 ToolbarInfo::item_iterator const end = tbinfo->items.end();
433 for (; it != end; ++it)
434 if (!getStatus(it->func_).unknown())
435 m->add(addItem(*it));
440 case ToolbarItem::COMMAND: {
441 if (!getStatus(item.func_).unknown())
442 addAction(addItem(item));
451 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
453 // if tbinfo.state == auto *do not* set on/off
454 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
455 if (GuiToolbar::isVisible())
456 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
458 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
461 // no need to save it here.
462 Qt::ToolBarArea loc = owner_.toolBarArea(this);
464 if (loc == Qt::TopToolBarArea)
465 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
466 else if (loc == Qt::BottomToolBarArea)
467 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
468 else if (loc == Qt::RightToolBarArea)
469 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
470 else if (loc == Qt::LeftToolBarArea)
471 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
473 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
475 // save toolbar position. They are not used to restore toolbar position
476 // now because move(x,y) does not work for toolbar.
477 tbinfo.posx = pos().x();
478 tbinfo.posy = pos().y();
482 void GuiToolbar::updateContents()
484 // update visible toolbars only
487 // This is a speed bottleneck because this is called on every keypress
488 // and update calls getStatus, which copies the cursor at least two times
489 for (int i = 0; i < actions_.size(); ++i)
490 actions_[i]->update();
497 } // namespace frontend
500 #include "GuiToolbar_moc.cpp"