]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiCommandBuffer.cpp
Allow compiling with Qt6
[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 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) override {
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) override {
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 } // namespace
82
83
84 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
85         : view_(view)
86 {
87         for (auto const & name_code : lyxaction) {
88                 commands_.push_back(name_code.first);
89         }
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 #if QT_VERSION < 0x060000
127         layout->setMargin(0);
128 #else
129         layout->setContentsMargins(0, 0, 0, 0);
130 #endif
131         top->addLayout(layout);
132 #if QT_VERSION < 0x060000
133         top->setMargin(0);
134 #else
135         top->setContentsMargins(0, 0, 0, 0);
136 #endif
137         setFocusProxy(edit_);
138
139         LastCommandsSection::LastCommands last_commands
140                 = theSession().lastCommands().getcommands();
141         LastCommandsSection::LastCommands::const_iterator it
142                 = last_commands.begin();
143         LastCommandsSection::LastCommands::const_iterator end
144                 = last_commands.end();
145
146         upPB->setEnabled(it != end);
147
148         for(; it != end; ++it)
149                 history_.push_back(*it);
150         history_pos_ = history_.end();
151 }
152
153
154 void GuiCommandBuffer::dispatch()
155 {
156         std::string const cmd = fromqstr(edit_->text());
157         if (!cmd.empty())
158                 theSession().lastCommands().add(cmd);
159         DispatchResult const & dr = dispatch(cmd);
160         if (!dr.error()) {
161                 view_->setFocus();
162                 edit_->setText(QString());
163                 edit_->clearFocus();
164                 // If the toolbar was "auto", it is not needed anymore
165                 view_->resetCommandExecute();
166         }
167 }
168
169
170 void GuiCommandBuffer::listHistoryUp()
171 {
172         if (history_.size()==1) {
173                 edit_->setText(toqstr(history_.back()));
174                 upPB->setEnabled(false);
175                 return;
176         }
177         QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
178         showList(history_, pos, true);
179 }
180
181
182 void GuiCommandBuffer::complete()
183 {
184         string const input = fromqstr(edit_->text());
185         string new_input;
186         vector<string> const & comp = completions(input, new_input);
187
188         if (comp.empty()) {
189                 if (new_input != input)
190                         edit_->setText(toqstr(new_input));
191                 return;
192         }
193
194         edit_->setText(toqstr(new_input));
195         QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
196         showList(comp, pos);
197 }
198
199 void GuiCommandBuffer::showList(vector<string> const & list,
200         QPoint const & pos, bool reversed) const
201 {
202         QTempListBox * listBox = new QTempListBox;
203
204         // For some reason the scrollview's contents are larger
205         // than the number of actual items...
206         vector<string>::const_iterator cit = list.begin();
207         vector<string>::const_iterator end = list.end();
208         for (; cit != end; ++cit) {
209                 if (reversed)
210                         listBox->insertItem(0, toqstr(*cit));
211                 else
212                         listBox->addItem(toqstr(*cit));
213         }
214
215         listBox->resize(listBox->sizeHint());
216
217         int const y = max(0, pos.y() - listBox->height());
218         listBox->move(pos.x(), y);
219
220         connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
221                 this, SLOT(itemSelected(QListWidgetItem *)));
222         connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
223                 this, SLOT(itemSelected(QListWidgetItem *)));
224
225         listBox->show();
226         listBox->setFocus();
227 }
228
229
230 void GuiCommandBuffer::itemSelected(QListWidgetItem * item)
231 {
232         QWidget const * widget = static_cast<QWidget const *>(sender());
233         const_cast<QWidget *>(widget)->hide();
234         edit_->setText(item->text()+ ' ');
235         edit_->activateWindow();
236         edit_->setFocus();
237 }
238
239
240 void GuiCommandBuffer::up()
241 {
242         string const h = historyUp();
243
244         if (!h.empty())
245                 edit_->setText(toqstr(h));
246
247         upPB->setEnabled(history_pos_ != history_.begin());
248         downPB->setEnabled(history_pos_ != history_.end());
249 }
250
251
252 void GuiCommandBuffer::down()
253 {
254         string const h = historyDown();
255
256         if (!h.empty())
257                 edit_->setText(toqstr(h));
258
259         downPB->setEnabled(!history_.empty()
260                            && history_pos_ != history_.end() - 1);
261         upPB->setEnabled(history_pos_ != history_.begin());
262 }
263
264
265 void GuiCommandBuffer::hideParent()
266 {
267         view_->setFocus();
268         view_->resetCommandExecute();
269         edit_->setText(QString());
270         edit_->clearFocus();
271 }
272
273
274 string const GuiCommandBuffer::historyUp()
275 {
276         if (history_pos_ == history_.begin())
277                 return string();
278
279         return *(--history_pos_);
280 }
281
282
283 string const GuiCommandBuffer::historyDown()
284 {
285         if (history_pos_ == history_.end())
286                 return string();
287         if (history_pos_ + 1 == history_.end())
288                 return string();
289
290         return *(++history_pos_);
291 }
292
293
294 vector<string> const
295 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
296 {
297         vector<string> comp;
298         for (auto const & cmd : commands_) {
299                 if (prefixIs(cmd, prefix))
300                         comp.push_back(cmd);
301         }
302
303         if (comp.empty()) {
304                 new_prefix = prefix;
305                 return comp;
306         }
307
308         if (comp.size() == 1) {
309                 new_prefix = comp[0];
310                 return vector<string>();
311         }
312
313         // find maximal available prefix
314         string const tmp = comp[0];
315         string test = prefix;
316         if (tmp.length() > test.length())
317                 test += tmp[test.length()];
318         while (test.length() < tmp.length()) {
319                 vector<string> vtmp;
320                 for (auto const & cmd : comp) {
321                         if (prefixIs(cmd, test))
322                                 vtmp.push_back(cmd);
323                 }
324                 if (vtmp.size() != comp.size()) {
325                         test.erase(test.length() - 1);
326                         break;
327                 }
328                 test += tmp[test.length()];
329         }
330
331         new_prefix = test;
332         return comp;
333 }
334
335
336 DispatchResult const & GuiCommandBuffer::dispatch(string const & str)
337 {
338         if (str.empty()) {
339                 static DispatchResult empty_dr;
340                 return empty_dr;
341         }
342
343         history_.push_back(trim(str));
344         history_pos_ = history_.end();
345         upPB->setEnabled(history_pos_ != history_.begin());
346         downPB->setEnabled(history_pos_ != history_.end());
347         FuncRequest func = lyxaction.lookupFunc(str);
348         func.setOrigin(FuncRequest::COMMANDBUFFER);
349         return lyx::dispatch(func);
350 }
351
352 } // namespace frontend
353 } // namespace lyx
354
355 #include "moc_GuiCommandBuffer.cpp"