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"
19 #include "BufferView.h"
21 #include "support/debug.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "support/gettext.h"
25 #include "IconPalette.h"
28 #include "Paragraph.h"
29 #include "TextClass.h"
30 #include "ToolbarBackend.h"
33 #include "GuiCommandBuffer.h"
34 #include "GuiToolbar.h"
35 #include "LyXAction.h"
37 #include "qt_helpers.h"
38 #include "InsertTableWidget.h"
41 #include "support/filetools.h"
42 #include "support/lstrings.h"
43 #include "support/lyxalgo.h" // sorted
47 #include <QToolButton>
51 #include <boost/assert.hpp>
55 static void initializeResources()
57 static bool initialized = false;
59 Q_INIT_RESOURCE(Resources);
68 using support::libFileSearch;
70 using support::compare;
81 bool operator<(PngMap const & lhs, PngMap const & rhs)
83 return compare(lhs.key, rhs.key) < 0;
89 CompareKey(string const & name) : name_(name) {}
90 bool operator()(PngMap const & other) const { return other.key == name_; }
96 PngMap sorted_png_map[] = {
97 { "Bumpeq", "bumpeq2" },
100 { "Delta", "delta2" },
101 { "Downarrow", "downarrow2" },
102 { "Gamma", "gamma2" },
103 { "Lambda", "lambda2" },
104 { "Leftarrow", "leftarrow2" },
105 { "Leftrightarrow", "leftrightarrow2" },
106 { "Longleftarrow", "longleftarrow2" },
107 { "Longleftrightarrow", "longleftrightarrow2" },
108 { "Longrightarrow", "longrightarrow2" },
109 { "Omega", "omega2" },
113 { "Rightarrow", "rightarrow2" },
114 { "Sigma", "sigma2" },
115 { "Subset", "subset2" },
116 { "Supset", "supset2" },
117 { "Theta", "theta2" },
118 { "Uparrow", "uparrow2" },
119 { "Updownarrow", "updownarrow2" },
120 { "Upsilon", "upsilon2" },
121 { "Vdash", "vdash3" },
123 { "nLeftarrow", "nleftarrow2" },
124 { "nLeftrightarrow", "nleftrightarrow2" },
125 { "nRightarrow", "nrightarrow2" },
126 { "nVDash", "nvdash3" },
127 { "nvDash", "nvdash2" },
128 { "textrm \\AA", "textrm_AA"},
129 { "textrm \\O", "textrm_Oe"},
130 { "vDash", "vdash2" }
133 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
136 string const find_png(string const & name)
138 PngMap const * const begin = sorted_png_map;
139 PngMap const * const end = begin + nr_sorted_png_map;
140 BOOST_ASSERT(sorted(begin, end));
142 PngMap const * const it = std::find_if(begin, end, CompareKey(name));
146 png_name = it->value;
148 png_name = subst(name, "_", "underscore");
149 png_name = subst(png_name, ' ', '_');
151 // This way we can have "math-delim { }" on the toolbar.
152 png_name = subst(png_name, "(", "lparen");
153 png_name = subst(png_name, ")", "rparen");
154 png_name = subst(png_name, "[", "lbracket");
155 png_name = subst(png_name, "]", "rbracket");
156 png_name = subst(png_name, "{", "lbrace");
157 png_name = subst(png_name, "}", "rbrace");
158 png_name = subst(png_name, "|", "bars");
159 png_name = subst(png_name, ",", "thinspace");
160 png_name = subst(png_name, ":", "mediumspace");
161 png_name = subst(png_name, ";", "thickspace");
162 png_name = subst(png_name, "!", "negthinspace");
165 LYXERR(Debug::GUI, "find_png(" << name << ")\n"
166 << "Looking for math PNG called \"" << png_name << '"');
173 /// return a icon for the given action
174 static QIcon getIcon(FuncRequest const & f, bool unknown)
176 initializeResources();
184 case LFUN_MATH_INSERT:
185 if (!f.argument().empty()) {
187 name1 = find_png(to_utf8(f.argument()).substr(1));
190 case LFUN_MATH_DELIM:
191 case LFUN_MATH_BIGDELIM:
193 name1 = find_png(to_utf8(f.argument()));
197 name1 = to_utf8(f.argument());
200 name2 = lyxaction.getActionName(f.action);
203 if (!f.argument().empty())
204 name1 = subst(name2 + ' ' + to_utf8(f.argument()), ' ', '_');
207 fullname = libFileSearch("images/" + path, name1, "png").absFilename();
208 if (pm.load(toqstr(fullname)))
211 fullname = libFileSearch("images/" + path, name2, "png").absFilename();
212 if (pm.load(toqstr(fullname)))
215 if (pm.load(":/images/" + toqstr(path + name1) + ".png"))
218 if (pm.load(":/images/" + toqstr(path + name2) + ".png"))
221 LYXERR(Debug::GUI, "Cannot find icon for command \""
222 << lyxaction.getActionName(f.action)
223 << '(' << to_utf8(f.argument()) << ")\"");
225 pm.load(":/images/unknown.png");
231 /////////////////////////////////////////////////////////////////////
235 /////////////////////////////////////////////////////////////////////
237 GuiLayoutBox::GuiLayoutBox(GuiView & owner)
240 setSizeAdjustPolicy(QComboBox::AdjustToContents);
241 setFocusPolicy(Qt::ClickFocus);
242 setMinimumWidth(sizeHint().width());
243 setMaxVisibleItems(100);
245 QObject::connect(this, SIGNAL(activated(QString)),
246 this, SLOT(selected(QString)));
247 owner_.setLayoutDialog(this);
248 updateContents(true);
252 void GuiLayoutBox::set(docstring const & layout)
257 QString const & name = toqstr(translateIfPossible(
258 (*text_class_)[layout]->name()));
260 if (name == currentText())
263 int i = findText(name);
265 lyxerr << "Trying to select non existent layout type "
266 << fromqstr(name) << endl;
274 void GuiLayoutBox::addItemSort(QString const & item, bool sorted)
276 int const end = count();
277 if (!sorted || end < 2 || item[0].category() != QChar::Letter_Uppercase) {
282 // Let the default one be at the beginning
284 for (setCurrentIndex(i); currentText() < item;) {
285 // e.g. --Separator--
286 if (currentText()[0].category() != QChar::Letter_Uppercase)
297 void GuiLayoutBox::updateContents(bool reset)
299 Buffer const * buffer = owner_.buffer();
308 TextClass const * text_class = &buffer->params().getTextClass();
309 if (!reset && text_class_ == text_class) {
310 set(owner_.view()->cursor().innerParagraph().layout()->name());
314 text_class_ = text_class;
316 setUpdatesEnabled(false);
319 TextClass::const_iterator it = text_class_->begin();
320 TextClass::const_iterator const end = text_class_->end();
321 for (; it != end; ++it) {
322 // ignore obsolete entries
323 addItemSort(toqstr(translateIfPossible((*it)->name())), lyxrc.sort_layouts);
328 // needed to recalculate size hint
330 setMinimumWidth(sizeHint().width());
331 set(owner_.view()->cursor().innerParagraph().layout()->name());
334 setUpdatesEnabled(true);
338 void GuiLayoutBox::selected(const QString & str)
341 updateContents(false);
345 docstring const name = qstring_to_ucs4(str);
346 TextClass::const_iterator it = text_class_->begin();
347 TextClass::const_iterator const end = text_class_->end();
348 for (; it != end; ++it) {
349 docstring const & itname = (*it)->name();
350 if (translateIfPossible(itname) == name) {
351 FuncRequest const func(LFUN_LAYOUT, itname,
352 FuncRequest::TOOLBAR);
353 theLyXFunc().setLyXView(&owner_);
358 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
363 /////////////////////////////////////////////////////////////////////
367 /////////////////////////////////////////////////////////////////////
370 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
371 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
372 layout_(0), command_buffer_(0)
374 // give visual separation between adjacent toolbars
377 // TODO: save toolbar position
380 ToolbarInfo::item_iterator it = tbinfo.items.begin();
381 ToolbarInfo::item_iterator end = tbinfo.items.end();
382 for (; it != end; ++it)
387 Action * GuiToolbar::addItem(ToolbarItem const & item)
389 Action * act = new Action(owner_,
390 getIcon(item.func_, false),
391 toqstr(item.label_), item.func_, toqstr(item.label_));
392 actions_.append(act);
397 void GuiToolbar::add(ToolbarItem const & item)
399 switch (item.type_) {
400 case ToolbarItem::SEPARATOR:
403 case ToolbarItem::LAYOUTS:
404 layout_ = new GuiLayoutBox(owner_);
407 case ToolbarItem::MINIBUFFER:
408 command_buffer_ = new GuiCommandBuffer(&owner_);
409 addWidget(command_buffer_);
410 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
411 //setHorizontalStretchable(true);
413 case ToolbarItem::TABLEINSERT: {
414 QToolButton * tb = new QToolButton;
415 tb->setCheckable(true);
416 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
417 tb->setToolTip(qt_(to_ascii(item.label_)));
418 tb->setStatusTip(qt_(to_ascii(item.label_)));
419 tb->setText(qt_(to_ascii(item.label_)));
420 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
421 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
422 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
423 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
427 case ToolbarItem::ICONPALETTE: {
428 QToolButton * tb = new QToolButton(this);
429 tb->setToolTip(qt_(to_ascii(item.label_)));
430 tb->setStatusTip(qt_(to_ascii(item.label_)));
431 tb->setText(qt_(to_ascii(item.label_)));
432 connect(this, SIGNAL(iconSizeChanged(QSize)),
433 tb, SLOT(setIconSize(QSize)));
434 IconPalette * panel = new IconPalette(tb);
435 panel->setWindowTitle(qt_(to_ascii(item.label_)));
436 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
437 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
439 lyxerr << "Unknown toolbar " << item.name_ << endl;
442 ToolbarInfo::item_iterator it = tbinfo->items.begin();
443 ToolbarInfo::item_iterator const end = tbinfo->items.end();
444 for (; it != end; ++it)
445 if (!getStatus(it->func_).unknown()) {
446 panel->addButton(addItem(*it));
447 // use the icon of first action for the toolbar button
448 if (it == tbinfo->items.begin())
449 tb->setIcon(getIcon(it->func_, true));
451 tb->setCheckable(true);
452 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
453 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
457 case ToolbarItem::POPUPMENU: {
458 QToolButton * tb = new QToolButton;
459 tb->setPopupMode(QToolButton::InstantPopup);
460 tb->setToolTip(qt_(to_ascii(item.label_)));
461 tb->setStatusTip(qt_(to_ascii(item.label_)));
462 tb->setText(qt_(to_ascii(item.label_)));
463 tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
464 connect(this, SIGNAL(iconSizeChanged(QSize)),
465 tb, SLOT(setIconSize(QSize)));
467 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
468 m->setWindowTitle(qt_(to_ascii(item.label_)));
469 m->setTearOffEnabled(true);
470 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
471 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
473 lyxerr << "Unknown toolbar " << item.name_ << endl;
476 ToolbarInfo::item_iterator it = tbinfo->items.begin();
477 ToolbarInfo::item_iterator const end = tbinfo->items.end();
478 for (; it != end; ++it)
479 if (!getStatus(it->func_).unknown())
480 m->add(addItem(*it));
485 case ToolbarItem::COMMAND: {
486 if (!getStatus(item.func_).unknown())
487 addAction(addItem(item));
496 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
498 // if tbinfo.state == auto *do not* set on/off
499 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
500 if (GuiToolbar::isVisible())
501 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
503 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
506 // no need to save it here.
507 Qt::ToolBarArea loc = owner_.toolBarArea(this);
509 if (loc == Qt::TopToolBarArea)
510 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
511 else if (loc == Qt::BottomToolBarArea)
512 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
513 else if (loc == Qt::RightToolBarArea)
514 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
515 else if (loc == Qt::LeftToolBarArea)
516 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
518 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
520 // save toolbar position. They are not used to restore toolbar position
521 // now because move(x,y) does not work for toolbar.
522 tbinfo.posx = pos().x();
523 tbinfo.posy = pos().y();
527 void GuiToolbar::updateContents()
529 // update visible toolbars only
532 // This is a speed bottleneck because this is called on every keypress
533 // and update calls getStatus, which copies the cursor at least two times
534 for (int i = 0; i < actions_.size(); ++i)
535 actions_[i]->update();
538 layout_->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
545 } // namespace frontend
548 #include "GuiToolbar_moc.cpp"