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.
6 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
10 * \author Abdelrazak Younes
12 * Full author contact details are available in file CREDITS.
18 #include "bufferparams.h"
20 #include "funcrequest.h"
21 #include "FuncStatus.h"
26 #include "QLToolbar.h"
28 #include "qt_helpers.h"
29 #include "InsertTableWidget.h"
33 #include <QToolButton>
45 LyXTextClass const & getTextClass(LyXView const & lv)
47 return lv.buffer()->params().getLyXTextClass();
51 /// \todo Remove Qt::Dock getPosition(ToolbarBackend::Flags const & flags) if not needed anymore
52 Qt::Dock getPosition(ToolbarBackend::Flags const & flags)
54 if (flags & ToolbarBackend::TOP)
56 if (flags & ToolbarBackend::BOTTOM)
57 return Qt::DockBottom;
58 if (flags & ToolbarBackend::LEFT)
60 if (flags & ToolbarBackend::RIGHT)
66 Qt::ToolBarArea getToolBarPosition(ToolbarBackend::Flags const & flags)
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;
82 QLayoutBox::QLayoutBox(QToolBar * toolbar, GuiView & owner)
85 combo_ = new QComboBox;
86 combo_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
87 combo_->setFocusPolicy(Qt::ClickFocus);
88 combo_->setMinimumWidth(combo_->sizeHint().width());
89 combo_->setMaxVisibleItems(100);
91 QObject::connect(combo_, SIGNAL(activated(const QString &)),
92 this, SLOT(selected(const QString &)));
94 toolbar->addWidget(combo_);
98 void QLayoutBox::set(string const & layout)
100 LyXTextClass const & tc = getTextClass(owner_);
102 QString const & name = qt_(tc[layout]->name());
105 for (; i < combo_->count(); ++i) {
106 if (name == combo_->itemText(i))
110 if (i == combo_->count()) {
111 lyxerr << "Trying to select non existent layout type "
112 << fromqstr(name) << endl;
116 combo_->setCurrentIndex(i);
120 void QLayoutBox::update()
122 LyXTextClass const & tc = getTextClass(owner_);
124 combo_->setUpdatesEnabled(false);
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()));
136 // needed to recalculate size hint
138 combo_->setMinimumWidth(combo_->sizeHint().width());
141 combo_->setUpdatesEnabled(true);
146 void QLayoutBox::clear()
152 void QLayoutBox::open()
158 void QLayoutBox::setEnabled(bool enable)
160 // Workaround for Qt bug where setEnabled(true) closes
162 if (enable != combo_->isEnabled())
163 combo_->setEnabled(enable);
167 void QLayoutBox::selected(const QString & str)
169 string const sel = fromqstr(str);
171 owner_.centralWidget()->setFocus();
173 layoutSelected(owner_, sel);
176 } // namespace frontend
179 Toolbars::ToolbarPtr make_toolbar(ToolbarBackend::Toolbar const & tbb,
182 using lyx::frontend::QLToolbar;
183 return Toolbars::ToolbarPtr(new QLToolbar(tbb, owner));
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)))
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);
197 case Qt::TopToolBarArea:
198 owner_.addToolBar(tba, toolbar_);
199 owner_.addToolBarBreak(tba);
201 // case Qt::BottomToolBarArea:
202 //bottomToolbarVector.push_back(toolbar_);
203 // owner_.addToolBar(tba, toolbar_);
204 // //if owner_.insertToolBarBreak(toolbar_);
207 owner_.addToolBar(Qt::TopToolBarArea, toolbar_);
208 owner_.addToolBarBreak(Qt::TopToolBarArea);
212 // give visual separation between adjacent toolbars
213 toolbar_->addSeparator();
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);
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);
226 void QLToolbar::add(FuncRequest const & func, string const & tooltip)
228 switch (func.action) {
229 case ToolbarBackend::SEPARATOR:
230 toolbar_->addSeparator();
232 case ToolbarBackend::LAYOUTS:
233 layout_.reset(new QLayoutBox(toolbar_, owner_));
235 case ToolbarBackend::MINIBUFFER:
236 owner_.addCommandBuffer(toolbar_);
237 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
238 //toolbar_->setHorizontalStretchable(true);
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);
254 if (owner_.getLyXFunc().getStatus(func).unknown())
257 Action * action = new Action(owner_, toolbarbackend.getIcon(func), "", func, tooltip);
258 toolbar_->addAction(action);
259 ActionVector.push_back(action);
266 void QLToolbar::hide(bool)
272 void QLToolbar::show(bool)
278 void QLToolbar::update()
280 for (size_t i=0; i<ActionVector.size(); ++i)
281 ActionVector[i]->update();
288 } // namespace frontend
291 #include "QLToolbar_moc.cpp"