2 * \file qt3/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>
40 LyXTextClass const & getTextClass(LyXView const & lv)
42 return lv.buffer()->params().getLyXTextClass();
45 QMainWindow::ToolBarDock getPosition(ToolbarBackend::Flags const & flags)
47 if (flags & ToolbarBackend::TOP)
48 return QMainWindow::Top;
49 if (flags & ToolbarBackend::BOTTOM)
50 return QMainWindow::Bottom;
51 if (flags & ToolbarBackend::LEFT)
52 return QMainWindow::Left;
53 if (flags & ToolbarBackend::RIGHT)
54 return QMainWindow::Right;
55 return QMainWindow::Top;
61 QLayoutBox::QLayoutBox(QWidget * parent, QtView & owner)
62 : combo_(new QComboBox(parent)),
65 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
66 combo_->setSizePolicy(p);
67 combo_->setFocusPolicy(QWidget::ClickFocus);
68 combo_->setMinimumWidth(combo_->sizeHint().width());
70 QObject::connect(combo_, SIGNAL(activated(const QString &)),
71 this, SLOT(selected(const QString &)));
75 void QLayoutBox::set(string const & layout)
77 LyXTextClass const & tc = getTextClass(owner_);
79 QString const & name = qt_(tc[layout]->name());
82 for (; i < combo_->count(); ++i) {
83 if (name == combo_->text(i))
87 if (i == combo_->count()) {
88 lyxerr << "Trying to select non existent layout type "
89 << fromqstr(name) << endl;
93 combo_->setCurrentItem(i);
97 void QLayoutBox::update()
99 LyXTextClass const & tc = getTextClass(owner_);
101 combo_->setUpdatesEnabled(false);
105 LyXTextClass::const_iterator it = tc.begin();
106 LyXTextClass::const_iterator const end = tc.end();
107 for (; it != end; ++it) {
108 // ignore obsolete entries
109 if ((*it)->obsoleted_by().empty())
110 combo_->insertItem(qt_((*it)->name()));
113 // needed to recalculate size hint
115 combo_->setMinimumWidth(combo_->sizeHint().width());
118 combo_->setUpdatesEnabled(true);
123 void QLayoutBox::clear()
129 void QLayoutBox::open()
135 void QLayoutBox::setEnabled(bool enable)
137 // Workaround for Qt bug where setEnabled(true) closes
139 if (enable != combo_->isEnabled())
140 combo_->setEnabled(enable);
144 void QLayoutBox::selected(const QString & str)
146 string const sel = fromqstr(str);
148 owner_.centralWidget()->setFocus();
150 layoutSelected(owner_, sel);
153 } // namespace frontend
156 Toolbars::ToolbarPtr make_toolbar(ToolbarBackend::Toolbar const & tbb,
159 using lyx::frontend::QLToolbar;
160 return Toolbars::ToolbarPtr(new QLToolbar(tbb, owner));
166 QLToolbar::QLToolbar(ToolbarBackend::Toolbar const & tbb, LyXView & owner)
167 : owner_(dynamic_cast<QtView &>(owner)),
168 toolbar_(new QToolBar(qt_(tbb.gui_name), &owner_,
169 getPosition(tbb.flags)))
171 // give visual separation between adjacent toolbars
172 toolbar_->addSeparator();
174 ToolbarBackend::item_iterator it = tbb.items.begin();
175 ToolbarBackend::item_iterator end = tbb.items.end();
176 for (; it != end; ++it)
177 add(it->first, it->second);
181 void QLToolbar::add(FuncRequest const & func, string const & tooltip)
183 switch (func.action) {
184 case ToolbarBackend::SEPARATOR:
185 toolbar_->addSeparator();
187 case ToolbarBackend::LAYOUTS:
188 layout_.reset(new QLayoutBox(toolbar_, owner_));
190 case ToolbarBackend::MINIBUFFER:
191 owner_.addCommandBuffer(toolbar_);
192 toolbar_->setHorizontalStretchable(true);
195 if (owner_.getLyXFunc().getStatus(func).unknown())
197 QPixmap p = QPixmap(toolbarbackend.getIcon(func).c_str());
198 QToolButton * button =
199 new QToolButton(p, toqstr(tooltip), "",
200 this, SLOT(clicked()), toolbar_);
209 void QLToolbar::hide(bool)
215 void QLToolbar::show(bool)
221 void QLToolbar::update()
223 ButtonMap::const_iterator p = map_.begin();
224 ButtonMap::const_iterator end = map_.end();
226 for (; p != end; ++p) {
227 QToolButton * button = p->first;
228 FuncRequest const & func = p->second;
230 FuncStatus const status =
231 owner_.getLyXFunc().getStatus(func);
233 button->setToggleButton(true);
234 button->setOn(status.onoff(true));
235 button->setEnabled(status.enabled());
240 void QLToolbar::clicked()
242 QToolButton const * const_button =
243 static_cast<QToolButton const *>(sender());
244 QToolButton * button =
245 const_cast<QToolButton *>(const_button);
247 ButtonMap::const_iterator it = map_.find(button);
249 if (it != map_.end())
250 owner_.getLyXFunc().dispatch(it->second);
252 lyxerr << "non existent tool button selected !" << endl;
255 } // namespace frontend