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 "BufferParams.h"
19 #include "BufferView.h"
21 #include "support/debug.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "support/gettext.h"
25 #include "IconPalette.h"
28 #include "Paragraph.h"
29 #include "TextClass.h"
30 #include "ToolbarBackend.h"
33 #include "GuiCommandBuffer.h"
34 #include "GuiToolbar.h"
35 #include "LyXAction.h"
37 #include "qt_helpers.h"
38 #include "InsertTableWidget.h"
41 #include "support/filetools.h"
42 #include "support/lstrings.h"
43 #include "support/lyxalgo.h" // sorted
47 #include <QToolButton>
51 #include <boost/assert.hpp>
54 using namespace lyx::support;
56 static void initializeResources()
58 static bool initialized = false;
60 Q_INIT_RESOURCE(Resources);
77 bool operator<(PngMap const & lhs, PngMap const & rhs)
79 return strcmp(lhs.key, rhs.key) < 0;
85 CompareKey(string const & name) : name_(name) {}
86 bool operator()(PngMap const & other) const { return other.key == name_; }
92 PngMap sorted_png_map[] = {
93 { "Bumpeq", "bumpeq2" },
96 { "Delta", "delta2" },
97 { "Downarrow", "downarrow2" },
98 { "Gamma", "gamma2" },
99 { "Lambda", "lambda2" },
100 { "Leftarrow", "leftarrow2" },
101 { "Leftrightarrow", "leftrightarrow2" },
102 { "Longleftarrow", "longleftarrow2" },
103 { "Longleftrightarrow", "longleftrightarrow2" },
104 { "Longrightarrow", "longrightarrow2" },
105 { "Omega", "omega2" },
109 { "Rightarrow", "rightarrow2" },
110 { "Sigma", "sigma2" },
111 { "Subset", "subset2" },
112 { "Supset", "supset2" },
113 { "Theta", "theta2" },
114 { "Uparrow", "uparrow2" },
115 { "Updownarrow", "updownarrow2" },
116 { "Upsilon", "upsilon2" },
117 { "Vdash", "vdash3" },
119 { "nLeftarrow", "nleftarrow2" },
120 { "nLeftrightarrow", "nleftrightarrow2" },
121 { "nRightarrow", "nrightarrow2" },
122 { "nVDash", "nvdash3" },
123 { "nvDash", "nvdash2" },
124 { "textrm \\AA", "textrm_AA"},
125 { "textrm \\O", "textrm_O"},
126 { "vDash", "vdash2" }
129 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
132 string const find_png(string const & name)
134 PngMap const * const begin = sorted_png_map;
135 PngMap const * const end = begin + nr_sorted_png_map;
136 BOOST_ASSERT(sorted(begin, end));
138 PngMap const * const it = find_if(begin, end, CompareKey(name));
142 png_name = it->value;
144 png_name = subst(name, "_", "underscore");
145 png_name = subst(png_name, ' ', '_');
147 // This way we can have "math-delim { }" on the toolbar.
148 png_name = subst(png_name, "(", "lparen");
149 png_name = subst(png_name, ")", "rparen");
150 png_name = subst(png_name, "[", "lbracket");
151 png_name = subst(png_name, "]", "rbracket");
152 png_name = subst(png_name, "{", "lbrace");
153 png_name = subst(png_name, "}", "rbrace");
154 png_name = subst(png_name, "|", "bars");
155 png_name = subst(png_name, ",", "thinspace");
156 png_name = subst(png_name, ":", "mediumspace");
157 png_name = subst(png_name, ";", "thickspace");
158 png_name = subst(png_name, "!", "negthinspace");
161 LYXERR(Debug::GUI, "find_png(" << name << ")\n"
162 << "Looking for math PNG called \"" << png_name << '"');
169 /// return a icon for the given action
170 static QIcon getIcon(FuncRequest const & f, bool unknown)
172 initializeResources();
180 case LFUN_MATH_INSERT:
181 if (!f.argument().empty()) {
183 name1 = find_png(to_utf8(f.argument()).substr(1));
186 case LFUN_MATH_DELIM:
187 case LFUN_MATH_BIGDELIM:
189 name1 = find_png(to_utf8(f.argument()));
193 name1 = to_utf8(f.argument());
196 name2 = lyxaction.getActionName(f.action);
199 if (!f.argument().empty())
200 name1 = subst(name2 + ' ' + to_utf8(f.argument()), ' ', '_');
203 fullname = libFileSearch("images/" + path, name1, "png").absFilename();
204 if (pm.load(toqstr(fullname)))
207 fullname = libFileSearch("images/" + path, name2, "png").absFilename();
208 if (pm.load(toqstr(fullname)))
211 if (pm.load(":/images/" + toqstr(path + name1) + ".png"))
214 if (pm.load(":/images/" + toqstr(path + name2) + ".png"))
217 LYXERR(Debug::GUI, "Cannot find icon for command \""
218 << lyxaction.getActionName(f.action)
219 << '(' << to_utf8(f.argument()) << ")\"");
221 pm.load(":/images/unknown.png");
227 /////////////////////////////////////////////////////////////////////
231 /////////////////////////////////////////////////////////////////////
233 GuiLayoutBox::GuiLayoutBox(GuiView & owner)
236 setSizeAdjustPolicy(QComboBox::AdjustToContents);
237 setFocusPolicy(Qt::ClickFocus);
238 setMinimumWidth(sizeHint().width());
239 setMaxVisibleItems(100);
241 QObject::connect(this, SIGNAL(activated(QString)),
242 this, SLOT(selected(QString)));
243 owner_.setLayoutDialog(this);
244 updateContents(true);
248 void GuiLayoutBox::set(docstring const & layout)
253 QString const & name = toqstr(translateIfPossible(
254 (*text_class_)[layout]->name()));
256 if (name == currentText())
259 int i = findText(name);
261 lyxerr << "Trying to select non existent layout type "
262 << fromqstr(name) << endl;
270 void GuiLayoutBox::addItemSort(QString const & item, bool sorted)
272 int const end = count();
273 if (!sorted || end < 2 || item[0].category() != QChar::Letter_Uppercase) {
278 // Let the default one be at the beginning
280 for (setCurrentIndex(i); currentText() < item;) {
281 // e.g. --Separator--
282 if (currentText()[0].category() != QChar::Letter_Uppercase)
293 void GuiLayoutBox::updateContents(bool reset)
295 Buffer const * buffer = owner_.buffer();
304 TextClass const * text_class = &buffer->params().getTextClass();
305 if (!reset && text_class_ == text_class) {
306 set(owner_.view()->cursor().innerParagraph().layout()->name());
310 text_class_ = text_class;
312 setUpdatesEnabled(false);
315 TextClass::const_iterator it = text_class_->begin();
316 TextClass::const_iterator const end = text_class_->end();
317 for (; it != end; ++it) {
318 // ignore obsolete entries
319 addItemSort(toqstr(translateIfPossible((*it)->name())), lyxrc.sort_layouts);
324 // needed to recalculate size hint
326 setMinimumWidth(sizeHint().width());
327 set(owner_.view()->cursor().innerParagraph().layout()->name());
330 setUpdatesEnabled(true);
334 void GuiLayoutBox::selected(const QString & str)
337 updateContents(false);
341 docstring const name = qstring_to_ucs4(str);
342 TextClass::const_iterator it = text_class_->begin();
343 TextClass::const_iterator const end = text_class_->end();
344 for (; it != end; ++it) {
345 docstring const & itname = (*it)->name();
346 if (translateIfPossible(itname) == name) {
347 FuncRequest const func(LFUN_LAYOUT, itname,
348 FuncRequest::TOOLBAR);
349 theLyXFunc().setLyXView(&owner_);
354 lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
359 /////////////////////////////////////////////////////////////////////
363 /////////////////////////////////////////////////////////////////////
366 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
367 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
368 layout_(0), command_buffer_(0)
370 // give visual separation between adjacent toolbars
373 // TODO: save toolbar position
376 ToolbarInfo::item_iterator it = tbinfo.items.begin();
377 ToolbarInfo::item_iterator end = tbinfo.items.end();
378 for (; it != end; ++it)
383 Action * GuiToolbar::addItem(ToolbarItem const & item)
385 Action * act = new Action(owner_,
386 getIcon(item.func_, false),
387 toqstr(item.label_), item.func_, toqstr(item.label_));
388 actions_.append(act);
393 void GuiToolbar::add(ToolbarItem const & item)
395 switch (item.type_) {
396 case ToolbarItem::SEPARATOR:
399 case ToolbarItem::LAYOUTS:
400 layout_ = new GuiLayoutBox(owner_);
403 case ToolbarItem::MINIBUFFER:
404 command_buffer_ = new GuiCommandBuffer(&owner_);
405 addWidget(command_buffer_);
406 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
407 //setHorizontalStretchable(true);
409 case ToolbarItem::TABLEINSERT: {
410 QToolButton * tb = new QToolButton;
411 tb->setCheckable(true);
412 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
413 tb->setToolTip(qt_(to_ascii(item.label_)));
414 tb->setStatusTip(qt_(to_ascii(item.label_)));
415 tb->setText(qt_(to_ascii(item.label_)));
416 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
417 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
418 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
419 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
423 case ToolbarItem::ICONPALETTE: {
424 QToolButton * tb = new QToolButton(this);
425 tb->setToolTip(qt_(to_ascii(item.label_)));
426 tb->setStatusTip(qt_(to_ascii(item.label_)));
427 tb->setText(qt_(to_ascii(item.label_)));
428 connect(this, SIGNAL(iconSizeChanged(QSize)),
429 tb, SLOT(setIconSize(QSize)));
430 IconPalette * panel = new IconPalette(tb);
431 panel->setWindowTitle(qt_(to_ascii(item.label_)));
432 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
433 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
435 lyxerr << "Unknown toolbar " << item.name_ << endl;
438 ToolbarInfo::item_iterator it = tbinfo->items.begin();
439 ToolbarInfo::item_iterator const end = tbinfo->items.end();
440 for (; it != end; ++it)
441 if (!getStatus(it->func_).unknown()) {
442 panel->addButton(addItem(*it));
443 // use the icon of first action for the toolbar button
444 if (it == tbinfo->items.begin())
445 tb->setIcon(getIcon(it->func_, true));
447 tb->setCheckable(true);
448 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
449 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
453 case ToolbarItem::POPUPMENU: {
454 QToolButton * tb = new QToolButton;
455 tb->setPopupMode(QToolButton::InstantPopup);
456 tb->setToolTip(qt_(to_ascii(item.label_)));
457 tb->setStatusTip(qt_(to_ascii(item.label_)));
458 tb->setText(qt_(to_ascii(item.label_)));
459 tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
460 connect(this, SIGNAL(iconSizeChanged(QSize)),
461 tb, SLOT(setIconSize(QSize)));
463 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
464 m->setWindowTitle(qt_(to_ascii(item.label_)));
465 m->setTearOffEnabled(true);
466 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
467 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
469 lyxerr << "Unknown toolbar " << item.name_ << endl;
472 ToolbarInfo::item_iterator it = tbinfo->items.begin();
473 ToolbarInfo::item_iterator const end = tbinfo->items.end();
474 for (; it != end; ++it)
475 if (!getStatus(it->func_).unknown())
476 m->add(addItem(*it));
481 case ToolbarItem::COMMAND: {
482 if (!getStatus(item.func_).unknown())
483 addAction(addItem(item));
492 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
494 // if tbinfo.state == auto *do not* set on/off
495 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
496 if (GuiToolbar::isVisible())
497 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
499 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
502 // no need to save it here.
503 Qt::ToolBarArea loc = owner_.toolBarArea(this);
505 if (loc == Qt::TopToolBarArea)
506 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
507 else if (loc == Qt::BottomToolBarArea)
508 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
509 else if (loc == Qt::RightToolBarArea)
510 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
511 else if (loc == Qt::LeftToolBarArea)
512 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
514 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
516 // save toolbar position. They are not used to restore toolbar position
517 // now because move(x,y) does not work for toolbar.
518 tbinfo.posx = pos().x();
519 tbinfo.posy = pos().y();
523 void GuiToolbar::updateContents()
525 // update visible toolbars only
528 // This is a speed bottleneck because this is called on every keypress
529 // and update calls getStatus, which copies the cursor at least two times
530 for (int i = 0; i < actions_.size(); ++i)
531 actions_[i]->update();
534 layout_->setEnabled(lyx::getStatus(FuncRequest(LFUN_LAYOUT)).enabled());
541 } // namespace frontend
544 #include "GuiToolbar_moc.cpp"