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.
20 #include "bufferparams.h"
22 #include "funcrequest.h"
23 #include "FuncStatus.h"
28 #include "QLToolbar.h"
30 #include "qt_helpers.h"
31 #include "InsertTableWidget.h"
35 #include <QToolButton>
47 LyXTextClass const & getTextClass(LyXView const & lv)
49 return lv.buffer()->params().getLyXTextClass();
53 /// \todo Remove Qt::Dock getPosition(ToolbarBackend::Flags const & flags) if not needed anymore
54 Qt::Dock getPosition(ToolbarBackend::Flags const & flags)
56 if (flags & ToolbarBackend::TOP)
58 if (flags & ToolbarBackend::BOTTOM)
59 return Qt::DockBottom;
60 if (flags & ToolbarBackend::LEFT)
62 if (flags & ToolbarBackend::RIGHT)
68 Qt::ToolBarArea getToolBarPosition(ToolbarBackend::Flags const & flags)
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;
84 QLayoutBox::QLayoutBox(QToolBar * toolbar, GuiView & owner)
87 combo_ = new QComboBox;
88 combo_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
89 combo_->setFocusPolicy(Qt::ClickFocus);
90 combo_->setMinimumWidth(combo_->sizeHint().width());
91 combo_->setMaxVisibleItems(100);
93 QObject::connect(combo_, SIGNAL(activated(const QString &)),
94 this, SLOT(selected(const QString &)));
96 toolbar->addWidget(combo_);
100 void QLayoutBox::set(string const & layout)
102 LyXTextClass const & tc = getTextClass(owner_);
104 QString const & name = qt_(tc[layout]->name());
107 for (; i < combo_->count(); ++i) {
108 if (name == combo_->itemText(i))
112 if (i == combo_->count()) {
113 lyxerr << "Trying to select non existent layout type "
114 << fromqstr(name) << endl;
118 combo_->setCurrentIndex(i);
122 void QLayoutBox::update()
124 LyXTextClass const & tc = getTextClass(owner_);
126 combo_->setUpdatesEnabled(false);
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()));
138 // needed to recalculate size hint
140 combo_->setMinimumWidth(combo_->sizeHint().width());
143 combo_->setUpdatesEnabled(true);
148 void QLayoutBox::clear()
154 void QLayoutBox::open()
160 void QLayoutBox::setEnabled(bool enable)
162 // Workaround for Qt bug where setEnabled(true) closes
164 if (enable != combo_->isEnabled())
165 combo_->setEnabled(enable);
169 void QLayoutBox::selected(const QString & str)
171 string const sel = fromqstr(str);
173 owner_.centralWidget()->setFocus();
175 layoutSelected(owner_, sel);
178 } // namespace frontend
181 Toolbars::ToolbarPtr make_toolbar(ToolbarBackend::Toolbar const & tbb,
184 using lyx::frontend::QLToolbar;
185 return Toolbars::ToolbarPtr(new QLToolbar(tbb, owner));
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)))
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);
199 case Qt::TopToolBarArea:
200 owner_.addToolBar(tba, toolbar_);
201 owner_.addToolBarBreak(tba);
203 // case Qt::BottomToolBarArea:
204 //bottomToolbarVector.push_back(toolbar_);
205 // owner_.addToolBar(tba, toolbar_);
206 // //if owner_.insertToolBarBreak(toolbar_);
209 owner_.addToolBar(Qt::TopToolBarArea, toolbar_);
210 owner_.addToolBarBreak(Qt::TopToolBarArea);
214 // give visual separation between adjacent toolbars
215 toolbar_->addSeparator();
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);
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);
228 void QLToolbar::add(FuncRequest const & func, string const & tooltip)
230 switch (func.action) {
231 case ToolbarBackend::SEPARATOR:
232 toolbar_->addSeparator();
234 case ToolbarBackend::LAYOUTS:
235 layout_.reset(new QLayoutBox(toolbar_, owner_));
237 case ToolbarBackend::MINIBUFFER:
238 owner_.addCommandBuffer(toolbar_);
239 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
240 //toolbar_->setHorizontalStretchable(true);
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);
256 if (owner_.getLyXFunc().getStatus(func).unknown())
259 Action * action = new Action(owner_, toolbarbackend.getIcon(func), "", func, tooltip);
260 toolbar_->addAction(action);
261 ActionVector.push_back(action);
268 void QLToolbar::hide(bool)
274 void QLToolbar::show(bool)
280 void QLToolbar::update()
282 for (size_t i=0; i<ActionVector.size(); ++i)
283 ActionVector[i]->update();
290 } // namespace frontend
293 #include "QLToolbar_moc.cpp"