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
15 #pragma implementation
18 #include "ToolbarDefaults.h"
21 #include "FuncStatus.h"
22 #include "BufferView.h"
24 #include "LyXAction.h"
25 #include "qt_helpers.h"
27 #include "support/LAssert.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
31 #include "ControlMath.h"
33 #include <boost/tuple/tuple.hpp>
36 #include "Toolbar_pimpl.h"
39 #include <qcombobox.h>
41 #include <qsizepolicy.h>
47 QPixmap getIconPixmap(int action)
49 FuncRequest f = lyxaction.retrieveActionArg(action);
53 if (f.action == LFUN_INSERT_MATH && !f.argument.empty()) {
54 fullname = find_xpm(f.argument.substr(1));
56 string const name = lyxaction.getActionName(f.action);
57 string xpm_name(name);
59 if (!f.argument.empty())
60 xpm_name = subst(name + ' ' + f.argument, ' ','_');
62 fullname = LibFileSearch("images", xpm_name, "xpm");
66 if (!fullname.empty()) {
67 lyxerr[Debug::GUI] << "Full icon name is `"
68 << fullname << '\'' << endl;
69 return QPixmap(toqstr(fullname));
72 lyxerr << "Unable to find icon `" << fullname << '\'' << endl;
73 fullname = LibFileSearch("images", "unknown", "xpm");
74 if (!fullname.empty()) {
75 lyxerr[Debug::GUI] << "Using default `unknown' icon"
78 return QPixmap(toqstr(fullname));
84 class QLComboBox : public QComboBox {
86 QLComboBox(QWidget * parent) : QComboBox(parent) {}
87 void popup() { QComboBox::popup(); }
91 Toolbar::Pimpl::Pimpl(LyXView * o, int, int)
92 : owner_(static_cast<QtView *>(o)),
95 proxy_.reset(new ToolbarProxy(*this));
99 Toolbar::Pimpl::~Pimpl()
104 void Toolbar::Pimpl::update()
106 ButtonMap::const_iterator p = map_.begin();
107 ButtonMap::const_iterator end = map_.end();
109 for (; p != end; ++p) {
110 QToolButton * button = p->first;
111 int action = p->second;
113 FuncStatus const status =
114 owner_->getLyXFunc().getStatus(action);
116 button->setToggleButton(true);
117 button->setOn(status.onoff(true));
118 button->setEnabled(!status.disabled());
122 combo_->setEnabled(!owner_->getLyXFunc().getStatus(LFUN_LAYOUT).disabled());
126 void Toolbar::Pimpl::button_selected(QToolButton * button)
128 ButtonMap::const_iterator cit = map_.find(button);
130 if (cit == map_.end()) {
131 lyxerr << "non existent tool button selected !" << endl;
135 owner_->getLyXFunc().dispatch(cit->second, true);
139 void Toolbar::Pimpl::changed_layout(string const & sel)
141 owner_->centralWidget()->setFocus();
143 LyXTextClass const & tc =
144 owner_->buffer()->params.getLyXTextClass();
146 LyXTextClass::const_iterator end = tc.end();
147 for (LyXTextClass::const_iterator cit = tc.begin();
149 // Yes, the _() is correct
150 if (_((*cit)->name()) == sel) {
151 owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
155 lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
160 void Toolbar::Pimpl::setLayout(string const & layout)
162 LyXTextClass const & tc =
163 owner_->buffer()->params.getLyXTextClass();
165 QString const & name = qt_(tc[layout]->name());
168 for (; i < combo_->count(); ++i) {
169 if (name == combo_->text(i))
173 if (i == combo_->count()) {
174 lyxerr << "Trying to select non existent layout type "
175 << fromqstr(name) << endl;
179 combo_->setCurrentItem(i);
183 void Toolbar::Pimpl::updateLayoutList(bool force)
185 // if we don't need an update, don't ...
186 if (combo_->count() && !force)
189 LyXTextClass const & tc =
190 owner_->buffer()->params.getLyXTextClass();
192 combo_->setUpdatesEnabled(false);
196 LyXTextClass::const_iterator cit = tc.begin();
197 LyXTextClass::const_iterator end = tc.end();
198 for (; cit != end; ++cit) {
199 // ignore obsolete entries
200 if ((*cit)->obsoleted_by().empty())
201 combo_->insertItem(qt_((*cit)->name()));
204 // needed to recalculate size hint
206 combo_->setMinimumWidth(combo_->sizeHint().width());
209 combo_->setUpdatesEnabled(true);
214 void Toolbar::Pimpl::clearLayoutList()
220 void Toolbar::Pimpl::openLayoutList()
226 void Toolbar::Pimpl::add(int action)
228 if (!toolbars_.size()) {
229 toolbars_.push_back(new QToolBar(owner_));
233 case ToolbarDefaults::SEPARATOR:
234 toolbars_.back()->addSeparator();
236 case ToolbarDefaults::NEWLINE:
237 toolbars_.push_back(new QToolBar(owner_));
239 case ToolbarDefaults::LAYOUTS: {
240 combo_ = new QLComboBox(toolbars_.back());
241 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
242 combo_->setSizePolicy(p);
243 combo_->setFocusPolicy(QWidget::ClickFocus);
244 combo_->setMinimumWidth(combo_->sizeHint().width());
246 QObject::connect(combo_, SIGNAL(activated(const QString &)),
247 proxy_.get(), SLOT(layout_selected(const QString &)));
252 new QToolButton(getIconPixmap(action),
253 qt_(lyxaction.helpText(action)), "",
254 proxy_.get(), SLOT(button_selected()), toolbars_.back());