]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCommandBuffer.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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         QString cmd = edit_->text();
132         view_->setFocus();
133         edit_->setText(QString());
134         edit_->clearFocus();
135         dispatch(fromqstr(cmd));
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"