]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QLToolbar.C
This commit saves the need to check for lyx::use_gui in a number of places.
[lyx.git] / src / frontends / qt4 / QLToolbar.C
1 /**
2  * \file qt4/QLToolbar.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author Abdelrazak Younes
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "buffer.h"
18 #include "bufferparams.h"
19 #include "debug.h"
20 #include "funcrequest.h"
21 #include "FuncStatus.h"
22 #include "gettext.h"
23 #include "lyxfunc.h"
24
25 #include "GuiView.h"
26 #include "QLToolbar.h"
27 #include "Action.h"
28 #include "qt_helpers.h"
29 #include "InsertTableWidget.h"
30
31 #include <QComboBox>
32 #include <QToolBar>
33 #include <QToolButton>
34 #include <QAction>
35 #include <QPixmap>
36
37 using std::endl;
38 using std::string;
39
40 namespace lyx {
41 namespace frontend {
42
43 namespace {
44
45 LyXTextClass const & getTextClass(LyXView const & lv)
46 {
47         return lv.buffer()->params().getLyXTextClass();
48 }
49
50
51 } // namespace anon
52
53
54 QLayoutBox::QLayoutBox(QToolBar * toolbar, GuiView & owner)
55         : owner_(owner)
56 {
57         combo_ = new QComboBox;
58         combo_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
59         combo_->setFocusPolicy(Qt::ClickFocus);
60         combo_->setMinimumWidth(combo_->sizeHint().width());
61         combo_->setMaxVisibleItems(100);
62
63         QObject::connect(combo_, SIGNAL(activated(const QString &)),
64                          this, SLOT(selected(const QString &)));
65
66         toolbar->addWidget(combo_);
67 }
68
69
70 void QLayoutBox::set(string const & layout)
71 {
72         LyXTextClass const & tc = getTextClass(owner_);
73
74         QString const & name = qt_(tc[layout]->name());
75
76         int i = 0;
77         for (; i < combo_->count(); ++i) {
78                 if (name == combo_->itemText(i))
79                         break;
80         }
81
82         if (i == combo_->count()) {
83                 lyxerr << "Trying to select non existent layout type "
84                         << fromqstr(name) << endl;
85                 return;
86         }
87
88         combo_->setCurrentIndex(i);
89 }
90
91
92 void QLayoutBox::update()
93 {
94         LyXTextClass const & tc = getTextClass(owner_);
95
96         combo_->setUpdatesEnabled(false);
97
98         combo_->clear();
99
100         LyXTextClass::const_iterator it = tc.begin();
101         LyXTextClass::const_iterator const end = tc.end();
102         for (; it != end; ++it) {
103                 // ignore obsolete entries
104                 if ((*it)->obsoleted_by().empty())
105                         combo_->addItem(qt_((*it)->name()));
106         }
107
108         // needed to recalculate size hint
109         combo_->hide();
110         combo_->setMinimumWidth(combo_->sizeHint().width());
111         combo_->show();
112
113         combo_->setUpdatesEnabled(true);
114         combo_->update();
115 }
116
117
118 void QLayoutBox::clear()
119 {
120         combo_->clear();
121 }
122
123
124 void QLayoutBox::open()
125 {
126         combo_->showPopup();
127 }
128
129
130 void QLayoutBox::setEnabled(bool enable)
131 {
132         // Workaround for Qt bug where setEnabled(true) closes
133         // the popup
134         if (enable != combo_->isEnabled())
135                 combo_->setEnabled(enable);
136 }
137
138
139 void QLayoutBox::selected(const QString & str)
140 {
141         string const sel = fromqstr(str);
142
143         owner_.centralWidget()->setFocus();
144
145         layoutSelected(owner_, sel);
146 }
147
148
149 QLToolbar::QLToolbar(ToolbarBackend::Toolbar const & tbb, GuiView & owner)
150         : QToolBar(qt_(tbb.gui_name), &owner), owner_(owner)
151 {
152         // give visual separation between adjacent toolbars
153         addSeparator();
154
155         // allowing the toolbars to tear off is too easily done,
156         // and we don't save their orientation anyway. Disable the handle.
157         setMovable(false);
158
159         ToolbarBackend::item_iterator it = tbb.items.begin();
160         ToolbarBackend::item_iterator end = tbb.items.end();
161         // FIXME UNICODE: ToolbarBackend!!!
162         for (; it != end; ++it)
163                 add(it->first, lyx::from_utf8(it->second));
164 }
165
166
167 void QLToolbar::add(FuncRequest const & func, docstring const & tooltip)
168 {
169         switch (func.action) {
170         case ToolbarBackend::SEPARATOR:
171                 addSeparator();
172                 break;
173         case ToolbarBackend::LAYOUTS:
174                 layout_.reset(new QLayoutBox(this, owner_));
175                 break;
176         case ToolbarBackend::MINIBUFFER:
177                 owner_.addCommandBuffer(this);
178                 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
179                 //setHorizontalStretchable(true);
180                 break;
181         case LFUN_TABULAR_INSERT: {
182                 QToolButton * tb = new QToolButton;
183                 tb->setCheckable(true);
184                 tb->setIcon(QPixmap(toqstr(toolbarbackend.getIcon(func))));
185                 tb->setToolTip(toqstr(tooltip));
186                 tb->setFocusPolicy(Qt::NoFocus);
187                 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
188                 connect(tb, SIGNAL(toggled(bool)), iv, SLOT(show(bool)));
189                 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
190                 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
191                 addWidget(tb);
192                 break;
193                 }
194         default: {
195                 if (lyx::getStatus(func).unknown())
196                         break;
197
198                 Action * action = new Action(owner_, toolbarbackend.getIcon(func), lyx::docstring(), func, tooltip);
199                 addAction(action);
200                 ActionVector.push_back(action);
201                 break;
202                 }
203         }
204 }
205
206
207 void QLToolbar::hide(bool)
208 {
209         QToolBar::hide();
210 }
211
212
213 void QLToolbar::show(bool)
214 {
215         QToolBar::show();
216 }
217
218
219 void QLToolbar::update()
220 {
221         for (size_t i=0; i<ActionVector.size(); ++i)
222                 ActionVector[i]->update();
223
224         // emit signal
225         updated();
226 }
227
228
229 } // namespace frontend
230 } // namespace lyx
231
232 #include "QLToolbar_moc.cpp"