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