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
10 * Full author contact details are available in file CREDITS.
16 #include "bufferparams.h"
18 #include "funcrequest.h"
19 #include "FuncStatus.h"
24 #include "QLToolbar.h"
26 #include <qcombobox.h>
33 class QLComboBox : public QComboBox {
35 QLComboBox(QWidget * parent) : QComboBox(parent) {}
36 void popup() { QComboBox::popup(); }
40 QLToolbar::QLToolbar(LyXView * o)
41 : owner_(static_cast<QtView *>(o)),
44 proxy_.reset(new ToolbarProxy(*this));
48 void QLToolbar::displayToolbar(ToolbarBackend::Toolbar const & tb, bool show)
50 QToolBar * qtb = toolbars_[tb.name];
59 void QLToolbar::update()
61 ButtonMap::const_iterator p = map_.begin();
62 ButtonMap::const_iterator end = map_.end();
64 for (; p != end; ++p) {
65 QToolButton * button = p->first;
66 FuncRequest const & func = p->second;
68 FuncStatus const status =
69 owner_->getLyXFunc().getStatus(func);
71 button->setToggleButton(true);
72 button->setOn(status.onoff(true));
73 button->setEnabled(!status.disabled());
76 bool const enable = !owner_->getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT)).disabled();
78 // Workaround for Qt bug where setEnabled(true) closes
80 if (combo_ && enable != combo_->isEnabled())
81 combo_->setEnabled(enable);
85 void QLToolbar::button_selected(QToolButton * button)
87 ButtonMap::const_iterator cit = map_.find(button);
89 if (cit == map_.end()) {
90 lyxerr << "non existent tool button selected !" << endl;
94 owner_->getLyXFunc().dispatch(cit->second, true);
98 void QLToolbar::changed_layout(string const & sel)
100 owner_->centralWidget()->setFocus();
102 LyXTextClass const & tc =
103 owner_->buffer()->params().getLyXTextClass();
105 LyXTextClass::const_iterator end = tc.end();
106 for (LyXTextClass::const_iterator cit = tc.begin();
108 // Yes, the _() is correct
109 if (_((*cit)->name()) == sel) {
110 owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
114 lyxerr << "ERROR (QLToolbar::layoutSelected): layout not found!"
119 void QLToolbar::setLayout(string const & layout)
124 LyXTextClass const & tc =
125 owner_->buffer()->params().getLyXTextClass();
127 QString const & name = qt_(tc[layout]->name());
130 for (; i < combo_->count(); ++i) {
131 if (name == combo_->text(i))
135 if (i == combo_->count()) {
136 lyxerr << "Trying to select non existent layout type "
137 << fromqstr(name) << endl;
141 combo_->setCurrentItem(i);
145 void QLToolbar::updateLayoutList()
150 LyXTextClass const & tc =
151 owner_->buffer()->params().getLyXTextClass();
153 combo_->setUpdatesEnabled(false);
157 LyXTextClass::const_iterator cit = tc.begin();
158 LyXTextClass::const_iterator end = tc.end();
159 for (; cit != end; ++cit) {
160 // ignore obsolete entries
161 if ((*cit)->obsoleted_by().empty())
162 combo_->insertItem(qt_((*cit)->name()));
165 // needed to recalculate size hint
167 combo_->setMinimumWidth(combo_->sizeHint().width());
170 combo_->setUpdatesEnabled(true);
175 void QLToolbar::clearLayoutList()
180 Toolbar::clearLayoutList();
185 void QLToolbar::openLayoutList()
196 QMainWindow::ToolBarDock getPosition(ToolbarBackend::Flags const & flags)
198 if (flags & ToolbarBackend::TOP)
199 return QMainWindow::Top;
200 if (flags & ToolbarBackend::BOTTOM)
201 return QMainWindow::Bottom;
202 if (flags & ToolbarBackend::LEFT)
203 return QMainWindow::Left;
204 if (flags & ToolbarBackend::RIGHT)
205 return QMainWindow::Right;
206 return QMainWindow::Top;
212 void QLToolbar::add(ToolbarBackend::Toolbar const & tb)
214 QToolBar * qtb = new QToolBar(qt_(tb.name), owner_, getPosition(tb.flags));
215 // give visual separation between adjacent toolbars
218 ToolbarBackend::item_iterator it = tb.items.begin();
219 ToolbarBackend::item_iterator end = tb.items.end();
220 for (; it != end; ++it)
221 add(qtb, it->first, it->second);
223 toolbars_[tb.name] = qtb;
224 displayToolbar(tb, tb.flags & ToolbarBackend::ON);
229 void QLToolbar::add(QToolBar * tb,
230 FuncRequest const & func, string const & tooltip)
232 switch (func.action) {
233 case ToolbarBackend::SEPARATOR:
236 case ToolbarBackend::LAYOUTS: {
237 combo_ = new QLComboBox(tb);
238 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
239 combo_->setSizePolicy(p);
240 combo_->setFocusPolicy(QWidget::ClickFocus);
241 combo_->setMinimumWidth(combo_->sizeHint().width());
243 QObject::connect(combo_, SIGNAL(activated(const QString &)),
244 proxy_.get(), SLOT(layout_selected(const QString &)));
247 case ToolbarBackend::MINIBUFFER:
248 owner_->addCommandBuffer(tb);
249 tb->setHorizontalStretchable(true);
252 if (owner_->getLyXFunc().getStatus(func).unknown())
254 QPixmap p = QPixmap(toolbarbackend.getIcon(func).c_str());
255 QToolButton * button =
256 new QToolButton(p, toqstr(tooltip), "",
257 proxy_.get(), SLOT(button_selected()), tb);