]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLToolbar.C
a1f75666153c4125336d3cef3b9390524e4bc27c
[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.disabled());
74         }
75
76         bool const enable = !owner_->getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT)).disabled();
77
78         // Workaround for Qt bug where setEnabled(true) closes
79         // the popup
80         if (combo_ && enable != combo_->isEnabled())
81                 combo_->setEnabled(enable);
82 }
83
84
85 void QLToolbar::button_selected(QToolButton * button)
86 {
87         ButtonMap::const_iterator cit = map_.find(button);
88
89         if (cit == map_.end()) {
90                 lyxerr << "non existent tool button selected !" << endl;
91                 return;
92         }
93
94         owner_->getLyXFunc().dispatch(cit->second, true);
95 }
96
97
98 void QLToolbar::changed_layout(string const & sel)
99 {
100         owner_->centralWidget()->setFocus();
101
102         LyXTextClass const & tc =
103                 owner_->buffer()->params().getLyXTextClass();
104
105         LyXTextClass::const_iterator end = tc.end();
106         for (LyXTextClass::const_iterator cit = tc.begin();
107              cit != end; ++cit) {
108                 // Yes, the _() is correct
109                 if (_((*cit)->name()) == sel) {
110                         owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
111                         return;
112                 }
113         }
114         lyxerr << "ERROR (QLToolbar::layoutSelected): layout not found!"
115                << endl;
116 }
117
118
119 void QLToolbar::setLayout(string const & layout)
120 {
121         if (!combo_)
122                 return;
123
124         LyXTextClass const & tc =
125                 owner_->buffer()->params().getLyXTextClass();
126
127         QString const & name = qt_(tc[layout]->name());
128
129         int i = 0;
130         for (; i < combo_->count(); ++i) {
131                 if (name == combo_->text(i))
132                         break;
133         }
134
135         if (i == combo_->count()) {
136                 lyxerr << "Trying to select non existent layout type "
137                         << fromqstr(name) << endl;
138                 return;
139         }
140
141         combo_->setCurrentItem(i);
142 }
143
144
145 void QLToolbar::updateLayoutList()
146 {
147         if (!combo_)
148                 return;
149
150         LyXTextClass const & tc =
151                 owner_->buffer()->params().getLyXTextClass();
152
153         combo_->setUpdatesEnabled(false);
154
155         combo_->clear();
156
157         LyXTextClass::const_iterator cit = tc.begin();
158         LyXTextClass::const_iterator end = tc.end();
159         for (; cit != end; ++cit) {
160                 // ignore obsolete entries
161                 if ((*cit)->obsoleted_by().empty())
162                         combo_->insertItem(qt_((*cit)->name()));
163         }
164
165         // needed to recalculate size hint
166         combo_->hide();
167         combo_->setMinimumWidth(combo_->sizeHint().width());
168         combo_->show();
169
170         combo_->setUpdatesEnabled(true);
171         combo_->update();
172 }
173
174
175 void QLToolbar::clearLayoutList()
176 {
177         if (!combo_)
178                 return;
179
180         Toolbar::clearLayoutList();
181         combo_->clear();
182 }
183
184
185 void QLToolbar::openLayoutList()
186 {
187         if (!combo_)
188                 return;
189
190         combo_->popup();
191 }
192
193
194 namespace {
195
196 QMainWindow::ToolBarDock getPosition(ToolbarBackend::Flags const & flags)
197 {
198         if (flags & ToolbarBackend::TOP)
199                 return QMainWindow::Top;
200         if (flags & ToolbarBackend::BOTTOM)
201                 return QMainWindow::Bottom;
202         if (flags & ToolbarBackend::LEFT)
203                 return QMainWindow::Left;
204         if (flags & ToolbarBackend::RIGHT)
205                 return QMainWindow::Right;
206         return QMainWindow::Top;
207 }
208
209 };
210
211
212 void QLToolbar::add(ToolbarBackend::Toolbar const & tb)
213 {
214         QToolBar * qtb = new QToolBar(qt_(tb.name), owner_, getPosition(tb.flags));
215         // give visual separation between adjacent toolbars
216         qtb->addSeparator();
217
218         ToolbarBackend::item_iterator it = tb.items.begin();
219         ToolbarBackend::item_iterator end = tb.items.end();
220         for (; it != end; ++it)
221                 add(qtb, it->first, it->second);
222
223         toolbars_[tb.name] = qtb;
224         displayToolbar(tb, tb.flags & ToolbarBackend::ON);
225
226 }
227
228
229 void QLToolbar::add(QToolBar * tb,
230                     FuncRequest const & func, string const & tooltip)
231 {
232         switch (func.action) {
233         case ToolbarBackend::SEPARATOR:
234                 tb->addSeparator();
235                 break;
236         case ToolbarBackend::LAYOUTS: {
237                 combo_ = new QLComboBox(tb);
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         case ToolbarBackend::MINIBUFFER:
248                 owner_->addCommandBuffer(tb);
249                 tb->setHorizontalStretchable(true);
250                 break;
251         default: {
252                 if (owner_->getLyXFunc().getStatus(func).unknown())
253                         break;
254                 QPixmap p = QPixmap(toolbarbackend.getIcon(func).c_str());
255                 QToolButton * button =
256                         new QToolButton(p, toqstr(tooltip), "",
257                         proxy_.get(), SLOT(button_selected()), tb);
258
259                 map_[button] = func;
260                 break;
261         }
262         }
263 }