2 * \file qt4/GuiToolbar.cpp
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
10 * \author Abdelrazak Younes
12 * Full author contact details are available in file CREDITS.
18 #include "BufferParams.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
23 #include "IconPalette.h"
25 #include "ToolbarBackend.h"
28 #include "GuiCommandBuffer.h"
29 #include "GuiToolbar.h"
30 #include "LyXAction.h"
32 #include "qt_helpers.h"
33 #include "InsertTableWidget.h"
35 #include "support/filetools.h"
36 #include "support/lstrings.h"
38 #include "controllers/ControlMath.h"
42 #include <QToolButton>
50 using support::FileName;
51 using support::libFileSearch;
56 static TextClass const & textClass(LyXView const & lv)
58 return lv.buffer()->params().getTextClass();
62 GuiLayoutBox::GuiLayoutBox(QToolBar * toolbar, GuiViewBase & owner)
65 combo_ = new QComboBox;
66 combo_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
67 combo_->setFocusPolicy(Qt::ClickFocus);
68 combo_->setMinimumWidth(combo_->sizeHint().width());
69 combo_->setMaxVisibleItems(100);
71 QObject::connect(combo_, SIGNAL(activated(const QString &)),
72 this, SLOT(selected(const QString &)));
74 toolbar->addWidget(combo_);
78 void GuiLayoutBox::set(docstring const & layout)
80 TextClass const & tc = textClass(owner_);
82 QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
85 for (; i < combo_->count(); ++i) {
86 if (name == combo_->itemText(i))
90 if (i == combo_->count()) {
91 lyxerr << "Trying to select non existent layout type "
92 << fromqstr(name) << endl;
96 combo_->setCurrentIndex(i);
100 void GuiLayoutBox::update()
102 TextClass const & tc = textClass(owner_);
104 combo_->setUpdatesEnabled(false);
108 TextClass::const_iterator it = tc.begin();
109 TextClass::const_iterator const end = tc.end();
110 for (; it != end; ++it) {
111 // ignore obsolete entries
112 if ((*it)->obsoleted_by().empty())
113 combo_->addItem(toqstr(translateIfPossible((*it)->name())));
116 // needed to recalculate size hint
118 combo_->setMinimumWidth(combo_->sizeHint().width());
121 combo_->setUpdatesEnabled(true);
126 void GuiLayoutBox::clear()
132 void GuiLayoutBox::open()
138 void GuiLayoutBox::setEnabled(bool enable)
140 // Workaround for Qt bug where setEnabled(true) closes
142 if (enable != combo_->isEnabled())
143 combo_->setEnabled(enable);
147 void GuiLayoutBox::selected(const QString & str)
151 layoutSelected(owner_, qstring_to_ucs4(str));
155 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiViewBase & owner)
156 : QToolBar(qt_(tbinfo.gui_name), &owner), command_buffer_(0), owner_(owner)
158 // give visual separation between adjacent toolbars
161 // TODO: save toolbar position
164 ToolbarInfo::item_iterator it = tbinfo.items.begin();
165 ToolbarInfo::item_iterator end = tbinfo.items.end();
166 for (; it != end; ++it)
171 void GuiToolbar::focusCommandBuffer()
174 command_buffer_->setFocus();
178 void GuiToolbar::add(ToolbarItem const & item)
180 switch (item.type_) {
181 case ToolbarItem::SEPARATOR:
184 case ToolbarItem::LAYOUTS:
185 layout_.reset(new GuiLayoutBox(this, owner_));
187 case ToolbarItem::MINIBUFFER:
188 command_buffer_ = new GuiCommandBuffer(&owner_);
189 addWidget(command_buffer_);
190 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
191 //setHorizontalStretchable(true);
193 case ToolbarItem::TABLEINSERT: {
194 QToolButton * tb = new QToolButton;
195 tb->setCheckable(true);
196 tb->setIcon(QPixmap(toqstr(getIcon(FuncRequest(LFUN_TABULAR_INSERT)))));
197 tb->setToolTip(qt_(to_ascii(item.label_)));
198 tb->setStatusTip(qt_(to_ascii(item.label_)));
199 tb->setText(qt_(to_ascii(item.label_)));
200 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
201 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
202 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
203 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
207 case ToolbarItem::ICONPALETTE: {
208 QToolButton * tb = new QToolButton(this);
209 tb->setToolTip(qt_(to_ascii(item.label_)));
210 tb->setStatusTip(qt_(to_ascii(item.label_)));
211 tb->setText(qt_(to_ascii(item.label_)));
212 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
213 tb, SLOT(setIconSize(const QSize &)));
214 IconPalette * panel = new IconPalette(tb);
215 panel->setWindowTitle(qt_(to_ascii(item.label_)));
216 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
217 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
219 lyxerr << "Unknown toolbar " << item.name_ << endl;
222 ToolbarInfo::item_iterator it = tbinfo->items.begin();
223 ToolbarInfo::item_iterator const end = tbinfo->items.end();
224 for (; it != end; ++it)
225 if (!getStatus(it->func_).unknown()) {
226 Action * action = new Action(owner_,
231 panel->addButton(action);
232 actions_.push_back(action);
233 // use the icon of first action for the toolbar button
234 if (it == tbinfo->items.begin())
235 tb->setIcon(QPixmap(getIcon(it->func_).c_str()));
237 tb->setCheckable(true);
238 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
239 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
243 case ToolbarItem::POPUPMENU: {
244 QToolButton * tb = new QToolButton;
245 tb->setPopupMode(QToolButton::InstantPopup);
246 tb->setToolTip(qt_(to_ascii(item.label_)));
247 tb->setStatusTip(qt_(to_ascii(item.label_)));
248 tb->setText(qt_(to_ascii(item.label_)));
249 FileName icon_path = libFileSearch("images/math", item.name_, "png");
250 tb->setIcon(QIcon(toqstr(icon_path.absFilename())));
251 connect(this, SIGNAL(iconSizeChanged(const QSize &)),
252 tb, SLOT(setIconSize(const QSize &)));
254 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
255 m->setWindowTitle(qt_(to_ascii(item.label_)));
256 m->setTearOffEnabled(true);
257 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
258 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
260 lyxerr << "Unknown toolbar " << item.name_ << endl;
263 ToolbarInfo::item_iterator it = tbinfo->items.begin();
264 ToolbarInfo::item_iterator const end = tbinfo->items.end();
265 for (; it != end; ++it)
266 if (!getStatus(it->func_).unknown()) {
267 Action * action = new Action(owner_,
268 getIcon(it->func_, false),
273 actions_.push_back(action);
279 case ToolbarItem::COMMAND: {
280 if (getStatus(item.func_).unknown())
283 Action * action = new Action(owner_,
289 actions_.push_back(action);
298 void GuiToolbar::hide(bool)
304 void GuiToolbar::show(bool)
310 bool GuiToolbar::isVisible() const
312 return QToolBar::isVisible();
316 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
318 // if tbinfo.state == auto *do not* set on/off
319 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
320 if (GuiToolbar::isVisible())
321 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
323 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
326 // no need to save it here.
327 Qt::ToolBarArea loc = owner_.toolBarArea(this);
329 if (loc == Qt::TopToolBarArea)
330 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
331 else if (loc == Qt::BottomToolBarArea)
332 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
333 else if (loc == Qt::RightToolBarArea)
334 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
335 else if (loc == Qt::LeftToolBarArea)
336 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
338 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
340 // save toolbar position. They are not used to restore toolbar position
341 // now because move(x,y) does not work for toolbar.
342 tbinfo.posx = pos().x();
343 tbinfo.posy = pos().y();
347 void GuiToolbar::update()
349 // This is a speed bottleneck because this is called on every keypress
350 // and update calls getStatus, which copies the cursor at least two times
351 for (size_t i = 0; i < actions_.size(); ++i)
352 actions_[i]->update();
359 string const getIcon(FuncRequest const & f, bool unknown)
361 using frontend::find_png;
366 case LFUN_MATH_INSERT:
367 if (!f.argument().empty())
368 fullname = find_png(to_utf8(f.argument()).substr(1));
370 case LFUN_MATH_DELIM:
371 case LFUN_MATH_BIGDELIM:
372 fullname = find_png(to_utf8(f.argument()));
375 string const name = lyxaction.getActionName(f.action);
376 string png_name = name;
378 if (!f.argument().empty())
379 png_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
381 fullname = libFileSearch("images", png_name, "png").absFilename();
383 if (fullname.empty()) {
384 // try without the argument
385 fullname = libFileSearch("images", name, "png").absFilename();
389 if (!fullname.empty()) {
390 LYXERR(Debug::GUI) << "Full icon name is `"
391 << fullname << '\'' << endl;
395 LYXERR(Debug::GUI) << "Cannot find icon for command \""
396 << lyxaction.getActionName(f.action)
397 << '(' << to_utf8(f.argument()) << ")\"" << endl;
399 return libFileSearch("images", "unknown", "png").absFilename();
405 } // namespace frontend
408 #include "GuiToolbar_moc.cpp"