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 #include "ToolbarDefaults.h"
18 #include "FuncStatus.h"
19 #include "BufferView.h"
21 #include "LyXAction.h"
22 #include "qt_helpers.h"
24 #include "support/LAssert.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
28 #include "ControlMath.h"
30 #include <boost/tuple/tuple.hpp>
33 #include "Toolbar_pimpl.h"
36 #include <qcombobox.h>
38 #include <qsizepolicy.h>
44 QPixmap getIconPixmap(int action)
46 FuncRequest f = lyxaction.retrieveActionArg(action);
50 if (f.action == LFUN_INSERT_MATH && !f.argument.empty()) {
51 fullname = find_xpm(f.argument.substr(1));
53 string const name = lyxaction.getActionName(f.action);
54 string xpm_name(name);
56 if (!f.argument.empty())
57 xpm_name = subst(name + ' ' + f.argument, ' ','_');
59 fullname = LibFileSearch("images", xpm_name, "xpm");
63 if (!fullname.empty()) {
64 lyxerr[Debug::GUI] << "Full icon name is `"
65 << fullname << '\'' << endl;
66 return QPixmap(toqstr(fullname));
69 lyxerr << "Unable to find icon `" << fullname << '\'' << endl;
70 fullname = LibFileSearch("images", "unknown", "xpm");
71 if (!fullname.empty()) {
72 lyxerr[Debug::GUI] << "Using default `unknown' icon"
75 return QPixmap(toqstr(fullname));
81 class QLComboBox : public QComboBox {
83 QLComboBox(QWidget * parent) : QComboBox(parent) {}
84 void popup() { QComboBox::popup(); }
88 Toolbar::Pimpl::Pimpl(LyXView * o, int, int)
89 : owner_(static_cast<QtView *>(o)),
92 proxy_.reset(new ToolbarProxy(*this));
96 Toolbar::Pimpl::~Pimpl()
101 void Toolbar::Pimpl::update()
103 ButtonMap::const_iterator p = map_.begin();
104 ButtonMap::const_iterator end = map_.end();
106 for (; p != end; ++p) {
107 QToolButton * button = p->first;
108 int action = p->second;
110 FuncStatus const status =
111 owner_->getLyXFunc().getStatus(action);
113 button->setToggleButton(true);
114 button->setOn(status.onoff(true));
115 button->setEnabled(!status.disabled());
119 combo_->setEnabled(!owner_->getLyXFunc().getStatus(LFUN_LAYOUT).disabled());
123 void Toolbar::Pimpl::button_selected(QToolButton * button)
125 ButtonMap::const_iterator cit = map_.find(button);
127 if (cit == map_.end()) {
128 lyxerr << "non existent tool button selected !" << endl;
132 owner_->getLyXFunc().dispatch(cit->second, true);
136 void Toolbar::Pimpl::changed_layout(string const & sel)
138 owner_->centralWidget()->setFocus();
140 LyXTextClass const & tc =
141 owner_->buffer()->params.getLyXTextClass();
143 LyXTextClass::const_iterator end = tc.end();
144 for (LyXTextClass::const_iterator cit = tc.begin();
146 // Yes, the _() is correct
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 QString const & name = qt_(tc[layout]->name());
165 for (; i < combo_->count(); ++i) {
166 if (name == combo_->text(i))
170 if (i == combo_->count()) {
171 lyxerr << "Trying to select non existent layout type "
172 << fromqstr(name) << endl;
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(qt_((*cit)->name()));
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 qt_(lyxaction.helpText(action)), "",
251 proxy_.get(), SLOT(button_selected()), toolbars_.back());