2 * \file GuiCommandBuffer.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Asger and Jürgen
10 * Full author contact details are available in file CREDITS.
15 #include "GuiCommandBuffer.h"
17 #include "GuiApplication.h"
18 #include "GuiCommandEdit.h"
20 #include "qt_helpers.h"
22 #include "BufferView.h"
25 #include "LyXAction.h"
26 #include "FuncRequest.h"
29 #include "support/lstrings.h"
31 #include <QHBoxLayout>
33 #include <QListWidget>
34 #include <QMouseEvent>
36 #include <QPushButton>
38 #include <QVBoxLayout>
41 using namespace lyx::support;
48 // Simple wrapper around the LastCommand session stuff.
50 // The whole last commands list
51 vector<string> const & history() { return theSession().lastCommands().getcommands(); }
53 // add command to the list (and remove duplicates)
54 void addHistory(string const & cmd) { theSession().lastCommands().add(cmd); }
57 class QTempListBox : public QListWidget {
60 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
61 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
62 setWindowModality(Qt::WindowModal);
63 setWindowFlags(Qt::Popup);
64 setAttribute(Qt::WA_DeleteOnClose);
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())
74 if (me->x() < 0 || me->y() < 0
75 || me->x() > width() || me->y() > height())
80 return QListWidget::event(ev);
83 void keyPressEvent(QKeyEvent * ev) override {
84 if (ev->key() == Qt::Key_Escape) {
87 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
89 itemClicked(currentItem());
91 QListWidget::keyPressEvent(ev);
98 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
101 QPixmap qpup = getPixmap("images/", "up", "svgz,png");
102 QPixmap qpdown = getPixmap("images/", "down", "svgz,png");
104 QVBoxLayout * top = new QVBoxLayout(this);
105 QHBoxLayout * layout = new QHBoxLayout(0);
107 edit_ = new GuiCommandEdit(this);
108 edit_->setMinimumSize(edit_->sizeHint());
109 edit_->setFocusPolicy(Qt::ClickFocus);
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);
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()));
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()));
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_);
141 upPB->setEnabled(!history().empty());
143 history_pos_ = history().end();
147 void GuiCommandBuffer::dispatch()
149 std::string const cmd = fromqstr(edit_->text());
150 DispatchResult const & dr = dispatch(cmd);
153 edit_->setText(QString());
155 // If the toolbar was "auto", it is not needed anymore
156 view_->resetCommandExecute();
161 void GuiCommandBuffer::listHistoryUp()
163 if (history().size() == 1) {
164 edit_->setText(toqstr(history().back()));
165 upPB->setEnabled(false);
168 QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
169 showList(history(), pos, true);
173 void GuiCommandBuffer::complete()
175 string const input = fromqstr(edit_->text());
177 vector<string> const & comp = completions(input, new_input);
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() + " ");
188 edit_->setText(toqstr(new_input));
189 QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
193 void GuiCommandBuffer::showList(vector<string> const & list,
194 QPoint const & pos, bool reversed) const
196 QTempListBox * listBox = new QTempListBox;
198 // For some reason the scrollview's contents are larger
199 // than the number of actual items...
200 for (auto const & item : list) {
202 listBox->insertItem(0, toqstr(item));
204 listBox->addItem(toqstr(item));
206 // Select the first item
207 listBox->setCurrentItem(listBox->item(0));
209 listBox->resize(listBox->sizeHint());
211 int const y = max(0, pos.y() - listBox->height());
212 listBox->move(pos.x(), y);
214 connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
215 this, SLOT(itemSelected(QListWidgetItem *)));
216 connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
217 this, SLOT(itemSelected(QListWidgetItem *)));
224 void GuiCommandBuffer::itemSelected(QListWidgetItem * item)
226 QWidget const * widget = static_cast<QWidget const *>(sender());
227 const_cast<QWidget *>(widget)->hide();
228 edit_->setText(item->text()+ ' ');
229 edit_->activateWindow();
234 void GuiCommandBuffer::up()
236 string const h = historyUp();
239 edit_->setText(toqstr(h));
241 upPB->setEnabled(history_pos_ != history().begin());
242 downPB->setEnabled(history_pos_ != history().end());
246 void GuiCommandBuffer::down()
248 string const h = historyDown();
251 edit_->setText(toqstr(h));
253 downPB->setEnabled(!history().empty()
254 && history_pos_ != history().end() - 1);
255 upPB->setEnabled(history_pos_ != history().begin());
259 void GuiCommandBuffer::hideParent()
262 view_->resetCommandExecute();
263 edit_->setText(QString());
268 string const GuiCommandBuffer::historyUp()
270 if (history_pos_ == history().begin())
273 return *(--history_pos_);
277 string const GuiCommandBuffer::historyDown()
279 if (history_pos_ == history().end())
281 if (history_pos_ + 1 == history().end())
284 return *(++history_pos_);
289 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
292 for (auto const & act : lyxaction) {
293 if (prefixIs(act.first, prefix))
294 comp.push_back(act.first);
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);
307 if (comp.size() == 1) {
308 new_prefix = comp[0];
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()) {
319 for (auto const & cmd : comp) {
320 if (prefixIs(cmd, test))
323 if (vtmp.size() != comp.size()) {
324 test.erase(test.length() - 1);
327 test += tmp[test.length()];
335 DispatchResult const & GuiCommandBuffer::dispatch(string const & str)
338 static DispatchResult empty_dr;
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);
351 } // namespace frontend
354 #include "moc_GuiCommandBuffer.cpp"