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