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.enabled());
76 bool const enable = owner_->getLyXFunc().
77 getStatus(FuncRequest(LFUN_LAYOUT)).enabled();
79 // Workaround for Qt bug where setEnabled(true) closes
81 if (combo_ && enable != combo_->isEnabled())
82 combo_->setEnabled(enable);
86 void QLToolbar::button_selected(QToolButton * button)
88 ButtonMap::const_iterator cit = map_.find(button);
90 if (cit == map_.end()) {
91 lyxerr << "non existent tool button selected !" << endl;
95 owner_->getLyXFunc().dispatch(cit->second, true);
99 void QLToolbar::changed_layout(string const & sel)
101 owner_->centralWidget()->setFocus();
103 LyXTextClass const & tc =
104 owner_->buffer()->params().getLyXTextClass();
106 LyXTextClass::const_iterator end = tc.end();
107 for (LyXTextClass::const_iterator cit = tc.begin();
109 // Yes, the _() is correct
110 if (_((*cit)->name()) == sel) {
111 owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
115 lyxerr << "ERROR (QLToolbar::layoutSelected): layout not found!"
120 void QLToolbar::setLayout(string const & layout)
125 LyXTextClass const & tc =
126 owner_->buffer()->params().getLyXTextClass();
128 QString const & name = qt_(tc[layout]->name());
131 for (; i < combo_->count(); ++i) {
132 if (name == combo_->text(i))
136 if (i == combo_->count()) {
137 lyxerr << "Trying to select non existent layout type "
138 << fromqstr(name) << endl;
142 combo_->setCurrentItem(i);
146 void QLToolbar::updateLayoutList()
151 LyXTextClass const & tc =
152 owner_->buffer()->params().getLyXTextClass();
154 combo_->setUpdatesEnabled(false);
158 LyXTextClass::const_iterator cit = tc.begin();
159 LyXTextClass::const_iterator end = tc.end();
160 for (; cit != end; ++cit) {
161 // ignore obsolete entries
162 if ((*cit)->obsoleted_by().empty())
163 combo_->insertItem(qt_((*cit)->name()));
166 // needed to recalculate size hint
168 combo_->setMinimumWidth(combo_->sizeHint().width());
171 combo_->setUpdatesEnabled(true);
176 void QLToolbar::clearLayoutList()
181 Toolbar::clearLayoutList();
186 void QLToolbar::openLayoutList()
197 QMainWindow::ToolBarDock getPosition(ToolbarBackend::Flags const & flags)
199 if (flags & ToolbarBackend::TOP)
200 return QMainWindow::Top;
201 if (flags & ToolbarBackend::BOTTOM)
202 return QMainWindow::Bottom;
203 if (flags & ToolbarBackend::LEFT)
204 return QMainWindow::Left;
205 if (flags & ToolbarBackend::RIGHT)
206 return QMainWindow::Right;
207 return QMainWindow::Top;
213 void QLToolbar::add(ToolbarBackend::Toolbar const & tb)
215 QToolBar * qtb = new QToolBar(qt_(tb.gui_name), owner_,
216 getPosition(tb.flags));
217 // give visual separation between adjacent toolbars
220 ToolbarBackend::item_iterator it = tb.items.begin();
221 ToolbarBackend::item_iterator end = tb.items.end();
222 for (; it != end; ++it)
223 add(qtb, it->first, it->second);
225 toolbars_[tb.name] = qtb;
226 displayToolbar(tb, tb.flags & ToolbarBackend::ON);
231 void QLToolbar::add(QToolBar * tb,
232 FuncRequest const & func, string const & tooltip)
234 switch (func.action) {
235 case ToolbarBackend::SEPARATOR:
238 case ToolbarBackend::LAYOUTS: {
239 combo_ = new QLComboBox(tb);
240 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
241 combo_->setSizePolicy(p);
242 combo_->setFocusPolicy(QWidget::ClickFocus);
243 combo_->setMinimumWidth(combo_->sizeHint().width());
245 QObject::connect(combo_, SIGNAL(activated(const QString &)),
246 proxy_.get(), SLOT(layout_selected(const QString &)));
249 case ToolbarBackend::MINIBUFFER:
250 owner_->addCommandBuffer(tb);
251 tb->setHorizontalStretchable(true);
254 if (owner_->getLyXFunc().getStatus(func).unknown())
256 QPixmap p = QPixmap(toolbarbackend.getIcon(func).c_str());
257 QToolButton * button =
258 new QToolButton(p, toqstr(tooltip), "",
259 proxy_.get(), SLOT(button_selected()), tb);