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