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 extern void qInitResources();
49 static bool initialized = false;
64 using support::compare;
75 bool operator<(PngMap const & lhs, PngMap const & rhs)
77 return compare(lhs.key, rhs.key) < 0;
83 CompareKey(string const & name) : name_(name) {}
84 bool operator()(PngMap const & other) const { return other.key == name_; }
90 PngMap sorted_png_map[] = {
91 { "Bumpeq", "bumpeq2" },
94 { "Delta", "delta2" },
95 { "Downarrow", "downarrow2" },
96 { "Gamma", "gamma2" },
97 { "Lambda", "lambda2" },
98 { "Leftarrow", "leftarrow2" },
99 { "Leftrightarrow", "leftrightarrow2" },
100 { "Longleftarrow", "longleftarrow2" },
101 { "Longleftrightarrow", "longleftrightarrow2" },
102 { "Longrightarrow", "longrightarrow2" },
103 { "Omega", "omega2" },
107 { "Rightarrow", "rightarrow2" },
108 { "Sigma", "sigma2" },
109 { "Subset", "subset2" },
110 { "Supset", "supset2" },
111 { "Theta", "theta2" },
112 { "Uparrow", "uparrow2" },
113 { "Updownarrow", "updownarrow2" },
114 { "Upsilon", "upsilon2" },
115 { "Vdash", "vdash3" },
117 { "nLeftarrow", "nleftarrow2" },
118 { "nLeftrightarrow", "nleftrightarrow2" },
119 { "nRightarrow", "nrightarrow2" },
120 { "nVDash", "nvdash3" },
121 { "nvDash", "nvdash2" },
122 { "textrm \\AA", "textrm_AA"},
123 { "textrm \\O", "textrm_Oe"},
124 { "vDash", "vdash2" }
127 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
130 string const find_png(string const & name)
132 PngMap const * const begin = sorted_png_map;
133 PngMap const * const end = begin + nr_sorted_png_map;
134 BOOST_ASSERT(sorted(begin, end));
136 PngMap const * const it = std::find_if(begin, end, CompareKey(name));
140 png_name = it->value;
142 png_name = subst(name, "_", "underscore");
143 png_name = subst(png_name, ' ', '_');
145 // This way we can have "math-delim { }" on the toolbar.
146 png_name = subst(png_name, "(", "lparen");
147 png_name = subst(png_name, ")", "rparen");
148 png_name = subst(png_name, "[", "lbracket");
149 png_name = subst(png_name, "]", "rbracket");
150 png_name = subst(png_name, "{", "lbrace");
151 png_name = subst(png_name, "}", "rbrace");
152 png_name = subst(png_name, "|", "bars");
153 png_name = subst(png_name, ",", "thinspace");
154 png_name = subst(png_name, ":", "mediumspace");
155 png_name = subst(png_name, ";", "thickspace");
156 png_name = subst(png_name, "!", "negthinspace");
159 LYXERR(Debug::GUI) << "find_png(" << name << ")\n"
160 << "Looking for math PNG called \""
161 << png_name << '"' << std::endl;
168 /// return a icon for the given action
169 static QIcon getIcon(FuncRequest const & f, bool unknown)
171 initializeResources();
177 case LFUN_MATH_INSERT:
178 if (!f.argument().empty())
179 name1 = "math/" + find_png(to_utf8(f.argument()).substr(1));
181 case LFUN_MATH_DELIM:
182 case LFUN_MATH_BIGDELIM:
183 name1 = "math/" + find_png(to_utf8(f.argument()));
186 name2 = lyxaction.getActionName(f.action);
189 if (!f.argument().empty())
190 name1 = subst(name2 + ' ' + to_utf8(f.argument()), ' ', '_');
193 if (pm.load(":/images/" + toqstr(name1) + ".png"))
196 if (pm.load(":/images/" + toqstr(name2) + ".png"))
199 LYXERR(Debug::GUI) << "Cannot find icon for command \""
200 << lyxaction.getActionName(f.action)
201 << '(' << to_utf8(f.argument()) << ")\"" << endl;
203 pm.load(":/images/unknown.png");
209 static TextClass const & textClass(LyXView const & lv)
211 return lv.buffer()->params().getTextClass();
215 /////////////////////////////////////////////////////////////////////
219 /////////////////////////////////////////////////////////////////////
221 GuiLayoutBox::GuiLayoutBox(GuiViewBase & owner)
224 setSizeAdjustPolicy(QComboBox::AdjustToContents);
225 setFocusPolicy(Qt::ClickFocus);
226 setMinimumWidth(sizeHint().width());
227 setMaxVisibleItems(100);
229 QObject::connect(this, SIGNAL(activated(QString)),
230 this, SLOT(selected(QString)));
234 void GuiLayoutBox::set(docstring const & layout)
236 TextClass const & tc = textClass(owner_);
238 QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
241 for (; i < count(); ++i) {
242 if (name == itemText(i))
247 lyxerr << "Trying to select non existent layout type "
248 << fromqstr(name) << endl;
256 void GuiLayoutBox::updateContents()
258 TextClass const & tc = textClass(owner_);
260 setUpdatesEnabled(false);
263 TextClass::const_iterator it = tc.begin();
264 TextClass::const_iterator const end = tc.end();
265 for (; it != end; ++it) {
266 // ignore obsolete entries
267 addItem(toqstr(translateIfPossible((*it)->name())));
270 // needed to recalculate size hint
272 setMinimumWidth(sizeHint().width());
275 setUpdatesEnabled(true);
279 void GuiLayoutBox::selected(const QString & str)
282 TextClass const & tc = owner_.buffer()->params().getTextClass();
283 docstring const name = qstring_to_ucs4(str);
284 TextClass::const_iterator it = tc.begin();
285 TextClass::const_iterator const end = tc.end();
286 for (; it != end; ++it) {
287 docstring const & itname = (*it)->name();
288 if (translateIfPossible(itname) == name) {
289 FuncRequest const func(LFUN_LAYOUT, itname,
290 FuncRequest::TOOLBAR);
291 owner_.dispatch(func);
295 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
300 /////////////////////////////////////////////////////////////////////
304 /////////////////////////////////////////////////////////////////////
307 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiViewBase & owner)
308 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
309 layout_(0), command_buffer_(0)
311 // give visual separation between adjacent toolbars
314 // TODO: save toolbar position
317 ToolbarInfo::item_iterator it = tbinfo.items.begin();
318 ToolbarInfo::item_iterator end = tbinfo.items.end();
319 for (; it != end; ++it)
324 Action * GuiToolbar::addItem(ToolbarItem const & item)
326 Action * act = new Action(owner_,
327 getIcon(item.func_, false),
328 toqstr(item.label_), item.func_, toqstr(item.label_));
329 actions_.append(act);
334 void GuiToolbar::add(ToolbarItem const & item)
336 switch (item.type_) {
337 case ToolbarItem::SEPARATOR:
340 case ToolbarItem::LAYOUTS:
341 layout_ = new GuiLayoutBox(owner_);
344 case ToolbarItem::MINIBUFFER:
345 command_buffer_ = new GuiCommandBuffer(&owner_);
346 addWidget(command_buffer_);
347 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
348 //setHorizontalStretchable(true);
350 case ToolbarItem::TABLEINSERT: {
351 QToolButton * tb = new QToolButton;
352 tb->setCheckable(true);
353 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
354 tb->setToolTip(qt_(to_ascii(item.label_)));
355 tb->setStatusTip(qt_(to_ascii(item.label_)));
356 tb->setText(qt_(to_ascii(item.label_)));
357 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
358 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
359 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
360 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
364 case ToolbarItem::ICONPALETTE: {
365 QToolButton * tb = new QToolButton(this);
366 tb->setToolTip(qt_(to_ascii(item.label_)));
367 tb->setStatusTip(qt_(to_ascii(item.label_)));
368 tb->setText(qt_(to_ascii(item.label_)));
369 connect(this, SIGNAL(iconSizeChanged(QSize)),
370 tb, SLOT(setIconSize(QSize)));
371 IconPalette * panel = new IconPalette(tb);
372 panel->setWindowTitle(qt_(to_ascii(item.label_)));
373 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
374 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
376 lyxerr << "Unknown toolbar " << item.name_ << endl;
379 ToolbarInfo::item_iterator it = tbinfo->items.begin();
380 ToolbarInfo::item_iterator const end = tbinfo->items.end();
381 for (; it != end; ++it)
382 if (!getStatus(it->func_).unknown()) {
383 panel->addButton(addItem(*it));
384 // use the icon of first action for the toolbar button
385 if (it == tbinfo->items.begin())
386 tb->setIcon(getIcon(it->func_, true));
388 tb->setCheckable(true);
389 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
390 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
394 case ToolbarItem::POPUPMENU: {
395 QToolButton * tb = new QToolButton;
396 tb->setPopupMode(QToolButton::InstantPopup);
397 tb->setToolTip(qt_(to_ascii(item.label_)));
398 tb->setStatusTip(qt_(to_ascii(item.label_)));
399 tb->setText(qt_(to_ascii(item.label_)));
400 tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
401 connect(this, SIGNAL(iconSizeChanged(QSize)),
402 tb, SLOT(setIconSize(QSize)));
404 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
405 m->setWindowTitle(qt_(to_ascii(item.label_)));
406 m->setTearOffEnabled(true);
407 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
408 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
410 lyxerr << "Unknown toolbar " << item.name_ << endl;
413 ToolbarInfo::item_iterator it = tbinfo->items.begin();
414 ToolbarInfo::item_iterator const end = tbinfo->items.end();
415 for (; it != end; ++it)
416 if (!getStatus(it->func_).unknown())
417 m->add(addItem(*it));
422 case ToolbarItem::COMMAND: {
423 if (!getStatus(item.func_).unknown())
424 addAction(addItem(item));
433 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
435 // if tbinfo.state == auto *do not* set on/off
436 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
437 if (GuiToolbar::isVisible())
438 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
440 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
443 // no need to save it here.
444 Qt::ToolBarArea loc = owner_.toolBarArea(this);
446 if (loc == Qt::TopToolBarArea)
447 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
448 else if (loc == Qt::BottomToolBarArea)
449 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
450 else if (loc == Qt::RightToolBarArea)
451 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
452 else if (loc == Qt::LeftToolBarArea)
453 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
455 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
457 // save toolbar position. They are not used to restore toolbar position
458 // now because move(x,y) does not work for toolbar.
459 tbinfo.posx = pos().x();
460 tbinfo.posy = pos().y();
464 void GuiToolbar::updateContents()
466 // update visible toolbars only
469 // This is a speed bottleneck because this is called on every keypress
470 // and update calls getStatus, which copies the cursor at least two times
471 for (int i = 0; i < actions_.size(); ++i)
472 actions_[i]->update();
479 } // namespace frontend
482 #include "GuiToolbar_moc.cpp"