]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QLToolbar.cpp
f1bab092b5e17a111662d61b40ac6ef5140dd1af
[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                 IconPalette * panel = new IconPalette(tb);
215                 panel->setWindowTitle(qt_(to_ascii(item.label_)));
216                 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
217                 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
218                 if (!tbinfo) {
219                         lyxerr << "Unknown toolbar " << item.name_ << endl;
220                         break;
221                 }
222                 ToolbarInfo::item_iterator it = tbinfo->items.begin();
223                 ToolbarInfo::item_iterator const end = tbinfo->items.end();
224                 for (; it != end; ++it)
225                         if (!getStatus(it->func_).unknown()) {
226                                 Action * action = new Action(owner_,
227                                         getIcon(it->func_),
228                                         it->label_,
229                                         it->func_,
230                                         it->label_);
231                                 panel->addButton(action);
232                                 ActionVector.push_back(action);
233                                 // use the icon of first action for the toolbar button
234                                 if (it == tbinfo->items.begin())
235                                         tb->setIcon(QPixmap(getIcon(it->func_).c_str()));
236                         }
237                 tb->setCheckable(true);
238                 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
239                 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
240                 addWidget(tb);
241                 break;
242                 }
243         case ToolbarItem::POPUPMENU: {
244                 QToolButton * tb = new QToolButton;
245                 tb->setPopupMode(QToolButton::InstantPopup);
246                 tb->setToolTip(qt_(to_ascii(item.label_)));
247                 tb->setStatusTip(qt_(to_ascii(item.label_)));
248                 tb->setText(qt_(to_ascii(item.label_)));
249                 FileName icon_path = libFileSearch("images/math", item.name_, "xpm");
250                 tb->setIcon(QIcon(toqstr(icon_path.absFilename())));
251                 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
252                         tb, SLOT(setIconSize(const QSize &)));
253
254                 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
255                 m->setWindowTitle(qt_(to_ascii(item.label_)));
256                 m->setTearOffEnabled(true);
257                 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
258                 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
259                 if (!tbinfo) {
260                         lyxerr << "Unknown toolbar " << item.name_ << endl;
261                         break;
262                 }
263                 ToolbarInfo::item_iterator it = tbinfo->items.begin();
264                 ToolbarInfo::item_iterator const end = tbinfo->items.end();
265                 for (; it != end; ++it)
266                         if (!getStatus(it->func_).unknown()) {
267                                 Action * action = new Action(owner_,
268                                         getIcon(it->func_, false),
269                                         it->label_,
270                                         it->func_,
271                                         it->label_);
272                                 m->add(action);
273                                 ActionVector.push_back(action);
274                         }
275                 tb->setMenu(m);
276                 addWidget(tb);
277                 break;
278                 }
279         case ToolbarItem::COMMAND: {
280                 if (getStatus(item.func_).unknown())
281                         break;
282
283                 Action * action = new Action(owner_,
284                         getIcon(item.func_),
285                         item.label_,
286                         item.func_,
287                         item.label_);
288                 addAction(action);
289                 ActionVector.push_back(action);
290                 break;
291                 }
292         default:
293                 break;
294         }
295 }
296
297
298 void QLToolbar::hide(bool)
299 {
300         QToolBar::hide();
301 }
302
303
304 void QLToolbar::show(bool)
305 {
306         QToolBar::show();
307 }
308
309
310 bool QLToolbar::isVisible() const
311 {
312         return QToolBar::isVisible();
313 }
314
315
316 void QLToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
317 {
318         // if tbinfo.state == auto *do not* set on/off
319         if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
320                 if (QLToolbar::isVisible())
321                         tbinfo.state = ToolbarSection::ToolbarInfo::ON;
322                 else
323                         tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
324         }
325         //
326         // no need to save it here.
327         Qt::ToolBarArea loc = owner_.toolBarArea(this);
328
329         if (loc == Qt::TopToolBarArea)
330                 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
331         else if (loc == Qt::BottomToolBarArea)
332                 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
333         else if (loc == Qt::RightToolBarArea)
334                 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
335         else if (loc == Qt::LeftToolBarArea)
336                 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
337         else
338                 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
339
340         // save toolbar position. They are not used to restore toolbar position
341         // now because move(x,y) does not work for toolbar.
342         tbinfo.posx = pos().x();
343         tbinfo.posy = pos().y();
344 }
345
346
347 void QLToolbar::update()
348 {
349         // This is a speed bottleneck because this is called on every keypress
350         // and update calls getStatus, which copies the cursor at least two times
351         for (size_t i = 0; i < ActionVector.size(); ++i)
352                 ActionVector[i]->update();
353
354         // emit signal
355         updated();
356 }
357
358
359 string const getIcon(FuncRequest const & f, bool unknown)
360 {
361         using frontend::find_xpm;
362
363         string fullname;
364
365         switch (f.action) {
366         case LFUN_MATH_INSERT:
367                 if (!f.argument().empty())
368                         fullname = find_xpm(to_utf8(f.argument()).substr(1));
369                 break;
370         case LFUN_MATH_DELIM:
371         case LFUN_MATH_BIGDELIM:
372                 fullname = find_xpm(to_utf8(f.argument()));
373                 break;
374         default:
375                 string const name = lyxaction.getActionName(f.action);
376                 string xpm_name(name);
377
378                 if (!f.argument().empty())
379                         xpm_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
380
381                 fullname = libFileSearch("images", xpm_name, "xpm").absFilename();
382
383                 if (fullname.empty()) {
384                         // try without the argument
385                         fullname = libFileSearch("images", name, "xpm").absFilename();
386                 }
387         }
388
389         if (!fullname.empty()) {
390                 LYXERR(Debug::GUI) << "Full icon name is `"
391                                    << fullname << '\'' << endl;
392                 return fullname;
393         }
394
395         LYXERR(Debug::GUI) << "Cannot find icon for command \""
396                            << lyxaction.getActionName(f.action)
397                            << '(' << to_utf8(f.argument()) << ")\"" << endl;
398         if (unknown)
399                 return libFileSearch("images", "unknown", "xpm").absFilename();
400         else
401                 return string();
402 }
403
404
405 } // namespace frontend
406 } // namespace lyx
407
408 #include "QLToolbar_moc.cpp"