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
40 #include "controllers/ControlMath.h"
44 #include <QToolButton>
53 using support::FileName;
54 using support::libFileSearch;
56 using support::compare;
61 static TextClass const & textClass(LyXView const & lv)
63 return lv.buffer()->params().getTextClass();
67 /////////////////////////////////////////////////////////////////////
71 /////////////////////////////////////////////////////////////////////
73 GuiLayoutBox::GuiLayoutBox(GuiViewBase & owner)
76 setSizeAdjustPolicy(QComboBox::AdjustToContents);
77 setFocusPolicy(Qt::ClickFocus);
78 setMinimumWidth(sizeHint().width());
79 setMaxVisibleItems(100);
81 QObject::connect(this, SIGNAL(activated(QString)),
82 this, SLOT(selected(QString)));
86 void GuiLayoutBox::set(docstring const & layout)
88 TextClass const & tc = textClass(owner_);
90 QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
93 for (; i < count(); ++i) {
94 if (name == itemText(i))
99 lyxerr << "Trying to select non existent layout type "
100 << fromqstr(name) << endl;
108 void GuiLayoutBox::updateContents()
110 TextClass const & tc = textClass(owner_);
112 setUpdatesEnabled(false);
115 TextClass::const_iterator it = tc.begin();
116 TextClass::const_iterator const end = tc.end();
117 for (; it != end; ++it) {
118 // ignore obsolete entries
119 if ((*it)->obsoleted_by().empty())
120 addItem(toqstr(translateIfPossible((*it)->name())));
123 // needed to recalculate size hint
125 setMinimumWidth(sizeHint().width());
128 setUpdatesEnabled(true);
132 void GuiLayoutBox::selected(const QString & str)
135 TextClass const & tc = owner_.buffer()->params().getTextClass();
136 docstring const name = qstring_to_ucs4(str);
137 TextClass::const_iterator it = tc.begin();
138 TextClass::const_iterator const end = tc.end();
139 for (; it != end; ++it) {
140 docstring const & itname = (*it)->name();
141 if (translateIfPossible(itname) == name) {
142 FuncRequest const func(LFUN_LAYOUT, itname,
143 FuncRequest::TOOLBAR);
144 owner_.dispatch(func);
148 lyxerr << "ERROR (layoutSelected): layout not found!"
154 /////////////////////////////////////////////////////////////////////
158 /////////////////////////////////////////////////////////////////////
161 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiViewBase & owner)
162 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
163 layout_(0), command_buffer_(0)
165 // give visual separation between adjacent toolbars
168 // TODO: save toolbar position
171 ToolbarInfo::item_iterator it = tbinfo.items.begin();
172 ToolbarInfo::item_iterator end = tbinfo.items.end();
173 for (; it != end; ++it)
178 Action * GuiToolbar::addItem(ToolbarItem const & item)
180 Action * act = new Action(owner_,
181 getIcon(item.func_, false).c_str(),
182 toqstr(item.label_), item.func_, toqstr(item.label_));
183 actions_.append(act);
188 void GuiToolbar::add(ToolbarItem const & item)
190 switch (item.type_) {
191 case ToolbarItem::SEPARATOR:
194 case ToolbarItem::LAYOUTS:
195 layout_ = new GuiLayoutBox(owner_);
198 case ToolbarItem::MINIBUFFER:
199 command_buffer_ = new GuiCommandBuffer(&owner_);
200 addWidget(command_buffer_);
201 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
202 //setHorizontalStretchable(true);
204 case ToolbarItem::TABLEINSERT: {
205 QToolButton * tb = new QToolButton;
206 tb->setCheckable(true);
207 tb->setIcon(QPixmap(toqstr(getIcon(FuncRequest(LFUN_TABULAR_INSERT)))));
208 tb->setToolTip(qt_(to_ascii(item.label_)));
209 tb->setStatusTip(qt_(to_ascii(item.label_)));
210 tb->setText(qt_(to_ascii(item.label_)));
211 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
212 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
213 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
214 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
218 case ToolbarItem::ICONPALETTE: {
219 QToolButton * tb = new QToolButton(this);
220 tb->setToolTip(qt_(to_ascii(item.label_)));
221 tb->setStatusTip(qt_(to_ascii(item.label_)));
222 tb->setText(qt_(to_ascii(item.label_)));
223 connect(this, SIGNAL(iconSizeChanged(QSize)),
224 tb, SLOT(setIconSize(QSize)));
225 IconPalette * panel = new IconPalette(tb);
226 panel->setWindowTitle(qt_(to_ascii(item.label_)));
227 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
228 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
230 lyxerr << "Unknown toolbar " << item.name_ << endl;
233 ToolbarInfo::item_iterator it = tbinfo->items.begin();
234 ToolbarInfo::item_iterator const end = tbinfo->items.end();
235 for (; it != end; ++it)
236 if (!getStatus(it->func_).unknown()) {
237 panel->addButton(addItem(*it));
238 // use the icon of first action for the toolbar button
239 if (it == tbinfo->items.begin())
240 tb->setIcon(QPixmap(getIcon(it->func_).c_str()));
242 tb->setCheckable(true);
243 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
244 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
248 case ToolbarItem::POPUPMENU: {
249 QToolButton * tb = new QToolButton;
250 tb->setPopupMode(QToolButton::InstantPopup);
251 tb->setToolTip(qt_(to_ascii(item.label_)));
252 tb->setStatusTip(qt_(to_ascii(item.label_)));
253 tb->setText(qt_(to_ascii(item.label_)));
254 FileName icon_path = libFileSearch("images/math", item.name_, "png");
255 tb->setIcon(QIcon(toqstr(icon_path.absFilename())));
256 connect(this, SIGNAL(iconSizeChanged(QSize)),
257 tb, SLOT(setIconSize(QSize)));
259 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
260 m->setWindowTitle(qt_(to_ascii(item.label_)));
261 m->setTearOffEnabled(true);
262 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
263 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
265 lyxerr << "Unknown toolbar " << item.name_ << endl;
268 ToolbarInfo::item_iterator it = tbinfo->items.begin();
269 ToolbarInfo::item_iterator const end = tbinfo->items.end();
270 for (; it != end; ++it)
271 if (!getStatus(it->func_).unknown())
272 m->add(addItem(*it));
277 case ToolbarItem::COMMAND: {
278 if (!getStatus(item.func_).unknown())
279 addAction(addItem(item));
288 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
290 // if tbinfo.state == auto *do not* set on/off
291 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
292 if (GuiToolbar::isVisible())
293 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
295 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
298 // no need to save it here.
299 Qt::ToolBarArea loc = owner_.toolBarArea(this);
301 if (loc == Qt::TopToolBarArea)
302 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
303 else if (loc == Qt::BottomToolBarArea)
304 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
305 else if (loc == Qt::RightToolBarArea)
306 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
307 else if (loc == Qt::LeftToolBarArea)
308 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
310 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
312 // save toolbar position. They are not used to restore toolbar position
313 // now because move(x,y) does not work for toolbar.
314 tbinfo.posx = pos().x();
315 tbinfo.posy = pos().y();
319 void GuiToolbar::updateContents()
321 // update visible toolbars only
324 // This is a speed bottleneck because this is called on every keypress
325 // and update calls getStatus, which copies the cursor at least two times
326 for (int i = 0; i < actions_.size(); ++i)
327 actions_[i]->update();
342 bool operator<(PngMap const & lhs, PngMap const & rhs)
344 return compare(lhs.key, rhs.key) < 0;
350 CompareKey(string const & name) : name_(name) {}
351 bool operator()(PngMap const & other) const { return other.key == name_; }
357 PngMap sorted_png_map[] = {
358 { "Bumpeq", "bumpeq2" },
361 { "Delta", "delta2" },
362 { "Downarrow", "downarrow2" },
363 { "Gamma", "gamma2" },
364 { "Lambda", "lambda2" },
365 { "Leftarrow", "leftarrow2" },
366 { "Leftrightarrow", "leftrightarrow2" },
367 { "Longleftarrow", "longleftarrow2" },
368 { "Longleftrightarrow", "longleftrightarrow2" },
369 { "Longrightarrow", "longrightarrow2" },
370 { "Omega", "omega2" },
374 { "Rightarrow", "rightarrow2" },
375 { "Sigma", "sigma2" },
376 { "Subset", "subset2" },
377 { "Supset", "supset2" },
378 { "Theta", "theta2" },
379 { "Uparrow", "uparrow2" },
380 { "Updownarrow", "updownarrow2" },
381 { "Upsilon", "upsilon2" },
382 { "Vdash", "vdash3" },
384 { "nLeftarrow", "nleftarrow2" },
385 { "nLeftrightarrow", "nleftrightarrow2" },
386 { "nRightarrow", "nrightarrow2" },
387 { "nVDash", "nvdash3" },
388 { "nvDash", "nvdash2" },
389 { "textrm \\AA", "textrm_AA"},
390 { "textrm \\O", "textrm_Oe"},
391 { "vDash", "vdash2" }
394 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
397 string const find_png(string const & name)
399 PngMap const * const begin = sorted_png_map;
400 PngMap const * const end = begin + nr_sorted_png_map;
401 BOOST_ASSERT(sorted(begin, end));
403 PngMap const * const it = std::find_if(begin, end, CompareKey(name));
407 png_name = it->value;
409 png_name = subst(name, "_", "underscore");
410 png_name = subst(png_name, ' ', '_');
412 // This way we can have "math-delim { }" on the toolbar.
413 png_name = subst(png_name, "(", "lparen");
414 png_name = subst(png_name, ")", "rparen");
415 png_name = subst(png_name, "[", "lbracket");
416 png_name = subst(png_name, "]", "rbracket");
417 png_name = subst(png_name, "{", "lbrace");
418 png_name = subst(png_name, "}", "rbrace");
419 png_name = subst(png_name, "|", "bars");
420 png_name = subst(png_name, ",", "thinspace");
421 png_name = subst(png_name, ":", "mediumspace");
422 png_name = subst(png_name, ";", "thickspace");
423 png_name = subst(png_name, "!", "negthinspace");
426 LYXERR(Debug::GUI) << "find_png(" << name << ")\n"
427 << "Looking for math PNG called \""
428 << png_name << '"' << std::endl;
430 return libFileSearch("images/math/", png_name, "png").absFilename();
436 string const getIcon(FuncRequest const & f, bool unknown)
441 case LFUN_MATH_INSERT:
442 if (!f.argument().empty())
443 fullname = find_png(to_utf8(f.argument()).substr(1));
445 case LFUN_MATH_DELIM:
446 case LFUN_MATH_BIGDELIM:
447 fullname = find_png(to_utf8(f.argument()));
450 string const name = lyxaction.getActionName(f.action);
451 string png_name = name;
453 if (!f.argument().empty())
454 png_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
456 fullname = libFileSearch("images", png_name, "png").absFilename();
458 if (fullname.empty()) {
459 // try without the argument
460 fullname = libFileSearch("images", name, "png").absFilename();
464 if (!fullname.empty()) {
465 LYXERR(Debug::GUI) << "Full icon name is `"
466 << fullname << '\'' << endl;
470 LYXERR(Debug::GUI) << "Cannot find icon for command \""
471 << lyxaction.getActionName(f.action)
472 << '(' << to_utf8(f.argument()) << ")\"" << endl;
474 return libFileSearch("images", "unknown", "png").absFilename();
480 } // namespace frontend
483 #include "GuiToolbar_moc.cpp"