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