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