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