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 "GuiCommandBuffer.h"
19 #include "GuiToolbar.h"
20 #include "LyXAction.h"
22 #include "qt_helpers.h"
23 #include "InsertTableWidget.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
29 #include "FuncRequest.h"
30 #include "FuncStatus.h"
31 #include "IconPalette.h"
35 #include "Paragraph.h"
36 #include "TextClass.h"
37 #include "ToolbarBackend.h"
39 #include "support/debug.h"
40 #include "support/filetools.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
43 #include "support/lyxalgo.h" // sorted
47 #include <QToolButton>
51 #include <boost/assert.hpp>
54 using namespace lyx::support;
56 static void initializeResources()
58 static bool initialized = false;
60 Q_INIT_RESOURCE(Resources);
77 bool operator<(PngMap const & lhs, PngMap const & rhs)
79 return strcmp(lhs.key, rhs.key) < 0;
85 CompareKey(string const & name) : name_(name) {}
86 bool operator()(PngMap const & other) const { return other.key == name_; }
92 PngMap sorted_png_map[] = {
93 { "Bumpeq", "bumpeq2" },
96 { "Delta", "delta2" },
97 { "Downarrow", "downarrow2" },
98 { "Gamma", "gamma2" },
99 { "Lambda", "lambda2" },
100 { "Leftarrow", "leftarrow2" },
101 { "Leftrightarrow", "leftrightarrow2" },
102 { "Longleftarrow", "longleftarrow2" },
103 { "Longleftrightarrow", "longleftrightarrow2" },
104 { "Longrightarrow", "longrightarrow2" },
105 { "Omega", "omega2" },
109 { "Rightarrow", "rightarrow2" },
110 { "Sigma", "sigma2" },
111 { "Subset", "subset2" },
112 { "Supset", "supset2" },
113 { "Theta", "theta2" },
114 { "Uparrow", "uparrow2" },
115 { "Updownarrow", "updownarrow2" },
116 { "Upsilon", "upsilon2" },
117 { "Vdash", "vdash3" },
119 { "nLeftarrow", "nleftarrow2" },
120 { "nLeftrightarrow", "nleftrightarrow2" },
121 { "nRightarrow", "nrightarrow2" },
122 { "nVDash", "nvdash3" },
123 { "nvDash", "nvdash2" },
124 { "textrm \\AA", "textrm_AA"},
125 { "textrm \\O", "textrm_O"},
126 { "vDash", "vdash2" }
129 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
132 string const find_png(string const & name)
134 PngMap const * const begin = sorted_png_map;
135 PngMap const * const end = begin + nr_sorted_png_map;
136 BOOST_ASSERT(sorted(begin, end));
138 PngMap const * const it = find_if(begin, end, CompareKey(name));
142 png_name = it->value;
144 png_name = subst(name, "_", "underscore");
145 png_name = subst(png_name, ' ', '_');
147 // This way we can have "math-delim { }" on the toolbar.
148 png_name = subst(png_name, "(", "lparen");
149 png_name = subst(png_name, ")", "rparen");
150 png_name = subst(png_name, "[", "lbracket");
151 png_name = subst(png_name, "]", "rbracket");
152 png_name = subst(png_name, "{", "lbrace");
153 png_name = subst(png_name, "}", "rbrace");
154 png_name = subst(png_name, "|", "bars");
155 png_name = subst(png_name, ",", "thinspace");
156 png_name = subst(png_name, ":", "mediumspace");
157 png_name = subst(png_name, ";", "thickspace");
158 png_name = subst(png_name, "!", "negthinspace");
161 LYXERR(Debug::GUI, "find_png(" << name << ")\n"
162 << "Looking for math PNG called \"" << png_name << '"');
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()) << ")\"");
221 pm.load(":/images/unknown.png");
227 /////////////////////////////////////////////////////////////////////
231 /////////////////////////////////////////////////////////////////////
233 GuiLayoutBox::GuiLayoutBox(GuiView & owner)
236 setSizeAdjustPolicy(QComboBox::AdjustToContents);
237 setFocusPolicy(Qt::ClickFocus);
238 setMinimumWidth(sizeHint().width());
239 setMaxVisibleItems(100);
241 QObject::connect(this, SIGNAL(activated(QString)),
242 this, SLOT(selected(QString)));
243 owner_.setLayoutDialog(this);
244 updateContents(true);
248 void GuiLayoutBox::set(docstring const & layout)
253 QString const & name = toqstr(translateIfPossible(
254 (*text_class_)[layout]->name()));
256 if (name == currentText())
259 int i = findText(name);
261 lyxerr << "Trying to select non existent layout type "
262 << fromqstr(name) << endl;
270 void GuiLayoutBox::addItemSort(QString const & item, bool sorted)
273 //Since we are only storing the text used for display, we have no choice
274 //below but to compare translated strings to figure out which layout the
275 //user wants. This is not ideal. A better way is the way module names are
276 //handled in GuiDocument: viz, the untranslated name can be associated
277 //with the item via QComboBox::setItemData(). It may be that this can
278 //even be done by passing: addItem(item, untransName).
279 int const end = count();
280 if (!sorted || end < 2 || item[0].category() != QChar::Letter_Uppercase) {
285 // Let the default one be at the beginning
287 for (setCurrentIndex(i); currentText().localeAwareCompare(item) < 0; ) {
288 // e.g. --Separator--
289 if (currentText()[0].category() != QChar::Letter_Uppercase)
300 void GuiLayoutBox::updateContents(bool reset)
302 Buffer const * buffer = owner_.buffer();
311 TextClass const * text_class = &buffer->params().textClass();
312 Inset const * inset =
313 owner_.view()->cursor().innerParagraph().inInset();
315 // we'll only update the layout list if the text class has changed
316 // or we've moved from one inset to another
317 if (!reset && text_class_ == text_class && inset_ == inset) {
318 set(owner_.view()->cursor().innerParagraph().layout()->name());
323 text_class_ = text_class;
327 for (size_t i = 0; i != text_class_->layoutCount(); ++i) {
328 Layout const & lt = *text_class_->layout(i);
329 docstring const & name = lt.name();
330 // if this inset requires the empty layout, we skip the default
332 if (name == text_class_->defaultLayoutName() && inset &&
333 (inset->forceEmptyLayout() || inset->useEmptyLayout()))
335 // if it doesn't require the empty layout, we skip it
336 if (name == text_class_->emptyLayoutName() && inset &&
337 !inset->forceEmptyLayout() && !inset->useEmptyLayout())
339 addItemSort(toqstr(translateIfPossible(name)), lyxrc.sort_layouts);
342 set(owner_.view()->cursor().innerParagraph().layout()->name());
344 // needed to recalculate size hint
346 setMinimumWidth(sizeHint().width());
348 setEnabled(!buffer->isReadonly());
353 void GuiLayoutBox::selected(const QString & str)
356 updateContents(false);
360 docstring const name = qstring_to_ucs4(str);
361 for (size_t i = 0; i != text_class_->layoutCount(); ++i) {
362 docstring const & itname = text_class_->layout(i)->name();
363 // FIXME: Comparing translated strings is not ideal.
364 // This should be done the way module names are handled
365 // in GuiDocument: viz, the untranslated name should be
366 // associated with the item via QComboBox::setItemData().
367 if (translateIfPossible(itname) == name) {
368 FuncRequest const func(LFUN_LAYOUT, itname,
369 FuncRequest::TOOLBAR);
370 theLyXFunc().setLyXView(&owner_);
372 updateContents(false);
376 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
381 /////////////////////////////////////////////////////////////////////
385 /////////////////////////////////////////////////////////////////////
388 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
389 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
390 layout_(0), command_buffer_(0)
392 // give visual separation between adjacent toolbars
395 // TODO: save toolbar position
398 ToolbarInfo::item_iterator it = tbinfo.items.begin();
399 ToolbarInfo::item_iterator end = tbinfo.items.end();
400 for (; it != end; ++it)
405 Action * GuiToolbar::addItem(ToolbarItem const & item)
407 Action * act = new Action(owner_,
408 getIcon(item.func_, false),
409 toqstr(item.label_), item.func_, toqstr(item.label_));
410 actions_.append(act);
416 class PaletteButton : public QToolButton
420 ToolbarItem const & tbitem_;
423 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
424 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
426 QString const label = qt_(to_ascii(tbitem_.label_));
430 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
431 this, SLOT(setIconSize(QSize)));
433 ToolbarInfo const * tbinfo =
434 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
436 // use the icon of first action for the toolbar button
437 setIcon(getIcon(tbinfo->items.begin()->func_, true));
440 void mousePressEvent(QMouseEvent * e)
443 QToolButton::mousePressEvent(e);
449 ToolbarInfo const * tbinfo =
450 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
452 lyxerr << "Unknown toolbar " << tbitem_.name_ << endl;
455 IconPalette * panel = new IconPalette(this);
456 QString const label = qt_(to_ascii(tbitem_.label_));
457 panel->setWindowTitle(label);
458 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
459 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
460 ToolbarInfo::item_iterator it = tbinfo->items.begin();
461 ToolbarInfo::item_iterator const end = tbinfo->items.end();
462 for (; it != end; ++it)
463 if (!getStatus(it->func_).unknown())
464 panel->addButton(bar_->addItem(*it));
466 QToolButton::mousePressEvent(e);
470 class MenuButton : public QToolButton
474 ToolbarItem const & tbitem_;
477 MenuButton(GuiToolbar * bar, ToolbarItem const & item)
478 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
480 setPopupMode(QToolButton::InstantPopup);
481 QString const label = qt_(to_ascii(tbitem_.label_));
485 setIcon(QPixmap(":images/math/" + toqstr(tbitem_.name_) + ".png"));
486 connect(bar, SIGNAL(iconSizeChanged(QSize)),
487 this, SLOT(setIconSize(QSize)));
490 void mousePressEvent(QMouseEvent * e)
493 QToolButton::mousePressEvent(e);
499 QString const label = qt_(to_ascii(tbitem_.label_));
500 ButtonMenu * m = new ButtonMenu(label, this);
501 m->setWindowTitle(label);
502 m->setTearOffEnabled(true);
503 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
504 ToolbarInfo const * tbinfo =
505 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
507 lyxerr << "Unknown toolbar " << tbitem_.name_ << endl;
510 ToolbarInfo::item_iterator it = tbinfo->items.begin();
511 ToolbarInfo::item_iterator const end = tbinfo->items.end();
512 for (; it != end; ++it)
513 if (!getStatus(it->func_).unknown())
514 m->add(bar_->addItem(*it));
517 QToolButton::mousePressEvent(e);
524 void GuiToolbar::add(ToolbarItem const & item)
526 switch (item.type_) {
527 case ToolbarItem::SEPARATOR:
530 case ToolbarItem::LAYOUTS:
531 layout_ = new GuiLayoutBox(owner_);
534 case ToolbarItem::MINIBUFFER:
535 command_buffer_ = new GuiCommandBuffer(&owner_);
536 addWidget(command_buffer_);
537 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
538 //setHorizontalStretchable(true);
540 case ToolbarItem::TABLEINSERT: {
541 QToolButton * tb = new QToolButton;
542 tb->setCheckable(true);
543 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
544 QString const label = qt_(to_ascii(item.label_));
545 tb->setToolTip(label);
546 tb->setStatusTip(label);
548 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
549 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
550 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
551 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
555 case ToolbarItem::ICONPALETTE:
556 addWidget(new PaletteButton(this, item));
559 case ToolbarItem::POPUPMENU: {
560 addWidget(new MenuButton(this, item));
563 case ToolbarItem::COMMAND: {
564 if (!getStatus(item.func_).unknown())
565 addAction(addItem(item));
574 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
576 // if tbinfo.state == auto *do not* set on/off
577 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
578 if (GuiToolbar::isVisible())
579 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
581 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
584 // no need to save it here.
585 Qt::ToolBarArea loc = owner_.toolBarArea(this);
587 if (loc == Qt::TopToolBarArea)
588 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
589 else if (loc == Qt::BottomToolBarArea)
590 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
591 else if (loc == Qt::RightToolBarArea)
592 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
593 else if (loc == Qt::LeftToolBarArea)
594 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
596 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
598 // save toolbar position. They are not used to restore toolbar position
599 // now because move(x,y) does not work for toolbar.
600 tbinfo.posx = pos().x();
601 tbinfo.posy = pos().y();
605 void GuiToolbar::updateContents()
607 // update visible toolbars only
610 // This is a speed bottleneck because this is called on every keypress
611 // and update calls getStatus, which copies the cursor at least two times
612 for (int i = 0; i < actions_.size(); ++i)
613 actions_[i]->update();
616 layout_->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
623 } // namespace frontend
626 #include "GuiToolbar_moc.cpp"