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