]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiToolbar.cpp
Introducing TextClassPtr.h to minimize header dependencies.
[features.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
37 #include "support/filetools.h"
38 #include "support/lstrings.h"
39 #include "support/lyxalgo.h" // sorted
40
41 #include <QComboBox>
42 #include <QToolBar>
43 #include <QToolButton>
44 #include <QAction>
45 #include <QPixmap>
46
47 #include <boost/assert.hpp>
48
49
50 static void initializeResources()
51 {
52         static bool initialized = false;
53         if (!initialized) {
54                 Q_INIT_RESOURCE(Resources); 
55                 initialized = true;
56         }
57 }
58
59
60 namespace lyx {
61 namespace frontend {
62
63 using std::string;
64 using std::endl;
65
66 using support::libFileSearch;
67 using support::subst;
68 using support::compare;
69
70
71 namespace {
72
73 struct PngMap {
74         char const * key;
75         char const * value;
76 };
77
78
79 bool operator<(PngMap const & lhs, PngMap const & rhs)
80 {
81                 return compare(lhs.key, rhs.key) < 0;
82 }
83
84
85 class CompareKey {
86 public:
87         CompareKey(string const & name) : name_(name) {}
88         bool operator()(PngMap const & other) const { return other.key == name_; }
89 private:
90         string const name_;
91 };
92
93
94 PngMap sorted_png_map[] = {
95         { "Bumpeq", "bumpeq2" },
96         { "Cap", "cap2" },
97         { "Cup", "cup2" },
98         { "Delta", "delta2" },
99         { "Downarrow", "downarrow2" },
100         { "Gamma", "gamma2" },
101         { "Lambda", "lambda2" },
102         { "Leftarrow", "leftarrow2" },
103         { "Leftrightarrow", "leftrightarrow2" },
104         { "Longleftarrow", "longleftarrow2" },
105         { "Longleftrightarrow", "longleftrightarrow2" },
106         { "Longrightarrow", "longrightarrow2" },
107         { "Omega", "omega2" },
108         { "Phi", "phi2" },
109         { "Pi", "pi2" },
110         { "Psi", "psi2" },
111         { "Rightarrow", "rightarrow2" },
112         { "Sigma", "sigma2" },
113         { "Subset", "subset2" },
114         { "Supset", "supset2" },
115         { "Theta", "theta2" },
116         { "Uparrow", "uparrow2" },
117         { "Updownarrow", "updownarrow2" },
118         { "Upsilon", "upsilon2" },
119         { "Vdash", "vdash3" },
120         { "Xi", "xi2" },
121         { "nLeftarrow", "nleftarrow2" },
122         { "nLeftrightarrow", "nleftrightarrow2" },
123         { "nRightarrow", "nrightarrow2" },
124         { "nVDash", "nvdash3" },
125         { "nvDash", "nvdash2" },
126         { "textrm \\AA", "textrm_AA"},
127         { "textrm \\O", "textrm_Oe"},
128         { "vDash", "vdash2" }
129 };
130
131 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
132
133
134 string const find_png(string const & name)
135 {
136         PngMap const * const begin = sorted_png_map;
137         PngMap const * const end = begin + nr_sorted_png_map;
138         BOOST_ASSERT(sorted(begin, end));
139
140         PngMap const * const it = std::find_if(begin, end, CompareKey(name));
141
142         string png_name;
143         if (it != end)
144                 png_name = it->value;
145         else {
146                 png_name = subst(name, "_", "underscore");
147                 png_name = subst(png_name, ' ', '_');
148
149                 // This way we can have "math-delim { }" on the toolbar.
150                 png_name = subst(png_name, "(", "lparen");
151                 png_name = subst(png_name, ")", "rparen");
152                 png_name = subst(png_name, "[", "lbracket");
153                 png_name = subst(png_name, "]", "rbracket");
154                 png_name = subst(png_name, "{", "lbrace");
155                 png_name = subst(png_name, "}", "rbrace");
156                 png_name = subst(png_name, "|", "bars");
157                 png_name = subst(png_name, ",", "thinspace");
158                 png_name = subst(png_name, ":", "mediumspace");
159                 png_name = subst(png_name, ";", "thickspace");
160                 png_name = subst(png_name, "!", "negthinspace");
161         }
162
163         LYXERR(Debug::GUI) << "find_png(" << name << ")\n"
164                            << "Looking for math PNG called \""
165                            << png_name << '"' << std::endl;
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()) << ")\"" << endl;
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::updateContents()
278 {
279         TextClass const & tc = textClass(owner_);
280
281         setUpdatesEnabled(false);
282         clear();
283
284         TextClass::const_iterator it = tc.begin();
285         TextClass::const_iterator const end = tc.end();
286         for (; it != end; ++it) {
287                 // ignore obsolete entries
288                 addItem(toqstr(translateIfPossible((*it)->name())));
289         }
290
291         // needed to recalculate size hint
292         hide();
293         setMinimumWidth(sizeHint().width());
294         show();
295
296         setUpdatesEnabled(true);
297 }
298
299
300 void GuiLayoutBox::selected(const QString & str)
301 {
302         owner_.setFocus();
303         TextClass const & tc = owner_.buffer()->params().getTextClass();
304         docstring const name = qstring_to_ucs4(str);
305         TextClass::const_iterator it  = tc.begin();
306         TextClass::const_iterator const end = tc.end();
307         for (; it != end; ++it) {
308                 docstring const & itname = (*it)->name();
309                 if (translateIfPossible(itname) == name) {
310                         FuncRequest const func(LFUN_LAYOUT, itname,
311                                                FuncRequest::TOOLBAR);
312                         owner_.dispatch(func);
313                         return;
314                 }
315         }
316         lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
317 }
318
319
320
321 /////////////////////////////////////////////////////////////////////
322 //
323 // GuiToolbar
324 //
325 /////////////////////////////////////////////////////////////////////
326
327
328 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
329         : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
330           layout_(0), command_buffer_(0)
331 {
332         // give visual separation between adjacent toolbars
333         addSeparator();
334
335         // TODO: save toolbar position
336         setMovable(true);
337
338         ToolbarInfo::item_iterator it = tbinfo.items.begin();
339         ToolbarInfo::item_iterator end = tbinfo.items.end();
340         for (; it != end; ++it)
341                 add(*it);
342 }
343
344
345 Action * GuiToolbar::addItem(ToolbarItem const & item)
346 {
347         Action * act = new Action(owner_,
348                 getIcon(item.func_, false),
349           toqstr(item.label_), item.func_, toqstr(item.label_));
350         actions_.append(act);
351         return act;
352 }
353
354
355 void GuiToolbar::add(ToolbarItem const & item)
356 {
357         switch (item.type_) {
358         case ToolbarItem::SEPARATOR:
359                 addSeparator();
360                 break;
361         case ToolbarItem::LAYOUTS:
362                 layout_ = new GuiLayoutBox(owner_);
363                 addWidget(layout_);
364                 break;
365         case ToolbarItem::MINIBUFFER:
366                 command_buffer_ = new GuiCommandBuffer(&owner_);
367                 addWidget(command_buffer_);
368                 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
369                 //setHorizontalStretchable(true);
370                 break;
371         case ToolbarItem::TABLEINSERT: {
372                 QToolButton * tb = new QToolButton;
373                 tb->setCheckable(true);
374                 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
375                 tb->setToolTip(qt_(to_ascii(item.label_)));
376                 tb->setStatusTip(qt_(to_ascii(item.label_)));
377                 tb->setText(qt_(to_ascii(item.label_)));
378                 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
379                 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
380                 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
381                 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
382                 addWidget(tb);
383                 break;
384                 }
385         case ToolbarItem::ICONPALETTE: {
386                 QToolButton * tb = new QToolButton(this);
387                 tb->setToolTip(qt_(to_ascii(item.label_)));
388                 tb->setStatusTip(qt_(to_ascii(item.label_)));
389                 tb->setText(qt_(to_ascii(item.label_)));
390                 connect(this, SIGNAL(iconSizeChanged(QSize)),
391                         tb, SLOT(setIconSize(QSize)));
392                 IconPalette * panel = new IconPalette(tb);
393                 panel->setWindowTitle(qt_(to_ascii(item.label_)));
394                 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
395                 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
396                 if (!tbinfo) {
397                         lyxerr << "Unknown toolbar " << item.name_ << endl;
398                         break;
399                 }
400                 ToolbarInfo::item_iterator it = tbinfo->items.begin();
401                 ToolbarInfo::item_iterator const end = tbinfo->items.end();
402                 for (; it != end; ++it)
403                         if (!getStatus(it->func_).unknown()) {
404                                 panel->addButton(addItem(*it));
405                                 // use the icon of first action for the toolbar button
406                                 if (it == tbinfo->items.begin())
407                                         tb->setIcon(getIcon(it->func_, true));
408                         }
409                 tb->setCheckable(true);
410                 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
411                 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
412                 addWidget(tb);
413                 break;
414                 }
415         case ToolbarItem::POPUPMENU: {
416                 QToolButton * tb = new QToolButton;
417                 tb->setPopupMode(QToolButton::InstantPopup);
418                 tb->setToolTip(qt_(to_ascii(item.label_)));
419                 tb->setStatusTip(qt_(to_ascii(item.label_)));
420                 tb->setText(qt_(to_ascii(item.label_)));
421                 tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
422                 connect(this, SIGNAL(iconSizeChanged(QSize)),
423                         tb, SLOT(setIconSize(QSize)));
424
425                 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
426                 m->setWindowTitle(qt_(to_ascii(item.label_)));
427                 m->setTearOffEnabled(true);
428                 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
429                 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
430                 if (!tbinfo) {
431                         lyxerr << "Unknown toolbar " << item.name_ << endl;
432                         break;
433                 }
434                 ToolbarInfo::item_iterator it = tbinfo->items.begin();
435                 ToolbarInfo::item_iterator const end = tbinfo->items.end();
436                 for (; it != end; ++it)
437                         if (!getStatus(it->func_).unknown())
438                                 m->add(addItem(*it));
439                 tb->setMenu(m);
440                 addWidget(tb);
441                 break;
442                 }
443         case ToolbarItem::COMMAND: {
444                 if (!getStatus(item.func_).unknown())
445                         addAction(addItem(item));
446                 break;
447                 }
448         default:
449                 break;
450         }
451 }
452
453
454 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
455 {
456         // if tbinfo.state == auto *do not* set on/off
457         if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
458                 if (GuiToolbar::isVisible())
459                         tbinfo.state = ToolbarSection::ToolbarInfo::ON;
460                 else
461                         tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
462         }
463         //
464         // no need to save it here.
465         Qt::ToolBarArea loc = owner_.toolBarArea(this);
466
467         if (loc == Qt::TopToolBarArea)
468                 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
469         else if (loc == Qt::BottomToolBarArea)
470                 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
471         else if (loc == Qt::RightToolBarArea)
472                 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
473         else if (loc == Qt::LeftToolBarArea)
474                 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
475         else
476                 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
477
478         // save toolbar position. They are not used to restore toolbar position
479         // now because move(x,y) does not work for toolbar.
480         tbinfo.posx = pos().x();
481         tbinfo.posy = pos().y();
482 }
483
484
485 void GuiToolbar::updateContents()
486 {
487         // update visible toolbars only
488         if (!isVisible())
489                 return;
490         // This is a speed bottleneck because this is called on every keypress
491         // and update calls getStatus, which copies the cursor at least two times
492         for (int i = 0; i < actions_.size(); ++i)
493                 actions_[i]->update();
494
495         // emit signal
496         updated();
497 }
498
499
500 } // namespace frontend
501 } // namespace lyx
502
503 #include "GuiToolbar_moc.cpp"