]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLToolbar.C
split LyXFunc::getStatus() into inset specific chunks
[lyx.git] / src / frontends / qt2 / QLToolbar.C
1 /**
2  * \file qt2/QLToolbar.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  * \author Jean-Marc Lasgouttes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "debug.h"
18 #include "funcrequest.h"
19 #include "FuncStatus.h"
20 #include "gettext.h"
21 #include "lyxfunc.h"
22
23 #include "QtView.h"
24 #include "QLToolbar.h"
25
26 #include <qcombobox.h>
27 #include <qtoolbar.h>
28
29 using std::endl;
30 using std::string;
31
32
33 class QLComboBox : public QComboBox {
34 public:
35         QLComboBox(QWidget * parent) : QComboBox(parent) {}
36         void popup() { QComboBox::popup(); }
37 };
38
39
40 QLToolbar::QLToolbar(LyXView * o)
41         : owner_(static_cast<QtView *>(o)),
42           combo_(0)
43 {
44         proxy_.reset(new ToolbarProxy(*this));
45 }
46
47
48 void QLToolbar::displayToolbar(ToolbarBackend::Toolbar const & tb, bool show)
49 {
50         QToolBar * qtb = toolbars_[tb.name];
51         if (show) {
52                 qtb->show();
53         } else {
54                 qtb->hide();
55         }
56 }
57
58
59 void QLToolbar::update()
60 {
61         ButtonMap::const_iterator p = map_.begin();
62         ButtonMap::const_iterator end = map_.end();
63
64         for (; p != end; ++p) {
65                 QToolButton * button = p->first;
66                 FuncRequest const & func = p->second;
67
68                 FuncStatus const status =
69                         owner_->getLyXFunc().getStatus(func);
70
71                 button->setToggleButton(true);
72                 button->setOn(status.onoff(true));
73                 button->setEnabled(status.enabled());
74         }
75
76         bool const enable = owner_->getLyXFunc().
77                 getStatus(FuncRequest(LFUN_LAYOUT)).enabled();
78
79         // Workaround for Qt bug where setEnabled(true) closes
80         // the popup
81         if (combo_ && enable != combo_->isEnabled())
82                 combo_->setEnabled(enable);
83 }
84
85
86 void QLToolbar::button_selected(QToolButton * button)
87 {
88         ButtonMap::const_iterator cit = map_.find(button);
89
90         if (cit == map_.end()) {
91                 lyxerr << "non existent tool button selected !" << endl;
92                 return;
93         }
94
95         owner_->getLyXFunc().dispatch(cit->second, true);
96 }
97
98
99 void QLToolbar::changed_layout(string const & sel)
100 {
101         owner_->centralWidget()->setFocus();
102
103         LyXTextClass const & tc =
104                 owner_->buffer()->params().getLyXTextClass();
105
106         LyXTextClass::const_iterator end = tc.end();
107         for (LyXTextClass::const_iterator cit = tc.begin();
108              cit != end; ++cit) {
109                 // Yes, the _() is correct
110                 if (_((*cit)->name()) == sel) {
111                         owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
112                         return;
113                 }
114         }
115         lyxerr << "ERROR (QLToolbar::layoutSelected): layout not found!"
116                << endl;
117 }
118
119
120 void QLToolbar::setLayout(string const & layout)
121 {
122         if (!combo_)
123                 return;
124
125         LyXTextClass const & tc =
126                 owner_->buffer()->params().getLyXTextClass();
127
128         QString const & name = qt_(tc[layout]->name());
129
130         int i = 0;
131         for (; i < combo_->count(); ++i) {
132                 if (name == combo_->text(i))
133                         break;
134         }
135
136         if (i == combo_->count()) {
137                 lyxerr << "Trying to select non existent layout type "
138                         << fromqstr(name) << endl;
139                 return;
140         }
141
142         combo_->setCurrentItem(i);
143 }
144
145
146 void QLToolbar::updateLayoutList()
147 {
148         if (!combo_)
149                 return;
150
151         LyXTextClass const & tc =
152                 owner_->buffer()->params().getLyXTextClass();
153
154         combo_->setUpdatesEnabled(false);
155
156         combo_->clear();
157
158         LyXTextClass::const_iterator cit = tc.begin();
159         LyXTextClass::const_iterator end = tc.end();
160         for (; cit != end; ++cit) {
161                 // ignore obsolete entries
162                 if ((*cit)->obsoleted_by().empty())
163                         combo_->insertItem(qt_((*cit)->name()));
164         }
165
166         // needed to recalculate size hint
167         combo_->hide();
168         combo_->setMinimumWidth(combo_->sizeHint().width());
169         combo_->show();
170
171         combo_->setUpdatesEnabled(true);
172         combo_->update();
173 }
174
175
176 void QLToolbar::clearLayoutList()
177 {
178         if (!combo_)
179                 return;
180
181         Toolbar::clearLayoutList();
182         combo_->clear();
183 }
184
185
186 void QLToolbar::openLayoutList()
187 {
188         if (!combo_)
189                 return;
190
191         combo_->popup();
192 }
193
194
195 namespace {
196
197 QMainWindow::ToolBarDock getPosition(ToolbarBackend::Flags const & flags)
198 {
199         if (flags & ToolbarBackend::TOP)
200                 return QMainWindow::Top;
201         if (flags & ToolbarBackend::BOTTOM)
202                 return QMainWindow::Bottom;
203         if (flags & ToolbarBackend::LEFT)
204                 return QMainWindow::Left;
205         if (flags & ToolbarBackend::RIGHT)
206                 return QMainWindow::Right;
207         return QMainWindow::Top;
208 }
209
210 };
211
212
213 void QLToolbar::add(ToolbarBackend::Toolbar const & tb)
214 {
215         QToolBar * qtb = new QToolBar(qt_(tb.name), owner_, getPosition(tb.flags));
216         // give visual separation between adjacent toolbars
217         qtb->addSeparator();
218
219         ToolbarBackend::item_iterator it = tb.items.begin();
220         ToolbarBackend::item_iterator end = tb.items.end();
221         for (; it != end; ++it)
222                 add(qtb, it->first, it->second);
223
224         toolbars_[tb.name] = qtb;
225         displayToolbar(tb, tb.flags & ToolbarBackend::ON);
226
227 }
228
229
230 void QLToolbar::add(QToolBar * tb,
231                     FuncRequest const & func, string const & tooltip)
232 {
233         switch (func.action) {
234         case ToolbarBackend::SEPARATOR:
235                 tb->addSeparator();
236                 break;
237         case ToolbarBackend::LAYOUTS: {
238                 combo_ = new QLComboBox(tb);
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         case ToolbarBackend::MINIBUFFER:
249                 owner_->addCommandBuffer(tb);
250                 tb->setHorizontalStretchable(true);
251                 break;
252         default: {
253                 if (owner_->getLyXFunc().getStatus(func).unknown())
254                         break;
255                 QPixmap p = QPixmap(toolbarbackend.getIcon(func).c_str());
256                 QToolButton * button =
257                         new QToolButton(p, toqstr(tooltip), "",
258                         proxy_.get(), SLOT(button_selected()), tb);
259
260                 map_[button] = func;
261                 break;
262         }
263         }
264 }