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