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();
54 QLayoutBox::QLayoutBox(QToolBar * toolbar, GuiView & owner)
57 combo_ = new QComboBox;
58 combo_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
59 combo_->setFocusPolicy(Qt::ClickFocus);
60 combo_->setMinimumWidth(combo_->sizeHint().width());
61 combo_->setMaxVisibleItems(100);
63 QObject::connect(combo_, SIGNAL(activated(const QString &)),
64 this, SLOT(selected(const QString &)));
66 toolbar->addWidget(combo_);
70 void QLayoutBox::set(string const & layout)
72 LyXTextClass const & tc = getTextClass(owner_);
74 QString const & name = qt_(tc[layout]->name());
77 for (; i < combo_->count(); ++i) {
78 if (name == combo_->itemText(i))
82 if (i == combo_->count()) {
83 lyxerr << "Trying to select non existent layout type "
84 << fromqstr(name) << endl;
88 combo_->setCurrentIndex(i);
92 void QLayoutBox::update()
94 LyXTextClass const & tc = getTextClass(owner_);
96 combo_->setUpdatesEnabled(false);
100 LyXTextClass::const_iterator it = tc.begin();
101 LyXTextClass::const_iterator const end = tc.end();
102 for (; it != end; ++it) {
103 // ignore obsolete entries
104 if ((*it)->obsoleted_by().empty())
105 combo_->addItem(qt_((*it)->name()));
108 // needed to recalculate size hint
110 combo_->setMinimumWidth(combo_->sizeHint().width());
113 combo_->setUpdatesEnabled(true);
118 void QLayoutBox::clear()
124 void QLayoutBox::open()
130 void QLayoutBox::setEnabled(bool enable)
132 // Workaround for Qt bug where setEnabled(true) closes
134 if (enable != combo_->isEnabled())
135 combo_->setEnabled(enable);
139 void QLayoutBox::selected(const QString & str)
141 string const sel = fromqstr(str);
145 layoutSelected(owner_, sel);
149 QLToolbar::QLToolbar(ToolbarBackend::Toolbar const & tbb, GuiView & owner)
150 : QToolBar(qt_(tbb.gui_name), &owner), owner_(owner)
152 // give visual separation between adjacent toolbars
155 // TODO: save toolbar position
158 ToolbarBackend::item_iterator it = tbb.items.begin();
159 ToolbarBackend::item_iterator end = tbb.items.end();
160 for (; it != end; ++it)
161 add(it->first, it->second);
165 void QLToolbar::add(FuncRequest const & func, docstring const & tooltip)
167 switch (func.action) {
168 case ToolbarBackend::SEPARATOR:
171 case ToolbarBackend::LAYOUTS:
172 layout_.reset(new QLayoutBox(this, owner_));
174 case ToolbarBackend::MINIBUFFER:
175 owner_.addCommandBuffer(this);
176 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
177 //setHorizontalStretchable(true);
179 case LFUN_TABULAR_INSERT: {
180 QToolButton * tb = new QToolButton;
181 tb->setCheckable(true);
182 tb->setIcon(QPixmap(toqstr(toolbarbackend.getIcon(func))));
183 tb->setToolTip(toqstr(tooltip));
184 tb->setFocusPolicy(Qt::NoFocus);
185 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
186 connect(tb, SIGNAL(toggled(bool)), iv, SLOT(show(bool)));
187 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
188 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
193 if (lyx::getStatus(func).unknown())
196 Action * action = new Action(owner_, toolbarbackend.getIcon(func), lyx::docstring(), func, tooltip);
198 ActionVector.push_back(action);
205 void QLToolbar::hide(bool)
211 void QLToolbar::show(bool)
217 void QLToolbar::saveInfo(ToolbarSection::ToolbarInfo & info)
219 // if info.state == auto *do not* set on/off
220 if (info.state != ToolbarSection::ToolbarInfo::AUTO) {
221 if (QLToolbar::isVisible())
222 info.state = ToolbarSection::ToolbarInfo::ON;
224 info.state = ToolbarSection::ToolbarInfo::OFF;
227 // no need to save it here.
228 Qt::ToolBarArea loc = owner_.toolBarArea(this);
230 if (loc == Qt::TopToolBarArea)
231 info.location = ToolbarSection::ToolbarInfo::TOP;
232 else if (loc == Qt::BottomToolBarArea)
233 info.location = ToolbarSection::ToolbarInfo::BOTTOM;
234 else if (loc == Qt::RightToolBarArea)
235 info.location = ToolbarSection::ToolbarInfo::RIGHT;
236 else if (loc == Qt::LeftToolBarArea)
237 info.location = ToolbarSection::ToolbarInfo::LEFT;
239 info.location = ToolbarSection::ToolbarInfo::NOTSET;
243 void QLToolbar::update()
245 // This is a speed bottleneck because this is called on every keypress
246 // and update calls getStatus, which copies the cursor at least two times
247 for (size_t i=0; i<ActionVector.size(); ++i)
248 ActionVector[i]->update();
255 } // namespace frontend
258 #include "QLToolbar_moc.cpp"