2 * \file qt2/Toolbar_pimpl.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
9 * Full author contact details are available in file CREDITS
18 #include "FuncStatus.h"
19 #include "BufferView.h"
21 #include "LyXAction.h"
22 #include "qt_helpers.h"
24 #include "support/LAssert.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
28 #include <boost/tuple/tuple.hpp>
31 #include "Toolbar_pimpl.h"
34 #include <qcombobox.h>
36 #include <qsizepolicy.h>
41 class QLComboBox : public QComboBox {
43 QLComboBox(QWidget * parent) : QComboBox(parent) {}
44 void popup() { QComboBox::popup(); }
48 Toolbar::Pimpl::Pimpl(LyXView * o, int, int)
49 : owner_(static_cast<QtView *>(o)),
52 proxy_.reset(new ToolbarProxy(*this));
56 Toolbar::Pimpl::~Pimpl()
61 void Toolbar::Pimpl::displayToolbar(ToolbarBackend::Toolbar const & tb, bool show)
63 QToolBar * qtb = toolbars_[tb.name];
72 void Toolbar::Pimpl::update()
74 ButtonMap::const_iterator p = map_.begin();
75 ButtonMap::const_iterator end = map_.end();
77 for (; p != end; ++p) {
78 QToolButton * button = p->first;
79 int action = p->second;
81 FuncStatus const status =
82 owner_->getLyXFunc().getStatus(action);
84 button->setToggleButton(true);
85 button->setOn(status.onoff(true));
86 button->setEnabled(!status.disabled());
89 bool const enable = !owner_->getLyXFunc().getStatus(LFUN_LAYOUT).disabled();
91 // Workaround for Qt bug where setEnabled(true) closes
93 if (combo_ && enable != combo_->isEnabled())
94 combo_->setEnabled(enable);
98 void Toolbar::Pimpl::button_selected(QToolButton * button)
100 ButtonMap::const_iterator cit = map_.find(button);
102 if (cit == map_.end()) {
103 lyxerr << "non existent tool button selected !" << endl;
107 owner_->getLyXFunc().dispatch(cit->second, true);
111 void Toolbar::Pimpl::changed_layout(string const & sel)
113 owner_->centralWidget()->setFocus();
115 LyXTextClass const & tc =
116 owner_->buffer()->params.getLyXTextClass();
118 LyXTextClass::const_iterator end = tc.end();
119 for (LyXTextClass::const_iterator cit = tc.begin();
121 // Yes, the _() is correct
122 if (_((*cit)->name()) == sel) {
123 owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
127 lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
132 void Toolbar::Pimpl::setLayout(string const & layout)
137 LyXTextClass const & tc =
138 owner_->buffer()->params.getLyXTextClass();
140 QString const & name = qt_(tc[layout]->name());
143 for (; i < combo_->count(); ++i) {
144 if (name == combo_->text(i))
148 if (i == combo_->count()) {
149 lyxerr << "Trying to select non existent layout type "
150 << fromqstr(name) << endl;
154 combo_->setCurrentItem(i);
158 void Toolbar::Pimpl::updateLayoutList(bool force)
163 // if we don't need an update, don't ...
164 if (combo_->count() && !force)
167 LyXTextClass const & tc =
168 owner_->buffer()->params.getLyXTextClass();
170 combo_->setUpdatesEnabled(false);
174 LyXTextClass::const_iterator cit = tc.begin();
175 LyXTextClass::const_iterator end = tc.end();
176 for (; cit != end; ++cit) {
177 // ignore obsolete entries
178 if ((*cit)->obsoleted_by().empty())
179 combo_->insertItem(qt_((*cit)->name()));
182 // needed to recalculate size hint
184 combo_->setMinimumWidth(combo_->sizeHint().width());
187 combo_->setUpdatesEnabled(true);
192 void Toolbar::Pimpl::clearLayoutList()
201 void Toolbar::Pimpl::openLayoutList()
212 QMainWindow::ToolBarDock getPosition(ToolbarBackend::Flags const & flags)
214 if (flags & ToolbarBackend::TOP)
215 return QMainWindow::Top;
216 if (flags & ToolbarBackend::BOTTOM)
217 return QMainWindow::Bottom;
218 if (flags & ToolbarBackend::LEFT)
219 return QMainWindow::Left;
220 if (flags & ToolbarBackend::RIGHT)
221 return QMainWindow::Right;
222 return QMainWindow::Top;
228 void Toolbar::Pimpl::add(ToolbarBackend::Toolbar const & tb)
230 QToolBar * qtb = new QToolBar(qt_(tb.name), owner_, getPosition(tb.flags));
231 // give visual separation between adjacent toolbars
234 ToolbarBackend::item_iterator it = tb.items.begin();
235 ToolbarBackend::item_iterator end = tb.items.end();
236 for (; it != end; ++it)
237 add(qtb, it->first, it->second);
239 toolbars_[tb.name] = qtb;
240 displayToolbar(tb, tb.flags & ToolbarBackend::ON);
245 void Toolbar::Pimpl::add(QToolBar * tb, int action, string const & tooltip)
248 case ToolbarBackend::SEPARATOR:
251 case ToolbarBackend::LAYOUTS: {
252 combo_ = new QLComboBox(tb);
253 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
254 combo_->setSizePolicy(p);
255 combo_->setFocusPolicy(QWidget::ClickFocus);
256 combo_->setMinimumWidth(combo_->sizeHint().width());
258 QObject::connect(combo_, SIGNAL(activated(const QString &)),
259 proxy_.get(), SLOT(layout_selected(const QString &)));
262 case ToolbarBackend::MINIBUFFER:
263 owner_->addCommandBuffer(tb);
264 tb->setHorizontalStretchable(true);
267 if (owner_->getLyXFunc().getStatus(action).unknown())
269 QPixmap p = QPixmap(toolbarbackend.getIcon(action).c_str());
270 QToolButton * button =
271 new QToolButton(p, toqstr(tooltip), "",
272 proxy_.get(), SLOT(button_selected()), tb);
274 map_[button] = action;