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