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