]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiToolbar.cpp
reduce line noise
[lyx.git] / src / frontends / qt4 / GuiToolbar.cpp
1 /**
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.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author Abdelrazak Younes
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "debug.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "gettext.h"
23 #include "IconPalette.h"
24 #include "Layout.h"
25 #include "LyXFunc.h"
26 #include "TextClass.h"
27 #include "ToolbarBackend.h"
28
29 #include "GuiView.h"
30 #include "GuiCommandBuffer.h"
31 #include "GuiToolbar.h"
32 #include "LyXAction.h"
33 #include "Action.h"
34 #include "qt_helpers.h"
35 #include "InsertTableWidget.h"
36 #include "LyXRC.h"
37
38 #include "support/filetools.h"
39 #include "support/lstrings.h"
40 #include "support/lyxalgo.h" // sorted
41
42 #include <QComboBox>
43 #include <QToolBar>
44 #include <QToolButton>
45 #include <QAction>
46 #include <QPixmap>
47
48 #include <boost/assert.hpp>
49
50
51 static void initializeResources()
52 {
53         static bool initialized = false;
54         if (!initialized) {
55                 Q_INIT_RESOURCE(Resources); 
56                 initialized = true;
57         }
58 }
59
60
61 namespace lyx {
62 namespace frontend {
63
64 using std::string;
65 using std::endl;
66
67 using support::libFileSearch;
68 using support::subst;
69 using support::compare;
70
71
72 namespace {
73
74 struct PngMap {
75         char const * key;
76         char const * value;
77 };
78
79
80 bool operator<(PngMap const & lhs, PngMap const & rhs)
81 {
82                 return compare(lhs.key, rhs.key) < 0;
83 }
84
85
86 class CompareKey {
87 public:
88         CompareKey(string const & name) : name_(name) {}
89         bool operator()(PngMap const & other) const { return other.key == name_; }
90 private:
91         string const name_;
92 };
93
94
95 PngMap sorted_png_map[] = {
96         { "Bumpeq", "bumpeq2" },
97         { "Cap", "cap2" },
98         { "Cup", "cup2" },
99         { "Delta", "delta2" },
100         { "Downarrow", "downarrow2" },
101         { "Gamma", "gamma2" },
102         { "Lambda", "lambda2" },
103         { "Leftarrow", "leftarrow2" },
104         { "Leftrightarrow", "leftrightarrow2" },
105         { "Longleftarrow", "longleftarrow2" },
106         { "Longleftrightarrow", "longleftrightarrow2" },
107         { "Longrightarrow", "longrightarrow2" },
108         { "Omega", "omega2" },
109         { "Phi", "phi2" },
110         { "Pi", "pi2" },
111         { "Psi", "psi2" },
112         { "Rightarrow", "rightarrow2" },
113         { "Sigma", "sigma2" },
114         { "Subset", "subset2" },
115         { "Supset", "supset2" },
116         { "Theta", "theta2" },
117         { "Uparrow", "uparrow2" },
118         { "Updownarrow", "updownarrow2" },
119         { "Upsilon", "upsilon2" },
120         { "Vdash", "vdash3" },
121         { "Xi", "xi2" },
122         { "nLeftarrow", "nleftarrow2" },
123         { "nLeftrightarrow", "nleftrightarrow2" },
124         { "nRightarrow", "nrightarrow2" },
125         { "nVDash", "nvdash3" },
126         { "nvDash", "nvdash2" },
127         { "textrm \\AA", "textrm_AA"},
128         { "textrm \\O", "textrm_Oe"},
129         { "vDash", "vdash2" }
130 };
131
132 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
133
134
135 string const find_png(string const & name)
136 {
137         PngMap const * const begin = sorted_png_map;
138         PngMap const * const end = begin + nr_sorted_png_map;
139         BOOST_ASSERT(sorted(begin, end));
140
141         PngMap const * const it = std::find_if(begin, end, CompareKey(name));
142
143         string png_name;
144         if (it != end)
145                 png_name = it->value;
146         else {
147                 png_name = subst(name, "_", "underscore");
148                 png_name = subst(png_name, ' ', '_');
149
150                 // This way we can have "math-delim { }" on the toolbar.
151                 png_name = subst(png_name, "(", "lparen");
152                 png_name = subst(png_name, ")", "rparen");
153                 png_name = subst(png_name, "[", "lbracket");
154                 png_name = subst(png_name, "]", "rbracket");
155                 png_name = subst(png_name, "{", "lbrace");
156                 png_name = subst(png_name, "}", "rbrace");
157                 png_name = subst(png_name, "|", "bars");
158                 png_name = subst(png_name, ",", "thinspace");
159                 png_name = subst(png_name, ":", "mediumspace");
160                 png_name = subst(png_name, ";", "thickspace");
161                 png_name = subst(png_name, "!", "negthinspace");
162         }
163
164         LYXERR(Debug::GUI, "find_png(" << name << ")\n"
165                 << "Looking for math PNG called \"" << png_name << '"');
166         return png_name;
167 }
168
169 } // namespace anon
170
171
172 /// return a icon for the given action
173 static QIcon getIcon(FuncRequest const & f, bool unknown)
174 {
175         initializeResources();
176         QPixmap pm;
177         string name1;
178         string name2;
179         string path;
180         string fullname;
181
182         switch (f.action) {
183         case LFUN_MATH_INSERT:
184                 if (!f.argument().empty()) {
185                         path = "math/";
186                         name1 = find_png(to_utf8(f.argument()).substr(1));
187                 }
188                 break;
189         case LFUN_MATH_DELIM:
190         case LFUN_MATH_BIGDELIM:
191                 path = "math/";
192                 name1 = find_png(to_utf8(f.argument()));
193                 break;
194         case LFUN_CALL:
195                 path = "commands/";
196                 name1 = to_utf8(f.argument());
197                 break;
198         default:
199                 name2 = lyxaction.getActionName(f.action);
200                 name1 = name2;
201
202                 if (!f.argument().empty())
203                         name1 = subst(name2 + ' ' + to_utf8(f.argument()), ' ', '_');
204         }
205
206         fullname = libFileSearch("images/" + path, name1, "png").absFilename();
207         if (pm.load(toqstr(fullname)))
208                 return pm;
209
210         fullname = libFileSearch("images/" + path, name2, "png").absFilename();
211         if (pm.load(toqstr(fullname)))
212                 return pm;
213
214         if (pm.load(":/images/" + toqstr(path + name1) + ".png"))
215                 return pm;
216
217         if (pm.load(":/images/" + toqstr(path + name2) + ".png"))
218                 return pm;
219
220         LYXERR(Debug::GUI, "Cannot find icon for command \""
221                            << lyxaction.getActionName(f.action)
222                            << '(' << to_utf8(f.argument()) << ")\"");
223         if (unknown)
224                 pm.load(":/images/unknown.png");
225
226         return pm;
227 }
228
229
230 static TextClass const & textClass(LyXView const & lv)
231 {
232         return lv.buffer()->params().getTextClass();
233 }
234
235
236 /////////////////////////////////////////////////////////////////////
237 //
238 // GuiLayoutBox
239 //
240 /////////////////////////////////////////////////////////////////////
241
242 GuiLayoutBox::GuiLayoutBox(GuiView & owner)
243         : owner_(owner)
244 {
245         setSizeAdjustPolicy(QComboBox::AdjustToContents);
246         setFocusPolicy(Qt::ClickFocus);
247         setMinimumWidth(sizeHint().width());
248         setMaxVisibleItems(100);
249
250         QObject::connect(this, SIGNAL(activated(QString)),
251                          this, SLOT(selected(QString)));
252 }
253
254
255 void GuiLayoutBox::set(docstring const & layout)
256 {
257         TextClass const & tc = textClass(owner_);
258
259         QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
260
261         int i = 0;
262         for (; i < count(); ++i) {
263                 if (name == itemText(i))
264                         break;
265         }
266
267         if (i == count()) {
268                 lyxerr << "Trying to select non existent layout type "
269                         << fromqstr(name) << endl;
270                 return;
271         }
272
273         setCurrentIndex(i);
274 }
275
276
277 void GuiLayoutBox::addItemSort(QString const & item, bool sorted)
278 {
279         int const end = count();
280         if (!sorted || end < 2 || item[0].category() != QChar::Letter_Uppercase) {
281                 addItem(item);
282                 return;
283         }
284
285         // Let the default one be at the beginning
286         int i = 1;
287         for (setCurrentIndex(i); currentText() < item;) {
288                 // e.g. --Separator--
289                 if (currentText()[0].category() != QChar::Letter_Uppercase)
290                         break;
291                 if (++i == end)
292                         break;
293                 setCurrentIndex(i);
294         }
295
296         insertItem(i, item);
297 }
298
299
300 void GuiLayoutBox::updateContents()
301 {
302         TextClass const & tc = textClass(owner_);
303
304         setUpdatesEnabled(false);
305         clear();
306
307         TextClass::const_iterator it = tc.begin();
308         TextClass::const_iterator const end = tc.end();
309         for (; it != end; ++it) {
310                 // ignore obsolete entries
311                 addItemSort(toqstr(translateIfPossible((*it)->name())), lyxrc.sort_layouts);
312         }
313
314         setCurrentIndex(0);
315
316         // needed to recalculate size hint
317         hide();
318         setMinimumWidth(sizeHint().width());
319         show();
320
321         setUpdatesEnabled(true);
322 }
323
324
325 void GuiLayoutBox::selected(const QString & str)
326 {
327         owner_.setFocus();
328         TextClass const & tc = owner_.buffer()->params().getTextClass();
329         docstring const name = qstring_to_ucs4(str);
330         TextClass::const_iterator it  = tc.begin();
331         TextClass::const_iterator const end = tc.end();
332         for (; it != end; ++it) {
333                 docstring const & itname = (*it)->name();
334                 if (translateIfPossible(itname) == name) {
335                         FuncRequest const func(LFUN_LAYOUT, itname,
336                                                FuncRequest::TOOLBAR);
337                         owner_.dispatch(func);
338                         return;
339                 }
340         }
341         lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
342 }
343
344
345
346 /////////////////////////////////////////////////////////////////////
347 //
348 // GuiToolbar
349 //
350 /////////////////////////////////////////////////////////////////////
351
352
353 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
354         : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
355           layout_(0), command_buffer_(0)
356 {
357         // give visual separation between adjacent toolbars
358         addSeparator();
359
360         // TODO: save toolbar position
361         setMovable(true);
362
363         ToolbarInfo::item_iterator it = tbinfo.items.begin();
364         ToolbarInfo::item_iterator end = tbinfo.items.end();
365         for (; it != end; ++it)
366                 add(*it);
367 }
368
369
370 Action * GuiToolbar::addItem(ToolbarItem const & item)
371 {
372         Action * act = new Action(owner_,
373                 getIcon(item.func_, false),
374           toqstr(item.label_), item.func_, toqstr(item.label_));
375         actions_.append(act);
376         return act;
377 }
378
379
380 void GuiToolbar::add(ToolbarItem const & item)
381 {
382         switch (item.type_) {
383         case ToolbarItem::SEPARATOR:
384                 addSeparator();
385                 break;
386         case ToolbarItem::LAYOUTS:
387                 layout_ = new GuiLayoutBox(owner_);
388                 addWidget(layout_);
389                 break;
390         case ToolbarItem::MINIBUFFER:
391                 command_buffer_ = new GuiCommandBuffer(&owner_);
392                 addWidget(command_buffer_);
393                 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
394                 //setHorizontalStretchable(true);
395                 break;
396         case ToolbarItem::TABLEINSERT: {
397                 QToolButton * tb = new QToolButton;
398                 tb->setCheckable(true);
399                 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
400                 tb->setToolTip(qt_(to_ascii(item.label_)));
401                 tb->setStatusTip(qt_(to_ascii(item.label_)));
402                 tb->setText(qt_(to_ascii(item.label_)));
403                 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
404                 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
405                 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
406                 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
407                 addWidget(tb);
408                 break;
409                 }
410         case ToolbarItem::ICONPALETTE: {
411                 QToolButton * tb = new QToolButton(this);
412                 tb->setToolTip(qt_(to_ascii(item.label_)));
413                 tb->setStatusTip(qt_(to_ascii(item.label_)));
414                 tb->setText(qt_(to_ascii(item.label_)));
415                 connect(this, SIGNAL(iconSizeChanged(QSize)),
416                         tb, SLOT(setIconSize(QSize)));
417                 IconPalette * panel = new IconPalette(tb);
418                 panel->setWindowTitle(qt_(to_ascii(item.label_)));
419                 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
420                 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
421                 if (!tbinfo) {
422                         lyxerr << "Unknown toolbar " << item.name_ << endl;
423                         break;
424                 }
425                 ToolbarInfo::item_iterator it = tbinfo->items.begin();
426                 ToolbarInfo::item_iterator const end = tbinfo->items.end();
427                 for (; it != end; ++it)
428                         if (!getStatus(it->func_).unknown()) {
429                                 panel->addButton(addItem(*it));
430                                 // use the icon of first action for the toolbar button
431                                 if (it == tbinfo->items.begin())
432                                         tb->setIcon(getIcon(it->func_, true));
433                         }
434                 tb->setCheckable(true);
435                 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
436                 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
437                 addWidget(tb);
438                 break;
439                 }
440         case ToolbarItem::POPUPMENU: {
441                 QToolButton * tb = new QToolButton;
442                 tb->setPopupMode(QToolButton::InstantPopup);
443                 tb->setToolTip(qt_(to_ascii(item.label_)));
444                 tb->setStatusTip(qt_(to_ascii(item.label_)));
445                 tb->setText(qt_(to_ascii(item.label_)));
446                 tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
447                 connect(this, SIGNAL(iconSizeChanged(QSize)),
448                         tb, SLOT(setIconSize(QSize)));
449
450                 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
451                 m->setWindowTitle(qt_(to_ascii(item.label_)));
452                 m->setTearOffEnabled(true);
453                 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
454                 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
455                 if (!tbinfo) {
456                         lyxerr << "Unknown toolbar " << item.name_ << endl;
457                         break;
458                 }
459                 ToolbarInfo::item_iterator it = tbinfo->items.begin();
460                 ToolbarInfo::item_iterator const end = tbinfo->items.end();
461                 for (; it != end; ++it)
462                         if (!getStatus(it->func_).unknown())
463                                 m->add(addItem(*it));
464                 tb->setMenu(m);
465                 addWidget(tb);
466                 break;
467                 }
468         case ToolbarItem::COMMAND: {
469                 if (!getStatus(item.func_).unknown())
470                         addAction(addItem(item));
471                 break;
472                 }
473         default:
474                 break;
475         }
476 }
477
478
479 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
480 {
481         // if tbinfo.state == auto *do not* set on/off
482         if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
483                 if (GuiToolbar::isVisible())
484                         tbinfo.state = ToolbarSection::ToolbarInfo::ON;
485                 else
486                         tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
487         }
488         //
489         // no need to save it here.
490         Qt::ToolBarArea loc = owner_.toolBarArea(this);
491
492         if (loc == Qt::TopToolBarArea)
493                 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
494         else if (loc == Qt::BottomToolBarArea)
495                 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
496         else if (loc == Qt::RightToolBarArea)
497                 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
498         else if (loc == Qt::LeftToolBarArea)
499                 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
500         else
501                 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
502
503         // save toolbar position. They are not used to restore toolbar position
504         // now because move(x,y) does not work for toolbar.
505         tbinfo.posx = pos().x();
506         tbinfo.posy = pos().y();
507 }
508
509
510 void GuiToolbar::updateContents()
511 {
512         // update visible toolbars only
513         if (!isVisible())
514                 return;
515         // This is a speed bottleneck because this is called on every keypress
516         // and update calls getStatus, which copies the cursor at least two times
517         for (int i = 0; i < actions_.size(); ++i)
518                 actions_[i]->update();
519
520         // emit signal
521         updated();
522 }
523
524
525 } // namespace frontend
526 } // namespace lyx
527
528 #include "GuiToolbar_moc.cpp"