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 QString text = underlineFilter(opt.text);
271 opt.text = QString();
274 if (lyxrc.group_layouts) {
275 QString stdCat = category(*model->sourceModel(), 0);
276 QString cat = category(*index.model(), index.row());
278 // not the standard layout and not the same as in the previous line?
280 && (index.row() == 0 || cat != category(*index.model(), index.row() - 1))) {
281 // draw category header
282 paintBackground(painter, opt);
283 paintCategoryHeader(painter, opt,
284 category(*index.model(), index.row()));
286 // move rect down below header
287 opt.rect.moveTop(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() - opt.fontMetrics.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());
329 // we use a compressed menu style here
330 QStyleOptionMenuItem opt = getStyleOption(option, index);
331 QSize size = combo->style()->sizeFromContents(
332 QStyle::CT_MenuItem, &opt, opt.rect.size(), combo);
333 size.setHeight(opt.rect.height());
335 /// QComboBox uses the first row height to estimate the
336 /// complete popup height during QComboBox::showPopup().
337 /// To avoid scrolling we have to sneak in space for the headers.
338 /// So we tweak this value accordingly. It's not nice, but the
339 /// only possible way it seems.
340 if (lyxrc.group_layouts && index.row() == 0 && combo->inShowPopup_) {
341 int itemHeight = size.height();
343 // we have to show \c cats many headers:
344 unsigned cats = combo->visibleCategories_;
346 // and we have \c n items to distribute the needed space over
347 unsigned n = combo->model()->rowCount();
349 // so the needed average height (rounded upwards) is:
350 size.setHeight((headerHeight(opt) * cats + itemHeight * n + n - 1) / n);
354 // Add space for the category headers here?
355 // Not for the standard layout though.
356 QString stdCat = category(*model->sourceModel(), 0);
357 QString cat = category(*index.model(), index.row());
358 if (lyxrc.group_layouts && stdCat != cat
359 && (index.row() == 0 || cat != category(*index.model(), index.row() - 1))) {
360 size.setHeight(size.height() + headerHeight(opt));
368 QString category(QAbstractItemModel const & model, int row) const
370 return model.data(model.index(row, 2), Qt::DisplayRole).toString();
374 void paintBackground(QPainter * painter, QStyleOptionMenuItem const & opt) const
376 QComboBox * combo = static_cast<QComboBox *>(parent());
378 // we only want to paint a background using the style, so
379 // disable every thing else
380 QStyleOptionMenuItem sopt = opt;
381 sopt.menuRect = sopt.rect;
382 sopt.state = QStyle::State_Active | QStyle::State_Enabled;
383 sopt.checked = false;
384 sopt.text = QString();
387 combo->style()->drawControl(QStyle::CE_MenuItem, &sopt,
388 painter, combo->view());
393 int headerHeight(QStyleOptionMenuItem const & opt) const
395 return opt.fontMetrics.height() * 8 / 10;
398 void paintCategoryHeader(QPainter * painter, QStyleOptionMenuItem const & opt,
399 QString const & category) const
403 // slightly blended color
404 QColor lcol = opt.palette.text().color();
406 painter->setPen(lcol);
408 // set 80% scaled, bold font
409 QFont font = opt.font;
411 font.setWeight(QFont::Black);
412 font.setPointSize(opt.font.pointSize() * 8 / 10);
413 painter->setFont(font);
415 // draw the centered text
416 QFontMetrics fm(font);
417 int w = fm.width(category);
418 int x = opt.rect.x() + (opt.rect.width() - w) / 2;
419 int y = opt.rect.y() + fm.ascent();
422 painter->drawText(x, y, category);
424 // the vertical position of the line: middle of lower case chars
425 int ymid = y - 1 - fm.xHeight() / 2; // -1 for the baseline
427 // draw the horizontal line
428 if (!category.isEmpty()) {
429 painter->drawLine(opt.rect.x(), ymid, left - 1, ymid);
430 painter->drawLine(right + 1, ymid, opt.rect.right(), ymid);
432 painter->drawLine(opt.rect.x(), ymid, opt.rect.right(), ymid);
439 QString underlineFilter(QString const & s) const
442 GuiLayoutBox * p = static_cast<GuiLayoutBox *>(parent());
443 QString const & f = p->filter();
447 // step through data item and put "(x)" for every matching character
451 for (int i = 0; i < f.length(); ++i) {
452 int p = s.indexOf(f[i], lastp + 1, Qt::CaseInsensitive);
453 BOOST_ASSERT(p != -1);
454 if (lastp == p - 1 && lastp != -1) {
455 // remove ")" and append "x)"
456 r = r.left(r.length() - 4) + s[p] + "</u>";
459 r += s.mid(lastp + 1, p - lastp - 1);
460 r += QString("<u>") + s[p] + "</u>";
464 r += s.mid(lastp + 1);
469 QStyleOptionMenuItem getStyleOption(QStyleOptionViewItem const & option,
470 QModelIndex const & index) const
472 QComboBox * combo = static_cast<QComboBox *>(parent());
474 // create the options for a menu item
475 QStyleOptionMenuItem menuOption;
476 menuOption.palette = QApplication::palette("QMenu");
477 menuOption.state = QStyle::State_Active | QStyle::State_Enabled;
478 menuOption.font = combo->font();
479 menuOption.fontMetrics = QFontMetrics(menuOption.font);
480 menuOption.rect = option.rect;
481 menuOption.rect.setHeight(menuOption.fontMetrics.height());
482 menuOption.menuRect = menuOption.rect;
483 menuOption.tabWidth = 0;
484 menuOption.text = index.model()->data(index, Qt::DisplayRole).toString()
485 .replace(QLatin1Char('&'), QLatin1String("&&"));
486 menuOption.menuItemType = QStyleOptionMenuItem::Normal;
487 if (option.state & QStyle::State_Selected)
488 menuOption.state |= QStyle::State_Selected;
489 menuOption.checkType = QStyleOptionMenuItem::NotCheckable;
490 menuOption.checked = false;
496 class GuiLayoutFilterModel : public QSortFilterProxyModel {
499 GuiLayoutFilterModel(QObject * parent = 0)
500 : QSortFilterProxyModel(parent)
504 void triggerLayoutChange()
506 layoutAboutToBeChanged();
512 GuiLayoutBox::GuiLayoutBox(GuiView & owner)
513 : owner_(owner), lastSel_(-1), layoutItemDelegate_(new LayoutItemDelegate(this)),
514 visibleCategories_(0), inShowPopup_(false)
516 setSizeAdjustPolicy(QComboBox::AdjustToContents);
517 setFocusPolicy(Qt::ClickFocus);
518 setMinimumWidth(sizeHint().width());
519 setMaxVisibleItems(100);
521 // set the layout model with two columns
522 // 1st: translated layout names
523 // 2nd: raw layout names
524 model_ = new QStandardItemModel(0, 2, this);
525 filterModel_ = new GuiLayoutFilterModel(this);
526 filterModel_->setSourceModel(model_);
527 setModel(filterModel_);
529 // for the filtering we have to intercept characters
530 view()->installEventFilter(this);
531 view()->setItemDelegateForColumn(0, layoutItemDelegate_);
533 QObject::connect(this, SIGNAL(activated(int)),
534 this, SLOT(selected(int)));
535 owner_.setLayoutDialog(this);
536 updateContents(true);
540 void GuiLayoutBox::setFilter(QString const & s)
543 owner_.message(_("Filtering layouts with \"" + fromqstr(s) + "\". "
544 "Press ESC to remove filter."));
546 owner_.message(_("Enter characters to filter the layout list."));
548 bool enabled = view()->updatesEnabled();
549 view()->setUpdatesEnabled(false);
551 // remember old selection
552 int sel = currentIndex();
554 lastSel_ = filterModel_->mapToSource(filterModel_->index(sel, 0)).row();
557 filterModel_->setFilterRegExp(charFilterRegExp(filter_));
560 // restore old selection
561 if (lastSel_ != -1) {
562 QModelIndex i = filterModel_->mapFromSource(model_->index(lastSel_, 0));
564 setCurrentIndex(i.row());
567 // Workaround to resize to content size
568 // FIXME: There must be a better way. The QComboBox::AdjustToContents)
570 if (view()->isVisible()) {
571 // call QComboBox::showPopup. But set the inShowPopup_ flag to switch on
572 // the hack in the item delegate to make space for the headers.
573 // We do not call our implementation of showPopup because that
574 // would reset the filter again. This is only needed if the user clicks
576 BOOST_ASSERT(!inShowPopup_);
578 QComboBox::showPopup();
579 inShowPopup_ = false;
581 // The item delegate hack is off again. So trigger a relayout of the popup.
582 filterModel_->triggerLayoutChange();
585 view()->setUpdatesEnabled(enabled);
589 void GuiLayoutBox::countCategories()
591 int n = filterModel_->rowCount();
592 visibleCategories_ = 0;
593 if (n == 0 || !lyxrc.group_layouts)
596 // skip the "Standard" category
597 QString prevCat = model_->index(0, 2).data().toString();
600 for (int i = 0; i < n; ++i) {
601 QString cat = filterModel_->index(i, 2).data().toString();
603 ++visibleCategories_;
609 QString GuiLayoutBox::charFilterRegExp(QString const & filter)
612 for (int i = 0; i < filter.length(); ++i) {
615 re += ".*[" + QRegExp::escape(c) + QRegExp::escape(c.toUpper()) + "]";
617 re += ".*" + QRegExp::escape(c);
623 void GuiLayoutBox::resetFilter()
625 setFilter(QString());
629 void GuiLayoutBox::showPopup()
631 owner_.message(_("Enter characters to filter the layout list."));
633 bool enabled = view()->updatesEnabled();
634 view()->setUpdatesEnabled(false);
638 // call QComboBox::showPopup. But set the inShowPopup_ flag to switch on
639 // the hack in the item delegate to make space for the headers.
640 BOOST_ASSERT(!inShowPopup_);
642 QComboBox::showPopup();
643 inShowPopup_ = false;
645 // The item delegate hack is off again. So trigger a relayout of the popup.
646 filterModel_->triggerLayoutChange();
648 view()->setUpdatesEnabled(enabled);
652 bool GuiLayoutBox::eventFilter(QObject * o, QEvent * e)
654 if (e->type() != QEvent::KeyPress)
655 return QComboBox::eventFilter(o, e);
657 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
658 bool modified = (ke->modifiers() == Qt::ControlModifier)
659 || (ke->modifiers() == Qt::AltModifier)
660 || (ke->modifiers() == Qt::MetaModifier);
664 if (!modified && !filter_.isEmpty()) {
669 case Qt::Key_Backspace:
671 // cut off one character
672 setFilter(filter_.left(filter_.length() - 1));
676 if (modified || ke->text().isEmpty())
678 // find chars for the filter string
680 for (int i = 0; i < ke->text().length(); ++i) {
681 QChar c = ke->text()[i];
682 if (c.isLetterOrNumber()
685 || c.category() == QChar::Separator_Space) {
690 // append new chars to the filter string
691 setFilter(filter_ + s);
697 return QComboBox::eventFilter(o, e);
701 void GuiLayoutBox::set(docstring const & layout)
708 QString const & name = toqstr((*text_class_)[layout].name());
709 if (name == currentText())
712 QList<QStandardItem *> r = model_->findItems(name, Qt::MatchExactly, 1);
714 lyxerr << "Trying to select non existent layout type "
715 << fromqstr(name) << endl;
719 setCurrentIndex(filterModel_->mapFromSource(r.first()->index()).row());
723 void GuiLayoutBox::addItemSort(docstring const & item, docstring const & category,
724 bool sorted, bool sortedByCat)
726 QString qitem = toqstr(item);
727 QString titem = toqstr(translateIfPossible(item));
728 QString qcat = toqstr(translateIfPossible(category));
730 QList<QStandardItem *> row;
731 row.append(new QStandardItem(titem));
732 row.append(new QStandardItem(qitem));
733 row.append(new QStandardItem(qcat));
735 // the first entry is easy
736 int const end = model_->rowCount();
738 model_->appendRow(row);
745 while (i < end && model_->item(i, 2)->text() != qcat)
749 // skip the Standard layout
753 // the simple unsorted case
756 // jump to the end of the category group
757 while (i < end && model_->item(i, 2)->text() == qcat)
759 model_->insertRow(i, row);
761 model_->appendRow(row);
765 // find row to insert the item, after the separator if it exists
767 // find alphabetic position
769 && model_->item(i, 0)->text().compare(titem) < 0
770 && (!sortedByCat || model_->item(i, 2)->text() == qcat))
774 model_->insertRow(i, row);
778 void GuiLayoutBox::updateContents(bool reset)
782 Buffer const * buffer = owner_.buffer();
791 // we'll only update the layout list if the text class has changed
792 // or we've moved from one inset to another
793 DocumentClass const * text_class = &buffer->params().documentClass();
794 Inset const * inset =
795 owner_.view()->cursor().innerParagraph().inInset();
796 if (!reset && text_class_ == text_class && inset_ == inset) {
797 set(owner_.view()->cursor().innerParagraph().layout().name());
802 text_class_ = text_class;
805 DocumentClass::const_iterator lit = text_class_->begin();
806 DocumentClass::const_iterator len = text_class_->end();
807 for (; lit != len; ++lit) {
808 docstring const & name = lit->name();
809 // if this inset requires the empty layout, we skip the default
811 if (name == text_class_->defaultLayoutName() && inset &&
812 (inset->forceEmptyLayout() || inset->useEmptyLayout()))
814 // if it doesn't require the empty layout, we skip it
815 if (name == text_class_->emptyLayoutName() && inset &&
816 !inset->forceEmptyLayout() && !inset->useEmptyLayout())
818 addItemSort(name, lit->category(), lyxrc.sort_layouts, lyxrc.group_layouts);
821 set(owner_.view()->cursor().innerParagraph().layout().name());
824 // needed to recalculate size hint
826 setMinimumWidth(sizeHint().width());
827 setEnabled(!buffer->isReadonly());
832 void GuiLayoutBox::selected(int index)
835 QModelIndex mindex = filterModel_->mapToSource(filterModel_->index(index, 1));
836 docstring const layoutName =
837 qstring_to_ucs4(model_->itemFromIndex(mindex)->text());
842 updateContents(false);
847 // find corresponding text class
848 if (text_class_->hasLayout(layoutName)) {
849 FuncRequest const func(LFUN_LAYOUT, layoutName, FuncRequest::TOOLBAR);
850 theLyXFunc().setLyXView(&owner_);
852 updateContents(false);
856 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
861 /////////////////////////////////////////////////////////////////////
865 /////////////////////////////////////////////////////////////////////
868 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
869 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
870 layout_(0), command_buffer_(0)
872 // give visual separation between adjacent toolbars
875 // TODO: save toolbar position
878 ToolbarInfo::item_iterator it = tbinfo.items.begin();
879 ToolbarInfo::item_iterator end = tbinfo.items.end();
880 for (; it != end; ++it)
885 Action * GuiToolbar::addItem(ToolbarItem const & item)
887 Action * act = new Action(owner_,
888 getIcon(item.func_, false),
889 toqstr(item.label_), item.func_, toqstr(item.label_));
890 actions_.append(act);
896 class PaletteButton : public QToolButton
900 ToolbarItem const & tbitem_;
903 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
904 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
906 QString const label = qt_(to_ascii(tbitem_.label_));
910 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
911 this, SLOT(setIconSize(QSize)));
913 ToolbarInfo const * tbinfo =
914 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
916 // use the icon of first action for the toolbar button
917 setIcon(getIcon(tbinfo->items.begin()->func_, true));
920 void mousePressEvent(QMouseEvent * e)
923 QToolButton::mousePressEvent(e);
929 ToolbarInfo const * tbinfo =
930 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
932 lyxerr << "Unknown toolbar " << tbitem_.name_ << endl;
935 IconPalette * panel = new IconPalette(this);
936 QString const label = qt_(to_ascii(tbitem_.label_));
937 panel->setWindowTitle(label);
938 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
939 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
940 ToolbarInfo::item_iterator it = tbinfo->items.begin();
941 ToolbarInfo::item_iterator const end = tbinfo->items.end();
942 for (; it != end; ++it)
943 if (!getStatus(it->func_).unknown())
944 panel->addButton(bar_->addItem(*it));
946 QToolButton::mousePressEvent(e);
950 class MenuButton : public QToolButton
954 ToolbarItem const & tbitem_;
957 MenuButton(GuiToolbar * bar, ToolbarItem const & item)
958 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
960 setPopupMode(QToolButton::InstantPopup);
961 QString const label = qt_(to_ascii(tbitem_.label_));
965 setIcon(QPixmap(":images/math/" + toqstr(tbitem_.name_) + ".png"));
966 connect(bar, SIGNAL(iconSizeChanged(QSize)),
967 this, SLOT(setIconSize(QSize)));
970 void mousePressEvent(QMouseEvent * e)
973 QToolButton::mousePressEvent(e);
979 QString const label = qt_(to_ascii(tbitem_.label_));
980 ButtonMenu * m = new ButtonMenu(label, this);
981 m->setWindowTitle(label);
982 m->setTearOffEnabled(true);
983 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
984 ToolbarInfo const * tbinfo =
985 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
987 lyxerr << "Unknown toolbar " << tbitem_.name_ << endl;
990 ToolbarInfo::item_iterator it = tbinfo->items.begin();
991 ToolbarInfo::item_iterator const end = tbinfo->items.end();
992 for (; it != end; ++it)
993 if (!getStatus(it->func_).unknown())
994 m->add(bar_->addItem(*it));
997 QToolButton::mousePressEvent(e);
1004 void GuiToolbar::add(ToolbarItem const & item)
1006 switch (item.type_) {
1007 case ToolbarItem::SEPARATOR:
1010 case ToolbarItem::LAYOUTS:
1011 layout_ = new GuiLayoutBox(owner_);
1014 case ToolbarItem::MINIBUFFER:
1015 command_buffer_ = new GuiCommandBuffer(&owner_);
1016 addWidget(command_buffer_);
1017 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
1018 //setHorizontalStretchable(true);
1020 case ToolbarItem::TABLEINSERT: {
1021 QToolButton * tb = new QToolButton;
1022 tb->setCheckable(true);
1023 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
1024 QString const label = qt_(to_ascii(item.label_));
1025 tb->setToolTip(label);
1026 tb->setStatusTip(label);
1028 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
1029 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
1030 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
1031 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
1035 case ToolbarItem::ICONPALETTE:
1036 addWidget(new PaletteButton(this, item));
1039 case ToolbarItem::POPUPMENU: {
1040 addWidget(new MenuButton(this, item));
1043 case ToolbarItem::COMMAND: {
1044 if (!getStatus(item.func_).unknown())
1045 addAction(addItem(item));
1054 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
1056 // if tbinfo.state == auto *do not* set on/off
1057 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
1058 if (GuiToolbar::isVisible())
1059 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
1061 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
1064 // no need to save it here.
1065 Qt::ToolBarArea loc = owner_.toolBarArea(this);
1067 if (loc == Qt::TopToolBarArea)
1068 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
1069 else if (loc == Qt::BottomToolBarArea)
1070 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
1071 else if (loc == Qt::RightToolBarArea)
1072 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
1073 else if (loc == Qt::LeftToolBarArea)
1074 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
1076 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
1078 // save toolbar position. They are not used to restore toolbar position
1079 // now because move(x,y) does not work for toolbar.
1080 tbinfo.posx = pos().x();
1081 tbinfo.posy = pos().y();
1085 void GuiToolbar::updateContents()
1087 // update visible toolbars only
1090 // This is a speed bottleneck because this is called on every keypress
1091 // and update calls getStatus, which copies the cursor at least two times
1092 for (int i = 0; i < actions_.size(); ++i)
1093 actions_[i]->update();
1096 layout_->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
1103 } // namespace frontend
1106 #include "GuiToolbar_moc.cpp"