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