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
19 #include "FuncStatus.h"
20 #include "BufferView.h"
22 #include "LyXAction.h"
23 #include "qt_helpers.h"
25 #include "support/LAssert.h"
26 #include "support/filetools.h"
27 #include "support/lstrings.h"
29 #include <boost/tuple/tuple.hpp>
32 #include "QLToolbar.h"
35 #include <qcombobox.h>
37 #include <qsizepolicy.h>
42 class QLComboBox : public QComboBox {
44 QLComboBox(QWidget * parent) : QComboBox(parent) {}
45 void popup() { QComboBox::popup(); }
49 QLToolbar::QLToolbar(LyXView * o)
50 : owner_(static_cast<QtView *>(o)),
53 proxy_.reset(new ToolbarProxy(*this));
57 void QLToolbar::displayToolbar(ToolbarBackend::Toolbar const & tb, bool show)
59 QToolBar * qtb = toolbars_[tb.name];
68 void QLToolbar::update()
70 ButtonMap::const_iterator p = map_.begin();
71 ButtonMap::const_iterator end = map_.end();
73 for (; p != end; ++p) {
74 QToolButton * button = p->first;
75 int action = p->second;
77 FuncStatus const status =
78 owner_->getLyXFunc().getStatus(action);
80 button->setToggleButton(true);
81 button->setOn(status.onoff(true));
82 button->setEnabled(!status.disabled());
85 bool const enable = !owner_->getLyXFunc().getStatus(LFUN_LAYOUT).disabled();
87 // Workaround for Qt bug where setEnabled(true) closes
89 if (combo_ && enable != combo_->isEnabled())
90 combo_->setEnabled(enable);
94 void QLToolbar::button_selected(QToolButton * button)
96 ButtonMap::const_iterator cit = map_.find(button);
98 if (cit == map_.end()) {
99 lyxerr << "non existent tool button selected !" << endl;
103 owner_->getLyXFunc().dispatch(cit->second, true);
107 void QLToolbar::changed_layout(string const & sel)
109 owner_->centralWidget()->setFocus();
111 LyXTextClass const & tc =
112 owner_->buffer()->params.getLyXTextClass();
114 LyXTextClass::const_iterator end = tc.end();
115 for (LyXTextClass::const_iterator cit = tc.begin();
117 // Yes, the _() is correct
118 if (_((*cit)->name()) == sel) {
119 owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
123 lyxerr << "ERROR (QLToolbar::layoutSelected): layout not found!"
128 void QLToolbar::setLayout(string const & layout)
133 LyXTextClass const & tc =
134 owner_->buffer()->params.getLyXTextClass();
136 QString const & name = qt_(tc[layout]->name());
139 for (; i < combo_->count(); ++i) {
140 if (name == combo_->text(i))
144 if (i == combo_->count()) {
145 lyxerr << "Trying to select non existent layout type "
146 << fromqstr(name) << endl;
150 combo_->setCurrentItem(i);
154 void QLToolbar::updateLayoutList()
159 LyXTextClass const & tc =
160 owner_->buffer()->params.getLyXTextClass();
162 combo_->setUpdatesEnabled(false);
166 LyXTextClass::const_iterator cit = tc.begin();
167 LyXTextClass::const_iterator end = tc.end();
168 for (; cit != end; ++cit) {
169 // ignore obsolete entries
170 if ((*cit)->obsoleted_by().empty())
171 combo_->insertItem(qt_((*cit)->name()));
174 // needed to recalculate size hint
176 combo_->setMinimumWidth(combo_->sizeHint().width());
179 combo_->setUpdatesEnabled(true);
184 void QLToolbar::clearLayoutList()
193 void QLToolbar::openLayoutList()
204 QMainWindow::ToolBarDock getPosition(ToolbarBackend::Flags const & flags)
206 if (flags & ToolbarBackend::TOP)
207 return QMainWindow::Top;
208 if (flags & ToolbarBackend::BOTTOM)
209 return QMainWindow::Bottom;
210 if (flags & ToolbarBackend::LEFT)
211 return QMainWindow::Left;
212 if (flags & ToolbarBackend::RIGHT)
213 return QMainWindow::Right;
214 return QMainWindow::Top;
220 void QLToolbar::add(ToolbarBackend::Toolbar const & tb)
222 QToolBar * qtb = new QToolBar(qt_(tb.name), owner_, getPosition(tb.flags));
223 // give visual separation between adjacent toolbars
226 ToolbarBackend::item_iterator it = tb.items.begin();
227 ToolbarBackend::item_iterator end = tb.items.end();
228 for (; it != end; ++it)
229 add(qtb, it->first, it->second);
231 toolbars_[tb.name] = qtb;
232 displayToolbar(tb, tb.flags & ToolbarBackend::ON);
237 void QLToolbar::add(QToolBar * tb, int action, string const & tooltip)
240 case ToolbarBackend::SEPARATOR:
243 case ToolbarBackend::LAYOUTS: {
244 combo_ = new QLComboBox(tb);
245 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
246 combo_->setSizePolicy(p);
247 combo_->setFocusPolicy(QWidget::ClickFocus);
248 combo_->setMinimumWidth(combo_->sizeHint().width());
250 QObject::connect(combo_, SIGNAL(activated(const QString &)),
251 proxy_.get(), SLOT(layout_selected(const QString &)));
254 case ToolbarBackend::MINIBUFFER:
255 owner_->addCommandBuffer(tb);
256 tb->setHorizontalStretchable(true);
259 if (owner_->getLyXFunc().getStatus(action).unknown())
261 QPixmap p = QPixmap(toolbarbackend.getIcon(action).c_str());
262 QToolButton * button =
263 new QToolButton(p, toqstr(tooltip), "",
264 proxy_.get(), SLOT(button_selected()), tb);
266 map_[button] = action;