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.
18 #include "GuiToolbar.h"
21 #include "GuiApplication.h"
22 #include "GuiCommandBuffer.h"
24 #include "IconPalette.h"
25 #include "InsertTableWidget.h"
26 #include "qt_helpers.h"
30 #include "BufferParams.h"
31 #include "BufferView.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
36 #include "LyXAction.h"
39 #include "Paragraph.h"
40 #include "TextClass.h"
42 #include "support/debug.h"
43 #include "support/filetools.h"
44 #include "support/gettext.h"
45 #include "support/lstrings.h"
46 #include "support/lyxalgo.h" // sorted
48 #include <QAbstractItemDelegate>
49 #include <QAbstractTextDocumentLayout>
50 #include <QApplication>
52 #include <QFontMetrics>
53 #include <QHeaderView>
54 #include <QItemDelegate>
61 #include <QSortFilterProxyModel>
62 #include <QStandardItem>
63 #include <QStandardItemModel>
65 #include <QTextDocument>
68 #include <QToolButton>
71 #include "support/lassert.h"
77 using namespace lyx::support;
79 static void initializeResources()
81 static bool initialized = false;
83 Q_INIT_RESOURCE(Resources);
100 bool operator<(PngMap const & lhs, PngMap const & rhs)
102 return lhs.key < rhs.key;
108 CompareKey(QString const & name) : name_(name) {}
109 bool operator()(PngMap const & other) const { return other.key == name_; }
115 // this must be sorted alphabetically
116 // Upper case comes before lower case
117 PngMap sorted_png_map[] = {
118 { "Bumpeq", "bumpeq2" },
121 { "Delta", "delta2" },
122 { "Downarrow", "downarrow2" },
123 { "Gamma", "gamma2" },
124 { "Lambda", "lambda2" },
125 { "Leftarrow", "leftarrow2" },
126 { "Leftrightarrow", "leftrightarrow2" },
127 { "Longleftarrow", "longleftarrow2" },
128 { "Longleftrightarrow", "longleftrightarrow2" },
129 { "Longrightarrow", "longrightarrow2" },
130 { "Omega", "omega2" },
134 { "Rightarrow", "rightarrow2" },
135 { "Sigma", "sigma2" },
136 { "Subset", "subset2" },
137 { "Supset", "supset2" },
138 { "Theta", "theta2" },
139 { "Uparrow", "uparrow2" },
140 { "Updownarrow", "updownarrow2" },
141 { "Upsilon", "upsilon2" },
142 { "Vdash", "vdash3" },
145 { "nLeftarrow", "nleftarrow2" },
146 { "nLeftrightarrow", "nleftrightarrow2" },
147 { "nRightarrow", "nrightarrow2" },
148 { "nVDash", "nvdash3" },
149 { "nvDash", "nvdash2" },
150 { "textrm \\AA", "textrm_AA"},
151 { "textrm \\O", "textrm_O"},
152 { "vDash", "vdash2" }
155 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
158 QString findPng(QString const & name)
160 PngMap const * const begin = sorted_png_map;
161 PngMap const * const end = begin + nr_sorted_png_map;
162 LASSERT(sorted(begin, end), /**/);
164 PngMap const * const it = find_if(begin, end, CompareKey(name));
168 png_name = it->value;
171 png_name.replace('_', "underscore");
172 png_name.replace(' ', '_');
174 // This way we can have "math-delim { }" on the toolbar.
175 png_name.replace('(', "lparen");
176 png_name.replace(')', "rparen");
177 png_name.replace('[', "lbracket");
178 png_name.replace(']', "rbracket");
179 png_name.replace('{', "lbrace");
180 png_name.replace('}', "rbrace");
181 png_name.replace('|', "bars");
182 png_name.replace(',', "thinspace");
183 png_name.replace(':', "mediumspace");
184 png_name.replace(';', "thickspace");
185 png_name.replace('!', "negthinspace");
188 LYXERR(Debug::GUI, "findPng(" << name << ")\n"
189 << "Looking for math PNG called \"" << png_name << '"');
196 /// return a icon for the given action
197 static QIcon getIcon(FuncRequest const & f, bool unknown)
199 initializeResources();
205 case LFUN_MATH_INSERT:
206 if (!f.argument().empty()) {
208 name1 = findPng(toqstr(f.argument()).mid(1));
211 case LFUN_MATH_DELIM:
212 case LFUN_MATH_BIGDELIM:
214 name1 = findPng(toqstr(f.argument()));
218 name1 = toqstr(f.argument());
221 name2 = toqstr(lyxaction.getActionName(f.action));
224 if (!f.argument().empty()) {
225 name1 = name2 + ' ' + toqstr(f.argument());
226 name1.replace(' ', '_');
230 string fullname = libFileSearch("images/" + path, name1, "png").absFilename();
231 if (pm.load(toqstr(fullname)))
234 fullname = libFileSearch("images/" + path, name2, "png").absFilename();
235 if (pm.load(toqstr(fullname)))
238 if (pm.load(":/images/" + path + name1 + ".png"))
241 if (pm.load(":/images/" + path + name2 + ".png"))
244 LYXERR(Debug::GUI, "Cannot find icon for command \""
245 << lyxaction.getActionName(f.action)
246 << '(' << to_utf8(f.argument()) << ")\"");
248 pm.load(":/images/unknown.png");
254 /////////////////////////////////////////////////////////////////////
258 /////////////////////////////////////////////////////////////////////
260 class LayoutItemDelegate : public QItemDelegate {
263 explicit LayoutItemDelegate(QObject * parent = 0)
264 : QItemDelegate(parent)
268 void paint(QPainter * painter, QStyleOptionViewItem const & option,
269 QModelIndex const & index) const
271 QStyleOptionViewItem opt = option;
273 // default background
274 painter->fillRect(opt.rect, opt.palette.color(QPalette::Base));
277 if (lyxrc.group_layouts) {
278 QSortFilterProxyModel const * model
279 = static_cast<QSortFilterProxyModel const *>(index.model());
281 QString stdCat = category(*model->sourceModel(), 0);
282 QString cat = category(*index.model(), index.row());
284 // not the standard layout and not the same as in the previous line?
286 && (index.row() == 0 || cat != category(*index.model(), index.row() - 1))) {
289 // draw unselected background
290 QStyle::State state = opt.state;
291 opt.state = opt.state & ~QStyle::State_Selected;
292 drawBackground(painter, opt, index);
295 // draw category header
296 drawCategoryHeader(painter, opt,
297 category(*index.model(), index.row()));
299 // move rect down below header
300 opt.rect.setTop(opt.rect.top() + headerHeight(opt));
306 QItemDelegate::paint(painter, opt, index);
310 void drawDisplay(QPainter * painter, QStyleOptionViewItem const & opt,
311 const QRect & /*rect*/, const QString & text ) const
313 QString utext = underlineFilter(text);
315 // Draw the rich text.
317 QColor col = opt.palette.text().color();
318 if (opt.state & QStyle::State_Selected)
319 col = opt.palette.highlightedText().color();
320 QAbstractTextDocumentLayout::PaintContext context;
321 context.palette.setColor(QPalette::Text, col);
324 doc.setDefaultFont(opt.font);
327 QTextFrameFormat fmt = doc.rootFrame()->frameFormat();
329 doc.rootFrame()->setFrameFormat(fmt);
331 painter->translate(opt.rect.x() + 5,
332 opt.rect.y() + (opt.rect.height() - opt.fontMetrics.height()) / 2);
333 doc.documentLayout()->draw(painter, context);
338 QSize sizeHint(QStyleOptionViewItem const & opt,
339 QModelIndex const & index) const
341 GuiLayoutBox * combo = static_cast<GuiLayoutBox *>(parent());
342 QSortFilterProxyModel const * model
343 = static_cast<QSortFilterProxyModel const *>(index.model());
344 QSize size = QItemDelegate::sizeHint(opt, index);
346 /// QComboBox uses the first row height to estimate the
347 /// complete popup height during QComboBox::showPopup().
348 /// To avoid scrolling we have to sneak in space for the headers.
349 /// So we tweak this value accordingly. It's not nice, but the
350 /// only possible way it seems.
351 if (lyxrc.group_layouts && index.row() == 0 && combo->inShowPopup_) {
352 int itemHeight = size.height();
354 // we have to show \c cats many headers:
355 unsigned cats = combo->visibleCategories_;
357 // and we have \c n items to distribute the needed space over
358 unsigned n = combo->model()->rowCount();
360 // so the needed average height (rounded upwards) is:
361 size.setHeight((headerHeight(opt) * cats + itemHeight * n + n - 1) / n);
365 // Add space for the category headers here?
366 // Not for the standard layout though.
367 QString stdCat = category(*model->sourceModel(), 0);
368 QString cat = category(*index.model(), index.row());
369 if (lyxrc.group_layouts && stdCat != cat
370 && (index.row() == 0 || cat != category(*index.model(), index.row() - 1))) {
371 size.setHeight(size.height() + headerHeight(opt));
379 QString category(QAbstractItemModel const & model, int row) const
381 return model.data(model.index(row, 2), Qt::DisplayRole).toString();
385 int headerHeight(QStyleOptionViewItem const & opt) const
387 return opt.fontMetrics.height() * 8 / 10;
390 void drawCategoryHeader(QPainter * painter, QStyleOptionViewItem const & opt,
391 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 if (!category.isEmpty()) {
419 painter->drawLine(opt.rect.x(), ymid, left - 1, ymid);
420 painter->drawLine(right + 1, ymid, opt.rect.right(), ymid);
422 painter->drawLine(opt.rect.x(), ymid, opt.rect.right(), ymid);
427 QString underlineFilter(QString const & s) const
430 GuiLayoutBox * p = static_cast<GuiLayoutBox *>(parent());
431 QString const & f = p->filter();
435 // step through data item and put "(x)" for every matching character
439 for (int i = 0; i < f.length(); ++i) {
440 int p = s.indexOf(f[i], lastp + 1, Qt::CaseInsensitive);
441 LASSERT(p != -1, /**/);
442 if (lastp == p - 1 && lastp != -1) {
443 // remove ")" and append "x)"
444 r = r.left(r.length() - 4) + s[p] + "</u>";
447 r += s.mid(lastp + 1, p - lastp - 1);
448 r += QString("<u>") + s[p] + "</u>";
452 r += s.mid(lastp + 1);
458 class GuiLayoutFilterModel : public QSortFilterProxyModel {
461 GuiLayoutFilterModel(QObject * parent = 0)
462 : QSortFilterProxyModel(parent)
466 void triggerLayoutChange()
468 layoutAboutToBeChanged();
474 GuiLayoutBox::GuiLayoutBox(GuiToolbar * bar, GuiView & owner)
475 : owner_(owner), bar_(bar), lastSel_(-1),
476 layoutItemDelegate_(new LayoutItemDelegate(this)),
477 visibleCategories_(0), inShowPopup_(false)
479 setSizeAdjustPolicy(QComboBox::AdjustToContents);
480 setFocusPolicy(Qt::ClickFocus);
481 setMinimumWidth(sizeHint().width());
482 setMaxVisibleItems(100);
484 // set the layout model with two columns
485 // 1st: translated layout names
486 // 2nd: raw layout names
487 model_ = new QStandardItemModel(0, 2, this);
488 filterModel_ = new GuiLayoutFilterModel(this);
489 filterModel_->setSourceModel(model_);
490 setModel(filterModel_);
492 // for the filtering we have to intercept characters
493 view()->installEventFilter(this);
494 view()->setItemDelegateForColumn(0, layoutItemDelegate_);
496 QObject::connect(this, SIGNAL(activated(int)),
497 this, SLOT(selected(int)));
498 QObject::connect(bar_, SIGNAL(iconSizeChanged(QSize)),
499 this, SLOT(setIconSize(QSize)));
501 owner_.setLayoutDialog(this);
502 updateContents(true);
506 void GuiLayoutBox::setFilter(QString const & s)
508 bool enabled = view()->updatesEnabled();
509 view()->setUpdatesEnabled(false);
511 // remember old selection
512 int sel = currentIndex();
514 lastSel_ = filterModel_->mapToSource(filterModel_->index(sel, 0)).row();
517 filterModel_->setFilterRegExp(charFilterRegExp(filter_));
520 // restore old selection
521 if (lastSel_ != -1) {
522 QModelIndex i = filterModel_->mapFromSource(model_->index(lastSel_, 0));
524 setCurrentIndex(i.row());
527 // Workaround to resize to content size
528 // FIXME: There must be a better way. The QComboBox::AdjustToContents)
530 if (view()->isVisible()) {
531 // call QComboBox::showPopup. But set the inShowPopup_ flag to switch on
532 // the hack in the item delegate to make space for the headers.
533 // We do not call our implementation of showPopup because that
534 // would reset the filter again. This is only needed if the user clicks
536 LASSERT(!inShowPopup_, /**/);
538 QComboBox::showPopup();
539 inShowPopup_ = false;
541 // The item delegate hack is off again. So trigger a relayout of the popup.
542 filterModel_->triggerLayoutChange();
545 owner_.message(_("Filtering layouts with \"" + fromqstr(s) + "\". "
546 "Press ESC to remove filter."));
548 owner_.message(_("Enter characters to filter the layout list."));
551 view()->setUpdatesEnabled(enabled);
555 void GuiLayoutBox::countCategories()
557 int n = filterModel_->rowCount();
558 visibleCategories_ = 0;
559 if (n == 0 || !lyxrc.group_layouts)
562 // skip the "Standard" category
563 QString prevCat = model_->index(0, 2).data().toString();
566 for (int i = 0; i < n; ++i) {
567 QString cat = filterModel_->index(i, 2).data().toString();
569 ++visibleCategories_;
575 QString GuiLayoutBox::charFilterRegExp(QString const & filter)
578 for (int i = 0; i < filter.length(); ++i) {
581 re += ".*[" + QRegExp::escape(c) + QRegExp::escape(c.toUpper()) + "]";
583 re += ".*" + QRegExp::escape(c);
589 void GuiLayoutBox::resetFilter()
591 setFilter(QString());
595 void GuiLayoutBox::showPopup()
597 owner_.message(_("Enter characters to filter the layout list."));
599 bool enabled = view()->updatesEnabled();
600 view()->setUpdatesEnabled(false);
604 // call QComboBox::showPopup. But set the inShowPopup_ flag to switch on
605 // the hack in the item delegate to make space for the headers.
606 LASSERT(!inShowPopup_, /**/);
608 QComboBox::showPopup();
609 inShowPopup_ = false;
611 // The item delegate hack is off again. So trigger a relayout of the popup.
612 filterModel_->triggerLayoutChange();
614 view()->setUpdatesEnabled(enabled);
618 bool GuiLayoutBox::eventFilter(QObject * o, QEvent * e)
620 if (e->type() != QEvent::KeyPress)
621 return QComboBox::eventFilter(o, e);
623 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
624 bool modified = (ke->modifiers() == Qt::ControlModifier)
625 || (ke->modifiers() == Qt::AltModifier)
626 || (ke->modifiers() == Qt::MetaModifier);
630 if (!modified && !filter_.isEmpty()) {
635 case Qt::Key_Backspace:
637 // cut off one character
638 setFilter(filter_.left(filter_.length() - 1));
642 if (modified || ke->text().isEmpty())
644 // find chars for the filter string
646 for (int i = 0; i < ke->text().length(); ++i) {
647 QChar c = ke->text()[i];
648 if (c.isLetterOrNumber()
651 || c.category() == QChar::Separator_Space) {
656 // append new chars to the filter string
657 setFilter(filter_ + s);
663 return QComboBox::eventFilter(o, e);
667 void GuiLayoutBox::setIconSize(QSize size)
670 bool small = size.height() < 20;
671 setAttribute(Qt::WA_MacSmallSize, small);
672 setAttribute(Qt::WA_MacNormalSize, !small);
677 void GuiLayoutBox::set(docstring const & layout)
684 QString const & name = toqstr((*text_class_)[layout].name());
685 if (name == currentText())
688 QList<QStandardItem *> r = model_->findItems(name, Qt::MatchExactly, 1);
690 LYXERR0("Trying to select non existent layout type " << name);
694 setCurrentIndex(filterModel_->mapFromSource(r.first()->index()).row());
698 void GuiLayoutBox::addItemSort(docstring const & item, docstring const & category,
699 bool sorted, bool sortedByCat)
701 QString qitem = toqstr(item);
702 QString titem = toqstr(translateIfPossible(item));
703 QString qcat = toqstr(translateIfPossible(category));
705 QList<QStandardItem *> row;
706 row.append(new QStandardItem(titem));
707 row.append(new QStandardItem(qitem));
708 row.append(new QStandardItem(qcat));
710 // the first entry is easy
711 int const end = model_->rowCount();
713 model_->appendRow(row);
720 while (i < end && model_->item(i, 2)->text() != qcat)
724 // skip the Standard layout
728 // the simple unsorted case
731 // jump to the end of the category group
732 while (i < end && model_->item(i, 2)->text() == qcat)
734 model_->insertRow(i, row);
736 model_->appendRow(row);
740 // find row to insert the item, after the separator if it exists
742 // find alphabetic position
744 && model_->item(i, 0)->text().localeAwareCompare(titem) < 0
745 && (!sortedByCat || model_->item(i, 2)->text() == qcat))
749 model_->insertRow(i, row);
753 void GuiLayoutBox::updateContents(bool reset)
757 Buffer const * buffer = owner_.buffer();
766 // we'll only update the layout list if the text class has changed
767 // or we've moved from one inset to another
768 DocumentClass const * text_class = &buffer->params().documentClass();
769 Inset const * inset =
770 owner_.view()->cursor().innerParagraph().inInset();
771 if (!reset && text_class_ == text_class && inset_ == inset) {
772 set(owner_.view()->cursor().innerParagraph().layout().name());
777 text_class_ = text_class;
780 DocumentClass::const_iterator lit = text_class_->begin();
781 DocumentClass::const_iterator len = text_class_->end();
783 for (; lit != len; ++lit) {
784 docstring const & name = lit->name();
785 bool const useEmpty = inset_->forceEmptyLayout() || inset_->useEmptyLayout();
786 // if this inset requires the empty layout, we skip the default
788 if (name == text_class_->defaultLayoutName() && inset_ && useEmpty)
790 // if it doesn't require the empty layout, we skip it
791 if (name == text_class_->emptyLayoutName() && inset_ && !useEmpty)
793 addItemSort(name, lit->category(), lyxrc.sort_layouts, lyxrc.group_layouts);
796 set(owner_.view()->cursor().innerParagraph().layout().name());
799 // needed to recalculate size hint
801 setMinimumWidth(sizeHint().width());
802 setEnabled(!buffer->isReadonly());
807 void GuiLayoutBox::selected(int index)
810 QModelIndex mindex = filterModel_->mapToSource(filterModel_->index(index, 1));
811 docstring const layoutName =
812 qstring_to_ucs4(model_->itemFromIndex(mindex)->text());
817 updateContents(false);
822 // find corresponding text class
823 if (text_class_->hasLayout(layoutName)) {
824 FuncRequest const func(LFUN_LAYOUT, layoutName, FuncRequest::TOOLBAR);
825 theLyXFunc().setLyXView(&owner_);
827 updateContents(false);
831 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
836 /////////////////////////////////////////////////////////////////////
840 /////////////////////////////////////////////////////////////////////
843 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
844 : QToolBar(qt_(tbinfo.gui_name), &owner), visibility_(0),
845 allowauto_(false), owner_(owner), layout_(0), command_buffer_(0),
846 tbinfo_(tbinfo), filled_(false)
848 // Toolbar dragging is allowed.
850 // This is used by QMainWindow::restoreState for proper main window state
852 setObjectName(toqstr(tbinfo.name));
858 void GuiToolbar::fill()
862 ToolbarInfo::item_iterator it = tbinfo_.items.begin();
863 ToolbarInfo::item_iterator end = tbinfo_.items.end();
864 for (; it != end; ++it)
870 void GuiToolbar::showEvent(QShowEvent * ev)
877 void GuiToolbar::setVisibility(int visibility)
879 visibility_ = visibility;
880 allowauto_ = visibility_ >= Toolbars::MATH;
884 Action * GuiToolbar::addItem(ToolbarItem const & item)
886 Action * act = new Action(&owner_, getIcon(item.func_, false),
887 toqstr(item.label_), item.func_, toqstr(item.label_), this);
888 actions_.append(act);
894 class PaletteButton : public QToolButton
898 ToolbarItem const & tbitem_;
901 PaletteButton(GuiToolbar * bar, ToolbarItem const & item)
902 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
904 QString const label = qt_(to_ascii(tbitem_.label_));
908 connect(bar_, SIGNAL(iconSizeChanged(QSize)),
909 this, SLOT(setIconSize(QSize)));
911 ToolbarInfo const * tbinfo = guiApp->toolbars().info(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 = guiApp->toolbars().info(tbitem_.name_);
928 lyxerr << "Unknown toolbar " << tbitem_.name_ << endl;
931 IconPalette * panel = new IconPalette(this);
932 QString const label = qt_(to_ascii(tbitem_.label_));
933 panel->setWindowTitle(label);
934 connect(this, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
935 connect(panel, SIGNAL(visible(bool)), this, SLOT(setChecked(bool)));
936 ToolbarInfo::item_iterator it = tbinfo->items.begin();
937 ToolbarInfo::item_iterator const end = tbinfo->items.end();
938 for (; it != end; ++it)
939 if (!getStatus(it->func_).unknown())
940 panel->addButton(bar_->addItem(*it));
942 QToolButton::mousePressEvent(e);
946 class MenuButton : public QToolButton
950 ToolbarItem const & tbitem_;
953 MenuButton(GuiToolbar * bar, ToolbarItem const & item)
954 : QToolButton(bar), bar_(bar), tbitem_(item), initialized_(false)
956 setPopupMode(QToolButton::InstantPopup);
957 QString const label = qt_(to_ascii(tbitem_.label_));
961 setIcon(QPixmap(":images/math/" + toqstr(tbitem_.name_) + ".png"));
962 connect(bar, SIGNAL(iconSizeChanged(QSize)),
963 this, SLOT(setIconSize(QSize)));
966 void mousePressEvent(QMouseEvent * e)
969 QToolButton::mousePressEvent(e);
975 QString const label = qt_(to_ascii(tbitem_.label_));
976 ButtonMenu * m = new ButtonMenu(label, this);
977 m->setWindowTitle(label);
978 m->setTearOffEnabled(true);
979 connect(bar_, SIGNAL(updated()), m, SLOT(updateParent()));
980 ToolbarInfo const * tbinfo = guiApp->toolbars().info(tbitem_.name_);
982 lyxerr << "Unknown toolbar " << tbitem_.name_ << endl;
985 ToolbarInfo::item_iterator it = tbinfo->items.begin();
986 ToolbarInfo::item_iterator const end = tbinfo->items.end();
987 for (; it != end; ++it)
988 if (!getStatus(it->func_).unknown())
989 m->add(bar_->addItem(*it));
992 QToolButton::mousePressEvent(e);
999 void GuiToolbar::add(ToolbarItem const & item)
1001 switch (item.type_) {
1002 case ToolbarItem::SEPARATOR:
1005 case ToolbarItem::LAYOUTS:
1006 layout_ = new GuiLayoutBox(this, owner_);
1009 case ToolbarItem::MINIBUFFER:
1010 command_buffer_ = new GuiCommandBuffer(&owner_);
1011 addWidget(command_buffer_);
1012 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
1013 //setHorizontalStretchable(true);
1015 case ToolbarItem::TABLEINSERT: {
1016 QToolButton * tb = new QToolButton;
1017 tb->setCheckable(true);
1018 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
1019 QString const label = qt_(to_ascii(item.label_));
1020 tb->setToolTip(label);
1021 tb->setStatusTip(label);
1023 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
1024 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
1025 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
1026 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
1030 case ToolbarItem::ICONPALETTE:
1031 addWidget(new PaletteButton(this, item));
1034 case ToolbarItem::POPUPMENU: {
1035 addWidget(new MenuButton(this, item));
1038 case ToolbarItem::COMMAND: {
1039 if (!getStatus(item.func_).unknown())
1040 addAction(addItem(item));
1049 void GuiToolbar::update(bool in_math, bool in_table, bool in_review,
1050 bool in_mathmacrotemplate)
1052 if (visibility_ & Toolbars::AUTO) {
1053 bool show_it = in_math && (visibility_ & Toolbars::MATH)
1054 || in_table && (visibility_ & Toolbars::TABLE)
1055 || in_review && (visibility_ & Toolbars::REVIEW)
1056 || in_mathmacrotemplate && (visibility_ & Toolbars::MATHMACROTEMPLATE);
1057 setVisible(show_it);
1060 // update visible toolbars only
1064 // This is a speed bottleneck because this is called on every keypress
1065 // and update calls getStatus, which copies the cursor at least two times
1066 for (int i = 0; i < actions_.size(); ++i)
1067 actions_[i]->update();
1070 layout_->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
1077 QString GuiToolbar::sessionKey() const
1079 return "view-" + QString::number(owner_.id()) + "/" + objectName();
1083 void GuiToolbar::saveSession() const
1086 settings.setValue(sessionKey() + "/visibility", visibility_);
1090 void GuiToolbar::restoreSession()
1093 setVisibility(settings.value(sessionKey() + "/visibility").toInt());
1097 void GuiToolbar::toggle()
1101 if (!(visibility_ & Toolbars::AUTO)) {
1102 visibility_ |= Toolbars::AUTO;
1106 visibility_ &= ~Toolbars::AUTO;
1125 owner_.message(bformat(_("Toolbar \"%1$s\" state set to %2$s"),
1126 qstring_to_ucs4(windowTitle()), state));
1129 } // namespace frontend
1132 #include "GuiToolbar_moc.cpp"