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