]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiCommandBuffer.cpp
Remove obsolete (and false) comment.
[lyx.git] / src / frontends / qt / 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 "LyX.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 } // namespace
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", "svgz,png");
92         QPixmap qpdown = getPixmap("images/", "down", "svgz,png");
93
94         QVBoxLayout * top = new QVBoxLayout(this);
95         QHBoxLayout * layout = new QHBoxLayout(0);
96
97         edit_ = new GuiCommandEdit(this);
98         edit_->setMinimumSize(edit_->sizeHint());
99         edit_->setFocusPolicy(Qt::ClickFocus);
100
101         int height = max(24, 2 * (edit_->sizeHint().height() / 2));
102         QSize button_size = QSize(height, height);
103         QSize icon_size = button_size - QSize(4, 4);
104
105         upPB = new QPushButton(qpup, "", this);
106         upPB->setToolTip(qt_("List of previous commands"));
107         upPB->setMaximumSize(button_size);
108         upPB->setIconSize(icon_size);
109         downPB = new QPushButton(qpdown, "", this);
110         downPB->setToolTip(qt_("Next command"));
111         downPB->setMaximumSize(button_size);
112         downPB->setIconSize(icon_size);
113         downPB->setEnabled(false);
114         connect(downPB, SIGNAL(clicked()), this, SLOT(down()));
115         connect(upPB, SIGNAL(pressed()), this, SLOT(listHistoryUp()));
116
117         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
118         connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
119         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
120         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
121         connect(edit_, SIGNAL(escapePressed()), this, SLOT(hideParent()));
122
123         layout->addWidget(upPB, 0);
124         layout->addWidget(downPB, 0);
125         layout->addWidget(edit_, 10);
126         layout->setMargin(0);
127         top->addLayout(layout);
128         top->setMargin(0);
129         setFocusProxy(edit_);
130
131         LastCommandsSection::LastCommands last_commands
132                 = theSession().lastCommands().getcommands();
133         LastCommandsSection::LastCommands::const_iterator it
134                 = last_commands.begin();
135         LastCommandsSection::LastCommands::const_iterator end
136                 = last_commands.end();
137
138         upPB->setEnabled(it != end);
139
140         for(; it != end; ++it)
141                 history_.push_back(*it);
142         history_pos_ = history_.end();
143 }
144
145
146 void GuiCommandBuffer::dispatch()
147 {
148         std::string const cmd = fromqstr(edit_->text());
149         if (!cmd.empty())
150                 theSession().lastCommands().add(cmd);
151         DispatchResult const & dr = dispatch(cmd);
152         if (!dr.error()) {
153                 view_->setFocus();
154                 edit_->setText(QString());
155                 edit_->clearFocus();
156                 // If the toolbar was "auto", it is not needed anymore
157                 view_->resetCommandExecute();
158         }
159 }
160
161
162 void GuiCommandBuffer::listHistoryUp()
163 {
164         if (history_.size()==1) {
165                 edit_->setText(toqstr(history_.back()));
166                 upPB->setEnabled(false);
167                 return;
168         }
169         QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
170         showList(history_, pos, true);
171 }
172
173
174 void GuiCommandBuffer::complete()
175 {
176         string const input = fromqstr(edit_->text());
177         string new_input;
178         vector<string> const & comp = completions(input, new_input);
179
180         if (comp.empty()) {
181                 if (new_input != input)
182                         edit_->setText(toqstr(new_input));
183                 return;
184         }
185
186         edit_->setText(toqstr(new_input));
187         QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
188         showList(comp, pos);
189 }
190
191 void GuiCommandBuffer::showList(vector<string> const & list,
192         QPoint const & pos, bool reversed) const
193 {
194         QTempListBox * listBox = new QTempListBox;
195
196         // For some reason the scrollview's contents are larger
197         // than the number of actual items...
198         vector<string>::const_iterator cit = list.begin();
199         vector<string>::const_iterator end = list.end();
200         for (; cit != end; ++cit) {
201                 if (reversed)
202                         listBox->insertItem(0, toqstr(*cit));
203                 else
204                         listBox->addItem(toqstr(*cit));
205         }
206
207         listBox->resize(listBox->sizeHint());
208
209         int const y = max(0, pos.y() - listBox->height());
210         listBox->move(pos.x(), y);
211
212         connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
213                 this, SLOT(itemSelected(QListWidgetItem *)));
214         connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
215                 this, SLOT(itemSelected(QListWidgetItem *)));
216
217         listBox->show();
218         listBox->setFocus();
219 }
220
221
222 void GuiCommandBuffer::itemSelected(QListWidgetItem * item)
223 {
224         QWidget const * widget = static_cast<QWidget const *>(sender());
225         const_cast<QWidget *>(widget)->hide();
226         edit_->setText(item->text()+ ' ');
227         edit_->activateWindow();
228         edit_->setFocus();
229 }
230
231
232 void GuiCommandBuffer::up()
233 {
234         string const h = historyUp();
235
236         if (!h.empty())
237                 edit_->setText(toqstr(h));
238
239         upPB->setEnabled(history_pos_ != history_.begin());
240         downPB->setEnabled(history_pos_ != history_.end());
241 }
242
243
244 void GuiCommandBuffer::down()
245 {
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         view_->resetCommandExecute();
261         edit_->setText(QString());
262         edit_->clearFocus();
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 } // 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 vector<string> const
299 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
300 {
301         vector<string> comp;
302
303         lyx::copy_if(commands_.begin(), commands_.end(),
304                 back_inserter(comp), prefix_p(prefix));
305
306         if (comp.empty()) {
307                 new_prefix = prefix;
308                 return comp;
309         }
310
311         if (comp.size() == 1) {
312                 new_prefix = comp[0];
313                 return vector<string>();
314         }
315
316         // find maximal available prefix
317         string const tmp = comp[0];
318         string test = prefix;
319         if (tmp.length() > test.length())
320                 test += tmp[test.length()];
321         while (test.length() < tmp.length()) {
322                 vector<string> vtmp;
323                 lyx::copy_if(comp.begin(), comp.end(),
324                         back_inserter(vtmp), prefix_p(test));
325                 if (vtmp.size() != comp.size()) {
326                         test.erase(test.length() - 1);
327                         break;
328                 }
329                 test += tmp[test.length()];
330         }
331
332         new_prefix = test;
333         return comp;
334 }
335
336
337 DispatchResult const & GuiCommandBuffer::dispatch(string const & str)
338 {
339         if (str.empty()) {
340                 static DispatchResult empty_dr;
341                 return empty_dr;
342         }
343
344         history_.push_back(trim(str));
345         history_pos_ = history_.end();
346         upPB->setEnabled(history_pos_ != history_.begin());
347         downPB->setEnabled(history_pos_ != history_.end());
348         FuncRequest func = lyxaction.lookupFunc(str);
349         func.setOrigin(FuncRequest::COMMANDBUFFER);
350         return lyx::dispatch(func);
351 }
352
353 } // namespace frontend
354 } // namespace lyx
355
356 #include "moc_GuiCommandBuffer.cpp"