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 "GuiCommandEdit.h"
19 #include "qt_helpers.h"
21 #include "BufferView.h"
24 #include "LyXAction.h"
25 #include "FuncRequest.h"
27 #include "support/lyxalgo.h"
28 #include "support/lstrings.h"
30 #include <QHBoxLayout>
33 #include <QListWidget>
34 #include <QMouseEvent>
36 #include <QPushButton>
38 #include <QVBoxLayout>
40 using std::back_inserter;
49 using support::prefixIs;
54 class QTempListBox : public QListWidget {
57 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
58 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
59 setWindowModality(Qt::WindowModal);
60 setWindowFlags(Qt::Popup);
61 setAttribute(Qt::WA_DeleteOnClose);
64 void mouseReleaseEvent(QMouseEvent * ev) {
65 if (ev->x() < 0 || ev->y() < 0
66 || ev->x() > width() || ev->y() > height()) {
70 itemPressed(currentItem());
74 void keyPressEvent(QKeyEvent * ev) {
75 if (ev->key() == Qt::Key_Escape) {
78 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
80 itemPressed(currentItem());
82 QListWidget::keyPressEvent(ev);
89 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
90 : view_(view), history_pos_(history_.end())
92 transform(lyxaction.func_begin(), lyxaction.func_end(),
93 back_inserter(commands_), firster());
95 QPixmap qpup(":/images/up.png");
96 QPixmap qpdown(":/images/down.png");
98 QVBoxLayout * top = new QVBoxLayout(this);
99 QHBoxLayout * layout = new QHBoxLayout(0);
101 QPushButton * up = new QPushButton(qpup, "", this);
102 up->setMaximumSize(24, 24);
103 QPushButton * down = new QPushButton(qpdown, "", this);
104 down->setToolTip(qt_("Next command"));
105 down->setMaximumSize(24, 24);
106 connect(down, SIGNAL(clicked()), this, SLOT(down()));
108 edit_ = new GuiCommandEdit(this);
109 edit_->setMinimumSize(edit_->sizeHint());
110 edit_->setFocusPolicy(Qt::ClickFocus);
112 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
113 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
114 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
115 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
116 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
117 connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
119 layout->addWidget(up, 0);
120 layout->addWidget(down, 0);
121 layout->addWidget(edit_, 10);
122 layout->setMargin(0);
123 top->addLayout(layout);
125 setFocusProxy(edit_);
129 void GuiCommandBuffer::cancel()
132 edit_->setText(QString());
136 void GuiCommandBuffer::dispatch()
138 dispatch(fromqstr(edit_->text()));
140 edit_->setText(QString());
145 void GuiCommandBuffer::complete()
147 string const input = fromqstr(edit_->text());
149 vector<string> comp = completions(input, new_input);
151 if (comp.empty() && new_input == input) {
152 // show_info_suffix(qt_("[no match]"), input);
157 edit_->setText(toqstr(new_input));
158 // show_info_suffix(("[only completion]"), new_input + ' ');
162 edit_->setText(toqstr(new_input));
164 QTempListBox * list = new QTempListBox;
166 // For some reason the scrollview's contents are larger
167 // than the number of actual items...
168 vector<string>::const_iterator cit = comp.begin();
169 vector<string>::const_iterator end = comp.end();
170 for (; cit != end; ++cit)
171 list->addItem(toqstr(*cit));
173 list->resize(list->sizeHint());
174 QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
176 int const y = std::max(0, pos.y() - list->height());
178 list->move(pos.x(), y);
180 connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
181 this, SLOT(complete_selected(QListWidgetItem *)));
182 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
183 this, SLOT(complete_selected(QListWidgetItem *)));
190 void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
192 QWidget const * widget = static_cast<QWidget const *>(sender());
193 const_cast<QWidget *>(widget)->hide();
194 edit_->setText(item->text() + ' ');
195 edit_->activateWindow();
200 void GuiCommandBuffer::up()
202 string const input = fromqstr(edit_->text());
203 string const h = historyUp();
206 // show_info_suffix(qt_("[Beginning of history]"), input);
208 edit_->setText(toqstr(h));
213 void GuiCommandBuffer::down()
215 string const input = fromqstr(edit_->text());
216 string const h = historyDown();
219 // show_info_suffix(qt_("[End of history]"), input);
221 edit_->setText(toqstr(h));
226 void GuiCommandBuffer::hideParent()
229 edit_->setText(QString());
240 prefix_p(string const & s) : p(s) {}
241 bool operator()(string const & s) const { return prefixIs(s, p); }
244 } // end of anon namespace
247 string const GuiCommandBuffer::historyUp()
249 if (history_pos_ == history_.begin())
252 return *(--history_pos_);
256 string const GuiCommandBuffer::historyDown()
258 if (history_pos_ == history_.end())
260 if (history_pos_ + 1 == history_.end())
263 return *(++history_pos_);
267 docstring const GuiCommandBuffer::getCurrentState() const
269 return view_->view()->cursor().currentState();
273 void GuiCommandBuffer::hide() const
275 view_->showMiniBuffer(false);
280 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
284 copy_if(commands_.begin(), commands_.end(),
285 back_inserter(comp), prefix_p(prefix));
292 if (comp.size() == 1) {
293 new_prefix = comp[0];
294 return vector<string>();
297 // find maximal available prefix
298 string const tmp = comp[0];
299 string test = prefix;
300 if (tmp.length() > test.length())
301 test += tmp[test.length()];
302 while (test.length() < tmp.length()) {
304 copy_if(comp.begin(), comp.end(),
305 back_inserter(vtmp), prefix_p(test));
306 if (vtmp.size() != comp.size()) {
307 test.erase(test.length() - 1);
310 test += tmp[test.length()];
318 void GuiCommandBuffer::dispatch(string const & str)
323 history_.push_back(str);
324 history_pos_ = history_.end();
325 FuncRequest func = lyxaction.lookupFunc(str);
326 func.origin = FuncRequest::COMMANDBUFFER;
327 view_->dispatch(func);
330 } // namespace frontend
333 #include "GuiCommandBuffer_moc.cpp"