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