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
45 #include <QAbstractItemDelegate>
46 #include <QAbstractTextDocumentLayout>
47 #include <QApplication>
49 #include <QFontMetrics>
50 #include <QHeaderView>
56 #include <QSortFilterProxyModel>
57 #include <QStandardItem>
58 #include <QStandardItemModel>
59 #include <QTextDocument>
61 #include <QToolButton>
64 #include <boost/assert.hpp>
70 using namespace lyx::support;
72 static void initializeResources()
74 static bool initialized = false;
76 Q_INIT_RESOURCE(Resources);
93 bool operator<(PngMap const & lhs, PngMap const & rhs)
95 return lhs.key < rhs.key;
101 CompareKey(QString const & name) : name_(name) {}
102 bool operator()(PngMap const & other) const { return other.key == name_; }
108 PngMap sorted_png_map[] = {
109 { "Bumpeq", "bumpeq2" },
112 { "Delta", "delta2" },
113 { "Downarrow", "downarrow2" },
114 { "Gamma", "gamma2" },
115 { "Lambda", "lambda2" },
116 { "Leftarrow", "leftarrow2" },
117 { "Leftrightarrow", "leftrightarrow2" },
118 { "Longleftarrow", "longleftarrow2" },
119 { "Longleftrightarrow", "longleftrightarrow2" },
120 { "Longrightarrow", "longrightarrow2" },
121 { "Omega", "omega2" },
125 { "Rightarrow", "rightarrow2" },
126 { "Sigma", "sigma2" },
127 { "Subset", "subset2" },
128 { "Supset", "supset2" },
129 { "Theta", "theta2" },
130 { "Uparrow", "uparrow2" },
131 { "Updownarrow", "updownarrow2" },
132 { "Upsilon", "upsilon2" },
133 { "Vdash", "vdash3" },
135 { "nLeftarrow", "nleftarrow2" },
136 { "nLeftrightarrow", "nleftrightarrow2" },
137 { "nRightarrow", "nrightarrow2" },
138 { "nVDash", "nvdash3" },
139 { "nvDash", "nvdash2" },
140 { "textrm \\AA", "textrm_AA"},
141 { "textrm \\O", "textrm_O"},
142 { "vDash", "vdash2" }
145 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
148 QString findPng(QString const & name)
150 PngMap const * const begin = sorted_png_map;
151 PngMap const * const end = begin + nr_sorted_png_map;
152 BOOST_ASSERT(sorted(begin, end));
154 PngMap const * const it = find_if(begin, end, CompareKey(name));
158 png_name = it->value;
161 png_name.replace('_', "underscore");
162 png_name.replace(' ', '_');
164 // This way we can have "math-delim { }" on the toolbar.
165 png_name.replace('(', "lparen");
166 png_name.replace(')', "rparen");
167 png_name.replace('[', "lbracket");
168 png_name.replace(']', "rbracket");
169 png_name.replace('{', "lbrace");
170 png_name.replace('}', "rbrace");
171 png_name.replace('|', "bars");
172 png_name.replace(',', "thinspace");
173 png_name.replace(':', "mediumspace");
174 png_name.replace(';', "thickspace");
175 png_name.replace('!', "negthinspace");
178 LYXERR(Debug::GUI, "findPng(" << fromqstr(name) << ")\n"
179 << "Looking for math PNG called \"" << fromqstr(png_name) << '"');
186 /// return a icon for the given action
187 static QIcon getIcon(FuncRequest const & f, bool unknown)
189 initializeResources();
195 case LFUN_MATH_INSERT:
196 if (!f.argument().empty()) {
198 name1 = findPng(toqstr(f.argument()).mid(1));
201 case LFUN_MATH_DELIM:
202 case LFUN_MATH_BIGDELIM:
204 name1 = findPng(toqstr(f.argument()));
208 name1 = toqstr(f.argument());
211 name2 = toqstr(lyxaction.getActionName(f.action));
214 if (!f.argument().empty()) {
215 name1 = name2 + ' ' + toqstr(f.argument());
216 name1.replace(' ', '_');
220 string fullname = libFileSearch("images/" + path, name1, "png").absFilename();
221 if (pm.load(toqstr(fullname)))
224 fullname = libFileSearch("images/" + path, name2, "png").absFilename();
225 if (pm.load(toqstr(fullname)))
228 if (pm.load(":/images/" + path + name1 + ".png"))
231 if (pm.load(":/images/" + path + name2 + ".png"))
234 LYXERR(Debug::GUI, "Cannot find icon for command \""
235 << lyxaction.getActionName(f.action)
236 << '(' << to_utf8(f.argument()) << ")\"");
238 pm.load(":/images/unknown.png");
244 /////////////////////////////////////////////////////////////////////
248 /////////////////////////////////////////////////////////////////////
250 class LayoutItemDelegate : public QAbstractItemDelegate {
253 explicit LayoutItemDelegate(QObject * parent = 0)
254 : QAbstractItemDelegate(parent)
258 void paint(QPainter * painter, QStyleOptionViewItem const & option,
259 QModelIndex const & index) const
261 QComboBox * combo = static_cast<QComboBox *>(parent());
262 QSortFilterProxyModel const * model
263 = static_cast<QSortFilterProxyModel const *>(index.model());
264 QStyleOptionMenuItem opt = getStyleOption(option, index);
266 painter->eraseRect(opt.rect);
268 QString text = underlineFilter(opt.text);
269 opt.text = QString();
272 if (lyxrc.group_layouts) {
273 QString stdCat = category(*model->sourceModel(), 0);
274 QString cat = category(*index.model(), index.row());
276 // not the standard layout and not the same as in the previous line?
278 && (index.row() == 0 || cat != category(*index.model(), index.row() - 1))) {
279 // draw category header
280 paintBackground(painter, opt);
281 paintCategoryHeader(painter, opt,
282 category(*index.model(), index.row()));
284 QFontMetrics fm(opt.font);
285 opt.rect.setTop(opt.rect.top() + headerHeight(opt));
286 opt.menuRect = opt.rect;
290 // Draw using the menu item style (this is how QComboBox does it).
291 // But for the rich text drawing below we will call it with an
292 // empty string, and later then draw over it the real string.
294 combo->style()->drawControl(QStyle::CE_MenuItem, &opt, painter, combo->view());
297 // Draw the rich text.
299 QColor col = opt.palette.text().color();
300 if (opt.state & QStyle::State_Selected)
301 col = opt.palette.highlightedText().color();
302 QAbstractTextDocumentLayout::PaintContext context;
303 context.palette.setColor(QPalette::Text, col);
306 doc.setDefaultFont(opt.font);
308 painter->translate(opt.rect.x() + 5, opt.rect.y());
309 doc.documentLayout()->draw(painter, context);
314 QSize sizeHint(QStyleOptionViewItem const & option,
315 QModelIndex const & index) const
317 GuiLayoutBox * combo = static_cast<GuiLayoutBox *>(parent());
318 QSortFilterProxyModel const * model
319 = static_cast<QSortFilterProxyModel const *>(index.model());
320 QStyleOptionMenuItem opt = getStyleOption(option, index);
321 QSize size = combo->style()->sizeFromContents(
322 QStyle::CT_MenuItem, &opt, option.rect.size(), combo);
324 /// QComboBox uses the first row height to estimate the
325 /// complete popup height during QComboBox::showPopup().
326 /// To avoid scrolling we have to sneak in space for the headers.
327 /// So we tweak this value accordingly. It's not nice, but the
328 /// only possible way it seems.
329 if (lyxrc.group_layouts && index.row() == 0 && combo->inShowPopup_) {
330 int itemHeight = size.height();
332 // we have to show \c cats many headers:
333 unsigned cats = combo->visibleCategories_;
335 // and we have \c n items to distribute the needed space over
336 unsigned n = combo->model()->rowCount();
338 // so the needed average height (rounded upwards) is:
339 size.setHeight((headerHeight(opt) * cats + itemHeight * n + n - 1) / n);
343 // Add space for the category headers here?
344 // Not for the standard layout though.
345 QString stdCat = category(*model->sourceModel(), 0);
346 QString cat = category(*index.model(), index.row());
347 if (lyxrc.group_layouts && stdCat != cat
348 && (index.row() == 0 || cat != category(*index.model(), index.row() - 1))) {
349 size.setHeight(size.height() + headerHeight(opt));
357 QString category(QAbstractItemModel const & model, int row) const
359 return model.data(model.index(row, 2), Qt::DisplayRole).toString();
363 void paintBackground(QPainter * painter, QStyleOptionMenuItem const & opt) const
365 QComboBox * combo = static_cast<QComboBox *>(parent());
367 // we only want to paint a background using the style, so
368 // disable every thing else
369 QStyleOptionMenuItem sopt = opt;
370 sopt.menuRect = sopt.rect;
371 sopt.state = QStyle::State_Active | QStyle::State_Enabled;
372 sopt.checked = false;
373 sopt.text = QString();
376 combo->style()->drawControl(QStyle::CE_MenuItem, &sopt,
377 painter, combo->view());
382 int headerHeight(QStyleOptionMenuItem const & opt) const
384 QFontMetrics fm(opt.font);
385 return fm.height() * 8 / 10;
388 void paintCategoryHeader(QPainter * painter, QStyleOptionMenuItem const & opt,
389 QString const & category) const
393 // slightly blended color
394 QColor lcol = opt.palette.text().color();
396 painter->setPen(lcol);
398 // set 80% scaled, bold font
399 QFont font = opt.font;
401 font.setWeight(QFont::Black);
402 font.setPointSize(opt.font.pointSize() * 8 / 10);
403 painter->setFont(font);
405 // draw the centered text
406 QFontMetrics fm(font);
407 int w = fm.width(category);
408 int x = opt.rect.x() + (opt.rect.width() - w) / 2;
409 int y = opt.rect.y() + fm.ascent();
412 painter->drawText(x, y, category);
414 // the vertical position of the line: middle of lower case chars
415 int ymid = y - 1 - fm.xHeight() / 2; // -1 for the baseline
417 // draw the horizontal line
418 painter->drawLine(opt.rect.x(), ymid, left - 1, ymid);
419 painter->drawLine(right + 1, ymid, opt.rect.right(), ymid);
426 QString underlineFilter(QString const & s) const
429 GuiLayoutBox * p = static_cast<GuiLayoutBox *>(parent());
430 QString const & f = p->filter();
434 // step through data item and put "(x)" for every matching character
438 for (int i = 0; i < f.length(); ++i) {
439 int p = s.indexOf(f[i], lastp + 1, Qt::CaseInsensitive);
440 BOOST_ASSERT(p != -1);
441 if (lastp == p - 1 && lastp != -1) {
442 // remove ")" and append "x)"
443 r = r.left(r.length() - 4) + s[p] + "</u>";
446 r += s.mid(lastp + 1, p - lastp - 1);
447 r += QString("<u>") + s[p] + "</u>";
451 r += s.mid(lastp + 1);
456 QStyleOptionMenuItem getStyleOption(QStyleOptionViewItem const & option,
457 QModelIndex const & index) const
459 QComboBox * combo = static_cast<QComboBox *>(parent());
461 // create the options for a menu item
462 QStyleOptionMenuItem menuOption;
463 menuOption.palette = QApplication::palette("QMenu");
464 menuOption.state = QStyle::State_Active | QStyle::State_Enabled;
465 menuOption.menuRect = option.rect;
466 menuOption.rect = option.rect;
467 menuOption.font = combo->font();
468 menuOption.fontMetrics = QFontMetrics(menuOption.font);
469 menuOption.tabWidth = 0;
470 menuOption.text = index.model()->data(index, Qt::DisplayRole).toString()
471 .replace(QLatin1Char('&'), QLatin1String("&&"));
472 menuOption.menuItemType = QStyleOptionMenuItem::Normal;
473 if (option.state & QStyle::State_Selected)
474 menuOption.state |= QStyle::State_Selected;
475 menuOption.checkType = QStyleOptionMenuItem::NotCheckable;
476 menuOption.checked = false;
482 class GuiLayoutFilterModel : public QSortFilterProxyModel {
485 GuiLayoutFilterModel(QObject * parent = 0)
486 : QSortFilterProxyModel(parent)
490 void triggerLayoutChange()
492 layoutAboutToBeChanged();
498 GuiLayoutBox::GuiLayoutBox(GuiView & owner)
499 : owner_(owner), lastSel_(-1), layoutItemDelegate_(new LayoutItemDelegate(this)),
500 visibleCategories_(0), inShowPopup_(false)
502 setSizeAdjustPolicy(QComboBox::AdjustToContents);
503 setFocusPolicy(Qt::ClickFocus);
504 setMinimumWidth(sizeHint().width());
505 setMaxVisibleItems(100);
507 // set the layout model with two columns
508 // 1st: translated layout names
509 // 2nd: raw layout names
510 model_ = new QStandardItemModel(0, 2, this);
511 filterModel_ = new GuiLayoutFilterModel(this);
512 filterModel_->setSourceModel(model_);
513 setModel(filterModel_);
515 // for the filtering we have to intercept characters
516 view()->installEventFilter(this);
517 view()->setItemDelegateForColumn(0, layoutItemDelegate_);
519 QObject::connect(this, SIGNAL(activated(int)),
520 this, SLOT(selected(int)));
521 owner_.setLayoutDialog(this);
522 updateContents(true);
526 void GuiLayoutBox::setFilter(QString const & s)
529 owner_.message(_("Filtering layouts with \"" + fromqstr(s) + "\". "
530 "Press ESC to remove filter."));
532 owner_.message(_("Enter characters to filter the layout list."));
534 bool enabled = view()->updatesEnabled();
535 view()->setUpdatesEnabled(false);
537 // remember old selection
538 int sel = currentIndex();
540 lastSel_ = filterModel_->mapToSource(filterModel_->index(sel, 0)).row();
543 filterModel_->setFilterRegExp(charFilterRegExp(filter_));
546 // restore old selection
547 if (lastSel_ != -1) {
548 QModelIndex i = filterModel_->mapFromSource(model_->index(lastSel_, 0));
550 setCurrentIndex(i.row());
553 // Workaround to resize to content size
554 // FIXME: There must be a better way. The QComboBox::AdjustToContents)
556 if (view()->isVisible()) {
557 // call QComboBox::showPopup. But set the inShowPopup_ flag to switch on
558 // the hack in the item delegate to make space for the headers.
559 // We do not call our implementation of showPopup because that
560 // would reset the filter again. This is only needed if the user clicks
562 BOOST_ASSERT(!inShowPopup_);
564 QComboBox::showPopup();
565 inShowPopup_ = false;
567 // The item delegate hack is off again. So trigger a relayout of the popup.
568 filterModel_->triggerLayoutChange();
571 view()->setUpdatesEnabled(enabled);
575 void GuiLayoutBox::countCategories()
577 int n = filterModel_->rowCount();
578 visibleCategories_ = 0;
579 if (n == 0 || !lyxrc.group_layouts)
582 // skip the "Standard" category
583 QString prevCat = model_->index(0, 2).data().toString();
586 for (int i = 0; i < n; ++i) {
587 QString cat = filterModel_->index(i, 2).data().toString();
589 ++visibleCategories_;
595 QString GuiLayoutBox::charFilterRegExp(QString const & filter)
598 for (int i = 0; i < filter.length(); ++i) {
601 re += ".*[" + QRegExp::escape(c) + QRegExp::escape(c.toUpper()) + "]";
603 re += ".*" + QRegExp::escape(c);
609 void GuiLayoutBox::resetFilter()
611 setFilter(QString());
615 void GuiLayoutBox::showPopup()
617 owner_.message(_("Enter characters to filter the layout list."));
619 bool enabled = view()->updatesEnabled();
620 view()->setUpdatesEnabled(false);
624 // call QComboBox::showPopup. But set the inShowPopup_ flag to switch on
625 // the hack in the item delegate to make space for the headers.
626 BOOST_ASSERT(!inShowPopup_);
628 QComboBox::showPopup();
629 inShowPopup_ = false;
631 // The item delegate hack is off again. So trigger a relayout of the popup.
632 filterModel_->triggerLayoutChange();
634 view()->setUpdatesEnabled(enabled);
638 bool GuiLayoutBox::eventFilter(QObject * o, QEvent * e)
640 if (e->type() != QEvent::KeyPress)
641 return QComboBox::eventFilter(o, e);
643 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
644 bool modified = (ke->modifiers() == Qt::ControlModifier)
645 || (ke->modifiers() == Qt::AltModifier)
646 || (ke->modifiers() == Qt::MetaModifier);
650 if (!modified && !filter_.isEmpty()) {
655 case Qt::Key_Backspace:
657 // cut off one character
658 setFilter(filter_.left(filter_.length() - 1));
662 if (modified || ke->text().isEmpty())
664 // find chars for the filter string
666 for (int i = 0; i < ke->text().length(); ++i) {
667 QChar c = ke->text()[i];
668 if (c.isLetterOrNumber()
671 || c.category() == QChar::Separator_Space) {
676 // append new chars to the filter string
677 setFilter(filter_ + s);
683 return QComboBox::eventFilter(o, e);
687 void GuiLayoutBox::set(docstring const & layout)
694 QString const & name = toqstr((*text_class_)[layout].name());
695 if (name == currentText())
698 QList<QStandardItem *> r = model_->findItems(name, Qt::MatchExactly, 1);
700 lyxerr << "Trying to select non existent layout type "
701 << fromqstr(name) << endl;
705 setCurrentIndex(filterModel_->mapFromSource(r.first()->index()).row());
709 void GuiLayoutBox::addItemSort(docstring const & item, docstring const & category,
710 bool sorted, bool sortedByCat)
712 QString qitem = toqstr(item);
713 QString titem = toqstr(translateIfPossible(item));
714 QString qcat = toqstr(translateIfPossible(category));
716 QList<QStandardItem *> row;
717 row.append(new QStandardItem(titem));
718 row.append(new QStandardItem(qitem));
719 row.append(new QStandardItem(qcat));
721 // the first entry is easy
722 int const end = model_->rowCount();
724 model_->appendRow(row);
731 while (i < end && model_->item(i, 2)->text() != qcat)
735 // skip the Standard layout
739 // the simple unsorted case
742 // jump to the end of the category group
743 while (i < end && model_->item(i, 2)->text() == qcat)
745 model_->insertRow(i, row);
747 model_->appendRow(row);
751 // find row to insert the item, after the separator if it exists
753 // find alphabetic position
755 && model_->item(i, 0)->text().compare(titem) < 0
756 && (!sortedByCat || model_->item(i, 2)->text() == qcat))
760 model_->insertRow(i, row);
764 void GuiLayoutBox::updateContents(bool reset)
768 Buffer const * buffer = owner_.buffer();
777 // we'll only update the layout list if the text class has changed
778 // or we've moved from one inset to another
779 DocumentClass const * text_class = &buffer->params().documentClass();
780 Inset const * inset =
781 owner_.view()->cursor().innerParagraph().inInset();
782 if (!reset && text_class_ == text_class && inset_ == inset) {
783 set(owner_.view()->cursor().innerParagraph().layout().name());
788 text_class_ = text_class;
791 DocumentClass::const_iterator lit = text_class_->begin();
792 DocumentClass::const_iterator len = text_class_->end();
793 for (; lit != len; ++lit) {
794 docstring const & name = lit->name();
795 // if this inset requires the empty layout, we skip the default
797 if (name == text_class_->defaultLayoutName() && inset &&
798 (inset->forceEmptyLayout() || inset->useEmptyLayout()))
800 // if it doesn't require the empty layout, we skip it
801 if (name == text_class_->emptyLayoutName() && inset &&
802 !inset->forceEmptyLayout() && !inset->useEmptyLayout())
804 addItemSort(name, lit->category(), lyxrc.sort_layouts, lyxrc.group_layouts);
807 set(owner_.view()->cursor().innerParagraph().layout().name());
810 // needed to recalculate size hint
812 setMinimumWidth(sizeHint().width());
813 setEnabled(!buffer->isReadonly());
818 void GuiLayoutBox::selected(int index)
821 QModelIndex mindex = filterModel_->mapToSource(filterModel_->index(index, 1));
822 docstring const layoutName =
823 qstring_to_ucs4(model_->itemFromIndex(mindex)->text());
828 updateContents(false);
833 // find corresponding text class
834 if (text_class_->hasLayout(layoutName)) {
835 FuncRequest const func(LFUN_LAYOUT, layoutName, FuncRequest::TOOLBAR);
836 theLyXFunc().setLyXView(&owner_);
838 updateContents(false);
842 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
847 /////////////////////////////////////////////////////////////////////
851 /////////////////////////////////////////////////////////////////////
854 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
855 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
856 layout_(0), command_buffer_(0)
858 // give visual separation between adjacent toolbars
861 // TODO: save toolbar position
864 ToolbarInfo::item_iterator it = tbinfo.items.begin();
865 ToolbarInfo::item_iterator end = tbinfo.items.end();
866 for (; it != end; ++it)
871 Action * GuiToolbar::addItem(ToolbarItem const & item)
873 Action * act = new Action(owner_,
874 getIcon(item.func_, false),
875 toqstr(item.label_), item.func_, toqstr(item.label_));
876 actions_.append(act);
882 class PaletteButton : public QToolButton
886 ToolbarItem const & tbitem_;
889 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
890 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
892 QString const label = qt_(to_ascii(tbitem_.label_));
896 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
897 this, SLOT(setIconSize(QSize)));
899 ToolbarInfo const * tbinfo =
900 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
902 // use the icon of first action for the toolbar button
903 setIcon(getIcon(tbinfo->items.begin()->func_, true));
906 void mousePressEvent(QMouseEvent * e)
909 QToolButton::mousePressEvent(e);
915 ToolbarInfo const * tbinfo =
916 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
918 lyxerr << "Unknown toolbar " << tbitem_.name_ << endl;
921 IconPalette * panel = new IconPalette(this);
922 QString const label = qt_(to_ascii(tbitem_.label_));
923 panel->setWindowTitle(label);
924 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
925 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
926 ToolbarInfo::item_iterator it = tbinfo->items.begin();
927 ToolbarInfo::item_iterator const end = tbinfo->items.end();
928 for (; it != end; ++it)
929 if (!getStatus(it->func_).unknown())
930 panel->addButton(bar_->addItem(*it));
932 QToolButton::mousePressEvent(e);
936 class MenuButton : public QToolButton
940 ToolbarItem const & tbitem_;
943 MenuButton(GuiToolbar * bar, ToolbarItem const & item)
944 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
946 setPopupMode(QToolButton::InstantPopup);
947 QString const label = qt_(to_ascii(tbitem_.label_));
951 setIcon(QPixmap(":images/math/" + toqstr(tbitem_.name_) + ".png"));
952 connect(bar, SIGNAL(iconSizeChanged(QSize)),
953 this, SLOT(setIconSize(QSize)));
956 void mousePressEvent(QMouseEvent * e)
959 QToolButton::mousePressEvent(e);
965 QString const label = qt_(to_ascii(tbitem_.label_));
966 ButtonMenu * m = new ButtonMenu(label, this);
967 m->setWindowTitle(label);
968 m->setTearOffEnabled(true);
969 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
970 ToolbarInfo const * tbinfo =
971 toolbarbackend.getDefinedToolbarInfo(tbitem_.name_);
973 lyxerr << "Unknown toolbar " << tbitem_.name_ << endl;
976 ToolbarInfo::item_iterator it = tbinfo->items.begin();
977 ToolbarInfo::item_iterator const end = tbinfo->items.end();
978 for (; it != end; ++it)
979 if (!getStatus(it->func_).unknown())
980 m->add(bar_->addItem(*it));
983 QToolButton::mousePressEvent(e);
990 void GuiToolbar::add(ToolbarItem const & item)
992 switch (item.type_) {
993 case ToolbarItem::SEPARATOR:
996 case ToolbarItem::LAYOUTS:
997 layout_ = new GuiLayoutBox(owner_);
1000 case ToolbarItem::MINIBUFFER:
1001 command_buffer_ = new GuiCommandBuffer(&owner_);
1002 addWidget(command_buffer_);
1003 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
1004 //setHorizontalStretchable(true);
1006 case ToolbarItem::TABLEINSERT: {
1007 QToolButton * tb = new QToolButton;
1008 tb->setCheckable(true);
1009 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
1010 QString const label = qt_(to_ascii(item.label_));
1011 tb->setToolTip(label);
1012 tb->setStatusTip(label);
1014 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
1015 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
1016 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
1017 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
1021 case ToolbarItem::ICONPALETTE:
1022 addWidget(new PaletteButton(this, item));
1025 case ToolbarItem::POPUPMENU: {
1026 addWidget(new MenuButton(this, item));
1029 case ToolbarItem::COMMAND: {
1030 if (!getStatus(item.func_).unknown())
1031 addAction(addItem(item));
1040 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
1042 // if tbinfo.state == auto *do not* set on/off
1043 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
1044 if (GuiToolbar::isVisible())
1045 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
1047 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
1050 // no need to save it here.
1051 Qt::ToolBarArea loc = owner_.toolBarArea(this);
1053 if (loc == Qt::TopToolBarArea)
1054 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
1055 else if (loc == Qt::BottomToolBarArea)
1056 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
1057 else if (loc == Qt::RightToolBarArea)
1058 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
1059 else if (loc == Qt::LeftToolBarArea)
1060 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
1062 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
1064 // save toolbar position. They are not used to restore toolbar position
1065 // now because move(x,y) does not work for toolbar.
1066 tbinfo.posx = pos().x();
1067 tbinfo.posy = pos().y();
1071 void GuiToolbar::updateContents()
1073 // update visible toolbars only
1076 // This is a speed bottleneck because this is called on every keypress
1077 // and update calls getStatus, which copies the cursor at least two times
1078 for (int i = 0; i < actions_.size(); ++i)
1079 actions_[i]->update();
1082 layout_->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
1089 } // namespace frontend
1092 #include "GuiToolbar_moc.cpp"