]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCommandBuffer.cpp
Oops.. compile fix.
[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         connect(up, SIGNAL(clicked()), this, SLOT(up()));
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         QString cmd = edit_->text();
133         view_->setFocus();
134         edit_->setText(QString());
135         edit_->clearFocus();
136         dispatch(fromqstr(cmd));
137 }
138
139
140 void GuiCommandBuffer::complete()
141 {
142         string const input = fromqstr(edit_->text());
143         string new_input;
144         vector<string> comp = completions(input, new_input);
145
146         if (comp.empty() && new_input == input) {
147                 // show_info_suffix(qt_("[no match]"), input);
148                 return;
149         }
150
151         if (comp.empty()) {
152                 edit_->setText(toqstr(new_input));
153         //      show_info_suffix(("[only completion]"), new_input + ' ');
154                 return;
155         }
156
157         edit_->setText(toqstr(new_input));
158
159         QTempListBox * list = new QTempListBox;
160
161         // For some reason the scrollview's contents are larger
162         // than the number of actual items...
163         vector<string>::const_iterator cit = comp.begin();
164         vector<string>::const_iterator end = comp.end();
165         for (; cit != end; ++cit)
166                 list->addItem(toqstr(*cit));
167
168         list->resize(list->sizeHint());
169         QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
170
171         int const y = max(0, pos.y() - list->height());
172
173         list->move(pos.x(), y);
174
175         connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
176                 this, SLOT(complete_selected(QListWidgetItem *)));
177         connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
178                 this, SLOT(complete_selected(QListWidgetItem *)));
179
180         list->show();
181         list->setFocus();
182 }
183
184
185 void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
186 {
187         QWidget const * widget = static_cast<QWidget const *>(sender());
188         const_cast<QWidget *>(widget)->hide();
189         edit_->setText(item->text() + ' ');
190         edit_->activateWindow();
191         edit_->setFocus();
192 }
193
194
195 void GuiCommandBuffer::up()
196 {
197         string const input = fromqstr(edit_->text());
198         string const h = historyUp();
199
200         if (h.empty()) {
201         //      show_info_suffix(qt_("[Beginning of history]"), input);
202         } else {
203                 edit_->setText(toqstr(h));
204         }
205 }
206
207
208 void GuiCommandBuffer::down()
209 {
210         string const input = fromqstr(edit_->text());
211         string const h = historyDown();
212
213         if (h.empty()) {
214         //      show_info_suffix(qt_("[End of history]"), input);
215         } else {
216                 edit_->setText(toqstr(h));
217         }
218 }
219
220
221 void GuiCommandBuffer::hideParent()
222 {
223         view_->setFocus();
224         edit_->setText(QString());
225         edit_->clearFocus();
226         hide();
227 }
228
229
230 namespace {
231
232 class prefix_p {
233 public:
234         string p;
235         prefix_p(string const & s) : p(s) {}
236         bool operator()(string const & s) const { return prefixIs(s, p); }
237 };
238
239 } // end of anon namespace
240
241
242 string const GuiCommandBuffer::historyUp()
243 {
244         if (history_pos_ == history_.begin())
245                 return string();
246
247         return *(--history_pos_);
248 }
249
250
251 string const GuiCommandBuffer::historyDown()
252 {
253         if (history_pos_ == history_.end())
254                 return string();
255         if (history_pos_ + 1 == history_.end())
256                 return string();
257
258         return *(++history_pos_);
259 }
260
261
262 docstring const GuiCommandBuffer::getCurrentState() const
263 {
264         return view_->view()->cursor().currentState();
265 }
266
267
268 void GuiCommandBuffer::hide() const
269 {
270         FuncRequest cmd(LFUN_COMMAND_EXECUTE, "off");
271         theLyXFunc().setLyXView(view_);
272         lyx::dispatch(cmd);
273 }
274
275
276 vector<string> const
277 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
278 {
279         vector<string> comp;
280
281         copy_if(commands_.begin(), commands_.end(),
282                 back_inserter(comp), prefix_p(prefix));
283
284         if (comp.empty()) {
285                 new_prefix = prefix;
286                 return comp;
287         }
288
289         if (comp.size() == 1) {
290                 new_prefix = comp[0];
291                 return vector<string>();
292         }
293
294         // find maximal available prefix
295         string const tmp = comp[0];
296         string test = prefix;
297         if (tmp.length() > test.length())
298                 test += tmp[test.length()];
299         while (test.length() < tmp.length()) {
300                 vector<string> vtmp;
301                 copy_if(comp.begin(), comp.end(),
302                         back_inserter(vtmp), prefix_p(test));
303                 if (vtmp.size() != comp.size()) {
304                         test.erase(test.length() - 1);
305                         break;
306                 }
307                 test += tmp[test.length()];
308         }
309
310         new_prefix = test;
311         return comp;
312 }
313
314
315 void GuiCommandBuffer::dispatch(string const & str)
316 {
317         if (str.empty())
318                 return;
319
320         history_.push_back(str);
321         history_pos_ = history_.end();
322         FuncRequest func = lyxaction.lookupFunc(str);
323         func.origin = FuncRequest::COMMANDBUFFER;
324         theLyXFunc().setLyXView(view_);
325         lyx::dispatch(func);
326 }
327
328 } // namespace frontend
329 } // namespace lyx
330
331 #include "moc_GuiCommandBuffer.cpp"