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"
33 #include "Toolbar_pimpl.h"
35 #include <boost/tuple/tuple.hpp>
38 #include <qcombobox.h>
40 #include <qsizepolicy.h>
46 QPixmap getIconPixmap(int action)
48 FuncRequest f = lyxaction.retrieveActionArg(action);
52 if (f.action == LFUN_INSERT_MATH && !f.argument.empty()) {
53 xpm_name = "math/" + subst(f.argument.substr(1), ' ', '_');
55 string const name = lyxaction.getActionName(f.action);
56 if (!f.argument.empty())
57 xpm_name = subst(name + ' ' + f.argument, ' ','_');
62 string fullname = LibFileSearch("images", xpm_name, "xpm");
64 if (!fullname.empty()) {
65 lyxerr[Debug::GUI] << "Full icon name is `"
66 << fullname << "'" << endl;
67 return QPixmap(fullname.c_str());
70 lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
71 fullname = LibFileSearch("images", "unknown", "xpm");
72 if (!fullname.empty()) {
73 lyxerr[Debug::GUI] << "Using default `unknown' icon"
76 return QPixmap(fullname.c_str());
82 class QLComboBox : public QComboBox {
84 QLComboBox(QWidget * parent) : QComboBox(parent) {}
85 void popup() { QComboBox::popup(); }
89 Toolbar::Pimpl::Pimpl(LyXView * o, int, int)
90 : owner_(static_cast<QtView *>(o)),
93 proxy_.reset(new ToolbarProxy(*this));
97 Toolbar::Pimpl::~Pimpl()
102 void Toolbar::Pimpl::update()
104 ButtonMap::const_iterator p = map_.begin();
105 ButtonMap::const_iterator end = map_.end();
107 for (; p != end; ++p) {
108 QToolButton * button = p->first;
109 int action = p->second;
111 FuncStatus const status =
112 owner_->getLyXFunc().getStatus(action);
114 button->setToggleButton(true);
115 button->setOn(status.onoff(true));
116 button->setEnabled(!status.disabled());
120 combo_->setEnabled(!owner_->getLyXFunc().getStatus(LFUN_LAYOUT).disabled());
124 void Toolbar::Pimpl::button_selected(QToolButton * button)
126 ButtonMap::const_iterator cit = map_.find(button);
128 if (cit == map_.end()) {
129 lyxerr << "non existent tool button selected !" << endl;
133 owner_->getLyXFunc().dispatch(cit->second, true);
137 void Toolbar::Pimpl::changed_layout(string const & sel)
139 owner_->centralWidget()->setFocus();
141 LyXTextClass const & tc =
142 owner_->buffer()->params.getLyXTextClass();
144 LyXTextClass::const_iterator end = tc.end();
145 for (LyXTextClass::const_iterator cit = tc.begin();
147 if (_((*cit)->name()) == sel) {
148 owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
152 lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
157 void Toolbar::Pimpl::setLayout(string const & layout)
159 LyXTextClass const & tc =
160 owner_->buffer()->params.getLyXTextClass();
162 string const & name = _(tc[layout]->name());
165 for (; i < combo_->count(); ++i) {
166 if (name == combo_->text(i).latin1())
170 if (i == combo_->count()) {
171 lyxerr << "Trying to select non existent layout type "
176 combo_->setCurrentItem(i);
180 void Toolbar::Pimpl::updateLayoutList(bool force)
182 // if we don't need an update, don't ...
183 if (combo_->count() && !force)
186 LyXTextClass const & tc =
187 owner_->buffer()->params.getLyXTextClass();
189 combo_->setUpdatesEnabled(false);
193 LyXTextClass::const_iterator cit = tc.begin();
194 LyXTextClass::const_iterator end = tc.end();
195 for (; cit != end; ++cit) {
196 // ignore obsolete entries
197 if ((*cit)->obsoleted_by().empty())
198 combo_->insertItem(_((*cit)->name()).c_str());
201 // needed to recalculate size hint
203 combo_->setMinimumWidth(combo_->sizeHint().width());
206 combo_->setUpdatesEnabled(true);
211 void Toolbar::Pimpl::clearLayoutList()
217 void Toolbar::Pimpl::openLayoutList()
223 void Toolbar::Pimpl::add(int action)
225 if (!toolbars_.size()) {
226 toolbars_.push_back(new QToolBar(owner_));
230 case ToolbarDefaults::SEPARATOR:
231 toolbars_.back()->addSeparator();
233 case ToolbarDefaults::NEWLINE:
234 toolbars_.push_back(new QToolBar(owner_));
236 case ToolbarDefaults::LAYOUTS: {
237 combo_ = new QLComboBox(toolbars_.back());
238 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
239 combo_->setSizePolicy(p);
240 combo_->setFocusPolicy(QWidget::ClickFocus);
241 combo_->setMinimumWidth(combo_->sizeHint().width());
243 QObject::connect(combo_, SIGNAL(activated(const QString &)),
244 proxy_.get(), SLOT(layout_selected(const QString &)));
249 new QToolButton(getIconPixmap(action),
250 _(lyxaction.helpText(action)).c_str(), "",
251 proxy_.get(), SLOT(button_selected()), toolbars_.back());