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 (me->x() < 0 || me->y() < 0
62 || me->x() > width() || me->y() > height())
66 return QListWidget::event(ev);
69 void keyPressEvent(QKeyEvent * ev) override {
70 if (ev->key() == Qt::Key_Escape) {
73 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
75 itemClicked(currentItem());
77 QListWidget::keyPressEvent(ev);
84 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
87 for (auto const & name_code : lyxaction) {
88 commands_.push_back(name_code.first);
91 QPixmap qpup = getPixmap("images/", "up", "svgz,png");
92 QPixmap qpdown = getPixmap("images/", "down", "svgz,png");
94 QVBoxLayout * top = new QVBoxLayout(this);
95 QHBoxLayout * layout = new QHBoxLayout(0);
97 edit_ = new GuiCommandEdit(this);
98 edit_->setMinimumSize(edit_->sizeHint());
99 edit_->setFocusPolicy(Qt::ClickFocus);
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);
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()));
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()));
123 layout->addWidget(upPB, 0);
124 layout->addWidget(downPB, 0);
125 layout->addWidget(edit_, 10);
126 layout->setMargin(0);
127 top->addLayout(layout);
129 setFocusProxy(edit_);
131 LastCommandsSection::LastCommands last_commands
132 = theSession().lastCommands().getcommands();
133 LastCommandsSection::LastCommands::const_iterator it
134 = last_commands.begin();
135 LastCommandsSection::LastCommands::const_iterator end
136 = last_commands.end();
138 upPB->setEnabled(it != end);
140 for(; it != end; ++it)
141 history_.push_back(*it);
142 history_pos_ = history_.end();
146 void GuiCommandBuffer::dispatch()
148 std::string const cmd = fromqstr(edit_->text());
150 theSession().lastCommands().add(cmd);
151 DispatchResult const & dr = dispatch(cmd);
154 edit_->setText(QString());
156 // If the toolbar was "auto", it is not needed anymore
157 view_->resetCommandExecute();
162 void GuiCommandBuffer::listHistoryUp()
164 if (history_.size()==1) {
165 edit_->setText(toqstr(history_.back()));
166 upPB->setEnabled(false);
169 QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
170 showList(history_, pos, true);
174 void GuiCommandBuffer::complete()
176 string const input = fromqstr(edit_->text());
178 vector<string> const & comp = completions(input, new_input);
181 if (new_input != input)
182 edit_->setText(toqstr(new_input));
186 edit_->setText(toqstr(new_input));
187 QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
191 void GuiCommandBuffer::showList(vector<string> const & list,
192 QPoint const & pos, bool reversed) const
194 QTempListBox * listBox = new QTempListBox;
196 // For some reason the scrollview's contents are larger
197 // than the number of actual items...
198 vector<string>::const_iterator cit = list.begin();
199 vector<string>::const_iterator end = list.end();
200 for (; cit != end; ++cit) {
202 listBox->insertItem(0, toqstr(*cit));
204 listBox->addItem(toqstr(*cit));
207 listBox->resize(listBox->sizeHint());
209 int const y = max(0, pos.y() - listBox->height());
210 listBox->move(pos.x(), y);
212 connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
213 this, SLOT(itemSelected(QListWidgetItem *)));
214 connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
215 this, SLOT(itemSelected(QListWidgetItem *)));
222 void GuiCommandBuffer::itemSelected(QListWidgetItem * item)
224 QWidget const * widget = static_cast<QWidget const *>(sender());
225 const_cast<QWidget *>(widget)->hide();
226 edit_->setText(item->text()+ ' ');
227 edit_->activateWindow();
232 void GuiCommandBuffer::up()
234 string const h = historyUp();
237 edit_->setText(toqstr(h));
239 upPB->setEnabled(history_pos_ != history_.begin());
240 downPB->setEnabled(history_pos_ != history_.end());
244 void GuiCommandBuffer::down()
246 string const h = historyDown();
249 edit_->setText(toqstr(h));
251 downPB->setEnabled(!history_.empty()
252 && history_pos_ != history_.end() - 1);
253 upPB->setEnabled(history_pos_ != history_.begin());
257 void GuiCommandBuffer::hideParent()
260 view_->resetCommandExecute();
261 edit_->setText(QString());
266 string const GuiCommandBuffer::historyUp()
268 if (history_pos_ == history_.begin())
271 return *(--history_pos_);
275 string const GuiCommandBuffer::historyDown()
277 if (history_pos_ == history_.end())
279 if (history_pos_ + 1 == history_.end())
282 return *(++history_pos_);
287 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
290 for (auto const & cmd : commands_) {
291 if (prefixIs(cmd, prefix))
300 if (comp.size() == 1) {
301 new_prefix = comp[0];
302 return vector<string>();
305 // find maximal available prefix
306 string const tmp = comp[0];
307 string test = prefix;
308 if (tmp.length() > test.length())
309 test += tmp[test.length()];
310 while (test.length() < tmp.length()) {
312 for (auto const & cmd : comp) {
313 if (prefixIs(cmd, test))
316 if (vtmp.size() != comp.size()) {
317 test.erase(test.length() - 1);
320 test += tmp[test.length()];
328 DispatchResult const & GuiCommandBuffer::dispatch(string const & str)
331 static DispatchResult empty_dr;
335 history_.push_back(trim(str));
336 history_pos_ = history_.end();
337 upPB->setEnabled(history_pos_ != history_.begin());
338 downPB->setEnabled(history_pos_ != history_.end());
339 FuncRequest func = lyxaction.lookupFunc(str);
340 func.setOrigin(FuncRequest::COMMANDBUFFER);
341 return lyx::dispatch(func);
344 } // namespace frontend
347 #include "moc_GuiCommandBuffer.cpp"