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"
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(fullname.c_str());
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(fullname.c_str());
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 if (_((*cit)->name()) == sel) {
150 owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
154 lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
159 void Toolbar::Pimpl::setLayout(string const & layout)
161 LyXTextClass const & tc =
162 owner_->buffer()->params.getLyXTextClass();
164 string const & name = _(tc[layout]->name());
167 for (; i < combo_->count(); ++i) {
168 if (name == combo_->text(i).latin1())
172 if (i == combo_->count()) {
173 lyxerr << "Trying to select non existent layout type "
178 combo_->setCurrentItem(i);
182 void Toolbar::Pimpl::updateLayoutList(bool force)
184 // if we don't need an update, don't ...
185 if (combo_->count() && !force)
188 LyXTextClass const & tc =
189 owner_->buffer()->params.getLyXTextClass();
191 combo_->setUpdatesEnabled(false);
195 LyXTextClass::const_iterator cit = tc.begin();
196 LyXTextClass::const_iterator end = tc.end();
197 for (; cit != end; ++cit) {
198 // ignore obsolete entries
199 if ((*cit)->obsoleted_by().empty())
200 combo_->insertItem(_((*cit)->name()).c_str());
203 // needed to recalculate size hint
205 combo_->setMinimumWidth(combo_->sizeHint().width());
208 combo_->setUpdatesEnabled(true);
213 void Toolbar::Pimpl::clearLayoutList()
219 void Toolbar::Pimpl::openLayoutList()
225 void Toolbar::Pimpl::add(int action)
227 if (!toolbars_.size()) {
228 toolbars_.push_back(new QToolBar(owner_));
232 case ToolbarDefaults::SEPARATOR:
233 toolbars_.back()->addSeparator();
235 case ToolbarDefaults::NEWLINE:
236 toolbars_.push_back(new QToolBar(owner_));
238 case ToolbarDefaults::LAYOUTS: {
239 combo_ = new QLComboBox(toolbars_.back());
240 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
241 combo_->setSizePolicy(p);
242 combo_->setFocusPolicy(QWidget::ClickFocus);
243 combo_->setMinimumWidth(combo_->sizeHint().width());
245 QObject::connect(combo_, SIGNAL(activated(const QString &)),
246 proxy_.get(), SLOT(layout_selected(const QString &)));
251 new QToolButton(getIconPixmap(action),
252 _(lyxaction.helpText(action)).c_str(), "",
253 proxy_.get(), SLOT(button_selected()), toolbars_.back());