]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/Toolbar_pimpl.C
math icon name fixups
[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 "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 "ControlMath.h" 
34 #include "Toolbar_pimpl.h"
35
36 #include <boost/tuple/tuple.hpp>
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(fullname.c_str());
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(fullname.c_str());
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                 if (_((*cit)->name()) == sel) {
150                         owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
151                         return;
152                 }
153         }
154         lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
155                << endl;
156 }
157
158
159 void Toolbar::Pimpl::setLayout(string const & layout)
160 {
161         LyXTextClass const & tc =
162                 owner_->buffer()->params.getLyXTextClass();
163
164         string const & name = _(tc[layout]->name());
165
166         int i = 0;
167         for (; i < combo_->count(); ++i) {
168                 if (name == combo_->text(i).latin1())
169                         break;
170         }
171
172         if (i == combo_->count()) {
173                 lyxerr << "Trying to select non existent layout type "
174                         << name << endl;
175                 return;
176         }
177
178         combo_->setCurrentItem(i);
179 }
180
181
182 void Toolbar::Pimpl::updateLayoutList(bool force)
183 {
184         // if we don't need an update, don't ...
185         if (combo_->count() && !force)
186                 return;
187
188         LyXTextClass const & tc =
189                 owner_->buffer()->params.getLyXTextClass();
190
191         combo_->setUpdatesEnabled(false);
192
193         combo_->clear();
194
195         LyXTextClass::const_iterator cit = tc.begin();
196         LyXTextClass::const_iterator end = tc.end();
197         for (; cit != end; ++cit) {
198                 // ignore obsolete entries
199                 if ((*cit)->obsoleted_by().empty())
200                         combo_->insertItem(_((*cit)->name()).c_str());
201         }
202
203         // needed to recalculate size hint
204         combo_->hide();
205         combo_->setMinimumWidth(combo_->sizeHint().width());
206         combo_->show();
207
208         combo_->setUpdatesEnabled(true);
209         combo_->update();
210 }
211
212
213 void Toolbar::Pimpl::clearLayoutList()
214 {
215         combo_->clear();
216 }
217
218
219 void Toolbar::Pimpl::openLayoutList()
220 {
221         combo_->popup();
222 }
223
224
225 void Toolbar::Pimpl::add(int action)
226 {
227         if (!toolbars_.size()) {
228                 toolbars_.push_back(new QToolBar(owner_));
229         }
230
231         switch (action) {
232         case ToolbarDefaults::SEPARATOR:
233                 toolbars_.back()->addSeparator();
234                 break;
235         case ToolbarDefaults::NEWLINE:
236                 toolbars_.push_back(new QToolBar(owner_));
237                 break;
238         case ToolbarDefaults::LAYOUTS: {
239                 combo_ = new QLComboBox(toolbars_.back());
240                 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
241                 combo_->setSizePolicy(p);
242                 combo_->setFocusPolicy(QWidget::ClickFocus);
243                 combo_->setMinimumWidth(combo_->sizeHint().width());
244
245                 QObject::connect(combo_, SIGNAL(activated(const QString &)),
246                         proxy_.get(), SLOT(layout_selected(const QString &)));
247                 break;
248         }
249         default: {
250                 QToolButton * tb =
251                         new QToolButton(getIconPixmap(action),
252                         _(lyxaction.helpText(action)).c_str(), "",
253                         proxy_.get(), SLOT(button_selected()), toolbars_.back());
254
255                 map_[tb] = action;
256                 break;
257         }
258         }
259 }