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.
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"
32 #include <QToolButton>
45 LyXTextClass const & getTextClass(LyXView const & lv)
47 return lv.buffer()->params().getLyXTextClass();
50 /// \todo Remove Qt::Dock getPosition(ToolbarBackend::Flags const & flags) if not needed anymore
51 Qt::Dock getPosition(ToolbarBackend::Flags const & flags)
53 if (flags & ToolbarBackend::TOP)
55 if (flags & ToolbarBackend::BOTTOM)
56 return Qt::DockBottom;
57 if (flags & ToolbarBackend::LEFT)
59 if (flags & ToolbarBackend::RIGHT)
64 Qt::ToolBarArea getToolBarPosition(ToolbarBackend::Flags const & flags)
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;
80 QLayoutBox::QLayoutBox(QToolBar * toolbar, QtView & owner)
83 combo_ = new QComboBox;
84 combo_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
85 combo_->setFocusPolicy(Qt::ClickFocus);
86 combo_->setMinimumWidth(combo_->sizeHint().width());
87 combo_->setMaxVisibleItems(30);
89 QObject::connect(combo_, SIGNAL(activated(const QString &)),
90 this, SLOT(selected(const QString &)));
92 toolbar->addWidget(combo_);
96 void QLayoutBox::set(string const & layout)
98 LyXTextClass const & tc = getTextClass(owner_);
100 QString const & name = qt_(tc[layout]->name());
103 for (; i < combo_->count(); ++i) {
104 if (name == combo_->text(i))
108 if (i == combo_->count()) {
109 lyxerr << "Trying to select non existent layout type "
110 << fromqstr(name) << endl;
114 combo_->setCurrentItem(i);
118 void QLayoutBox::update()
120 LyXTextClass const & tc = getTextClass(owner_);
122 combo_->setUpdatesEnabled(false);
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()));
134 // needed to recalculate size hint
136 combo_->setMinimumWidth(combo_->sizeHint().width());
139 combo_->setUpdatesEnabled(true);
144 void QLayoutBox::clear()
150 void QLayoutBox::open()
156 void QLayoutBox::setEnabled(bool enable)
158 // Workaround for Qt bug where setEnabled(true) closes
160 if (enable != combo_->isEnabled())
161 combo_->setEnabled(enable);
165 void QLayoutBox::selected(const QString & str)
167 string const sel = fromqstr(str);
169 owner_.centralWidget()->setFocus();
171 layoutSelected(owner_, sel);
174 } // namespace frontend
177 Toolbars::ToolbarPtr make_toolbar(ToolbarBackend::Toolbar const & tbb,
180 using lyx::frontend::QLToolbar;
181 return Toolbars::ToolbarPtr(new QLToolbar(tbb, owner));
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)))
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);
195 case Qt::TopToolBarArea:
196 owner_.addToolBar(tba, toolbar_);
197 owner_.addToolBarBreak(tba);
199 // case Qt::BottomToolBarArea:
200 //bottomToolbarVector.push_back(toolbar_);
201 // owner_.addToolBar(tba, toolbar_);
202 // //if owner_.insertToolBarBreak(toolbar_);
205 owner_.addToolBar(Qt::TopToolBarArea, toolbar_);
206 owner_.addToolBarBreak(Qt::TopToolBarArea);
210 // give visual separation between adjacent toolbars
211 toolbar_->addSeparator();
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);
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);
224 void QLToolbar::add(FuncRequest const & func, string const & tooltip)
226 switch (func.action) {
227 case ToolbarBackend::SEPARATOR:
228 toolbar_->addSeparator();
230 case ToolbarBackend::LAYOUTS:
231 layout_.reset(new QLayoutBox(toolbar_, owner_));
233 case ToolbarBackend::MINIBUFFER:
234 owner_.addCommandBuffer(toolbar_);
235 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
236 //toolbar_->setHorizontalStretchable(true);
239 if (owner_.getLyXFunc().getStatus(func).unknown())
242 QLAction * action = new QLAction(owner_, toolbarbackend.getIcon(func), "", func, tooltip);
243 toolbar_->addAction(action);
244 ActionVector.push_back(action);
252 void QLToolbar::hide(bool)
258 void QLToolbar::show(bool)
264 void QLToolbar::update()
266 for (size_t i=0; i<ActionVector.size(); ++i)
267 ActionVector[i]->update();
271 } // namespace frontend