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