]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/Toolbar_pimpl.C
qtabular skeleton
[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 "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 = 0;
165         for (; i < combo_->count(); ++i) {
166                 if (name == combo_->text(i).latin1())
167                         break;
168         }
169
170         if (i == combo_->count()) {
171                 lyxerr << "Trying to select non existent layout type "
172                         << 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(_((*cit)->name()).c_str());
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                         _(lyxaction.helpText(action)).c_str(), "",
251                         proxy_.get(), SLOT(button_selected()), toolbars_.back());
252
253                 map_[tb] = action;
254                 break;
255         }
256         }
257 }