]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCommandBuffer.cpp
math stuff
[lyx.git] / src / frontends / qt4 / GuiCommandBuffer.cpp
1 /**
2  * \file GuiCommandBuffer.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars
7  * \author Asger and Jürgen
8  * \author John Levon
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 // Qt defines a macro 'signals' that clashes with a boost namespace.
16 // All is well if the namespace is visible first.
17 #include "GuiView.h"
18
19 #include "GuiCommandBuffer.h"
20 #include "GuiCommandEdit.h"
21 #include "qt_helpers.h"
22
23 #include "BufferView.h"
24 #include "Cursor.h"
25 #include "LyXFunc.h"
26 #include "LyXAction.h"
27 #include "FuncRequest.h"
28
29 #include "frontends/LyXView.h"
30
31 #include "support/lyxalgo.h"
32 #include "support/lstrings.h"
33 #include "support/filetools.h"
34
35 #include <QHBoxLayout>
36 #include <QKeyEvent>
37 #include <QLayout>
38 #include <QListWidget>
39 #include <QMouseEvent>
40 #include <QPixmap>
41 #include <QPushButton>
42 #include <QToolTip>
43 #include <QVBoxLayout>
44
45 using std::back_inserter;
46 using std::transform;
47 using std::string;
48 using std::vector;
49
50
51 namespace lyx {
52 namespace frontend {
53
54 using support::prefixIs;
55 using support::libFileSearch;
56
57
58 namespace {
59
60 class QTempListBox : public QListWidget {
61 public:
62         QTempListBox() {
63                 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
64                 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65                 setWindowModality(Qt::WindowModal);
66                 setWindowFlags(Qt::Popup);
67                 setAttribute(Qt::WA_DeleteOnClose);
68         }
69 protected:
70         void mouseReleaseEvent(QMouseEvent * ev) {
71                 if (ev->x() < 0 || ev->y() < 0
72                     || ev->x() > width() || ev->y() > height()) {
73                         hide();
74                 } else {
75                         // emit signal
76                         itemPressed(currentItem());
77                 }
78         }
79
80         void keyPressEvent(QKeyEvent * ev) {
81                 if (ev->key() == Qt::Key_Escape) {
82                         hide();
83                         return;
84                 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
85                         // emit signal
86                         itemPressed(currentItem());
87                 } else
88                         QListWidget::keyPressEvent(ev);
89         }
90 };
91
92 } // end of anon
93
94
95 GuiCommandBuffer::GuiCommandBuffer(GuiViewBase * view)
96         : view_(view), lv_(*view), history_pos_(history_.end())
97 {
98         transform(lyxaction.func_begin(), lyxaction.func_end(),
99                 back_inserter(commands_), firster());
100
101         QPixmap qpup(toqstr(libFileSearch("images", "up", "png").absFilename()));
102         QPixmap qpdown(toqstr(libFileSearch("images", "down", "png").absFilename()));
103
104         QVBoxLayout * top = new QVBoxLayout(this);
105         QHBoxLayout * layout = new QHBoxLayout(0);
106
107         QPushButton * up = new QPushButton(qpup, "", this);
108         up->setMaximumSize(24, 24);
109         QPushButton * down = new QPushButton(qpdown, "", this);
110         down->setToolTip(qt_("Next command"));
111         down->setMaximumSize(24, 24);
112         connect(down, SIGNAL(clicked()), this, SLOT(down()));
113
114         edit_ = new GuiCommandEdit(this);
115         edit_->setMinimumSize(edit_->sizeHint());
116         edit_->setFocusPolicy(Qt::ClickFocus);
117
118         connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
119         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
120         connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
121         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
122         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
123         connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
124
125         layout->addWidget(up, 0);
126         layout->addWidget(down, 0);
127         layout->addWidget(edit_, 10);
128         layout->setMargin(0);
129         top->addLayout(layout);
130         top->setMargin(0);
131         setFocusProxy(edit_);
132 }
133
134
135 void GuiCommandBuffer::cancel()
136 {
137         view_->setFocus();
138         edit_->setText(QString());
139 }
140
141
142 void GuiCommandBuffer::dispatch()
143 {
144         dispatch(fromqstr(edit_->text()));
145         view_->setFocus();
146         edit_->setText(QString());
147         edit_->clearFocus();
148 }
149
150
151 void GuiCommandBuffer::complete()
152 {
153         string const input = fromqstr(edit_->text());
154         string new_input;
155         vector<string> comp = completions(input, new_input);
156
157         if (comp.empty() && new_input == input) {
158                 // show_info_suffix(qt_("[no match]"), input);
159                 return;
160         }
161
162         if (comp.empty()) {
163                 edit_->setText(toqstr(new_input));
164         //      show_info_suffix(("[only completion]"), new_input + ' ');
165                 return;
166         }
167
168         edit_->setText(toqstr(new_input));
169
170         QTempListBox * list = new QTempListBox;
171
172         // For some reason the scrollview's contents are larger
173         // than the number of actual items...
174         vector<string>::const_iterator cit = comp.begin();
175         vector<string>::const_iterator end = comp.end();
176         for (; cit != end; ++cit)
177                 list->addItem(toqstr(*cit));
178
179         list->resize(list->sizeHint());
180         QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
181
182         int const y = std::max(0, pos.y() - list->height());
183
184         list->move(pos.x(), y);
185
186         connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
187                 this, SLOT(complete_selected(QListWidgetItem *)));
188         connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
189                 this, SLOT(complete_selected(QListWidgetItem *)));
190
191         list->show();
192         list->setFocus();
193 }
194
195
196 void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
197 {
198         QWidget const * widget = static_cast<QWidget const *>(sender());
199         const_cast<QWidget *>(widget)->hide();
200         edit_->setText(item->text() + ' ');
201         edit_->activateWindow();
202         edit_->setFocus();
203 }
204
205
206 void GuiCommandBuffer::up()
207 {
208         string const input = fromqstr(edit_->text());
209         string const h = historyUp();
210
211         if (h.empty()) {
212         //      show_info_suffix(qt_("[Beginning of history]"), input);
213         } else {
214                 edit_->setText(toqstr(h));
215         }
216 }
217
218
219 void GuiCommandBuffer::down()
220 {
221         string const input = fromqstr(edit_->text());
222         string const h = historyDown();
223
224         if (h.empty()) {
225         //      show_info_suffix(qt_("[End of history]"), input);
226         } else {
227                 edit_->setText(toqstr(h));
228         }
229 }
230
231
232 void GuiCommandBuffer::hideParent()
233 {
234         view_->setFocus();
235         edit_->setText(QString());
236         edit_->clearFocus();
237         hide();
238 }
239
240
241 namespace {
242
243 class prefix_p {
244 public:
245         string p;
246         prefix_p(string const & s) : p(s) {}
247         bool operator()(string const & s) const { return prefixIs(s, p); }
248 };
249
250 } // end of anon namespace
251
252
253 string const GuiCommandBuffer::historyUp()
254 {
255         if (history_pos_ == history_.begin())
256                 return string();
257
258         return *(--history_pos_);
259 }
260
261
262 string const GuiCommandBuffer::historyDown()
263 {
264         if (history_pos_ == history_.end())
265                 return string();
266         if (history_pos_ + 1 == history_.end())
267                 return string();
268
269         return *(++history_pos_);
270 }
271
272
273 docstring const GuiCommandBuffer::getCurrentState() const
274 {
275         return lv_.view()->cursor().currentState();
276 }
277
278
279 void GuiCommandBuffer::hide() const
280 {
281         lv_.showMiniBuffer(false);
282 }
283
284
285 vector<string> const
286 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
287 {
288         vector<string> comp;
289
290         copy_if(commands_.begin(), commands_.end(),
291                 back_inserter(comp), prefix_p(prefix));
292
293         if (comp.empty()) {
294                 new_prefix = prefix;
295                 return comp;
296         }
297
298         if (comp.size() == 1) {
299                 new_prefix = comp[0];
300                 return vector<string>();
301         }
302
303         // find maximal available prefix
304         string const tmp = comp[0];
305         string test = prefix;
306         if (tmp.length() > test.length())
307                 test += tmp[test.length()];
308         while (test.length() < tmp.length()) {
309                 vector<string> vtmp;
310                 copy_if(comp.begin(), comp.end(),
311                         back_inserter(vtmp), prefix_p(test));
312                 if (vtmp.size() != comp.size()) {
313                         test.erase(test.length() - 1);
314                         break;
315                 }
316                 test += tmp[test.length()];
317         }
318
319         new_prefix = test;
320         return comp;
321 }
322
323
324 void GuiCommandBuffer::dispatch(string const & str)
325 {
326         if (str.empty())
327                 return;
328
329         history_.push_back(str);
330         history_pos_ = history_.end();
331         FuncRequest func = lyxaction.lookupFunc(str);
332         func.origin = FuncRequest::COMMANDBUFFER;
333         lv_.dispatch(func);
334 }
335
336 } // namespace frontend
337 } // namespace lyx
338
339 #include "GuiCommandBuffer_moc.cpp"