]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/Toolbar_pimpl.C
reverse last change
[lyx.git] / src / frontends / qt2 / Toolbar_pimpl.C
1 /**
2  * \file qt2/Toolbar_pimpl.C
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  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "ToolbarDefaults.h"
19 #include "debug.h"
20 #include "lyxfunc.h"
21 #include "FuncStatus.h"
22 #include "BufferView.h"
23 #include "buffer.h"
24 #include "LyXAction.h"
25 #include "qt_helpers.h"
26
27 #include "support/LAssert.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
30
31 #include "ControlMath.h"
32
33 #include <boost/tuple/tuple.hpp>
34
35 #include "QtView.h"
36 #include "Toolbar_pimpl.h"
37
38 #include <qtoolbar.h>
39 #include <qcombobox.h>
40 #include <qtooltip.h>
41 #include <qsizepolicy.h>
42
43 using std::endl;
44
45 namespace {
46
47 QPixmap getIconPixmap(int action)
48 {
49         FuncRequest f = lyxaction.retrieveActionArg(action);
50
51         string fullname;
52
53         if (f.action == LFUN_INSERT_MATH && !f.argument.empty()) {
54                 fullname = find_xpm(f.argument.substr(1));
55         } else {
56                 string const name = lyxaction.getActionName(f.action);
57                 string xpm_name(name);
58
59                 if (!f.argument.empty())
60                         xpm_name = subst(name + ' ' + f.argument, ' ','_');
61
62                 fullname = LibFileSearch("images", xpm_name, "xpm");
63         }
64
65
66         if (!fullname.empty()) {
67                 lyxerr[Debug::GUI] << "Full icon name is `"
68                                    << fullname << '\'' << endl;
69                 return QPixmap(toqstr(fullname));
70         }
71
72         lyxerr << "Unable to find icon `" << fullname << '\'' << endl;
73         fullname = LibFileSearch("images", "unknown", "xpm");
74         if (!fullname.empty()) {
75                 lyxerr[Debug::GUI] << "Using default `unknown' icon"
76                                    << endl;
77         }
78         return QPixmap(toqstr(fullname));
79 }
80
81 } // namespace anon
82
83
84 class QLComboBox : public QComboBox {
85 public:
86         QLComboBox(QWidget * parent) : QComboBox(parent) {}
87         void popup() { QComboBox::popup(); }
88 };
89
90
91 Toolbar::Pimpl::Pimpl(LyXView * o, int, int)
92         : owner_(static_cast<QtView *>(o)),
93         combo_(0)
94 {
95         proxy_.reset(new ToolbarProxy(*this));
96 }
97
98
99 Toolbar::Pimpl::~Pimpl()
100 {
101 }
102
103
104 void Toolbar::Pimpl::update()
105 {
106         ButtonMap::const_iterator p = map_.begin();
107         ButtonMap::const_iterator end = map_.end();
108
109         for (; p != end; ++p) {
110                 QToolButton * button = p->first;
111                 int action = p->second;
112
113                 FuncStatus const status =
114                         owner_->getLyXFunc().getStatus(action);
115
116                 button->setToggleButton(true);
117                 button->setOn(status.onoff(true));
118                 button->setEnabled(!status.disabled());
119         }
120
121         if (combo_)
122                 combo_->setEnabled(!owner_->getLyXFunc().getStatus(LFUN_LAYOUT).disabled());
123 }
124
125
126 void Toolbar::Pimpl::button_selected(QToolButton * button)
127 {
128         ButtonMap::const_iterator cit = map_.find(button);
129
130         if (cit == map_.end()) {
131                 lyxerr << "non existent tool button selected !" << endl;
132                 return;
133         }
134
135         owner_->getLyXFunc().dispatch(cit->second, true);
136 }
137
138
139 void Toolbar::Pimpl::changed_layout(string const & sel)
140 {
141         owner_->centralWidget()->setFocus();
142
143         LyXTextClass const & tc =
144                 owner_->buffer()->params.getLyXTextClass();
145
146         LyXTextClass::const_iterator end = tc.end();
147         for (LyXTextClass::const_iterator cit = tc.begin();
148              cit != end; ++cit) {
149                 // Yes, the _() is correct
150                 if (_((*cit)->name()) == sel) {
151                         owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
152                         return;
153                 }
154         }
155         lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
156                << endl;
157 }
158
159
160 void Toolbar::Pimpl::setLayout(string const & layout)
161 {
162         LyXTextClass const & tc =
163                 owner_->buffer()->params.getLyXTextClass();
164
165         QString const & name = qt_(tc[layout]->name());
166
167         int i = 0;
168         for (; i < combo_->count(); ++i) {
169                 if (name == combo_->text(i))
170                         break;
171         }
172
173         if (i == combo_->count()) {
174                 lyxerr << "Trying to select non existent layout type "
175                         << fromqstr(name) << endl;
176                 return;
177         }
178
179         combo_->setCurrentItem(i);
180 }
181
182
183 void Toolbar::Pimpl::updateLayoutList(bool force)
184 {
185         // if we don't need an update, don't ...
186         if (combo_->count() && !force)
187                 return;
188
189         LyXTextClass const & tc =
190                 owner_->buffer()->params.getLyXTextClass();
191
192         combo_->setUpdatesEnabled(false);
193
194         combo_->clear();
195
196         LyXTextClass::const_iterator cit = tc.begin();
197         LyXTextClass::const_iterator end = tc.end();
198         for (; cit != end; ++cit) {
199                 // ignore obsolete entries
200                 if ((*cit)->obsoleted_by().empty())
201                         combo_->insertItem(qt_((*cit)->name()));
202         }
203
204         // needed to recalculate size hint
205         combo_->hide();
206         combo_->setMinimumWidth(combo_->sizeHint().width());
207         combo_->show();
208
209         combo_->setUpdatesEnabled(true);
210         combo_->update();
211 }
212
213
214 void Toolbar::Pimpl::clearLayoutList()
215 {
216         combo_->clear();
217 }
218
219
220 void Toolbar::Pimpl::openLayoutList()
221 {
222         combo_->popup();
223 }
224
225
226 void Toolbar::Pimpl::add(int action)
227 {
228         if (!toolbars_.size()) {
229                 toolbars_.push_back(new QToolBar(owner_));
230         }
231
232         switch (action) {
233         case ToolbarDefaults::SEPARATOR:
234                 toolbars_.back()->addSeparator();
235                 break;
236         case ToolbarDefaults::NEWLINE:
237                 toolbars_.push_back(new QToolBar(owner_));
238                 break;
239         case ToolbarDefaults::LAYOUTS: {
240                 combo_ = new QLComboBox(toolbars_.back());
241                 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
242                 combo_->setSizePolicy(p);
243                 combo_->setFocusPolicy(QWidget::ClickFocus);
244                 combo_->setMinimumWidth(combo_->sizeHint().width());
245
246                 QObject::connect(combo_, SIGNAL(activated(const QString &)),
247                         proxy_.get(), SLOT(layout_selected(const QString &)));
248                 break;
249         }
250         default: {
251                 QToolButton * tb =
252                         new QToolButton(getIconPixmap(action),
253                         qt_(lyxaction.helpText(action)), "",
254                         proxy_.get(), SLOT(button_selected()), toolbars_.back());
255
256                 map_[tb] = action;
257                 break;
258         }
259         }
260 }