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