]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiCommandBuffer.cpp
Remove a workaround that seems to be useless since Qt 4.8
[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/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() || comp.size() == 1) {
188                 if (new_input != input)
189                         edit_->setText(toqstr(new_input));
190                 // If there is only one match, indicate this by adding a space
191                 if (comp.size() == 1)
192                         edit_->setText(edit_->text() + " ");
193                 return;
194         }
195
196         edit_->setText(toqstr(new_input));
197         QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
198         showList(comp, pos);
199 }
200
201 void GuiCommandBuffer::showList(vector<string> const & list,
202         QPoint const & pos, bool reversed) const
203 {
204         QTempListBox * listBox = new QTempListBox;
205
206         // For some reason the scrollview's contents are larger
207         // than the number of actual items...
208         for (auto const & item : list) {
209                 if (reversed)
210                         listBox->insertItem(0, toqstr(item));
211                 else
212                         listBox->addItem(toqstr(item));
213         }
214         // Select the first item
215         listBox->setCurrentItem(listBox->item(0));
216
217         listBox->resize(listBox->sizeHint());
218
219         int const y = max(0, pos.y() - listBox->height());
220         listBox->move(pos.x(), y);
221
222         connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
223                 this, SLOT(itemSelected(QListWidgetItem *)));
224         connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
225                 this, SLOT(itemSelected(QListWidgetItem *)));
226
227         listBox->show();
228         listBox->setFocus();
229 }
230
231
232 void GuiCommandBuffer::itemSelected(QListWidgetItem * item)
233 {
234         QWidget const * widget = static_cast<QWidget const *>(sender());
235         const_cast<QWidget *>(widget)->hide();
236         edit_->setText(item->text()+ ' ');
237         edit_->activateWindow();
238         edit_->setFocus();
239 }
240
241
242 void GuiCommandBuffer::up()
243 {
244         string const h = historyUp();
245
246         if (!h.empty())
247                 edit_->setText(toqstr(h));
248
249         upPB->setEnabled(history_pos_ != history().begin());
250         downPB->setEnabled(history_pos_ != history().end());
251 }
252
253
254 void GuiCommandBuffer::down()
255 {
256         string const h = historyDown();
257
258         if (!h.empty())
259                 edit_->setText(toqstr(h));
260
261         downPB->setEnabled(!history().empty()
262                            && history_pos_ != history().end() - 1);
263         upPB->setEnabled(history_pos_ != history().begin());
264 }
265
266
267 void GuiCommandBuffer::hideParent()
268 {
269         view_->setFocus();
270         view_->resetCommandExecute();
271         edit_->setText(QString());
272         edit_->clearFocus();
273 }
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 vector<string> const
297 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
298 {
299         vector<string> comp;
300         for (auto const & act : lyxaction) {
301                 if (prefixIs(act.first, prefix))
302                         comp.push_back(act.first);
303         }
304         // now add all the other items that contain the prefix
305         for (auto const & act : lyxaction) {
306                 if (!prefixIs(act.first, prefix) && contains(act.first, prefix))
307                         comp.push_back(act.first);
308         }
309
310         if (comp.empty()) {
311                 new_prefix = prefix;
312                 return comp;
313         }
314
315         if (comp.size() == 1) {
316                 new_prefix = comp[0];
317                 return comp;
318         }
319
320         // find maximal available prefix
321         string const tmp = comp[0];
322         string test = prefix;
323         if (tmp.length() > test.length())
324                 test += tmp[test.length()];
325         while (test.length() < tmp.length()) {
326                 vector<string> vtmp;
327                 for (auto const & cmd : comp) {
328                         if (prefixIs(cmd, test))
329                                 vtmp.push_back(cmd);
330                 }
331                 if (vtmp.size() != comp.size()) {
332                         test.erase(test.length() - 1);
333                         break;
334                 }
335                 test += tmp[test.length()];
336         }
337
338         new_prefix = test;
339         return comp;
340 }
341
342
343 DispatchResult const & GuiCommandBuffer::dispatch(string const & str)
344 {
345         if (str.empty()) {
346                 static DispatchResult empty_dr;
347                 return empty_dr;
348         }
349
350         addHistory(trim(str));
351         history_pos_ = history().end();
352         upPB->setEnabled(history_pos_ != history().begin());
353         downPB->setEnabled(history_pos_ != history().end());
354         FuncRequest func = lyxaction.lookupFunc(str);
355         func.setOrigin(FuncRequest::COMMANDBUFFER);
356         return lyx::dispatch(func);
357 }
358
359 } // namespace frontend
360 } // namespace lyx
361
362 #include "moc_GuiCommandBuffer.cpp"