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
9 * \author Angus Leeming
11 * Full author contact details are available in file CREDITS.
17 #include "bufferparams.h"
19 #include "funcrequest.h"
20 #include "FuncStatus.h"
25 #include "QLToolbar.h"
26 #include "qt_helpers.h"
28 #include <qcombobox.h>
30 #include <qtoolbutton.h>
38 LyXTextClass const & getTextClass(LyXView const & lv)
40 return lv.buffer()->params().getLyXTextClass();
43 QMainWindow::ToolBarDock getPosition(ToolbarBackend::Flags const & flags)
45 if (flags & ToolbarBackend::TOP)
46 return QMainWindow::Top;
47 if (flags & ToolbarBackend::BOTTOM)
48 return QMainWindow::Bottom;
49 if (flags & ToolbarBackend::LEFT)
50 return QMainWindow::Left;
51 if (flags & ToolbarBackend::RIGHT)
52 return QMainWindow::Right;
53 return QMainWindow::Top;
59 QLayoutBox::QLayoutBox(QWidget * parent, QtView & owner)
60 : combo_(new QComboBox(parent)),
63 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
64 combo_->setSizePolicy(p);
65 combo_->setFocusPolicy(QWidget::ClickFocus);
66 combo_->setMinimumWidth(combo_->sizeHint().width());
68 QObject::connect(combo_, SIGNAL(activated(const QString &)),
69 this, SLOT(selected(const QString &)));
73 void QLayoutBox::set(string const & layout)
75 LyXTextClass const & tc = getTextClass(owner_);
77 QString const & name = qt_(tc[layout]->name());
80 for (; i < combo_->count(); ++i) {
81 if (name == combo_->text(i))
85 if (i == combo_->count()) {
86 lyxerr << "Trying to select non existent layout type "
87 << fromqstr(name) << endl;
91 combo_->setCurrentItem(i);
95 void QLayoutBox::update()
97 LyXTextClass const & tc = getTextClass(owner_);
99 combo_->setUpdatesEnabled(false);
103 LyXTextClass::const_iterator it = tc.begin();
104 LyXTextClass::const_iterator const end = tc.end();
105 for (; it != end; ++it) {
106 // ignore obsolete entries
107 if ((*it)->obsoleted_by().empty())
108 combo_->insertItem(qt_((*it)->name()));
111 // needed to recalculate size hint
113 combo_->setMinimumWidth(combo_->sizeHint().width());
116 combo_->setUpdatesEnabled(true);
121 void QLayoutBox::clear()
127 void QLayoutBox::open()
133 void QLayoutBox::setEnabled(bool enable)
135 // Workaround for Qt bug where setEnabled(true) closes
137 if (enable != combo_->isEnabled())
138 combo_->setEnabled(enable);
142 void QLayoutBox::selected(const QString & str)
144 string const sel = fromqstr(str);
146 owner_.centralWidget()->setFocus();
148 LyXTextClass const & tc = getTextClass(owner_);
150 LyXTextClass::const_iterator it = tc.begin();
151 LyXTextClass::const_iterator const end = tc.end();
152 for (; it != end; ++it) {
153 string const & name = (*it)->name();
154 // Yes, the _() is correct
155 if (_(name) == sel) {
157 .dispatch(FuncRequest(LFUN_LAYOUT, name),
162 lyxerr << "ERROR (QLayoutBox::selected): layout not found!" << endl;
166 Toolbars::ToolbarPtr make_toolbar(ToolbarBackend::Toolbar const & tbb,
169 return Toolbars::ToolbarPtr(new QLToolbar(tbb, owner));
173 QLToolbar::QLToolbar(ToolbarBackend::Toolbar const & tbb, LyXView & owner)
174 : owner_(dynamic_cast<QtView &>(owner)),
175 toolbar_(new QToolBar(qt_(tbb.gui_name), &owner_,
176 getPosition(tbb.flags)))
178 // give visual separation between adjacent toolbars
179 toolbar_->addSeparator();
181 ToolbarBackend::item_iterator it = tbb.items.begin();
182 ToolbarBackend::item_iterator end = tbb.items.end();
183 for (; it != end; ++it)
184 add(it->first, it->second);
188 void QLToolbar::add(FuncRequest const & func, string const & tooltip)
190 switch (func.action) {
191 case ToolbarBackend::SEPARATOR:
192 toolbar_->addSeparator();
194 case ToolbarBackend::LAYOUTS:
195 layout_.reset(new QLayoutBox(toolbar_, owner_));
197 case ToolbarBackend::MINIBUFFER:
198 owner_.addCommandBuffer(toolbar_);
199 toolbar_->setHorizontalStretchable(true);
202 if (owner_.getLyXFunc().getStatus(func).unknown())
204 QPixmap p = QPixmap(toolbarbackend.getIcon(func).c_str());
205 QToolButton * button =
206 new QToolButton(p, toqstr(tooltip), "",
207 this, SLOT(clicked()), toolbar_);
216 void QLToolbar::hide(bool)
222 void QLToolbar::show(bool)
228 void QLToolbar::update()
230 ButtonMap::const_iterator p = map_.begin();
231 ButtonMap::const_iterator end = map_.end();
233 for (; p != end; ++p) {
234 QToolButton * button = p->first;
235 FuncRequest const & func = p->second;
237 FuncStatus const status =
238 owner_.getLyXFunc().getStatus(func);
240 button->setToggleButton(true);
241 button->setOn(status.onoff(true));
242 button->setEnabled(status.enabled());
247 void QLToolbar::clicked()
249 QToolButton const * const_button =
250 static_cast<QToolButton const *>(sender());
251 QToolButton * button =
252 const_cast<QToolButton *>(const_button);
254 ButtonMap::const_iterator it = map_.find(button);
256 if (it != map_.end())
257 owner_.getLyXFunc().dispatch(it->second, true);
259 lyxerr << "non existent tool button selected !" << endl;