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 Stefan Schimanski
11 * \author Abdelrazak Younes
13 * Full author contact details are available in file CREDITS.
19 #include "GuiCommandBuffer.h"
20 #include "GuiToolbar.h"
21 #include "LyXAction.h"
23 #include "qt_helpers.h"
24 #include "InsertTableWidget.h"
27 #include "BufferParams.h"
28 #include "BufferView.h"
30 #include "FuncRequest.h"
31 #include "FuncStatus.h"
32 #include "IconPalette.h"
36 #include "Paragraph.h"
37 #include "TextClass.h"
38 #include "ToolbarBackend.h"
40 #include "support/debug.h"
41 #include "support/filetools.h"
42 #include "support/gettext.h"
43 #include "support/lstrings.h"
44 #include "support/lyxalgo.h" // sorted
46 #include <QAbstractItemDelegate>
47 #include <QAbstractTextDocumentLayout>
48 #include <QApplication>
50 #include <QFontMetrics>
51 #include <QHeaderView>
57 #include <QSortFilterProxyModel>
58 #include <QStandardItem>
59 #include <QStandardItemModel>
60 #include <QTextDocument>
63 #include <QToolButton>
66 #include <boost/assert.hpp>
72 using namespace lyx::support;
74 static void initializeResources()
76 static bool initialized = false;
78 Q_INIT_RESOURCE(Resources);
95 bool operator<(PngMap const & lhs, PngMap const & rhs)
97 return lhs.key < rhs.key;
103 CompareKey(QString const & name) : name_(name) {}
104 bool operator()(PngMap const & other) const { return other.key == name_; }
110 PngMap sorted_png_map[] = {
111 { "Bumpeq", "bumpeq2" },
114 { "Delta", "delta2" },
115 { "Downarrow", "downarrow2" },
116 { "Gamma", "gamma2" },
117 { "Lambda", "lambda2" },
118 { "Leftarrow", "leftarrow2" },
119 { "Leftrightarrow", "leftrightarrow2" },
120 { "Longleftarrow", "longleftarrow2" },
121 { "Longleftrightarrow", "longleftrightarrow2" },
122 { "Longrightarrow", "longrightarrow2" },
123 { "Omega", "omega2" },
127 { "Rightarrow", "rightarrow2" },
128 { "Sigma", "sigma2" },
129 { "Subset", "subset2" },
130 { "Supset", "supset2" },
131 { "Theta", "theta2" },
132 { "Uparrow", "uparrow2" },
133 { "Updownarrow", "updownarrow2" },
134 { "Upsilon", "upsilon2" },
135 { "Vdash", "vdash3" },
137 { "nLeftarrow", "nleftarrow2" },
138 { "nLeftrightarrow", "nleftrightarrow2" },
139 { "nRightarrow", "nrightarrow2" },
140 { "nVDash", "nvdash3" },
141 { "nvDash", "nvdash2" },
142 { "textrm \\AA", "textrm_AA"},
143 { "textrm \\O", "textrm_O"},
144 { "vDash", "vdash2" }
147 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
150 QString findPng(QString const & name)
152 PngMap const * const begin = sorted_png_map;
153 PngMap const * const end = begin + nr_sorted_png_map;
154 BOOST_ASSERT(sorted(begin, end));
156 PngMap const * const it = find_if(begin, end, CompareKey(name));
160 png_name = it->value;
163 png_name.replace('_', "underscore");
164 png_name.replace(' ', '_');
166 // This way we can have "math-delim { }" on the toolbar.
167 png_name.replace('(', "lparen");
168 png_name.replace(')', "rparen");
169 png_name.replace('[', "lbracket");
170 png_name.replace(']', "rbracket");
171 png_name.replace('{', "lbrace");
172 png_name.replace('}', "rbrace");
173 png_name.replace('|', "bars");
174 png_name.replace(',', "thinspace");
175 png_name.replace(':', "mediumspace");
176 png_name.replace(';', "thickspace");
177 png_name.replace('!', "negthinspace");
180 LYXERR(Debug::GUI, "findPng(" << fromqstr(name) << ")\n"
181 << "Looking for math PNG called \"" << fromqstr(png_name) << '"');
188 /// return a icon for the given action
189 static QIcon getIcon(FuncRequest const & f, bool unknown)
191 initializeResources();
197 case LFUN_MATH_INSERT:
198 if (!f.argument().empty()) {
200 name1 = findPng(toqstr(f.argument()).mid(1));
203 case LFUN_MATH_DELIM:
204 case LFUN_MATH_BIGDELIM:
206 name1 = findPng(toqstr(f.argument()));
210 name1 = toqstr(f.argument());
213 name2 = toqstr(lyxaction.getActionName(f.action));
216 if (!f.argument().empty()) {
217 name1 = name2 + ' ' + toqstr(f.argument());
218 name1.replace(' ', '_');
222 string fullname = libFileSearch("images/" + path, name1, "png").absFilename();
223 if (pm.load(toqstr(fullname)))
226 fullname = libFileSearch("images/" + path, name2, "png").absFilename();
227 if (pm.load(toqstr(fullname)))
230 if (pm.load(":/images/" + path + name1 + ".png"))
233 if (pm.load(":/images/" + path + name2 + ".png"))
236 LYXERR(Debug::GUI, "Cannot find icon for command \""
237 << lyxaction.getActionName(f.action)
238 << '(' << to_utf8(f.argument()) << ")\"");
240 pm.load(":/images/unknown.png");
246 /////////////////////////////////////////////////////////////////////
250 /////////////////////////////////////////////////////////////////////
252 class LayoutItemDelegate : public QAbstractItemDelegate {
255 explicit LayoutItemDelegate(QObject * parent = 0)
256 : QAbstractItemDelegate(parent)
260 void paint(QPainter * painter, QStyleOptionViewItem const & option,
261 QModelIndex const & index) const
263 QComboBox * combo = static_cast<QComboBox *>(parent());
264 QSortFilterProxyModel const * model
265 = static_cast<QSortFilterProxyModel const *>(index.model());
266 QStyleOptionMenuItem opt = getStyleOption(option, index);
268 painter->eraseRect(opt.rect);
270 QFontMetrics fm(opt.font);
271 QString text = underlineFilter(opt.text);
272 opt.text = QString();
275 if (lyxrc.group_layouts) {
276 QString stdCat = category(*model->sourceModel(), 0);
277 QString cat = category(*index.model(), index.row());
279 // not the standard layout and not the same as in the previous line?
281 && (index.row() == 0 || cat != category(*index.model(), index.row() - 1))) {
282 // draw category header
283 paintBackground(painter, opt);
284 paintCategoryHeader(painter, opt,
285 category(*index.model(), index.row()));
287 opt.rect.setTop(opt.rect.top() + headerHeight(opt));
288 opt.menuRect = opt.rect;
292 // Draw using the menu item style (this is how QComboBox does it).
293 // But for the rich text drawing below we will call it with an
294 // empty string, and later then draw over it the real string.
296 combo->style()->drawControl(QStyle::CE_MenuItem, &opt, painter, combo->view());
299 // Draw the rich text.
301 QColor col = opt.palette.text().color();
302 if (opt.state & QStyle::State_Selected)
303 col = opt.palette.highlightedText().color();
304 QAbstractTextDocumentLayout::PaintContext context;
305 context.palette.setColor(QPalette::Text, col);
308 doc.setDefaultFont(opt.font);
311 QTextFrameFormat fmt = doc.rootFrame()->frameFormat();
313 doc.rootFrame()->setFrameFormat(fmt);
315 painter->translate(opt.rect.x() + 5,
316 opt.rect.y() + (opt.rect.height() - fm.height()) / 2);
317 doc.documentLayout()->draw(painter, context);
322 QSize sizeHint(QStyleOptionViewItem const & option,
323 QModelIndex const & index) const
325 GuiLayoutBox * combo = static_cast<GuiLayoutBox *>(parent());
326 QSortFilterProxyModel const * model
327 = static_cast<QSortFilterProxyModel const *>(index.model());
328 QStyleOptionMenuItem opt = getStyleOption(option, index);
329 QSize size = combo->style()->sizeFromContents(
330 QStyle::CT_MenuItem, &opt, option.rect.size(), combo);
332 /// QComboBox uses the first row height to estimate the
333 /// complete popup height during QComboBox::showPopup().
334 /// To avoid scrolling we have to sneak in space for the headers.
335 /// So we tweak this value accordingly. It's not nice, but the
336 /// only possible way it seems.
337 if (lyxrc.group_layouts && index.row() == 0 && combo->inShowPopup_) {
338 int itemHeight = size.height();
340 // we have to show \c cats many headers:
341 unsigned cats = combo->visibleCategories_;
343 // and we have \c n items to distribute the needed space over
344 unsigned n = combo->model()->rowCount();
346 // so the needed average height (rounded upwards) is:
347 size.setHeight((headerHeight(opt) * cats + itemHeight * n + n - 1) / n);
351 // Add space for the category headers here?
352 // Not for the standard layout though.
353 QString stdCat = category(*model->sourceModel(), 0);
354 QString cat = category(*index.model(), index.row());
355 if (lyxrc.group_layouts && stdCat != cat
356 && (index.row() == 0 || cat != category(*index.model(), index.row() - 1))) {
357 size.setHeight(size.height() + headerHeight(opt));
365 QString category(QAbstractItemModel const & model, int row) const
367 return model.data(model.index(row, 2), Qt::DisplayRole).toString();
371 void paintBackground(QPainter * painter, QStyleOptionMenuItem const & opt) const
373 QComboBox * combo = static_cast<QComboBox *>(parent());
375 // we only want to paint a background using the style, so
376 // disable every thing else
377 QStyleOptionMenuItem sopt = opt;
378 sopt.menuRect = sopt.rect;
379 sopt.state = QStyle::State_Active | QStyle::State_Enabled;
380 sopt.checked = false;
381 sopt.text = QString();
384 combo->style()->drawControl(QStyle::CE_MenuItem, &sopt,
385 painter, combo->view());
390 int headerHeight(QStyleOptionMenuItem const & opt) const
392 QFontMetrics fm(opt.font);
393 return fm.height() * 8 / 10;
396 void paintCategoryHeader(QPainter * painter, QStyleOptionMenuItem const & opt,
397 QString const & category) const
401 // slightly blended color
402 QColor lcol = opt.palette.text().color();
404 painter->setPen(lcol);
406 // set 80% scaled, bold font
407 QFont font = opt.font;
409 font.setWeight(QFont::Black);
410 font.setPointSize(opt.font.pointSize() * 8 / 10);
411 painter->setFont(font);
413 // draw the centered text
414 QFontMetrics fm(font);
415 int w = fm.width(category);
416 int x = opt.rect.x() + (opt.rect.width() - w) / 2;
417 int y = opt.rect.y() + fm.ascent();
420 painter->drawText(x, y, category);
422 // the vertical position of the line: middle of lower case chars
423 int ymid = y - 1 - fm.xHeight() / 2; // -1 for the baseline
425 // draw the horizontal line
426 if (!category.isEmpty()) {
427 painter->drawLine(opt.rect.x(), ymid, left - 1, ymid);
428 painter->drawLine(right + 1, ymid, opt.rect.right(), ymid);
430 painter->drawLine(opt.rect.x(), ymid, opt.rect.right(), ymid);
437 QString underlineFilter(QString const & s) const
440 GuiLayoutBox * p = static_cast<GuiLayoutBox *>(parent());
441 QString const & f = p->filter();
445 // step through data item and put "(x)" for every matching character
449 for (int i = 0; i < f.length(); ++i) {
450 int p = s.indexOf(f[i], lastp + 1, Qt::CaseInsensitive);
451 BOOST_ASSERT(p != -1);
452 if (lastp == p - 1 && lastp != -1) {
453 // remove ")" and append "x)"
454 r = r.left(r.length() - 4) + s[p] + "</u>";
457 r += s.mid(lastp + 1, p - lastp - 1);
458 r += QString("<u>") + s[p] + "</u>";
462 r += s.mid(lastp + 1);
467 QStyleOptionMenuItem getStyleOption(QStyleOptionViewItem const & option,
468 QModelIndex const & index) const
470 QComboBox * combo = static_cast<QComboBox *>(parent());
472 // create the options for a menu item
473 QStyleOptionMenuItem menuOption;
474 menuOption.palette = QApplication::palette("QMenu");
475 menuOption.state = QStyle::State_Active | QStyle::State_Enabled;
476 menuOption.menuRect = option.rect;
477 menuOption.rect = option.rect;
478 menuOption.font = combo->font();
479 menuOption.fontMetrics = QFontMetrics(menuOption.font);
480 menuOption.tabWidth = 0;
481 menuOption.text = index.model()->data(index, Qt::DisplayRole).toString()
482 .replace(QLatin1Char('&'), QLatin1String("&&"));
483 menuOption.menuItemType = QStyleOptionMenuItem::Normal;
484 if (option.state & QStyle::State_Selected)
485 menuOption.state |= QStyle::State_Selected;
486 menuOption.checkType = QStyleOptionMenuItem::NotCheckable;
487 menuOption.checked = false;
493 class GuiLayoutFilterModel : public QSortFilterProxyModel {
496 GuiLayoutFilterModel(QObject * parent = 0)
497 : QSortFilterProxyModel(parent)
501 void triggerLayoutChange()
503 layoutAboutToBeChanged();
509 GuiLayoutBox::GuiLayoutBox(GuiView & owner)
510 : owner_(owner), lastSel_(-1), layoutItemDelegate_(new LayoutItemDelegate(this)),
511 visibleCategories_(0), inShowPopup_(false)
513 setSizeAdjustPolicy(QComboBox::AdjustToContents);
514 setFocusPolicy(Qt::ClickFocus);
515 setMinimumWidth(sizeHint().width());
516 setMaxVisibleItems(100);
518 // set the layout model with two columns
519 // 1st: translated layout names
520 // 2nd: raw layout names
521 model_ = new QStandardItemModel(0, 2, this);
522 filterModel_ = new GuiLayoutFilterModel(this);
523 filterModel_->setSourceModel(model_);
524 setModel(filterModel_);
526 // for the filtering we have to intercept characters
527 view()->installEventFilter(this);
528 view()->setItemDelegateForColumn(0, layoutItemDelegate_);
530 QObject::connect(this, SIGNAL(activated(int)),
531 this, SLOT(selected(int)));
532 owner_.setLayoutDialog(this);
533 updateContents(true);
537 void GuiLayoutBox::setFilter(QString const & s)
540 owner_.message(_("Filtering layouts with \"" + fromqstr(s) + "\". "
541 "Press ESC to remove filter."));
543 owner_.message(_("Enter characters to filter the layout list."));
545 bool enabled = view()->updatesEnabled();
546 view()->setUpdatesEnabled(false);
548 // remember old selection
549 int sel = currentIndex();
551 lastSel_ = filterModel_->mapToSource(filterModel_->index(sel, 0)).row();
554 filterModel_->setFilterRegExp(charFilterRegExp(filter_));
557 // restore old selection
558 if (lastSel_ != -1) {
559 QModelIndex i = filterModel_->mapFromSource(model_->index(lastSel_, 0));
561 setCurrentIndex(i.row());
564 // Workaround to resize to content size
565 // FIXME: There must be a better way. The QComboBox::AdjustToContents)
567 if (view()->isVisible()) {
568 // call QComboBox::showPopup. But set the inShowPopup_ flag to switch on
569 // the hack in the item delegate to make space for the headers.
570 // We do not call our implementation of showPopup because that
571 // would reset the filter again. This is only needed if the user clicks
573 BOOST_ASSERT(!inShowPopup_);
575 QComboBox::showPopup();
576 inShowPopup_ = false;
578 // The item delegate hack is off again. So trigger a relayout of the popup.
579 filterModel_->triggerLayoutChange();
582 view()->setUpdatesEnabled(enabled);
586 void GuiLayoutBox::countCategories()
588 int n = filterModel_->rowCount();
589 visibleCategories_ = 0;
590 if (n == 0 || !lyxrc.group_layouts)
593 // skip the "Standard" category
594 QString prevCat = model_->index(0, 2).data().toString();
597 for (int i = 0; i < n; ++i) {
598 QString cat = filterModel_->index(i, 2).data().toString();
600 ++visibleCategories_;
606 QString GuiLayoutBox::charFilterRegExp(QString const & filter)
609 for (int i = 0; i < filter.length(); ++i) {
612 re += ".*[" + QRegExp::escape(c) + QRegExp::escape(c.toUpper()) + "]";
614 re += ".*" + QRegExp::escape(c);
620 void GuiLayoutBox::resetFilter()
622 setFilter(QString());
626 void GuiLayoutBox::showPopup()
628 owner_.message(_("Enter characters to filter the layout list."));
630 bool enabled = view()->updatesEnabled();
631 view()->setUpdatesEnabled(false);
635 // call QComboBox::showPopup. But set the inShowPopup_ flag to switch on
636 // the hack in the item delegate to make space for the headers.
637 BOOST_ASSERT(!inShowPopup_);
639 QComboBox::showPopup();
640 inShowPopup_ = false;
642 // The item delegate hack is off again. So trigger a relayout of the popup.
643 filterModel_->triggerLayoutChange();
645 view()->setUpdatesEnabled(enabled);
649 bool GuiLayoutBox::eventFilter(QObject * o, QEvent * e)
651 if (e->type() != QEvent::KeyPress)
652 return QComboBox::eventFilter(o, e);
654 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
655 bool modified = (ke->modifiers() == Qt::ControlModifier)
656 || (ke->modifiers() == Qt::AltModifier)
657 || (ke->modifiers() == Qt::MetaModifier);
661 if (!modified && !filter_.isEmpty()) {
666 case Qt::Key_Backspace:
668 // cut off one character
669 setFilter(filter_.left(filter_.length() - 1));
673 if (modified || ke->text().isEmpty())
675 // find chars for the filter string
677 for (int i = 0; i < ke->text().length(); ++i) {
678 QChar c = ke->text()[i];
679 if (c.isLetterOrNumber()
682 || c.category() == QChar::Separator_Space) {
687 // append new chars to the filter string
688 setFilter(filter_ + s);
694 return QComboBox::eventFilter(o, e);
698 void GuiLayoutBox::set(docstring const & layout)
705 QString const & name = toqstr((*text_class_)[layout].name());
706 if (name == currentText())
709 QList<QStandardItem *> r = model_->findItems(name, Qt::MatchExactly, 1);
711 lyxerr << "Trying to select non existent layout type "
712 << fromqstr(name) << endl;
716 setCurrentIndex(filterModel_->mapFromSource(r.first()->index()).row());
720 void GuiLayoutBox::addItemSort(docstring const & item, docstring const & category,
721 bool sorted, bool sortedByCat)
723 QString qitem = toqstr(item);
724 QString titem = toqstr(translateIfPossible(item));
725 QString qcat = toqstr(translateIfPossible(category));
727 QList<QStandardItem *> row;
728 row.append(new QStandardItem(titem));
729 row.append(new QStandardItem(qitem));
730 row.append(new QStandardItem(qcat));
732 // the first entry is easy
733 int const end = model_->rowCount();
735 model_->appendRow(row);
742 while (i < end && model_->item(i, 2)->text() != qcat)
746 // skip the Standard layout
750 // the simple unsorted case
753 // jump to the end of the category group
754 while (i < end && model_->item(i, 2)->text() == qcat)
756 model_->insertRow(i, row);
758 model_->appendRow(row);
762 // find row to insert the item, after the separator if it exists
764 // find alphabetic position
766 && model_->item(i, 0)->text().compare(titem) < 0
767 && (!sortedByCat || model_->item(i, 2)->text() == qcat))
771 model_->insertRow(i, row);
775 void GuiLayoutBox::updateContents(bool reset)
779 Buffer const * buffer = owner_.buffer();
788 // we'll only update the layout list if the text class has changed
789 // or we've moved from one inset to another
790 DocumentClass const * text_class = &buffer->params().documentClass();
791 Inset const * inset =
792 owner_.view()->cursor().innerParagraph().inInset();
793 if (!reset && text_class_ == text_class && inset_ == inset) {
794 set(owner_.view()->cursor().innerParagraph().layout().name());
799 text_class_ = text_class;
802 DocumentClass::const_iterator lit = text_class_->begin();
803 DocumentClass::const_iterator len = text_class_->end();
804 for (; lit != len; ++lit) {
805 docstring const & name = lit->name();
806 // if this inset requires the empty layout, we skip the default
808 if (name == text_class_->defaultLayoutName() && inset &&
809 (inset->forceEmptyLayout() || inset->useEmptyLayout()))
811 // if it doesn't require the empty layout, we skip it
812 if (name == text_class_->emptyLayoutName() && inset &&
813 !inset->forceEmptyLayout() && !inset->useEmptyLayout())
815 addItemSort(name, lit->category(), lyxrc.sort_layouts, lyxrc.group_layouts);
818 set(owner_.view()->cursor().innerParagraph().layout().name());
821 // needed to recalculate size hint
823 setMinimumWidth(sizeHint().width());
824 setEnabled(!buffer->isReadonly());
829 void GuiLayoutBox::selected(int index)
832 QModelIndex mindex = filterModel_->mapToSource(filterModel_->index(index, 1));
833 docstring const layoutName =
834 qstring_to_ucs4(model_->itemFromIndex(mindex)->text());
839 updateContents(false);
844 // find corresponding text class
845 if (text_class_->hasLayout(layoutName)) {
846 FuncRequest const func(LFUN_LAYOUT, layoutName, FuncRequest::TOOLBAR);
847 theLyXFunc().setLyXView(&owner_);
849 updateContents(false);
853 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
858 /////////////////////////////////////////////////////////////////////
862 /////////////////////////////////////////////////////////////////////
865 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
866 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
867 layout_(0), command_buffer_(0)
869 // give visual separation between adjacent toolbars
872 // TODO: save toolbar position
875 ToolbarInfo::item_iterator it = tbinfo.items.begin();
876 ToolbarInfo::item_iterator end = tbinfo.items.end();
877 for (; it != end; ++it)
882 Action * GuiToolbar::addItem(ToolbarItem const & item)
884 Action * act = new Action(owner_,
885 getIcon(item.func_, false),
886 toqstr(item.label_), item.func_, toqstr(item.label_));
887 actions_.append(act);
893 class PaletteButton : public QToolButton
897 ToolbarItem const & tbitem_;
900 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
901 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
903 QString const label = qt_(to_ascii(tbitem_.label_));
907 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
908 this, SLOT(setIconSize(QSize)));
910 ToolbarInfo const * tbinfo =
911 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
913 // use the icon of first action for the toolbar button
914 setIcon(getIcon(tbinfo->items.begin()->func_, true));
917 void mousePressEvent(QMouseEvent * e)
920 QToolButton::mousePressEvent(e);
926 ToolbarInfo const * tbinfo =
927 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
929 lyxerr << "Unknown toolbar " << tbitem_.name_ << endl;
932 IconPalette * panel = new IconPalette(this);
933 QString const label = qt_(to_ascii(tbitem_.label_));
934 panel->setWindowTitle(label);
935 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
936 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
937 ToolbarInfo::item_iterator it = tbinfo->items.begin();
938 ToolbarInfo::item_iterator const end = tbinfo->items.end();
939 for (; it != end; ++it)
940 if (!getStatus(it->func_).unknown())
941 panel->addButton(bar_->addItem(*it));
943 QToolButton::mousePressEvent(e);
947 class MenuButton : public QToolButton
951 ToolbarItem const & tbitem_;
954 MenuButton(GuiToolbar * bar, ToolbarItem const & item)
955 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
957 setPopupMode(QToolButton::InstantPopup);
958 QString const label = qt_(to_ascii(tbitem_.label_));
962 setIcon(QPixmap(":images/math/" + toqstr(tbitem_.name_) + ".png"));
963 connect(bar, SIGNAL(iconSizeChanged(QSize)),
964 this, SLOT(setIconSize(QSize)));
967 void mousePressEvent(QMouseEvent * e)
970 QToolButton::mousePressEvent(e);
976 QString const label = qt_(to_ascii(tbitem_.label_));
977 ButtonMenu * m = new ButtonMenu(label, this);
978 m->setWindowTitle(label);
979 m->setTearOffEnabled(true);
980 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
981 ToolbarInfo const * tbinfo =
982 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
984 lyxerr << "Unknown toolbar " << tbitem_.name_ << endl;
987 ToolbarInfo::item_iterator it = tbinfo->items.begin();
988 ToolbarInfo::item_iterator const end = tbinfo->items.end();
989 for (; it != end; ++it)
990 if (!getStatus(it->func_).unknown())
991 m->add(bar_->addItem(*it));
994 QToolButton::mousePressEvent(e);
1001 void GuiToolbar::add(ToolbarItem const & item)
1003 switch (item.type_) {
1004 case ToolbarItem::SEPARATOR:
1007 case ToolbarItem::LAYOUTS:
1008 layout_ = new GuiLayoutBox(owner_);
1011 case ToolbarItem::MINIBUFFER:
1012 command_buffer_ = new GuiCommandBuffer(&owner_);
1013 addWidget(command_buffer_);
1014 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
1015 //setHorizontalStretchable(true);
1017 case ToolbarItem::TABLEINSERT: {
1018 QToolButton * tb = new QToolButton;
1019 tb->setCheckable(true);
1020 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
1021 QString const label = qt_(to_ascii(item.label_));
1022 tb->setToolTip(label);
1023 tb->setStatusTip(label);
1025 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
1026 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
1027 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
1028 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
1032 case ToolbarItem::ICONPALETTE:
1033 addWidget(new PaletteButton(this, item));
1036 case ToolbarItem::POPUPMENU: {
1037 addWidget(new MenuButton(this, item));
1040 case ToolbarItem::COMMAND: {
1041 if (!getStatus(item.func_).unknown())
1042 addAction(addItem(item));
1051 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
1053 // if tbinfo.state == auto *do not* set on/off
1054 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
1055 if (GuiToolbar::isVisible())
1056 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
1058 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
1061 // no need to save it here.
1062 Qt::ToolBarArea loc = owner_.toolBarArea(this);
1064 if (loc == Qt::TopToolBarArea)
1065 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
1066 else if (loc == Qt::BottomToolBarArea)
1067 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
1068 else if (loc == Qt::RightToolBarArea)
1069 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
1070 else if (loc == Qt::LeftToolBarArea)
1071 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
1073 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
1075 // save toolbar position. They are not used to restore toolbar position
1076 // now because move(x,y) does not work for toolbar.
1077 tbinfo.posx = pos().x();
1078 tbinfo.posy = pos().y();
1082 void GuiToolbar::updateContents()
1084 // update visible toolbars only
1087 // This is a speed bottleneck because this is called on every keypress
1088 // and update calls getStatus, which copies the cursor at least two times
1089 for (int i = 0; i < actions_.size(); ++i)
1090 actions_[i]->update();
1093 layout_->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
1100 } // namespace frontend
1103 #include "GuiToolbar_moc.cpp"