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