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"
26 #include "ToolbarBackend.h"
29 #include "GuiCommandBuffer.h"
30 #include "GuiToolbar.h"
31 #include "LyXAction.h"
33 #include "qt_helpers.h"
34 #include "InsertTableWidget.h"
36 #include "support/filetools.h"
37 #include "support/lstrings.h"
39 #include "controllers/ControlMath.h"
43 #include <QToolButton>
51 using support::FileName;
52 using support::libFileSearch;
57 static TextClass const & textClass(LyXView const & lv)
59 return lv.buffer()->params().getTextClass();
63 /////////////////////////////////////////////////////////////////////
67 /////////////////////////////////////////////////////////////////////
69 GuiLayoutBox::GuiLayoutBox(GuiViewBase & owner)
72 setSizeAdjustPolicy(QComboBox::AdjustToContents);
73 setFocusPolicy(Qt::ClickFocus);
74 setMinimumWidth(sizeHint().width());
75 setMaxVisibleItems(100);
77 QObject::connect(this, SIGNAL(activated(QString)),
78 this, SLOT(selected(QString)));
82 void GuiLayoutBox::set(docstring const & layout)
84 TextClass const & tc = textClass(owner_);
86 QString const & name = toqstr(translateIfPossible(tc[layout]->name()));
89 for (; i < count(); ++i) {
90 if (name == itemText(i))
95 lyxerr << "Trying to select non existent layout type "
96 << fromqstr(name) << endl;
104 void GuiLayoutBox::updateContents()
106 TextClass const & tc = textClass(owner_);
108 setUpdatesEnabled(false);
111 TextClass::const_iterator it = tc.begin();
112 TextClass::const_iterator const end = tc.end();
113 for (; it != end; ++it) {
114 // ignore obsolete entries
115 if ((*it)->obsoleted_by().empty())
116 addItem(toqstr(translateIfPossible((*it)->name())));
119 // needed to recalculate size hint
121 setMinimumWidth(sizeHint().width());
124 setUpdatesEnabled(true);
128 void GuiLayoutBox::selected(const QString & str)
131 TextClass const & tc = owner_.buffer()->params().getTextClass();
132 docstring const name = qstring_to_ucs4(str);
133 TextClass::const_iterator it = tc.begin();
134 TextClass::const_iterator const end = tc.end();
135 for (; it != end; ++it) {
136 docstring const & itname = (*it)->name();
137 if (translateIfPossible(itname) == name) {
138 FuncRequest const func(LFUN_LAYOUT, itname,
139 FuncRequest::TOOLBAR);
140 owner_.dispatch(func);
144 lyxerr << "ERROR (layoutSelected): layout not found!"
150 /////////////////////////////////////////////////////////////////////
154 /////////////////////////////////////////////////////////////////////
157 GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiViewBase & owner)
158 : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
159 layout_(0), command_buffer_(0)
161 // give visual separation between adjacent toolbars
164 // TODO: save toolbar position
167 ToolbarInfo::item_iterator it = tbinfo.items.begin();
168 ToolbarInfo::item_iterator end = tbinfo.items.end();
169 for (; it != end; ++it)
174 Action * GuiToolbar::addItem(ToolbarItem const & item)
176 Action * act = new Action(owner_,
177 getIcon(item.func_, false).c_str(),
178 toqstr(item.label_), item.func_, toqstr(item.label_));
179 actions_.append(act);
184 void GuiToolbar::add(ToolbarItem const & item)
186 switch (item.type_) {
187 case ToolbarItem::SEPARATOR:
190 case ToolbarItem::LAYOUTS:
191 layout_ = new GuiLayoutBox(owner_);
194 case ToolbarItem::MINIBUFFER:
195 command_buffer_ = new GuiCommandBuffer(&owner_);
196 addWidget(command_buffer_);
197 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
198 //setHorizontalStretchable(true);
200 case ToolbarItem::TABLEINSERT: {
201 QToolButton * tb = new QToolButton;
202 tb->setCheckable(true);
203 tb->setIcon(QPixmap(toqstr(getIcon(FuncRequest(LFUN_TABULAR_INSERT)))));
204 tb->setToolTip(qt_(to_ascii(item.label_)));
205 tb->setStatusTip(qt_(to_ascii(item.label_)));
206 tb->setText(qt_(to_ascii(item.label_)));
207 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
208 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
209 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
210 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
214 case ToolbarItem::ICONPALETTE: {
215 QToolButton * tb = new QToolButton(this);
216 tb->setToolTip(qt_(to_ascii(item.label_)));
217 tb->setStatusTip(qt_(to_ascii(item.label_)));
218 tb->setText(qt_(to_ascii(item.label_)));
219 connect(this, SIGNAL(iconSizeChanged(QSize)),
220 tb, SLOT(setIconSize(QSize)));
221 IconPalette * panel = new IconPalette(tb);
222 panel->setWindowTitle(qt_(to_ascii(item.label_)));
223 connect(this, SIGNAL(updated()), panel, SLOT(updateParent()));
224 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
226 lyxerr << "Unknown toolbar " << item.name_ << endl;
229 ToolbarInfo::item_iterator it = tbinfo->items.begin();
230 ToolbarInfo::item_iterator const end = tbinfo->items.end();
231 for (; it != end; ++it)
232 if (!getStatus(it->func_).unknown()) {
233 panel->addButton(addItem(*it));
234 // use the icon of first action for the toolbar button
235 if (it == tbinfo->items.begin())
236 tb->setIcon(QPixmap(getIcon(it->func_).c_str()));
238 tb->setCheckable(true);
239 connect(tb, SIGNAL(clicked(bool)), panel, SLOT(setVisible(bool)));
240 connect(panel, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
244 case ToolbarItem::POPUPMENU: {
245 QToolButton * tb = new QToolButton;
246 tb->setPopupMode(QToolButton::InstantPopup);
247 tb->setToolTip(qt_(to_ascii(item.label_)));
248 tb->setStatusTip(qt_(to_ascii(item.label_)));
249 tb->setText(qt_(to_ascii(item.label_)));
250 FileName icon_path = libFileSearch("images/math", item.name_, "png");
251 tb->setIcon(QIcon(toqstr(icon_path.absFilename())));
252 connect(this, SIGNAL(iconSizeChanged(QSize)),
253 tb, SLOT(setIconSize(QSize)));
255 ButtonMenu * m = new ButtonMenu(qt_(to_ascii(item.label_)), tb);
256 m->setWindowTitle(qt_(to_ascii(item.label_)));
257 m->setTearOffEnabled(true);
258 connect(this, SIGNAL(updated()), m, SLOT(updateParent()));
259 ToolbarInfo const * tbinfo = toolbarbackend.getDefinedToolbarInfo(item.name_);
261 lyxerr << "Unknown toolbar " << item.name_ << endl;
264 ToolbarInfo::item_iterator it = tbinfo->items.begin();
265 ToolbarInfo::item_iterator const end = tbinfo->items.end();
266 for (; it != end; ++it)
267 if (!getStatus(it->func_).unknown())
268 m->add(addItem(*it));
273 case ToolbarItem::COMMAND: {
274 if (!getStatus(item.func_).unknown())
275 addAction(addItem(item));
284 void GuiToolbar::saveInfo(ToolbarSection::ToolbarInfo & tbinfo)
286 // if tbinfo.state == auto *do not* set on/off
287 if (tbinfo.state != ToolbarSection::ToolbarInfo::AUTO) {
288 if (GuiToolbar::isVisible())
289 tbinfo.state = ToolbarSection::ToolbarInfo::ON;
291 tbinfo.state = ToolbarSection::ToolbarInfo::OFF;
294 // no need to save it here.
295 Qt::ToolBarArea loc = owner_.toolBarArea(this);
297 if (loc == Qt::TopToolBarArea)
298 tbinfo.location = ToolbarSection::ToolbarInfo::TOP;
299 else if (loc == Qt::BottomToolBarArea)
300 tbinfo.location = ToolbarSection::ToolbarInfo::BOTTOM;
301 else if (loc == Qt::RightToolBarArea)
302 tbinfo.location = ToolbarSection::ToolbarInfo::RIGHT;
303 else if (loc == Qt::LeftToolBarArea)
304 tbinfo.location = ToolbarSection::ToolbarInfo::LEFT;
306 tbinfo.location = ToolbarSection::ToolbarInfo::NOTSET;
308 // save toolbar position. They are not used to restore toolbar position
309 // now because move(x,y) does not work for toolbar.
310 tbinfo.posx = pos().x();
311 tbinfo.posy = pos().y();
315 void GuiToolbar::updateContents()
317 // update visible toolbars only
320 // This is a speed bottleneck because this is called on every keypress
321 // and update calls getStatus, which copies the cursor at least two times
322 for (int i = 0; i < actions_.size(); ++i)
323 actions_[i]->update();
330 string const getIcon(FuncRequest const & f, bool unknown)
332 using frontend::find_png;
337 case LFUN_MATH_INSERT:
338 if (!f.argument().empty())
339 fullname = find_png(to_utf8(f.argument()).substr(1));
341 case LFUN_MATH_DELIM:
342 case LFUN_MATH_BIGDELIM:
343 fullname = find_png(to_utf8(f.argument()));
346 string const name = lyxaction.getActionName(f.action);
347 string png_name = name;
349 if (!f.argument().empty())
350 png_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
352 fullname = libFileSearch("images", png_name, "png").absFilename();
354 if (fullname.empty()) {
355 // try without the argument
356 fullname = libFileSearch("images", name, "png").absFilename();
360 if (!fullname.empty()) {
361 LYXERR(Debug::GUI) << "Full icon name is `"
362 << fullname << '\'' << endl;
366 LYXERR(Debug::GUI) << "Cannot find icon for command \""
367 << lyxaction.getActionName(f.action)
368 << '(' << to_utf8(f.argument()) << ")\"" << endl;
370 return libFileSearch("images", "unknown", "png").absFilename();
376 } // namespace frontend
379 #include "GuiToolbar_moc.cpp"