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