]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiToolbar.cpp
GuiWrap.cpp: disconnect the widgets that are by default disabled from readonly
[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 "LyXFunc.h"
25 #include "ToolbarBackend.h"
26
27 #include "GuiView.h"
28 #include "GuiCommandBuffer.h"
29 #include "GuiToolbar.h"
30 #include "LyXAction.h"
31 #include "Action.h"
32 #include "qt_helpers.h"
33 #include "InsertTableWidget.h"
34
35 #include "support/filetools.h"
36 #include "support/lstrings.h"
37
38 #include "controllers/ControlMath.h"
39
40 #include <QComboBox>
41 #include <QToolBar>
42 #include <QToolButton>
43 #include <QAction>
44 #include <QPixmap>
45
46 namespace lyx {
47
48 using std::string;
49 using std::endl;
50 using support::FileName;
51 using support::libFileSearch;
52 using support::subst;
53
54 namespace frontend {
55
56 static TextClass const & textClass(LyXView const & lv)
57 {
58         return lv.buffer()->params().getTextClass();
59 }
60
61
62 GuiLayoutBox::GuiLayoutBox(QToolBar * toolbar, GuiViewBase & owner)
63         : owner_(owner)
64 {
65         combo_ = new QComboBox;
66         combo_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
67         combo_->setFocusPolicy(Qt::ClickFocus);
68         combo_->setMinimumWidth(combo_->sizeHint().width());
69         combo_->setMaxVisibleItems(100);
70
71         QObject::connect(combo_, SIGNAL(activated(const QString &)),
72                          this, SLOT(selected(const QString &)));
73
74         toolbar->addWidget(combo_);
75 }
76
77
78 void GuiLayoutBox::set(docstring const & layout)
79 {
80         TextClass const & tc = textClass(owner_);
81
82         QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
83
84         int i = 0;
85         for (; i < combo_->count(); ++i) {
86                 if (name == combo_->itemText(i))
87                         break;
88         }
89
90         if (i == combo_->count()) {
91                 lyxerr << "Trying to select non existent layout type "
92                         << fromqstr(name) << endl;
93                 return;
94         }
95
96         combo_->setCurrentIndex(i);
97 }
98
99
100 void GuiLayoutBox::update()
101 {
102         TextClass const & tc = textClass(owner_);
103
104         combo_->setUpdatesEnabled(false);
105
106         combo_->clear();
107
108         TextClass::const_iterator it = tc.begin();
109         TextClass::const_iterator const end = tc.end();
110         for (; it != end; ++it) {
111                 // ignore obsolete entries
112                 if ((*it)->obsoleted_by().empty())
113                         combo_->addItem(toqstr(translateIfPossible((*it)->name())));
114         }
115
116         // needed to recalculate size hint
117         combo_->hide();
118         combo_->setMinimumWidth(combo_->sizeHint().width());
119         combo_->show();
120
121         combo_->setUpdatesEnabled(true);
122         combo_->update();
123 }
124
125
126 void GuiLayoutBox::clear()
127 {
128         combo_->clear();
129 }
130
131
132 void GuiLayoutBox::open()
133 {
134         combo_->showPopup();
135 }
136
137
138 void GuiLayoutBox::setEnabled(bool enable)
139 {
140         // Workaround for Qt bug where setEnabled(true) closes
141         // the popup
142         if (enable != combo_->isEnabled())
143                 combo_->setEnabled(enable);
144 }
145
146
147 void GuiLayoutBox::selected(const QString & str)
148 {
149         owner_.setFocus();
150
151         layoutSelected(owner_, qstring_to_ucs4(str));
152 }
153
154
155 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiViewBase & owner)
156         : QToolBar(qt_(tbinfo.gui_name), &owner), command_buffer_(0), owner_(owner)
157 {
158         // give visual separation between adjacent toolbars
159         addSeparator();
160
161         // TODO: save toolbar position
162         setMovable(true);
163
164         ToolbarInfo::item_iterator it = tbinfo.items.begin();
165         ToolbarInfo::item_iterator end = tbinfo.items.end();
166         for (; it != end; ++it)
167                 add(*it);
168 }
169
170
171 void GuiToolbar::focusCommandBuffer()
172 {
173         if (command_buffer_)
174                 command_buffer_->setFocus();
175 }
176
177
178 void GuiToolbar::add(ToolbarItem const & item)
179 {
180         switch (item.type_) {
181         case ToolbarItem::SEPARATOR:
182                 addSeparator();
183                 break;
184         case ToolbarItem::LAYOUTS:
185                 layout_.reset(new GuiLayoutBox(this, owner_));
186                 break;
187         case ToolbarItem::MINIBUFFER:
188                 command_buffer_ = new GuiCommandBuffer(&owner_);
189                 addWidget(command_buffer_);
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_, "png");
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 GuiToolbar::hide(bool)
299 {
300         QToolBar::hide();
301 }
302
303
304 void GuiToolbar::show(bool)
305 {
306         QToolBar::show();
307 }
308
309
310 bool GuiToolbar::isVisible() const
311 {
312         return QToolBar::isVisible();
313 }
314
315
316 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
317 {
318         // if tbinfo.state == auto *do not* set on/off
319         if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
320                 if (GuiToolbar::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 GuiToolbar::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_png;
362
363         string fullname;
364
365         switch (f.action) {
366         case LFUN_MATH_INSERT:
367                 if (!f.argument().empty())
368                         fullname = find_png(to_utf8(f.argument()).substr(1));
369                 break;
370         case LFUN_MATH_DELIM:
371         case LFUN_MATH_BIGDELIM:
372                 fullname = find_png(to_utf8(f.argument()));
373                 break;
374         default:
375                 string const name = lyxaction.getActionName(f.action);
376                 string png_name = name;
377
378                 if (!f.argument().empty())
379                         png_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
380
381                 fullname = libFileSearch("images", png_name, "png").absFilename();
382
383                 if (fullname.empty()) {
384                         // try without the argument
385                         fullname = libFileSearch("images", name, "png").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", "png").absFilename();
400         else
401                 return string();
402 }
403
404
405 } // namespace frontend
406 } // namespace lyx
407
408 #include "GuiToolbar_moc.cpp"