]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCommandBuffer.cpp
5f48010d761c7884257f3bb402a10694048ab981
[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 #include "Session.h"
27
28 #include "support/lyxalgo.h"
29 #include "support/lstrings.h"
30
31 #include <QHBoxLayout>
32 #include <QKeyEvent>
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         bool event(QEvent * ev) {
59                 if (ev->type() == QEvent::MouseButtonPress) {
60                         QMouseEvent * me = static_cast<QMouseEvent *>(ev);
61                         if (me->x() < 0 || me->y() < 0
62                             || me->x() > width() || me->y() > height())
63                                 hide();
64                         return true;
65                 }
66                 return QListWidget::event(ev);
67         }
68
69         void keyPressEvent(QKeyEvent * ev) {
70                 if (ev->key() == Qt::Key_Escape) {
71                         hide();
72                         return;
73                 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
74                         // emit signal
75                         itemClicked(currentItem());
76                 } else
77                         QListWidget::keyPressEvent(ev);
78         }
79 };
80
81 } // end of anon
82
83
84 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
85         : view_(view)
86 {
87         transform(lyxaction.func_begin(), lyxaction.func_end(),
88                 back_inserter(commands_), firster());
89
90         QPixmap qpup(":/images/up.png");
91         QPixmap qpdown(":/images/down.png");
92
93         QVBoxLayout * top = new QVBoxLayout(this);
94         QHBoxLayout * layout = new QHBoxLayout(0);
95
96         upPB = new QPushButton(qpup, "", this);
97         upPB->setToolTip(qt_("List of previous commands"));
98         upPB->setMaximumSize(24, 24);
99         downPB = new QPushButton(qpdown, "", this);
100         downPB->setToolTip(qt_("Next command"));
101         downPB->setMaximumSize(24, 24);
102         downPB->setEnabled(false);
103         connect(downPB, SIGNAL(clicked()), this, SLOT(down()));
104         connect(upPB, SIGNAL(pressed()), this, SLOT(listHistoryUp()));
105
106         edit_ = new GuiCommandEdit(this);
107         edit_->setMinimumSize(edit_->sizeHint());
108         edit_->setFocusPolicy(Qt::ClickFocus);
109
110         connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
111         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
112         connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
113         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
114         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
115         connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
116
117         layout->addWidget(upPB, 0);
118         layout->addWidget(downPB, 0);
119         layout->addWidget(edit_, 10);
120         layout->setMargin(0);
121         top->addLayout(layout);
122         top->setMargin(0);
123         setFocusProxy(edit_);
124
125         LastCommandsSection::LastCommands last_commands 
126                 = theSession().lastCommands().getcommands();
127         LastCommandsSection::LastCommands::const_iterator it 
128                 = last_commands.begin();
129         LastCommandsSection::LastCommands::const_iterator end 
130                 = last_commands.end();
131
132         upPB->setEnabled(it != end);
133
134         for(; it != end; ++it)
135                 history_.push_back(*it);
136         history_pos_ = history_.end();
137 }
138
139
140 void GuiCommandBuffer::cancel()
141 {
142         view_->setFocus();
143         edit_->setText(QString());
144 }
145
146
147 void GuiCommandBuffer::dispatch()
148 {
149         QString cmd = edit_->text();
150         view_->setFocus();
151         edit_->setText(QString());
152         edit_->clearFocus();
153         std::string const cmd_ = fromqstr(cmd);
154         theSession().lastCommands().add(cmd_);
155         dispatch(cmd_);
156 }
157
158
159 void GuiCommandBuffer::listHistoryUp()
160 {
161         if (history_.size()==1) {
162                 edit_->setText(toqstr(history_.back()));
163                 upPB->setEnabled(false);
164                 return;
165         }
166         QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
167         showList(history_, pos, true);
168 }
169
170
171 void GuiCommandBuffer::complete()
172 {
173         string const input = fromqstr(edit_->text());
174         string new_input;
175         vector<string> const & comp = completions(input, new_input);
176
177         if (comp.empty()) {
178                 if (new_input != input)
179                         edit_->setText(toqstr(new_input));
180                 return;
181         }
182
183         edit_->setText(toqstr(new_input));
184         QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
185         showList(comp, pos);
186 }
187
188 void GuiCommandBuffer::showList(vector<string> const & list,
189         QPoint const & pos, bool reversed) const
190 {
191         QTempListBox * listBox = new QTempListBox;
192
193         // For some reason the scrollview's contents are larger
194         // than the number of actual items...
195         vector<string>::const_iterator cit = list.begin();
196         vector<string>::const_iterator end = list.end();
197         for (; cit != end; ++cit) {
198                 if (reversed)
199                         listBox->insertItem(0, toqstr(*cit));
200                 else
201                         listBox->addItem(toqstr(*cit));
202         }
203
204         listBox->resize(listBox->sizeHint());
205
206         int const y = max(0, pos.y() - listBox->height());
207         listBox->move(pos.x(), y);
208
209         connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
210                 this, SLOT(item_selected(QListWidgetItem *)));
211         connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
212                 this, SLOT(item_selected(QListWidgetItem *)));
213
214         listBox->show();
215         listBox->setFocus();
216 }
217
218
219 void GuiCommandBuffer::item_selected(QListWidgetItem * item)
220 {
221         QWidget const * widget = static_cast<QWidget const *>(sender());
222         const_cast<QWidget *>(widget)->hide();
223         edit_->setText(item->text()+ ' ');
224         edit_->activateWindow();
225         edit_->setFocus();
226 }
227
228
229 void GuiCommandBuffer::up()
230 {
231         string const input = fromqstr(edit_->text());
232         string const h = historyUp();
233
234         if (!h.empty())
235                 edit_->setText(toqstr(h));
236
237         upPB->setEnabled(history_pos_ != history_.begin());
238         downPB->setEnabled(history_pos_ != history_.end());
239 }
240
241
242 void GuiCommandBuffer::down()
243 {
244         string const input = fromqstr(edit_->text());
245         string const h = historyDown();
246
247         if (!h.empty())
248                 edit_->setText(toqstr(h));
249
250         downPB->setEnabled(history_pos_ != history_.end()-1);
251         upPB->setEnabled(history_pos_ != history_.begin());
252 }
253         
254
255 void GuiCommandBuffer::hideParent()
256 {
257         view_->setFocus();
258         edit_->setText(QString());
259         edit_->clearFocus();
260         hide();
261 }
262
263
264 namespace {
265
266 class prefix_p {
267 public:
268         string p;
269         prefix_p(string const & s) : p(s) {}
270         bool operator()(string const & s) const { return prefixIs(s, p); }
271 };
272
273 } // end of anon namespace
274
275
276 string const GuiCommandBuffer::historyUp()
277 {
278         if (history_pos_ == history_.begin())
279                 return string();
280
281         return *(--history_pos_);
282 }
283
284
285 string const GuiCommandBuffer::historyDown()
286 {
287         if (history_pos_ == history_.end())
288                 return string();
289         if (history_pos_ + 1 == history_.end())
290                 return string();
291
292         return *(++history_pos_);
293 }
294
295
296 docstring const GuiCommandBuffer::getCurrentState() const
297 {
298         return view_->view()->cursor().currentState();
299 }
300
301
302 void GuiCommandBuffer::hide() const
303 {
304         FuncRequest cmd(LFUN_COMMAND_EXECUTE, "off");
305         theLyXFunc().setLyXView(view_);
306         lyx::dispatch(cmd);
307 }
308
309
310 vector<string> const
311 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
312 {
313         vector<string> comp;
314
315         copy_if(commands_.begin(), commands_.end(),
316                 back_inserter(comp), prefix_p(prefix));
317
318         if (comp.empty()) {
319                 new_prefix = prefix;
320                 return comp;
321         }
322
323         if (comp.size() == 1) {
324                 new_prefix = comp[0];
325                 return vector<string>();
326         }
327
328         // find maximal available prefix
329         string const tmp = comp[0];
330         string test = prefix;
331         if (tmp.length() > test.length())
332                 test += tmp[test.length()];
333         while (test.length() < tmp.length()) {
334                 vector<string> vtmp;
335                 copy_if(comp.begin(), comp.end(),
336                         back_inserter(vtmp), prefix_p(test));
337                 if (vtmp.size() != comp.size()) {
338                         test.erase(test.length() - 1);
339                         break;
340                 }
341                 test += tmp[test.length()];
342         }
343
344         new_prefix = test;
345         return comp;
346 }
347
348
349 void GuiCommandBuffer::dispatch(string const & str)
350 {
351         if (str.empty())
352                 return;
353
354         history_.push_back(trim(str));
355         history_pos_ = history_.end();
356         upPB->setEnabled(history_pos_ != history_.begin());
357         downPB->setEnabled(history_pos_ != history_.end());
358         FuncRequest func = lyxaction.lookupFunc(str);
359         func.origin = FuncRequest::COMMANDBUFFER;
360         theLyXFunc().setLyXView(view_);
361         lyx::dispatch(func);
362 }
363
364 } // namespace frontend
365 } // namespace lyx
366
367 #include "moc_GuiCommandBuffer.cpp"