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 #if QT_VERSION < 0x060000
137 layout->setMargin(0);
139 layout->setContentsMargins(0, 0, 0, 0);
141 top->addLayout(layout);
142 #if QT_VERSION < 0x060000
145 top->setContentsMargins(0, 0, 0, 0);
147 setFocusProxy(edit_);
149 upPB->setEnabled(!history().empty());
151 history_pos_ = history().end();
155 void GuiCommandBuffer::dispatch()
157 std::string const cmd = fromqstr(edit_->text());
158 DispatchResult const & dr = dispatch(cmd);
161 edit_->setText(QString());
163 // If the toolbar was "auto", it is not needed anymore
164 view_->resetCommandExecute();
169 void GuiCommandBuffer::listHistoryUp()
171 if (history().size() == 1) {
172 edit_->setText(toqstr(history().back()));
173 upPB->setEnabled(false);
176 QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
177 showList(history(), pos, true);
181 void GuiCommandBuffer::complete()
183 string const input = fromqstr(edit_->text());
185 vector<string> const & comp = completions(input, new_input);
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() + " ");
196 edit_->setText(toqstr(new_input));
197 QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
201 void GuiCommandBuffer::showList(vector<string> const & list,
202 QPoint const & pos, bool reversed) const
204 QTempListBox * listBox = new QTempListBox;
206 // For some reason the scrollview's contents are larger
207 // than the number of actual items...
208 for (auto const & item : list) {
210 listBox->insertItem(0, toqstr(item));
212 listBox->addItem(toqstr(item));
214 // Select the first item
215 listBox->setCurrentItem(listBox->item(0));
217 listBox->resize(listBox->sizeHint());
219 int const y = max(0, pos.y() - listBox->height());
220 listBox->move(pos.x(), y);
222 connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
223 this, SLOT(itemSelected(QListWidgetItem *)));
224 connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
225 this, SLOT(itemSelected(QListWidgetItem *)));
232 void GuiCommandBuffer::itemSelected(QListWidgetItem * item)
234 QWidget const * widget = static_cast<QWidget const *>(sender());
235 const_cast<QWidget *>(widget)->hide();
236 edit_->setText(item->text()+ ' ');
237 edit_->activateWindow();
242 void GuiCommandBuffer::up()
244 string const h = historyUp();
247 edit_->setText(toqstr(h));
249 upPB->setEnabled(history_pos_ != history().begin());
250 downPB->setEnabled(history_pos_ != history().end());
254 void GuiCommandBuffer::down()
256 string const h = historyDown();
259 edit_->setText(toqstr(h));
261 downPB->setEnabled(!history().empty()
262 && history_pos_ != history().end() - 1);
263 upPB->setEnabled(history_pos_ != history().begin());
267 void GuiCommandBuffer::hideParent()
270 view_->resetCommandExecute();
271 edit_->setText(QString());
276 string const GuiCommandBuffer::historyUp()
278 if (history_pos_ == history().begin())
281 return *(--history_pos_);
285 string const GuiCommandBuffer::historyDown()
287 if (history_pos_ == history().end())
289 if (history_pos_ + 1 == history().end())
292 return *(++history_pos_);
297 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
300 for (auto const & act : lyxaction) {
301 if (prefixIs(act.first, prefix))
302 comp.push_back(act.first);
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);
315 if (comp.size() == 1) {
316 new_prefix = comp[0];
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()) {
327 for (auto const & cmd : comp) {
328 if (prefixIs(cmd, test))
331 if (vtmp.size() != comp.size()) {
332 test.erase(test.length() - 1);
335 test += tmp[test.length()];
343 DispatchResult const & GuiCommandBuffer::dispatch(string const & str)
346 static DispatchResult empty_dr;
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);
359 } // namespace frontend
362 #include "moc_GuiCommandBuffer.cpp"