]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiToolbar.cpp
Compile fix
[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/lstrings.h"
37 #include "support/lyxalgo.h" // sorted
38
39 #include <QComboBox>
40 #include <QToolBar>
41 #include <QToolButton>
42 #include <QAction>
43 #include <QPixmap>
44
45
46 static void initializeResources()
47 {
48         extern void qInitResources();
49         static bool initialized = false;
50         if (!initialized) {
51                 qInitResources();
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::subst;
64 using support::compare;
65
66
67 namespace {
68
69 struct PngMap {
70         char const * key;
71         char const * value;
72 };
73
74
75 bool operator<(PngMap const & lhs, PngMap const & rhs)
76 {
77                 return compare(lhs.key, rhs.key) < 0;
78 }
79
80
81 class CompareKey {
82 public:
83         CompareKey(string const & name) : name_(name) {}
84         bool operator()(PngMap const & other) const { return other.key == name_; }
85 private:
86         string const name_;
87 };
88
89
90 PngMap sorted_png_map[] = {
91         { "Bumpeq", "bumpeq2" },
92         { "Cap", "cap2" },
93         { "Cup", "cup2" },
94         { "Delta", "delta2" },
95         { "Downarrow", "downarrow2" },
96         { "Gamma", "gamma2" },
97         { "Lambda", "lambda2" },
98         { "Leftarrow", "leftarrow2" },
99         { "Leftrightarrow", "leftrightarrow2" },
100         { "Longleftarrow", "longleftarrow2" },
101         { "Longleftrightarrow", "longleftrightarrow2" },
102         { "Longrightarrow", "longrightarrow2" },
103         { "Omega", "omega2" },
104         { "Phi", "phi2" },
105         { "Pi", "pi2" },
106         { "Psi", "psi2" },
107         { "Rightarrow", "rightarrow2" },
108         { "Sigma", "sigma2" },
109         { "Subset", "subset2" },
110         { "Supset", "supset2" },
111         { "Theta", "theta2" },
112         { "Uparrow", "uparrow2" },
113         { "Updownarrow", "updownarrow2" },
114         { "Upsilon", "upsilon2" },
115         { "Vdash", "vdash3" },
116         { "Xi", "xi2" },
117         { "nLeftarrow", "nleftarrow2" },
118         { "nLeftrightarrow", "nleftrightarrow2" },
119         { "nRightarrow", "nrightarrow2" },
120         { "nVDash", "nvdash3" },
121         { "nvDash", "nvdash2" },
122         { "textrm \\AA", "textrm_AA"},
123         { "textrm \\O", "textrm_Oe"},
124         { "vDash", "vdash2" }
125 };
126
127 size_t const nr_sorted_png_map = sizeof(sorted_png_map) / sizeof(PngMap);
128
129
130 string const find_png(string const & name)
131 {
132         PngMap const * const begin = sorted_png_map;
133         PngMap const * const end = begin + nr_sorted_png_map;
134         BOOST_ASSERT(sorted(begin, end));
135
136         PngMap const * const it = std::find_if(begin, end, CompareKey(name));
137
138         string png_name;
139         if (it != end)
140                 png_name = it->value;
141         else {
142                 png_name = subst(name, "_", "underscore");
143                 png_name = subst(png_name, ' ', '_');
144
145                 // This way we can have "math-delim { }" on the toolbar.
146                 png_name = subst(png_name, "(", "lparen");
147                 png_name = subst(png_name, ")", "rparen");
148                 png_name = subst(png_name, "[", "lbracket");
149                 png_name = subst(png_name, "]", "rbracket");
150                 png_name = subst(png_name, "{", "lbrace");
151                 png_name = subst(png_name, "}", "rbrace");
152                 png_name = subst(png_name, "|", "bars");
153                 png_name = subst(png_name, ",", "thinspace");
154                 png_name = subst(png_name, ":", "mediumspace");
155                 png_name = subst(png_name, ";", "thickspace");
156                 png_name = subst(png_name, "!", "negthinspace");
157         }
158
159         LYXERR(Debug::GUI) << "find_png(" << name << ")\n"
160                            << "Looking for math PNG called \""
161                            << png_name << '"' << std::endl;
162         return png_name;
163 }
164
165 } // namespace anon
166
167
168 /// return a icon for the given action
169 static QIcon getIcon(FuncRequest const & f, bool unknown)
170 {
171         initializeResources();
172         QPixmap pm;
173         string name1;
174         string name2;
175
176         switch (f.action) {
177         case LFUN_MATH_INSERT:
178                 if (!f.argument().empty())
179                         name1 = "math/" + find_png(to_utf8(f.argument()).substr(1));
180                 break;
181         case LFUN_MATH_DELIM:
182         case LFUN_MATH_BIGDELIM:
183                 name1 = "math/" + find_png(to_utf8(f.argument()));
184                 break;
185         default:
186                 name2 = lyxaction.getActionName(f.action);
187                 name1 = name2;
188
189                 if (!f.argument().empty())
190                         name1 = subst(name2 + ' ' + to_utf8(f.argument()), ' ', '_');
191         }
192
193         if (pm.load(":/images/" + toqstr(name1) + ".png"))
194                 return pm;
195
196         if (pm.load(":/images/" + toqstr(name2) + ".png"))
197                 return pm;
198
199         LYXERR(Debug::GUI) << "Cannot find icon for command \""
200                            << lyxaction.getActionName(f.action)
201                            << '(' << to_utf8(f.argument()) << ")\"" << endl;
202         if (unknown)
203                 pm.load(":/images/unknown.png");
204
205         return pm;
206 }
207
208
209 static TextClass const & textClass(LyXView const & lv)
210 {
211         return lv.buffer()->params().getTextClass();
212 }
213
214
215 /////////////////////////////////////////////////////////////////////
216 //
217 // GuiLayoutBox
218 //
219 /////////////////////////////////////////////////////////////////////
220
221 GuiLayoutBox::GuiLayoutBox(GuiViewBase & owner)
222         : owner_(owner)
223 {
224         setSizeAdjustPolicy(QComboBox::AdjustToContents);
225         setFocusPolicy(Qt::ClickFocus);
226         setMinimumWidth(sizeHint().width());
227         setMaxVisibleItems(100);
228
229         QObject::connect(this, SIGNAL(activated(QString)),
230                          this, SLOT(selected(QString)));
231 }
232
233
234 void GuiLayoutBox::set(docstring const & layout)
235 {
236         TextClass const & tc = textClass(owner_);
237
238         QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
239
240         int i = 0;
241         for (; i < count(); ++i) {
242                 if (name == itemText(i))
243                         break;
244         }
245
246         if (i == count()) {
247                 lyxerr << "Trying to select non existent layout type "
248                         << fromqstr(name) << endl;
249                 return;
250         }
251
252         setCurrentIndex(i);
253 }
254
255
256 void GuiLayoutBox::updateContents()
257 {
258         TextClass const & tc = textClass(owner_);
259
260         setUpdatesEnabled(false);
261         clear();
262
263         TextClass::const_iterator it = tc.begin();
264         TextClass::const_iterator const end = tc.end();
265         for (; it != end; ++it) {
266                 // ignore obsolete entries
267                 addItem(toqstr(translateIfPossible((*it)->name())));
268         }
269
270         // needed to recalculate size hint
271         hide();
272         setMinimumWidth(sizeHint().width());
273         show();
274
275         setUpdatesEnabled(true);
276 }
277
278
279 void GuiLayoutBox::selected(const QString & str)
280 {
281         owner_.setFocus();
282         TextClass const & tc = owner_.buffer()->params().getTextClass();
283         docstring const name = qstring_to_ucs4(str);
284         TextClass::const_iterator it  = tc.begin();
285         TextClass::const_iterator const end = tc.end();
286         for (; it != end; ++it) {
287                 docstring const & itname = (*it)->name();
288                 if (translateIfPossible(itname) == name) {
289                         FuncRequest const func(LFUN_LAYOUT, itname,
290                                                FuncRequest::TOOLBAR);
291                         owner_.dispatch(func);
292                         return;
293                 }
294         }
295         lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
296 }
297
298
299
300 /////////////////////////////////////////////////////////////////////
301 //
302 // GuiToolbar
303 //
304 /////////////////////////////////////////////////////////////////////
305
306
307 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiViewBase & owner)
308         : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
309           layout_(0), command_buffer_(0)
310 {
311         // give visual separation between adjacent toolbars
312         addSeparator();
313
314         // TODO: save toolbar position
315         setMovable(true);
316
317         ToolbarInfo::item_iterator it = tbinfo.items.begin();
318         ToolbarInfo::item_iterator end = tbinfo.items.end();
319         for (; it != end; ++it)
320                 add(*it);
321 }
322
323
324 Action * GuiToolbar::addItem(ToolbarItem const & item)
325 {
326         Action * act = new Action(owner_,
327                 getIcon(item.func_, false),
328           toqstr(item.label_), item.func_, toqstr(item.label_));
329         actions_.append(act);
330         return act;
331 }
332
333
334 void GuiToolbar::add(ToolbarItem const & item)
335 {
336         switch (item.type_) {
337         case ToolbarItem::SEPARATOR:
338                 addSeparator();
339                 break;
340         case ToolbarItem::LAYOUTS:
341                 layout_ = new GuiLayoutBox(owner_);
342                 addWidget(layout_);
343                 break;
344         case ToolbarItem::MINIBUFFER:
345                 command_buffer_ = new GuiCommandBuffer(&owner_);
346                 addWidget(command_buffer_);
347                 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
348                 //setHorizontalStretchable(true);
349                 break;
350         case ToolbarItem::TABLEINSERT: {
351                 QToolButton * tb = new QToolButton;
352                 tb->setCheckable(true);
353                 tb->setIcon(getIcon(FuncRequest(LFUN_TABULAR_INSERT), true));
354                 tb->setToolTip(qt_(to_ascii(item.label_)));
355                 tb->setStatusTip(qt_(to_ascii(item.label_)));
356                 tb->setText(qt_(to_ascii(item.label_)));
357                 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
358                 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
359                 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
360                 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
361                 addWidget(tb);
362                 break;
363                 }
364         case ToolbarItem::ICONPALETTE: {
365                 QToolButton * tb = new QToolButton(this);
366                 tb->setToolTip(qt_(to_ascii(item.label_)));
367                 tb->setStatusTip(qt_(to_ascii(item.label_)));
368                 tb->setText(qt_(to_ascii(item.label_)));
369                 connect(this, SIGNAL(iconSizeChanged(QSize)),
370                         tb, SLOT(setIconSize(QSize)));
371                 IconPalette * panel = new IconPalette(tb);
372                 panel->setWindowTitle(qt_(to_ascii(item.label_)));
373                 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
374                 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
375                 if (!tbinfo) {
376                         lyxerr << "Unknown toolbar " << item.name_ << endl;
377                         break;
378                 }
379                 ToolbarInfo::item_iterator it = tbinfo->items.begin();
380                 ToolbarInfo::item_iterator const end = tbinfo->items.end();
381                 for (; it != end; ++it)
382                         if (!getStatus(it->func_).unknown()) {
383                                 panel->addButton(addItem(*it));
384                                 // use the icon of first action for the toolbar button
385                                 if (it == tbinfo->items.begin())
386                                         tb->setIcon(getIcon(it->func_, true));
387                         }
388                 tb->setCheckable(true);
389                 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
390                 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
391                 addWidget(tb);
392                 break;
393                 }
394         case ToolbarItem::POPUPMENU: {
395                 QToolButton * tb = new QToolButton;
396                 tb->setPopupMode(QToolButton::InstantPopup);
397                 tb->setToolTip(qt_(to_ascii(item.label_)));
398                 tb->setStatusTip(qt_(to_ascii(item.label_)));
399                 tb->setText(qt_(to_ascii(item.label_)));
400                 tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
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 } // namespace frontend
480 } // namespace lyx
481
482 #include "GuiToolbar_moc.cpp"