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