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>
62 #include <QToolButton>
65 #include <boost/assert.hpp>
71 using namespace lyx::support;
73 static void initializeResources()
75 static bool initialized = false;
77 Q_INIT_RESOURCE(Resources);
94 bool operator<(PngMap const & lhs, PngMap const & rhs)
96 return lhs.key < rhs.key;
102 CompareKey(QString const & name) : name_(name) {}
103 bool operator()(PngMap const & other) const { return other.key == name_; }
109 PngMap sorted_png_map[] = {
110 { "Bumpeq", "bumpeq2" },
113 { "Delta", "delta2" },
114 { "Downarrow", "downarrow2" },
115 { "Gamma", "gamma2" },
116 { "Lambda", "lambda2" },
117 { "Leftarrow", "leftarrow2" },
118 { "Leftrightarrow", "leftrightarrow2" },
119 { "Longleftarrow", "longleftarrow2" },
120 { "Longleftrightarrow", "longleftrightarrow2" },
121 { "Longrightarrow", "longrightarrow2" },
122 { "Omega", "omega2" },
126 { "Rightarrow", "rightarrow2" },
127 { "Sigma", "sigma2" },
128 { "Subset", "subset2" },
129 { "Supset", "supset2" },
130 { "Theta", "theta2" },
131 { "Uparrow", "uparrow2" },
132 { "Updownarrow", "updownarrow2" },
133 { "Upsilon", "upsilon2" },
134 { "Vdash", "vdash3" },
136 { "nLeftarrow", "nleftarrow2" },
137 { "nLeftrightarrow", "nleftrightarrow2" },
138 { "nRightarrow", "nrightarrow2" },
139 { "nVDash", "nvdash3" },
140 { "nvDash", "nvdash2" },
141 { "textrm \\AA", "textrm_AA"},
142 { "textrm \\O", "textrm_O"},
143 { "vDash", "vdash2" }
146 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
149 QString findPng(QString const & name)
151 PngMap const * const begin = sorted_png_map;
152 PngMap const * const end = begin + nr_sorted_png_map;
153 BOOST_ASSERT(sorted(begin, end));
155 PngMap const * const it = find_if(begin, end, CompareKey(name));
159 png_name = it->value;
162 png_name.replace('_', "underscore");
163 png_name.replace(' ', '_');
165 // This way we can have "math-delim { }" on the toolbar.
166 png_name.replace('(', "lparen");
167 png_name.replace(')', "rparen");
168 png_name.replace('[', "lbracket");
169 png_name.replace(']', "rbracket");
170 png_name.replace('{', "lbrace");
171 png_name.replace('}', "rbrace");
172 png_name.replace('|', "bars");
173 png_name.replace(',', "thinspace");
174 png_name.replace(':', "mediumspace");
175 png_name.replace(';', "thickspace");
176 png_name.replace('!', "negthinspace");
179 LYXERR(Debug::GUI, "findPng(" << fromqstr(name) << ")\n"
180 << "Looking for math PNG called \"" << fromqstr(png_name) << '"');
187 /// return a icon for the given action
188 static QIcon getIcon(FuncRequest const & f, bool unknown)
190 initializeResources();
196 case LFUN_MATH_INSERT:
197 if (!f.argument().empty()) {
199 name1 = findPng(toqstr(f.argument()).mid(1));
202 case LFUN_MATH_DELIM:
203 case LFUN_MATH_BIGDELIM:
205 name1 = findPng(toqstr(f.argument()));
209 name1 = toqstr(f.argument());
212 name2 = toqstr(lyxaction.getActionName(f.action));
215 if (!f.argument().empty()) {
216 name1 = name2 + ' ' + toqstr(f.argument());
217 name1.replace(' ', '_');
221 string fullname = libFileSearch("images/" + path, name1, "png").absFilename();
222 if (pm.load(toqstr(fullname)))
225 fullname = libFileSearch("images/" + path, name2, "png").absFilename();
226 if (pm.load(toqstr(fullname)))
229 if (pm.load(":/images/" + path + name1 + ".png"))
232 if (pm.load(":/images/" + path + name2 + ".png"))
235 LYXERR(Debug::GUI, "Cannot find icon for command \""
236 << lyxaction.getActionName(f.action)
237 << '(' << to_utf8(f.argument()) << ")\"");
239 pm.load(":/images/unknown.png");
245 /////////////////////////////////////////////////////////////////////
249 /////////////////////////////////////////////////////////////////////
251 class LayoutItemDelegate : public QAbstractItemDelegate {
254 explicit LayoutItemDelegate(QObject * parent = 0)
255 : QAbstractItemDelegate(parent)
259 void paint(QPainter * painter, QStyleOptionViewItem const & option,
260 QModelIndex const & index) const
262 QComboBox * combo = static_cast<QComboBox *>(parent());
263 QSortFilterProxyModel const * model
264 = static_cast<QSortFilterProxyModel const *>(index.model());
265 QStyleOptionMenuItem opt = getStyleOption(option, index);
267 painter->eraseRect(opt.rect);
269 QString text = underlineFilter(opt.text);
270 opt.text = QString();
273 if (lyxrc.group_layouts) {
274 QString stdCat = category(*model->sourceModel(), 0);
275 QString cat = category(*index.model(), index.row());
277 // not the standard layout and not the same as in the previous line?
279 && (index.row() == 0 || cat != category(*index.model(), index.row() - 1))) {
280 // draw category header
281 paintBackground(painter, opt);
282 paintCategoryHeader(painter, opt,
283 category(*index.model(), index.row()));
285 QFontMetrics fm(opt.font);
286 opt.rect.setTop(opt.rect.top() + headerHeight(opt));
287 opt.menuRect = opt.rect;
291 // Draw using the menu item style (this is how QComboBox does it).
292 // But for the rich text drawing below we will call it with an
293 // empty string, and later then draw over it the real string.
295 combo->style()->drawControl(QStyle::CE_MenuItem, &opt, painter, combo->view());
298 // Draw the rich text.
300 QColor col = opt.palette.text().color();
301 if (opt.state & QStyle::State_Selected)
302 col = opt.palette.highlightedText().color();
303 QAbstractTextDocumentLayout::PaintContext context;
304 context.palette.setColor(QPalette::Text, col);
307 doc.setDefaultFont(opt.font);
309 painter->translate(opt.rect.x() + 5, opt.rect.y());
310 doc.documentLayout()->draw(painter, context);
315 QSize sizeHint(QStyleOptionViewItem const & option,
316 QModelIndex const & index) const
318 GuiLayoutBox * combo = static_cast<GuiLayoutBox *>(parent());
319 QSortFilterProxyModel const * model
320 = static_cast<QSortFilterProxyModel const *>(index.model());
321 QStyleOptionMenuItem opt = getStyleOption(option, index);
322 QSize size = combo->style()->sizeFromContents(
323 QStyle::CT_MenuItem, &opt, option.rect.size(), combo);
325 /// QComboBox uses the first row height to estimate the
326 /// complete popup height during QComboBox::showPopup().
327 /// To avoid scrolling we have to sneak in space for the headers.
328 /// So we tweak this value accordingly. It's not nice, but the
329 /// only possible way it seems.
330 if (lyxrc.group_layouts && index.row() == 0 && combo->inShowPopup_) {
331 int itemHeight = size.height();
333 // we have to show \c cats many headers:
334 unsigned cats = combo->visibleCategories_;
336 // and we have \c n items to distribute the needed space over
337 unsigned n = combo->model()->rowCount();
339 // so the needed average height (rounded upwards) is:
340 size.setHeight((headerHeight(opt) * cats + itemHeight * n + n - 1) / n);
344 // Add space for the category headers here?
345 // Not for the standard layout though.
346 QString stdCat = category(*model->sourceModel(), 0);
347 QString cat = category(*index.model(), index.row());
348 if (lyxrc.group_layouts && stdCat != cat
349 && (index.row() == 0 || cat != category(*index.model(), index.row() - 1))) {
350 size.setHeight(size.height() + headerHeight(opt));
358 QString category(QAbstractItemModel const & model, int row) const
360 return model.data(model.index(row, 2), Qt::DisplayRole).toString();
364 void paintBackground(QPainter * painter, QStyleOptionMenuItem const & opt) const
366 QComboBox * combo = static_cast<QComboBox *>(parent());
368 // we only want to paint a background using the style, so
369 // disable every thing else
370 QStyleOptionMenuItem sopt = opt;
371 sopt.menuRect = sopt.rect;
372 sopt.state = QStyle::State_Active | QStyle::State_Enabled;
373 sopt.checked = false;
374 sopt.text = QString();
377 combo->style()->drawControl(QStyle::CE_MenuItem, &sopt,
378 painter, combo->view());
383 int headerHeight(QStyleOptionMenuItem const & opt) const
385 QFontMetrics fm(opt.font);
386 return fm.height() * 8 / 10;
389 void paintCategoryHeader(QPainter * painter, QStyleOptionMenuItem const & opt,
390 QString const & category) const
394 // slightly blended color
395 QColor lcol = opt.palette.text().color();
397 painter->setPen(lcol);
399 // set 80% scaled, bold font
400 QFont font = opt.font;
402 font.setWeight(QFont::Black);
403 font.setPointSize(opt.font.pointSize() * 8 / 10);
404 painter->setFont(font);
406 // draw the centered text
407 QFontMetrics fm(font);
408 int w = fm.width(category);
409 int x = opt.rect.x() + (opt.rect.width() - w) / 2;
410 int y = opt.rect.y() + fm.ascent();
413 painter->drawText(x, y, category);
415 // the vertical position of the line: middle of lower case chars
416 int ymid = y - 1 - fm.xHeight() / 2; // -1 for the baseline
418 // draw the horizontal line
419 if (!category.isEmpty()) {
420 painter->drawLine(opt.rect.x(), ymid, left - 1, ymid);
421 painter->drawLine(right + 1, ymid, opt.rect.right(), ymid);
423 painter->drawLine(opt.rect.x(), ymid, opt.rect.right(), ymid);
430 QString underlineFilter(QString const & s) const
433 GuiLayoutBox * p = static_cast<GuiLayoutBox *>(parent());
434 QString const & f = p->filter();
438 // step through data item and put "(x)" for every matching character
442 for (int i = 0; i < f.length(); ++i) {
443 int p = s.indexOf(f[i], lastp + 1, Qt::CaseInsensitive);
444 BOOST_ASSERT(p != -1);
445 if (lastp == p - 1 && lastp != -1) {
446 // remove ")" and append "x)"
447 r = r.left(r.length() - 4) + s[p] + "</u>";
450 r += s.mid(lastp + 1, p - lastp - 1);
451 r += QString("<u>") + s[p] + "</u>";
455 r += s.mid(lastp + 1);
460 QStyleOptionMenuItem getStyleOption(QStyleOptionViewItem const & option,
461 QModelIndex const & index) const
463 QComboBox * combo = static_cast<QComboBox *>(parent());
465 // create the options for a menu item
466 QStyleOptionMenuItem menuOption;
467 menuOption.palette = QApplication::palette("QMenu");
468 menuOption.state = QStyle::State_Active | QStyle::State_Enabled;
469 menuOption.menuRect = option.rect;
470 menuOption.rect = option.rect;
471 menuOption.font = combo->font();
472 menuOption.fontMetrics = QFontMetrics(menuOption.font);
473 menuOption.tabWidth = 0;
474 menuOption.text = index.model()->data(index, Qt::DisplayRole).toString()
475 .replace(QLatin1Char('&'), QLatin1String("&&"));
476 menuOption.menuItemType = QStyleOptionMenuItem::Normal;
477 if (option.state & QStyle::State_Selected)
478 menuOption.state |= QStyle::State_Selected;
479 menuOption.checkType = QStyleOptionMenuItem::NotCheckable;
480 menuOption.checked = false;
486 class GuiLayoutFilterModel : public QSortFilterProxyModel {
489 GuiLayoutFilterModel(QObject * parent = 0)
490 : QSortFilterProxyModel(parent)
494 void triggerLayoutChange()
496 layoutAboutToBeChanged();
502 GuiLayoutBox::GuiLayoutBox(GuiView & owner)
503 : owner_(owner), lastSel_(-1), layoutItemDelegate_(new LayoutItemDelegate(this)),
504 visibleCategories_(0), inShowPopup_(false)
506 setSizeAdjustPolicy(QComboBox::AdjustToContents);
507 setFocusPolicy(Qt::ClickFocus);
508 setMinimumWidth(sizeHint().width());
509 setMaxVisibleItems(100);
511 // set the layout model with two columns
512 // 1st: translated layout names
513 // 2nd: raw layout names
514 model_ = new QStandardItemModel(0, 2, this);
515 filterModel_ = new GuiLayoutFilterModel(this);
516 filterModel_->setSourceModel(model_);
517 setModel(filterModel_);
519 // for the filtering we have to intercept characters
520 view()->installEventFilter(this);
521 view()->setItemDelegateForColumn(0, layoutItemDelegate_);
523 QObject::connect(this, SIGNAL(activated(int)),
524 this, SLOT(selected(int)));
525 owner_.setLayoutDialog(this);
526 updateContents(true);
530 void GuiLayoutBox::setFilter(QString const & s)
533 owner_.message(_("Filtering layouts with \"" + fromqstr(s) + "\". "
534 "Press ESC to remove filter."));
536 owner_.message(_("Enter characters to filter the layout list."));
538 bool enabled = view()->updatesEnabled();
539 view()->setUpdatesEnabled(false);
541 // remember old selection
542 int sel = currentIndex();
544 lastSel_ = filterModel_->mapToSource(filterModel_->index(sel, 0)).row();
547 filterModel_->setFilterRegExp(charFilterRegExp(filter_));
550 // restore old selection
551 if (lastSel_ != -1) {
552 QModelIndex i = filterModel_->mapFromSource(model_->index(lastSel_, 0));
554 setCurrentIndex(i.row());
557 // Workaround to resize to content size
558 // FIXME: There must be a better way. The QComboBox::AdjustToContents)
560 if (view()->isVisible()) {
561 // call QComboBox::showPopup. But set the inShowPopup_ flag to switch on
562 // the hack in the item delegate to make space for the headers.
563 // We do not call our implementation of showPopup because that
564 // would reset the filter again. This is only needed if the user clicks
566 BOOST_ASSERT(!inShowPopup_);
568 QComboBox::showPopup();
569 inShowPopup_ = false;
571 // The item delegate hack is off again. So trigger a relayout of the popup.
572 filterModel_->triggerLayoutChange();
575 view()->setUpdatesEnabled(enabled);
579 void GuiLayoutBox::countCategories()
581 int n = filterModel_->rowCount();
582 visibleCategories_ = 0;
583 if (n == 0 || !lyxrc.group_layouts)
586 // skip the "Standard" category
587 QString prevCat = model_->index(0, 2).data().toString();
590 for (int i = 0; i < n; ++i) {
591 QString cat = filterModel_->index(i, 2).data().toString();
593 ++visibleCategories_;
599 QString GuiLayoutBox::charFilterRegExp(QString const & filter)
602 for (int i = 0; i < filter.length(); ++i) {
605 re += ".*[" + QRegExp::escape(c) + QRegExp::escape(c.toUpper()) + "]";
607 re += ".*" + QRegExp::escape(c);
613 void GuiLayoutBox::resetFilter()
615 setFilter(QString());
619 void GuiLayoutBox::showPopup()
621 owner_.message(_("Enter characters to filter the layout list."));
623 bool enabled = view()->updatesEnabled();
624 view()->setUpdatesEnabled(false);
628 // call QComboBox::showPopup. But set the inShowPopup_ flag to switch on
629 // the hack in the item delegate to make space for the headers.
630 BOOST_ASSERT(!inShowPopup_);
632 QComboBox::showPopup();
633 inShowPopup_ = false;
635 // The item delegate hack is off again. So trigger a relayout of the popup.
636 filterModel_->triggerLayoutChange();
638 view()->setUpdatesEnabled(enabled);
642 bool GuiLayoutBox::eventFilter(QObject * o, QEvent * e)
644 if (e->type() != QEvent::KeyPress)
645 return QComboBox::eventFilter(o, e);
647 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
648 bool modified = (ke->modifiers() == Qt::ControlModifier)
649 || (ke->modifiers() == Qt::AltModifier)
650 || (ke->modifiers() == Qt::MetaModifier);
654 if (!modified && !filter_.isEmpty()) {
659 case Qt::Key_Backspace:
661 // cut off one character
662 setFilter(filter_.left(filter_.length() - 1));
666 if (modified || ke->text().isEmpty())
668 // find chars for the filter string
670 for (int i = 0; i < ke->text().length(); ++i) {
671 QChar c = ke->text()[i];
672 if (c.isLetterOrNumber()
675 || c.category() == QChar::Separator_Space) {
680 // append new chars to the filter string
681 setFilter(filter_ + s);
687 return QComboBox::eventFilter(o, e);
691 void GuiLayoutBox::set(docstring const & layout)
698 QString const & name = toqstr((*text_class_)[layout].name());
699 if (name == currentText())
702 QList<QStandardItem *> r = model_->findItems(name, Qt::MatchExactly, 1);
704 lyxerr << "Trying to select non existent layout type "
705 << fromqstr(name) << endl;
709 setCurrentIndex(filterModel_->mapFromSource(r.first()->index()).row());
713 void GuiLayoutBox::addItemSort(docstring const & item, docstring const & category,
714 bool sorted, bool sortedByCat)
716 QString qitem = toqstr(item);
717 QString titem = toqstr(translateIfPossible(item));
718 QString qcat = toqstr(translateIfPossible(category));
720 QList<QStandardItem *> row;
721 row.append(new QStandardItem(titem));
722 row.append(new QStandardItem(qitem));
723 row.append(new QStandardItem(qcat));
725 // the first entry is easy
726 int const end = model_->rowCount();
728 model_->appendRow(row);
735 while (i < end && model_->item(i, 2)->text() != qcat)
739 // skip the Standard layout
743 // the simple unsorted case
746 // jump to the end of the category group
747 while (i < end && model_->item(i, 2)->text() == qcat)
749 model_->insertRow(i, row);
751 model_->appendRow(row);
755 // find row to insert the item, after the separator if it exists
757 // find alphabetic position
759 && model_->item(i, 0)->text().compare(titem) < 0
760 && (!sortedByCat || model_->item(i, 2)->text() == qcat))
764 model_->insertRow(i, row);
768 void GuiLayoutBox::updateContents(bool reset)
772 Buffer const * buffer = owner_.buffer();
781 // we'll only update the layout list if the text class has changed
782 // or we've moved from one inset to another
783 DocumentClass const * text_class = &buffer->params().documentClass();
784 Inset const * inset =
785 owner_.view()->cursor().innerParagraph().inInset();
786 if (!reset && text_class_ == text_class && inset_ == inset) {
787 set(owner_.view()->cursor().innerParagraph().layout().name());
792 text_class_ = text_class;
795 DocumentClass::const_iterator lit = text_class_->begin();
796 DocumentClass::const_iterator len = text_class_->end();
797 for (; lit != len; ++lit) {
798 docstring const & name = lit->name();
799 // if this inset requires the empty layout, we skip the default
801 if (name == text_class_->defaultLayoutName() && inset &&
802 (inset->forceEmptyLayout() || inset->useEmptyLayout()))
804 // if it doesn't require the empty layout, we skip it
805 if (name == text_class_->emptyLayoutName() && inset &&
806 !inset->forceEmptyLayout() && !inset->useEmptyLayout())
808 addItemSort(name, lit->category(), lyxrc.sort_layouts, lyxrc.group_layouts);
811 set(owner_.view()->cursor().innerParagraph().layout().name());
814 // needed to recalculate size hint
816 setMinimumWidth(sizeHint().width());
817 setEnabled(!buffer->isReadonly());
822 void GuiLayoutBox::selected(int index)
825 QModelIndex mindex = filterModel_->mapToSource(filterModel_->index(index, 1));
826 docstring const layoutName =
827 qstring_to_ucs4(model_->itemFromIndex(mindex)->text());
832 updateContents(false);
837 // find corresponding text class
838 if (text_class_->hasLayout(layoutName)) {
839 FuncRequest const func(LFUN_LAYOUT, layoutName, FuncRequest::TOOLBAR);
840 theLyXFunc().setLyXView(&owner_);
842 updateContents(false);
846 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
851 /////////////////////////////////////////////////////////////////////
855 /////////////////////////////////////////////////////////////////////
858 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
859 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
860 layout_(0), command_buffer_(0)
862 // give visual separation between adjacent toolbars
865 // TODO: save toolbar position
868 ToolbarInfo::item_iterator it = tbinfo.items.begin();
869 ToolbarInfo::item_iterator end = tbinfo.items.end();
870 for (; it != end; ++it)
875 Action * GuiToolbar::addItem(ToolbarItem const & item)
877 Action * act = new Action(owner_,
878 getIcon(item.func_, false),
879 toqstr(item.label_), item.func_, toqstr(item.label_));
880 actions_.append(act);
886 class PaletteButton : public QToolButton
890 ToolbarItem const & tbitem_;
893 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
894 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
896 QString const label = qt_(to_ascii(tbitem_.label_));
900 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
901 this, SLOT(setIconSize(QSize)));
903 ToolbarInfo const * tbinfo =
904 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
906 // use the icon of first action for the toolbar button
907 setIcon(getIcon(tbinfo->items.begin()->func_, true));
910 void mousePressEvent(QMouseEvent * e)
913 QToolButton::mousePressEvent(e);
919 ToolbarInfo const * tbinfo =
920 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
922 lyxerr << "Unknown toolbar " << tbitem_.name_ << endl;
925 IconPalette * panel = new IconPalette(this);
926 QString const label = qt_(to_ascii(tbitem_.label_));
927 panel->setWindowTitle(label);
928 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
929 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
930 ToolbarInfo::item_iterator it = tbinfo->items.begin();
931 ToolbarInfo::item_iterator const end = tbinfo->items.end();
932 for (; it != end; ++it)
933 if (!getStatus(it->func_).unknown())
934 panel->addButton(bar_->addItem(*it));
936 QToolButton::mousePressEvent(e);
940 class MenuButton : public QToolButton
944 ToolbarItem const & tbitem_;
947 MenuButton(GuiToolbar * bar, ToolbarItem const & item)
948 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
950 setPopupMode(QToolButton::InstantPopup);
951 QString const label = qt_(to_ascii(tbitem_.label_));
955 setIcon(QPixmap(":images/math/" + toqstr(tbitem_.name_) + ".png"));
956 connect(bar, SIGNAL(iconSizeChanged(QSize)),
957 this, SLOT(setIconSize(QSize)));
960 void mousePressEvent(QMouseEvent * e)
963 QToolButton::mousePressEvent(e);
969 QString const label = qt_(to_ascii(tbitem_.label_));
970 ButtonMenu * m = new ButtonMenu(label, this);
971 m->setWindowTitle(label);
972 m->setTearOffEnabled(true);
973 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
974 ToolbarInfo const * tbinfo =
975 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
977 lyxerr << "Unknown toolbar " << tbitem_.name_ << endl;
980 ToolbarInfo::item_iterator it = tbinfo->items.begin();
981 ToolbarInfo::item_iterator const end = tbinfo->items.end();
982 for (; it != end; ++it)
983 if (!getStatus(it->func_).unknown())
984 m->add(bar_->addItem(*it));
987 QToolButton::mousePressEvent(e);
994 void GuiToolbar::add(ToolbarItem const & item)
996 switch (item.type_) {
997 case ToolbarItem::SEPARATOR:
1000 case ToolbarItem::LAYOUTS:
1001 layout_ = new GuiLayoutBox(owner_);
1004 case ToolbarItem::MINIBUFFER:
1005 command_buffer_ = new GuiCommandBuffer(&owner_);
1006 addWidget(command_buffer_);
1007 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
1008 //setHorizontalStretchable(true);
1010 case ToolbarItem::TABLEINSERT: {
1011 QToolButton * tb = new QToolButton;
1012 tb->setCheckable(true);
1013 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
1014 QString const label = qt_(to_ascii(item.label_));
1015 tb->setToolTip(label);
1016 tb->setStatusTip(label);
1018 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
1019 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
1020 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
1021 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
1025 case ToolbarItem::ICONPALETTE:
1026 addWidget(new PaletteButton(this, item));
1029 case ToolbarItem::POPUPMENU: {
1030 addWidget(new MenuButton(this, item));
1033 case ToolbarItem::COMMAND: {
1034 if (!getStatus(item.func_).unknown())
1035 addAction(addItem(item));
1044 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
1046 // if tbinfo.state == auto *do not* set on/off
1047 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
1048 if (GuiToolbar::isVisible())
1049 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
1051 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
1054 // no need to save it here.
1055 Qt::ToolBarArea loc = owner_.toolBarArea(this);
1057 if (loc == Qt::TopToolBarArea)
1058 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
1059 else if (loc == Qt::BottomToolBarArea)
1060 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
1061 else if (loc == Qt::RightToolBarArea)
1062 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
1063 else if (loc == Qt::LeftToolBarArea)
1064 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
1066 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
1068 // save toolbar position. They are not used to restore toolbar position
1069 // now because move(x,y) does not work for toolbar.
1070 tbinfo.posx = pos().x();
1071 tbinfo.posy = pos().y();
1075 void GuiToolbar::updateContents()
1077 // update visible toolbars only
1080 // This is a speed bottleneck because this is called on every keypress
1081 // and update calls getStatus, which copies the cursor at least two times
1082 for (int i = 0; i < actions_.size(); ++i)
1083 actions_[i]->update();
1086 layout_->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
1093 } // namespace frontend
1096 #include "GuiToolbar_moc.cpp"