]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QLToolbar.cpp
Fix stuff that slipped in with 18812
[features.git] / src / frontends / qt4 / QLToolbar.cpp
1 /**
2  * \file qt4/QLToolbar.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 "LyXFunc.h"
24 #include "IconPalette.h"
25
26 #include "GuiView.h"
27 #include "QLToolbar.h"
28 #include "LyXAction.h"
29 #include "Action.h"
30 #include "qt_helpers.h"
31 #include "InsertTableWidget.h"
32 #include "support/filetools.h"
33 #include "support/lstrings.h"
34 #include "controllers/ControlMath.h"
35 #include "ToolbarBackend.h"
36
37 #include <QComboBox>
38 #include <QToolBar>
39 #include <QToolButton>
40 #include <QAction>
41 #include <QPixmap>
42
43 namespace lyx {
44
45 using std::string;
46 using std::endl;
47 using support::FileName;
48 using support::libFileSearch;
49 using support::subst;
50
51
52 using support::libFileSearch;
53 using support::subst;
54
55 namespace frontend {
56
57 namespace {
58
59 TextClass const & getTextClass(LyXView const & lv)
60 {
61         return lv.buffer()->params().getTextClass();
62 }
63
64
65 } // namespace anon
66
67
68 QLayoutBox::QLayoutBox(QToolBar * toolbar, GuiView & owner)
69         : owner_(owner)
70 {
71         combo_ = new QComboBox;
72         combo_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
73         combo_->setFocusPolicy(Qt::ClickFocus);
74         combo_->setMinimumWidth(combo_->sizeHint().width());
75         combo_->setMaxVisibleItems(100);
76
77         QObject::connect(combo_, SIGNAL(activated(const QString &)),
78                          this, SLOT(selected(const QString &)));
79
80         toolbar->addWidget(combo_);
81 }
82
83
84 void QLayoutBox::set(string const & layout)
85 {
86         TextClass const & tc = getTextClass(owner_);
87
88         QString const & name = qt_(tc[layout]->name());
89
90         int i = 0;
91         for (; i < combo_->count(); ++i) {
92                 if (name == combo_->itemText(i))
93                         break;
94         }
95
96         if (i == combo_->count()) {
97                 lyxerr << "Trying to select non existent layout type "
98                         << fromqstr(name) << endl;
99                 return;
100         }
101
102         combo_->setCurrentIndex(i);
103 }
104
105
106 void QLayoutBox::update()
107 {
108         TextClass const & tc = getTextClass(owner_);
109
110         combo_->setUpdatesEnabled(false);
111
112         combo_->clear();
113
114         TextClass::const_iterator it = tc.begin();
115         TextClass::const_iterator const end = tc.end();
116         for (; it != end; ++it) {
117                 // ignore obsolete entries
118                 if ((*it)->obsoleted_by().empty())
119                         combo_->addItem(qt_((*it)->name()));
120         }
121
122         // needed to recalculate size hint
123         combo_->hide();
124         combo_->setMinimumWidth(combo_->sizeHint().width());
125         combo_->show();
126
127         combo_->setUpdatesEnabled(true);
128         combo_->update();
129 }
130
131
132 void QLayoutBox::clear()
133 {
134         combo_->clear();
135 }
136
137
138 void QLayoutBox::open()
139 {
140         combo_->showPopup();
141 }
142
143
144 void QLayoutBox::setEnabled(bool enable)
145 {
146         // Workaround for Qt bug where setEnabled(true) closes
147         // the popup
148         if (enable != combo_->isEnabled())
149                 combo_->setEnabled(enable);
150 }
151
152
153 void QLayoutBox::selected(const QString & str)
154 {
155         string const sel = fromqstr(str);
156
157         owner_.setFocus();
158
159         layoutSelected(owner_, sel);
160 }
161
162
163 QLToolbar::QLToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
164         : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner)
165 {
166         // give visual separation between adjacent toolbars
167         addSeparator();
168
169         // TODO: save toolbar position
170         setMovable(true);
171
172         ToolbarInfo::item_iterator it = tbinfo.items.begin();
173         ToolbarInfo::item_iterator end = tbinfo.items.end();
174         for (; it != end; ++it)
175                 add(*it);
176 }
177
178
179 void QLToolbar::add(ToolbarItem const & item)
180 {
181         switch (item.type_) {
182         case ToolbarItem::SEPARATOR:
183                 addSeparator();
184                 break;
185         case ToolbarItem::LAYOUTS:
186                 layout_.reset(new QLayoutBox(this, owner_));
187                 break;
188         case ToolbarItem::MINIBUFFER:
189                 owner_.addCommandBuffer(this);
190                 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
191                 //setHorizontalStretchable(true);
192                 break;
193         case ToolbarItem::TABLEINSERT: {
194                 QToolButton * tb = new QToolButton;
195                 tb->setCheckable(true);
196                 tb->setIcon(QPixmap(toqstr(getIcon(FuncRequest(LFUN_TABULAR_INSERT)))));
197                 tb->setToolTip(qt_(to_ascii(item.label_)));
198                 tb->setStatusTip(qt_(to_ascii(item.label_)));
199                 tb->setText(qt_(to_ascii(item.label_)));
200                 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
201                 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
202                 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
203                 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
204                 addWidget(tb);
205                 break;
206                 }
207         case ToolbarItem::ICONPALETTE: {
208                 QToolButton * tb = new QToolButton(this);
209                 tb->setToolTip(qt_(to_ascii(item.label_)));
210                 tb->setStatusTip(qt_(to_ascii(item.label_)));
211                 tb->setText(qt_(to_ascii(item.label_)));
212                 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
213                         tb, SLOT(setIconSize(const QSize &)));
214
215 #if QT_VERSION >= 0x040200
216                 IconPalette * panel = new IconPalette(&owner_);
217                 connect(panel, SIGNAL(enabled(bool)),
218                         tb, SLOT(setEnabled(bool)));
219                 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
220                         panel, SLOT(setIconSize(const QSize &)));
221 #else
222                 IconPalette * panel = new IconPalette(tb);
223 #endif
224                 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
225                 ToolbarInfo const & tbinfo = toolbarbackend.getToolbar(item.name_);
226                 ToolbarInfo::item_iterator it = tbinfo.items.begin();
227                 ToolbarInfo::item_iterator const end = tbinfo.items.end();
228                 for (; it != end; ++it)
229                         if (!getStatus(it->func_).unknown()) {
230                                 Action * action = new Action(owner_,
231                                         getIcon(it->func_),
232                                         it->label_,
233                                         it->func_,
234                                         it->label_);
235                                 panel->addButton(action);
236                                 ActionVector.push_back(action);
237                                 // use the icon of first action for the toolbar button
238                                 if (it == tbinfo.items.begin())
239                                         tb->setIcon(QPixmap(getIcon(it->func_).c_str()));
240                         }
241
242 #if QT_VERSION >= 0x040200
243                 QMenu * m = new QMenu(tb);
244                 m->addAction(panel);
245                 m->setTearOffEnabled(true);
246                 m->setWindowTitle(qt_(to_ascii(item.label_)));
247                 tb->setPopupMode(QToolButton::InstantPopup);
248                 tb->setMenu(m);
249 #else
250                 tb->setCheckable(true);
251                 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
252                 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
253 #endif // QT_VERSION >= 0x040200
254
255                 addWidget(tb);
256                 break;
257                 }
258         case ToolbarItem::POPUPMENU: {
259                 QToolButton * tb = new QToolButton;
260                 tb->setPopupMode(QToolButton::InstantPopup);
261                 tb->setToolTip(qt_(to_ascii(item.label_)));
262                 tb->setStatusTip(qt_(to_ascii(item.label_)));
263                 tb->setText(qt_(to_ascii(item.label_)));
264                 FileName icon_path = libFileSearch("images/math", item.name_, "xpm");
265                 tb->setIcon(QIcon(toqstr(icon_path.absFilename())));
266                 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
267                         tb, SLOT(setIconSize(const QSize &)));
268
269                 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
270                 m->setWindowTitle(qt_(to_ascii(item.label_)));
271                 m->setTearOffEnabled(true);
272                 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
273                 ToolbarInfo const & tbinfo = toolbarbackend.getToolbar(item.name_);
274                 ToolbarInfo::item_iterator it = tbinfo.items.begin();
275                 ToolbarInfo::item_iterator const end = tbinfo.items.end();
276                 for (; it != end; ++it)
277                         if (!getStatus(it->func_).unknown()) {
278                                 Action * action = new Action(owner_,
279                                         getIcon(it->func_, false),
280                                         it->label_,
281                                         it->func_,
282                                         it->label_);
283                                 m->add(action);
284                                 ActionVector.push_back(action);
285                         }
286                 tb->setMenu(m);
287                 addWidget(tb);
288                 break;
289                 }
290         case ToolbarItem::COMMAND: {
291                 if (getStatus(item.func_).unknown())
292                         break;
293
294                 Action * action = new Action(owner_,
295                         getIcon(item.func_),
296                         item.label_,
297                         item.func_,
298                         item.label_);
299                 addAction(action);
300                 ActionVector.push_back(action);
301                 break;
302                 }
303         default:
304                 break;
305         }
306 }
307
308
309 void QLToolbar::hide(bool)
310 {
311         QToolBar::hide();
312 }
313
314
315 void QLToolbar::show(bool)
316 {
317         QToolBar::show();
318 }
319
320
321 bool QLToolbar::isVisible() const
322 {
323         return QToolBar::isVisible();
324 }
325
326
327 void QLToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
328 {
329         // if tbinfo.state == auto *do not* set on/off
330         if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
331                 if (QLToolbar::isVisible())
332                         tbinfo.state = ToolbarSection::ToolbarInfo::ON;
333                 else
334                         tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
335         }
336         //
337         // no need to save it here.
338         Qt::ToolBarArea loc = owner_.toolBarArea(this);
339
340         if (loc == Qt::TopToolBarArea)
341                 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
342         else if (loc == Qt::BottomToolBarArea)
343                 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
344         else if (loc == Qt::RightToolBarArea)
345                 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
346         else if (loc == Qt::LeftToolBarArea)
347                 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
348         else
349                 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
350
351         // save toolbar position. They are not used to restore toolbar position
352         // now because move(x,y) does not work for toolbar.
353         tbinfo.posx = pos().x();
354         tbinfo.posy = pos().y();
355 }
356
357
358 void QLToolbar::update()
359 {
360         // This is a speed bottleneck because this is called on every keypress
361         // and update calls getStatus, which copies the cursor at least two times
362         for (size_t i = 0; i < ActionVector.size(); ++i)
363                 ActionVector[i]->update();
364
365         // emit signal
366         updated();
367 }
368
369
370 string const getIcon(FuncRequest const & f, bool unknown)
371 {
372         using frontend::find_xpm;
373
374         string fullname;
375
376         switch (f.action) {
377         case LFUN_MATH_INSERT:
378                 if (!f.argument().empty())
379                         fullname = find_xpm(to_utf8(f.argument()).substr(1));
380                 break;
381         case LFUN_MATH_DELIM:
382         case LFUN_MATH_BIGDELIM:
383                 fullname = find_xpm(to_utf8(f.argument()));
384                 break;
385         default:
386                 string const name = lyxaction.getActionName(f.action);
387                 string xpm_name(name);
388
389                 if (!f.argument().empty())
390                         xpm_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
391
392                 fullname = libFileSearch("images", xpm_name, "xpm").absFilename();
393
394                 if (fullname.empty()) {
395                         // try without the argument
396                         fullname = libFileSearch("images", name, "xpm").absFilename();
397                 }
398         }
399
400         if (!fullname.empty()) {
401                 LYXERR(Debug::GUI) << "Full icon name is `"
402                                    << fullname << '\'' << endl;
403                 return fullname;
404         }
405
406         LYXERR(Debug::GUI) << "Cannot find icon for command \""
407                            << lyxaction.getActionName(f.action)
408                            << '(' << to_utf8(f.argument()) << ")\"" << endl;
409         if (unknown)
410                 return libFileSearch("images", "unknown", "xpm").absFilename();
411         else
412                 return string();
413 }
414
415
416 } // namespace frontend
417 } // namespace lyx
418
419 #include "QLToolbar_moc.cpp"