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 class QTempListBox : public QListWidget {
51 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
52 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
53 setWindowModality(Qt::WindowModal);
54 setWindowFlags(Qt::Popup);
55 setAttribute(Qt::WA_DeleteOnClose);
58 bool event(QEvent * ev) override {
59 if (ev->type() == QEvent::MouseButtonPress) {
60 QMouseEvent * me = static_cast<QMouseEvent *>(ev);
61 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
62 if (me->position().x() < 0 || me->position().y() < 0
63 || me->position().x() > width() || me->position().y() > height())
65 if (me->x() < 0 || me->y() < 0
66 || me->x() > width() || me->y() > height())
71 return QListWidget::event(ev);
74 void keyPressEvent(QKeyEvent * ev) override {
75 if (ev->key() == Qt::Key_Escape) {
78 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
80 itemClicked(currentItem());
82 QListWidget::keyPressEvent(ev);
89 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
92 for (auto const & name_code : lyxaction) {
93 commands_.push_back(name_code.first);
96 QPixmap qpup = getPixmap("images/", "up", "svgz,png");
97 QPixmap qpdown = getPixmap("images/", "down", "svgz,png");
99 QVBoxLayout * top = new QVBoxLayout(this);
100 QHBoxLayout * layout = new QHBoxLayout(0);
102 edit_ = new GuiCommandEdit(this);
103 edit_->setMinimumSize(edit_->sizeHint());
104 edit_->setFocusPolicy(Qt::ClickFocus);
106 int height = max(24, 2 * (edit_->sizeHint().height() / 2));
107 QSize button_size = QSize(height, height);
108 QSize icon_size = button_size - QSize(4, 4);
110 upPB = new QPushButton(qpup, "", this);
111 upPB->setToolTip(qt_("List of previous commands"));
112 upPB->setMaximumSize(button_size);
113 upPB->setIconSize(icon_size);
114 downPB = new QPushButton(qpdown, "", this);
115 downPB->setToolTip(qt_("Next command"));
116 downPB->setMaximumSize(button_size);
117 downPB->setIconSize(icon_size);
118 downPB->setEnabled(false);
119 connect(downPB, SIGNAL(clicked()), this, SLOT(down()));
120 connect(upPB, SIGNAL(pressed()), this, SLOT(listHistoryUp()));
122 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
123 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
124 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
125 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
126 connect(edit_, SIGNAL(escapePressed()), this, SLOT(hideParent()));
128 layout->addWidget(upPB, 0);
129 layout->addWidget(downPB, 0);
130 layout->addWidget(edit_, 10);
131 #if QT_VERSION < 0x060000
132 layout->setMargin(0);
134 layout->setContentsMargins(0, 0, 0, 0);
136 top->addLayout(layout);
137 #if QT_VERSION < 0x060000
140 top->setContentsMargins(0, 0, 0, 0);
142 setFocusProxy(edit_);
144 LastCommandsSection::LastCommands last_commands
145 = theSession().lastCommands().getcommands();
146 LastCommandsSection::LastCommands::const_iterator it
147 = last_commands.begin();
148 LastCommandsSection::LastCommands::const_iterator end
149 = last_commands.end();
151 upPB->setEnabled(it != end);
153 for(; it != end; ++it)
154 history_.push_back(*it);
155 history_pos_ = history_.end();
159 void GuiCommandBuffer::dispatch()
161 std::string const cmd = fromqstr(edit_->text());
163 theSession().lastCommands().add(cmd);
164 DispatchResult const & dr = dispatch(cmd);
167 edit_->setText(QString());
169 // If the toolbar was "auto", it is not needed anymore
170 view_->resetCommandExecute();
175 void GuiCommandBuffer::listHistoryUp()
177 if (history_.size()==1) {
178 edit_->setText(toqstr(history_.back()));
179 upPB->setEnabled(false);
182 QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
183 showList(history_, pos, true);
187 void GuiCommandBuffer::complete()
189 string const input = fromqstr(edit_->text());
191 vector<string> const & comp = completions(input, new_input);
194 if (new_input != input)
195 edit_->setText(toqstr(new_input));
199 edit_->setText(toqstr(new_input));
200 QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
204 void GuiCommandBuffer::showList(vector<string> const & list,
205 QPoint const & pos, bool reversed) const
207 QTempListBox * listBox = new QTempListBox;
209 // For some reason the scrollview's contents are larger
210 // than the number of actual items...
211 vector<string>::const_iterator cit = list.begin();
212 vector<string>::const_iterator end = list.end();
213 for (; cit != end; ++cit) {
215 listBox->insertItem(0, toqstr(*cit));
217 listBox->addItem(toqstr(*cit));
220 listBox->resize(listBox->sizeHint());
222 int const y = max(0, pos.y() - listBox->height());
223 listBox->move(pos.x(), y);
225 connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
226 this, SLOT(itemSelected(QListWidgetItem *)));
227 connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
228 this, SLOT(itemSelected(QListWidgetItem *)));
235 void GuiCommandBuffer::itemSelected(QListWidgetItem * item)
237 QWidget const * widget = static_cast<QWidget const *>(sender());
238 const_cast<QWidget *>(widget)->hide();
239 edit_->setText(item->text()+ ' ');
240 edit_->activateWindow();
245 void GuiCommandBuffer::up()
247 string const h = historyUp();
250 edit_->setText(toqstr(h));
252 upPB->setEnabled(history_pos_ != history_.begin());
253 downPB->setEnabled(history_pos_ != history_.end());
257 void GuiCommandBuffer::down()
259 string const h = historyDown();
262 edit_->setText(toqstr(h));
264 downPB->setEnabled(!history_.empty()
265 && history_pos_ != history_.end() - 1);
266 upPB->setEnabled(history_pos_ != history_.begin());
270 void GuiCommandBuffer::hideParent()
273 view_->resetCommandExecute();
274 edit_->setText(QString());
279 string const GuiCommandBuffer::historyUp()
281 if (history_pos_ == history_.begin())
284 return *(--history_pos_);
288 string const GuiCommandBuffer::historyDown()
290 if (history_pos_ == history_.end())
292 if (history_pos_ + 1 == history_.end())
295 return *(++history_pos_);
300 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
303 for (auto const & cmd : commands_) {
304 if (prefixIs(cmd, prefix))
313 if (comp.size() == 1) {
314 new_prefix = comp[0];
315 return vector<string>();
318 // find maximal available prefix
319 string const tmp = comp[0];
320 string test = prefix;
321 if (tmp.length() > test.length())
322 test += tmp[test.length()];
323 while (test.length() < tmp.length()) {
325 for (auto const & cmd : comp) {
326 if (prefixIs(cmd, test))
329 if (vtmp.size() != comp.size()) {
330 test.erase(test.length() - 1);
333 test += tmp[test.length()];
341 DispatchResult const & GuiCommandBuffer::dispatch(string const & str)
344 static DispatchResult empty_dr;
348 history_.push_back(trim(str));
349 history_pos_ = history_.end();
350 upPB->setEnabled(history_pos_ != history_.begin());
351 downPB->setEnabled(history_pos_ != history_.end());
352 FuncRequest func = lyxaction.lookupFunc(str);
353 func.setOrigin(FuncRequest::COMMANDBUFFER);
354 return lyx::dispatch(func);
357 } // namespace frontend
360 #include "moc_GuiCommandBuffer.cpp"