]> git.lyx.org Git - features.git/blob - src/frontends/qt/GuiCommandBuffer.cpp
4d28bfb0c29e73db1b6e69c70921368c7bea85f3
[features.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/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 // Simple wrapper around the LastCommand session stuff.
49
50 // The whole last commands list
51 vector<string> const & history() { return theSession().lastCommands().getcommands(); }
52
53 // add command to the list (and remove duplicates)
54 void addHistory(string const & cmd) { theSession().lastCommands().add(cmd); }
55
56
57 class QTempListBox : public QListWidget {
58 public:
59         QTempListBox() {
60                 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
61                 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
62                 setWindowModality(Qt::WindowModal);
63                 setWindowFlags(Qt::Popup);
64                 setAttribute(Qt::WA_DeleteOnClose);
65         }
66 protected:
67         bool event(QEvent * ev) override {
68                 if (ev->type() == QEvent::MouseButtonPress) {
69                         QMouseEvent * me = static_cast<QMouseEvent *>(ev);
70 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
71                         if (me->position().x() < 0 || me->position().y() < 0
72                             || me->position().x() > width() || me->position().y() > height())
73 #else
74                         if (me->x() < 0 || me->y() < 0
75                             || me->x() > width() || me->y() > height())
76 #endif
77                                 hide();
78                         return true;
79                 }
80                 return QListWidget::event(ev);
81         }
82
83         void keyPressEvent(QKeyEvent * ev) override {
84                 if (ev->key() == Qt::Key_Escape) {
85                         hide();
86                         return;
87                 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
88                         // emit signal
89                         itemClicked(currentItem());
90                 } else
91                         QListWidget::keyPressEvent(ev);
92         }
93 };
94
95 } // namespace
96
97
98 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
99         : view_(view)
100 {
101         QPixmap qpup = getPixmap("images/", "up", "svgz,png");
102         QPixmap qpdown = getPixmap("images/", "down", "svgz,png");
103
104         QVBoxLayout * top = new QVBoxLayout(this);
105         QHBoxLayout * layout = new QHBoxLayout(0);
106
107         edit_ = new GuiCommandEdit(this);
108         edit_->setMinimumSize(edit_->sizeHint());
109         edit_->setFocusPolicy(Qt::ClickFocus);
110
111         int height = max(24, 2 * (edit_->sizeHint().height() / 2));
112         QSize button_size = QSize(height, height);
113         QSize icon_size = button_size - QSize(4, 4);
114
115         upPB = new QPushButton(qpup, "", this);
116         upPB->setToolTip(qt_("List of previous commands"));
117         upPB->setMaximumSize(button_size);
118         upPB->setIconSize(icon_size);
119         downPB = new QPushButton(qpdown, "", this);
120         downPB->setToolTip(qt_("Next command"));
121         downPB->setMaximumSize(button_size);
122         downPB->setIconSize(icon_size);
123         downPB->setEnabled(false);
124         connect(downPB, SIGNAL(clicked()), this, SLOT(down()));
125         connect(upPB, SIGNAL(pressed()), this, SLOT(listHistoryUp()));
126
127         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
128         connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
129         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
130         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
131         connect(edit_, SIGNAL(escapePressed()), this, SLOT(hideParent()));
132
133         layout->addWidget(upPB, 0);
134         layout->addWidget(downPB, 0);
135         layout->addWidget(edit_, 10);
136 #if QT_VERSION < 0x060000
137         layout->setMargin(0);
138 #else
139         layout->setContentsMargins(0, 0, 0, 0);
140 #endif
141         top->addLayout(layout);
142 #if QT_VERSION < 0x060000
143         top->setMargin(0);
144 #else
145         top->setContentsMargins(0, 0, 0, 0);
146 #endif
147         setFocusProxy(edit_);
148
149         upPB->setEnabled(!history().empty());
150
151         history_pos_ = history().end();
152 }
153
154
155 void GuiCommandBuffer::dispatch()
156 {
157         std::string const cmd = fromqstr(edit_->text());
158         DispatchResult const & dr = dispatch(cmd);
159         if (!dr.error()) {
160                 view_->setFocus();
161                 edit_->setText(QString());
162                 edit_->clearFocus();
163                 // If the toolbar was "auto", it is not needed anymore
164                 view_->resetCommandExecute();
165         }
166 }
167
168
169 void GuiCommandBuffer::listHistoryUp()
170 {
171         if (history().size() == 1) {
172                 edit_->setText(toqstr(history().back()));
173                 upPB->setEnabled(false);
174                 return;
175         }
176         QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
177         showList(history(), pos, true);
178 }
179
180
181 void GuiCommandBuffer::complete()
182 {
183         string const input = fromqstr(edit_->text());
184         string new_input;
185         vector<string> const & comp = completions(input, new_input);
186
187         if (comp.empty()) {
188                 if (new_input != input)
189                         edit_->setText(toqstr(new_input));
190                 return;
191         }
192
193         edit_->setText(toqstr(new_input));
194         QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
195         showList(comp, pos);
196 }
197
198 void GuiCommandBuffer::showList(vector<string> const & list,
199         QPoint const & pos, bool reversed) const
200 {
201         QTempListBox * listBox = new QTempListBox;
202
203         // For some reason the scrollview's contents are larger
204         // than the number of actual items...
205         for (auto const & item : list) {
206                 if (reversed)
207                         listBox->insertItem(0, toqstr(item));
208                 else
209                         listBox->addItem(toqstr(item));
210         }
211
212         listBox->resize(listBox->sizeHint());
213
214         int const y = max(0, pos.y() - listBox->height());
215         listBox->move(pos.x(), y);
216
217         connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
218                 this, SLOT(itemSelected(QListWidgetItem *)));
219         connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
220                 this, SLOT(itemSelected(QListWidgetItem *)));
221
222         listBox->show();
223         listBox->setFocus();
224 }
225
226
227 void GuiCommandBuffer::itemSelected(QListWidgetItem * item)
228 {
229         QWidget const * widget = static_cast<QWidget const *>(sender());
230         const_cast<QWidget *>(widget)->hide();
231         edit_->setText(item->text()+ ' ');
232         edit_->activateWindow();
233         edit_->setFocus();
234 }
235
236
237 void GuiCommandBuffer::up()
238 {
239         string const h = historyUp();
240
241         if (!h.empty())
242                 edit_->setText(toqstr(h));
243
244         upPB->setEnabled(history_pos_ != history().begin());
245         downPB->setEnabled(history_pos_ != history().end());
246 }
247
248
249 void GuiCommandBuffer::down()
250 {
251         string const h = historyDown();
252
253         if (!h.empty())
254                 edit_->setText(toqstr(h));
255
256         downPB->setEnabled(!history().empty()
257                            && history_pos_ != history().end() - 1);
258         upPB->setEnabled(history_pos_ != history().begin());
259 }
260
261
262 void GuiCommandBuffer::hideParent()
263 {
264         view_->setFocus();
265         view_->resetCommandExecute();
266         edit_->setText(QString());
267         edit_->clearFocus();
268 }
269
270
271 string const GuiCommandBuffer::historyUp()
272 {
273         if (history_pos_ == history().begin())
274                 return string();
275
276         return *(--history_pos_);
277 }
278
279
280 string const GuiCommandBuffer::historyDown()
281 {
282         if (history_pos_ == history().end())
283                 return string();
284         if (history_pos_ + 1 == history().end())
285                 return string();
286
287         return *(++history_pos_);
288 }
289
290
291 vector<string> const
292 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
293 {
294         vector<string> comp;
295         for (auto const & act : lyxaction) {
296                 if (prefixIs(act.first, prefix))
297                         comp.push_back(act.first);
298         }
299
300         if (comp.empty()) {
301                 new_prefix = prefix;
302                 return comp;
303         }
304
305         if (comp.size() == 1) {
306                 new_prefix = comp[0];
307                 return vector<string>();
308         }
309
310         // find maximal available prefix
311         string const tmp = comp[0];
312         string test = prefix;
313         if (tmp.length() > test.length())
314                 test += tmp[test.length()];
315         while (test.length() < tmp.length()) {
316                 vector<string> vtmp;
317                 for (auto const & cmd : comp) {
318                         if (prefixIs(cmd, test))
319                                 vtmp.push_back(cmd);
320                 }
321                 if (vtmp.size() != comp.size()) {
322                         test.erase(test.length() - 1);
323                         break;
324                 }
325                 test += tmp[test.length()];
326         }
327
328         new_prefix = test;
329         return comp;
330 }
331
332
333 DispatchResult const & GuiCommandBuffer::dispatch(string const & str)
334 {
335         if (str.empty()) {
336                 static DispatchResult empty_dr;
337                 return empty_dr;
338         }
339
340         addHistory(trim(str));
341         history_pos_ = history().end();
342         upPB->setEnabled(history_pos_ != history().begin());
343         downPB->setEnabled(history_pos_ != history().end());
344         FuncRequest func = lyxaction.lookupFunc(str);
345         func.setOrigin(FuncRequest::COMMANDBUFFER);
346         return lyx::dispatch(func);
347 }
348
349 } // namespace frontend
350 } // namespace lyx
351
352 #include "moc_GuiCommandBuffer.cpp"