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