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"
22 #include "FuncRequest.h"
23 #include "FuncStatus.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>
54 static void initializeResources()
56 static bool initialized = false;
58 Q_INIT_RESOURCE(Resources);
70 using support::libFileSearch;
72 using support::compare;
83 bool operator<(PngMap const & lhs, PngMap const & rhs)
85 return compare(lhs.key, rhs.key) < 0;
91 CompareKey(string const & name) : name_(name) {}
92 bool operator()(PngMap const & other) const { return other.key == name_; }
98 PngMap sorted_png_map[] = {
99 { "Bumpeq", "bumpeq2" },
102 { "Delta", "delta2" },
103 { "Downarrow", "downarrow2" },
104 { "Gamma", "gamma2" },
105 { "Lambda", "lambda2" },
106 { "Leftarrow", "leftarrow2" },
107 { "Leftrightarrow", "leftrightarrow2" },
108 { "Longleftarrow", "longleftarrow2" },
109 { "Longleftrightarrow", "longleftrightarrow2" },
110 { "Longrightarrow", "longrightarrow2" },
111 { "Omega", "omega2" },
115 { "Rightarrow", "rightarrow2" },
116 { "Sigma", "sigma2" },
117 { "Subset", "subset2" },
118 { "Supset", "supset2" },
119 { "Theta", "theta2" },
120 { "Uparrow", "uparrow2" },
121 { "Updownarrow", "updownarrow2" },
122 { "Upsilon", "upsilon2" },
123 { "Vdash", "vdash3" },
125 { "nLeftarrow", "nleftarrow2" },
126 { "nLeftrightarrow", "nleftrightarrow2" },
127 { "nRightarrow", "nrightarrow2" },
128 { "nVDash", "nvdash3" },
129 { "nvDash", "nvdash2" },
130 { "textrm \\AA", "textrm_AA"},
131 { "textrm \\O", "textrm_Oe"},
132 { "vDash", "vdash2" }
135 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
138 string const find_png(string const & name)
140 PngMap const * const begin = sorted_png_map;
141 PngMap const * const end = begin + nr_sorted_png_map;
142 BOOST_ASSERT(sorted(begin, end));
144 PngMap const * const it = std::find_if(begin, end, CompareKey(name));
148 png_name = it->value;
150 png_name = subst(name, "_", "underscore");
151 png_name = subst(png_name, ' ', '_');
153 // This way we can have "math-delim { }" on the toolbar.
154 png_name = subst(png_name, "(", "lparen");
155 png_name = subst(png_name, ")", "rparen");
156 png_name = subst(png_name, "[", "lbracket");
157 png_name = subst(png_name, "]", "rbracket");
158 png_name = subst(png_name, "{", "lbrace");
159 png_name = subst(png_name, "}", "rbrace");
160 png_name = subst(png_name, "|", "bars");
161 png_name = subst(png_name, ",", "thinspace");
162 png_name = subst(png_name, ":", "mediumspace");
163 png_name = subst(png_name, ";", "thickspace");
164 png_name = subst(png_name, "!", "negthinspace");
167 LYXERR(Debug::GUI, "find_png(" << name << ")\n"
168 << "Looking for math PNG called \"" << png_name << '"');
175 /// return a icon for the given action
176 static QIcon getIcon(FuncRequest const & f, bool unknown)
178 initializeResources();
186 case LFUN_MATH_INSERT:
187 if (!f.argument().empty()) {
189 name1 = find_png(to_utf8(f.argument()).substr(1));
192 case LFUN_MATH_DELIM:
193 case LFUN_MATH_BIGDELIM:
195 name1 = find_png(to_utf8(f.argument()));
199 name1 = to_utf8(f.argument());
202 name2 = lyxaction.getActionName(f.action);
205 if (!f.argument().empty())
206 name1 = subst(name2 + ' ' + to_utf8(f.argument()), ' ', '_');
209 fullname = libFileSearch("images/" + path, name1, "png").absFilename();
210 if (pm.load(toqstr(fullname)))
213 fullname = libFileSearch("images/" + path, name2, "png").absFilename();
214 if (pm.load(toqstr(fullname)))
217 if (pm.load(":/images/" + toqstr(path + name1) + ".png"))
220 if (pm.load(":/images/" + toqstr(path + name2) + ".png"))
223 LYXERR(Debug::GUI, "Cannot find icon for command \""
224 << lyxaction.getActionName(f.action)
225 << '(' << to_utf8(f.argument()) << ")\"");
227 pm.load(":/images/unknown.png");
233 /////////////////////////////////////////////////////////////////////
237 /////////////////////////////////////////////////////////////////////
239 GuiLayoutBox::GuiLayoutBox(GuiView & 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)));
249 owner_.setLayoutDialog(this);
250 updateContents(true);
254 void GuiLayoutBox::set(docstring const & layout)
259 QString const & name = toqstr(translateIfPossible(
260 (*text_class_)[layout]->name()));
262 if (name == currentText())
265 int i = findText(name);
267 lyxerr << "Trying to select non existent layout type "
268 << fromqstr(name) << endl;
276 void GuiLayoutBox::addItemSort(QString const & item, bool sorted)
278 int const end = count();
279 if (!sorted || end < 2 || item[0].category() != QChar::Letter_Uppercase) {
284 // Let the default one be at the beginning
286 for (setCurrentIndex(i); currentText() < item;) {
287 // e.g. --Separator--
288 if (currentText()[0].category() != QChar::Letter_Uppercase)
299 void GuiLayoutBox::updateContents(bool reset)
301 Buffer const * buffer = owner_.buffer();
310 TextClass const * text_class = &buffer->params().getTextClass();
311 if (!reset && text_class_ == text_class) {
312 set(owner_.view()->cursor().innerParagraph().layout()->name());
316 text_class_ = text_class;
318 setUpdatesEnabled(false);
321 TextClass::const_iterator it = text_class_->begin();
322 TextClass::const_iterator const end = text_class_->end();
323 for (; it != end; ++it) {
324 // ignore obsolete entries
325 addItemSort(toqstr(translateIfPossible((*it)->name())), lyxrc.sort_layouts);
330 // needed to recalculate size hint
332 setMinimumWidth(sizeHint().width());
333 set(owner_.view()->cursor().innerParagraph().layout()->name());
336 setUpdatesEnabled(true);
340 void GuiLayoutBox::selected(const QString & str)
343 updateContents(false);
347 docstring const name = qstring_to_ucs4(str);
348 TextClass::const_iterator it = text_class_->begin();
349 TextClass::const_iterator const end = text_class_->end();
350 for (; it != end; ++it) {
351 docstring const & itname = (*it)->name();
352 if (translateIfPossible(itname) == name) {
353 FuncRequest const func(LFUN_LAYOUT, itname,
354 FuncRequest::TOOLBAR);
355 owner_.dispatch(func);
359 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
364 /////////////////////////////////////////////////////////////////////
368 /////////////////////////////////////////////////////////////////////
371 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
372 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
373 layout_(0), command_buffer_(0)
375 // give visual separation between adjacent toolbars
378 // TODO: save toolbar position
381 ToolbarInfo::item_iterator it = tbinfo.items.begin();
382 ToolbarInfo::item_iterator end = tbinfo.items.end();
383 for (; it != end; ++it)
388 Action * GuiToolbar::addItem(ToolbarItem const & item)
390 Action * act = new Action(owner_,
391 getIcon(item.func_, false),
392 toqstr(item.label_), item.func_, toqstr(item.label_));
393 actions_.append(act);
398 void GuiToolbar::add(ToolbarItem const & item)
400 switch (item.type_) {
401 case ToolbarItem::SEPARATOR:
404 case ToolbarItem::LAYOUTS:
405 layout_ = new GuiLayoutBox(owner_);
408 case ToolbarItem::MINIBUFFER:
409 command_buffer_ = new GuiCommandBuffer(&owner_);
410 addWidget(command_buffer_);
411 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
412 //setHorizontalStretchable(true);
414 case ToolbarItem::TABLEINSERT: {
415 QToolButton * tb = new QToolButton;
416 tb->setCheckable(true);
417 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
418 tb->setToolTip(qt_(to_ascii(item.label_)));
419 tb->setStatusTip(qt_(to_ascii(item.label_)));
420 tb->setText(qt_(to_ascii(item.label_)));
421 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
422 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
423 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
424 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
428 case ToolbarItem::ICONPALETTE: {
429 QToolButton * tb = new QToolButton(this);
430 tb->setToolTip(qt_(to_ascii(item.label_)));
431 tb->setStatusTip(qt_(to_ascii(item.label_)));
432 tb->setText(qt_(to_ascii(item.label_)));
433 connect(this, SIGNAL(iconSizeChanged(QSize)),
434 tb, SLOT(setIconSize(QSize)));
435 IconPalette * panel = new IconPalette(tb);
436 panel->setWindowTitle(qt_(to_ascii(item.label_)));
437 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
438 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
440 lyxerr << "Unknown toolbar " << item.name_ << endl;
443 ToolbarInfo::item_iterator it = tbinfo->items.begin();
444 ToolbarInfo::item_iterator const end = tbinfo->items.end();
445 for (; it != end; ++it)
446 if (!getStatus(it->func_).unknown()) {
447 panel->addButton(addItem(*it));
448 // use the icon of first action for the toolbar button
449 if (it == tbinfo->items.begin())
450 tb->setIcon(getIcon(it->func_, true));
452 tb->setCheckable(true);
453 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
454 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
458 case ToolbarItem::POPUPMENU: {
459 QToolButton * tb = new QToolButton;
460 tb->setPopupMode(QToolButton::InstantPopup);
461 tb->setToolTip(qt_(to_ascii(item.label_)));
462 tb->setStatusTip(qt_(to_ascii(item.label_)));
463 tb->setText(qt_(to_ascii(item.label_)));
464 tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
465 connect(this, SIGNAL(iconSizeChanged(QSize)),
466 tb, SLOT(setIconSize(QSize)));
468 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
469 m->setWindowTitle(qt_(to_ascii(item.label_)));
470 m->setTearOffEnabled(true);
471 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
472 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
474 lyxerr << "Unknown toolbar " << item.name_ << endl;
477 ToolbarInfo::item_iterator it = tbinfo->items.begin();
478 ToolbarInfo::item_iterator const end = tbinfo->items.end();
479 for (; it != end; ++it)
480 if (!getStatus(it->func_).unknown())
481 m->add(addItem(*it));
486 case ToolbarItem::COMMAND: {
487 if (!getStatus(item.func_).unknown())
488 addAction(addItem(item));
497 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
499 // if tbinfo.state == auto *do not* set on/off
500 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
501 if (GuiToolbar::isVisible())
502 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
504 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
507 // no need to save it here.
508 Qt::ToolBarArea loc = owner_.toolBarArea(this);
510 if (loc == Qt::TopToolBarArea)
511 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
512 else if (loc == Qt::BottomToolBarArea)
513 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
514 else if (loc == Qt::RightToolBarArea)
515 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
516 else if (loc == Qt::LeftToolBarArea)
517 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
519 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
521 // save toolbar position. They are not used to restore toolbar position
522 // now because move(x,y) does not work for toolbar.
523 tbinfo.posx = pos().x();
524 tbinfo.posy = pos().y();
528 void GuiToolbar::updateContents()
530 // update visible toolbars only
533 // This is a speed bottleneck because this is called on every keypress
534 // and update calls getStatus, which copies the cursor at least two times
535 for (int i = 0; i < actions_.size(); ++i)
536 actions_[i]->update();
539 layout_->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
546 } // namespace frontend
549 #include "GuiToolbar_moc.cpp"