]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QLToolbar.C
QtView renamed to GuiView
[lyx.git] / src / frontends / qt4 / QLToolbar.C
1 /**
2  * \file qt4/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  * \author Abdelrazak Younes
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #undef QT3_SUPPORT
16
17 #include <config.h>
18
19 #include "buffer.h"
20 #include "bufferparams.h"
21 #include "debug.h"
22 #include "funcrequest.h"
23 #include "FuncStatus.h"
24 #include "gettext.h"
25 #include "lyxfunc.h"
26
27 #include "GuiView.h"
28 #include "QLToolbar.h"
29 #include "Action.h"
30 #include "qt_helpers.h"
31 #include "InsertTableWidget.h"
32
33 #include <QComboBox>
34 #include <QToolBar>
35 #include <QToolButton>
36 #include <QAction>
37 #include <QPixmap>
38
39 using std::endl;
40 using std::string;
41
42 namespace lyx {
43 namespace frontend {
44
45 namespace {
46
47 LyXTextClass const & getTextClass(LyXView const & lv)
48 {
49         return lv.buffer()->params().getLyXTextClass();
50 }
51
52 /*
53 /// \todo Remove Qt::Dock getPosition(ToolbarBackend::Flags const & flags) if not needed anymore
54 Qt::Dock getPosition(ToolbarBackend::Flags const & flags)
55 {
56         if (flags & ToolbarBackend::TOP)
57                 return Qt::DockTop;
58         if (flags & ToolbarBackend::BOTTOM)
59                 return Qt::DockBottom;
60         if (flags & ToolbarBackend::LEFT)
61                 return Qt::DockLeft;
62         if (flags & ToolbarBackend::RIGHT)
63                 return Qt::DockRight;
64         return Qt::DockTop;
65 }
66 */
67
68 Qt::ToolBarArea getToolBarPosition(ToolbarBackend::Flags const & flags)
69 {
70         if (flags & ToolbarBackend::TOP)
71                 return Qt::TopToolBarArea;
72         if (flags & ToolbarBackend::BOTTOM)
73                 return Qt::BottomToolBarArea;
74         if (flags & ToolbarBackend::LEFT)
75                 return Qt::LeftToolBarArea;
76         if (flags & ToolbarBackend::RIGHT)
77                 return Qt::RightToolBarArea;
78         return Qt::TopToolBarArea;
79 }
80
81 } // namespace anon
82
83
84 QLayoutBox::QLayoutBox(QToolBar * toolbar, GuiView & owner)
85         : owner_(owner)
86 {
87         combo_ = new QComboBox;
88         combo_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
89         combo_->setFocusPolicy(Qt::ClickFocus);
90         combo_->setMinimumWidth(combo_->sizeHint().width());
91         combo_->setMaxVisibleItems(100);
92
93         QObject::connect(combo_, SIGNAL(activated(const QString &)),
94                          this, SLOT(selected(const QString &)));
95
96         toolbar->addWidget(combo_);
97 }
98
99
100 void QLayoutBox::set(string const & layout)
101 {
102         LyXTextClass const & tc = getTextClass(owner_);
103
104         QString const & name = qt_(tc[layout]->name());
105
106         int i = 0;
107         for (; i < combo_->count(); ++i) {
108                 if (name == combo_->itemText(i))
109                         break;
110         }
111
112         if (i == combo_->count()) {
113                 lyxerr << "Trying to select non existent layout type "
114                         << fromqstr(name) << endl;
115                 return;
116         }
117
118         combo_->setCurrentIndex(i);
119 }
120
121
122 void QLayoutBox::update()
123 {
124         LyXTextClass const & tc = getTextClass(owner_);
125
126         combo_->setUpdatesEnabled(false);
127
128         combo_->clear();
129
130         LyXTextClass::const_iterator it = tc.begin();
131         LyXTextClass::const_iterator const end = tc.end();
132         for (; it != end; ++it) {
133                 // ignore obsolete entries
134                 if ((*it)->obsoleted_by().empty())
135                         combo_->addItem(qt_((*it)->name()));
136         }
137
138         // needed to recalculate size hint
139         combo_->hide();
140         combo_->setMinimumWidth(combo_->sizeHint().width());
141         combo_->show();
142
143         combo_->setUpdatesEnabled(true);
144         combo_->update();
145 }
146
147
148 void QLayoutBox::clear()
149 {
150         combo_->clear();
151 }
152
153
154 void QLayoutBox::open()
155 {
156         combo_->showPopup();
157 }
158
159
160 void QLayoutBox::setEnabled(bool enable)
161 {
162         // Workaround for Qt bug where setEnabled(true) closes
163         // the popup
164         if (enable != combo_->isEnabled())
165                 combo_->setEnabled(enable);
166 }
167
168
169 void QLayoutBox::selected(const QString & str)
170 {
171         string const sel = fromqstr(str);
172
173         owner_.centralWidget()->setFocus();
174
175         layoutSelected(owner_, sel);
176 }
177
178 } // namespace frontend
179 } // namespace lyx
180
181 Toolbars::ToolbarPtr make_toolbar(ToolbarBackend::Toolbar const & tbb,
182                                   LyXView & owner)
183 {
184         using lyx::frontend::QLToolbar;
185         return Toolbars::ToolbarPtr(new QLToolbar(tbb, owner));
186 }
187
188 namespace lyx {
189 namespace frontend {
190
191 QLToolbar::QLToolbar(ToolbarBackend::Toolbar const & tbb, LyXView & owner)
192         : owner_(dynamic_cast<GuiView &>(owner)),
193           toolbar_(new QToolBar(qt_(tbb.gui_name), (QWidget*) &owner_)) //, getPosition(tbb.flags)))
194 {
195         /// \toto Move \a addToolBar call into QView because, in Qt4,
196         /// the ToolBars placement is the duty of QMainWindow (aka QView)
197         Qt::ToolBarArea tba = getToolBarPosition(tbb.flags);
198         switch(tba) {
199         case Qt::TopToolBarArea:
200                 owner_.addToolBar(tba, toolbar_);
201                 owner_.addToolBarBreak(tba);
202                 break;
203 //      case Qt::BottomToolBarArea:
204                 //bottomToolbarVector.push_back(toolbar_);
205 //              owner_.addToolBar(tba, toolbar_);
206 //              //if owner_.insertToolBarBreak(toolbar_);
207                 break;
208         default:
209                 owner_.addToolBar(Qt::TopToolBarArea, toolbar_);
210                 owner_.addToolBarBreak(Qt::TopToolBarArea);
211                 break;
212         }
213
214         // give visual separation between adjacent toolbars
215         toolbar_->addSeparator();
216
217         // allowing the toolbars to tear off is too easily done,
218         // and we don't save their orientation anyway. Disable the handle.
219         toolbar_->setMovable(false);
220
221         ToolbarBackend::item_iterator it = tbb.items.begin();
222         ToolbarBackend::item_iterator end = tbb.items.end();
223         for (; it != end; ++it)
224                 add(it->first, it->second);
225 }
226
227
228 void QLToolbar::add(FuncRequest const & func, string const & tooltip)
229 {
230         switch (func.action) {
231         case ToolbarBackend::SEPARATOR:
232                 toolbar_->addSeparator();
233                 break;
234         case ToolbarBackend::LAYOUTS:
235                 layout_.reset(new QLayoutBox(toolbar_, owner_));
236                 break;
237         case ToolbarBackend::MINIBUFFER:
238                 owner_.addCommandBuffer(toolbar_);
239                 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
240                 //toolbar_->setHorizontalStretchable(true);
241                 break;
242         case LFUN_TABULAR_INSERT: {
243                 QToolButton * tb = new QToolButton;
244                 tb->setCheckable(true);
245                 tb->setIcon(QPixmap(toqstr(toolbarbackend.getIcon(func))));
246                 tb->setToolTip(toqstr(tooltip));
247                 tb->setFocusPolicy(Qt::NoFocus);
248                 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
249                 connect(tb, SIGNAL(toggled(bool)), iv, SLOT(show(bool)));
250                 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
251                 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
252                 toolbar_->addWidget(tb);
253                 break;
254                 }
255         default: {
256                 if (owner_.getLyXFunc().getStatus(func).unknown())
257                         break;
258
259                 Action * action = new Action(owner_, toolbarbackend.getIcon(func), "", func, tooltip);
260                 toolbar_->addAction(action);
261                 ActionVector.push_back(action);
262                 break;
263                 }
264         }
265 }
266
267
268 void QLToolbar::hide(bool)
269 {
270         toolbar_->hide();
271 }
272
273
274 void QLToolbar::show(bool)
275 {
276         toolbar_->show();
277 }
278
279
280 void QLToolbar::update()
281 {
282         for (size_t i=0; i<ActionVector.size(); ++i)
283                 ActionVector[i]->update();
284
285         emit updated();
286 }
287
288
289 } // namespace frontend
290 } // namespace lyx
291
292 #include "QLToolbar_moc.cpp"