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.
17 #include "GuiCommandBuffer.h"
18 #include "GuiCommandEdit.h"
19 #include "qt_helpers.h"
21 #include "BufferView.h"
24 #include "LyXAction.h"
25 #include "FuncRequest.h"
27 #include "frontends/LyXView.h"
29 #include "support/lyxalgo.h"
30 #include "support/lstrings.h"
32 #include <QHBoxLayout>
35 #include <QListWidget>
36 #include <QMouseEvent>
38 #include <QPushButton>
40 #include <QVBoxLayout>
42 using std::back_inserter;
51 using support::prefixIs;
56 class QTempListBox : public QListWidget {
59 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
60 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
61 setWindowModality(Qt::WindowModal);
62 setWindowFlags(Qt::Popup);
63 setAttribute(Qt::WA_DeleteOnClose);
66 void mouseReleaseEvent(QMouseEvent * ev) {
67 if (ev->x() < 0 || ev->y() < 0
68 || ev->x() > width() || ev->y() > height()) {
72 itemPressed(currentItem());
76 void keyPressEvent(QKeyEvent * ev) {
77 if (ev->key() == Qt::Key_Escape) {
80 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
82 itemPressed(currentItem());
84 QListWidget::keyPressEvent(ev);
91 GuiCommandBuffer::GuiCommandBuffer(GuiViewBase * view)
92 : view_(view), lv_(*view), history_pos_(history_.end())
94 transform(lyxaction.func_begin(), lyxaction.func_end(),
95 back_inserter(commands_), firster());
97 QPixmap qpup(":/images/up.png");
98 QPixmap qpdown(":/images/down.png");
100 QVBoxLayout * top = new QVBoxLayout(this);
101 QHBoxLayout * layout = new QHBoxLayout(0);
103 QPushButton * up = new QPushButton(qpup, "", this);
104 up->setMaximumSize(24, 24);
105 QPushButton * down = new QPushButton(qpdown, "", this);
106 down->setToolTip(qt_("Next command"));
107 down->setMaximumSize(24, 24);
108 connect(down, SIGNAL(clicked()), this, SLOT(down()));
110 edit_ = new GuiCommandEdit(this);
111 edit_->setMinimumSize(edit_->sizeHint());
112 edit_->setFocusPolicy(Qt::ClickFocus);
114 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
115 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
116 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
117 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
118 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
119 connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
121 layout->addWidget(up, 0);
122 layout->addWidget(down, 0);
123 layout->addWidget(edit_, 10);
124 layout->setMargin(0);
125 top->addLayout(layout);
127 setFocusProxy(edit_);
131 void GuiCommandBuffer::cancel()
134 edit_->setText(QString());
138 void GuiCommandBuffer::dispatch()
140 dispatch(fromqstr(edit_->text()));
142 edit_->setText(QString());
147 void GuiCommandBuffer::complete()
149 string const input = fromqstr(edit_->text());
151 vector<string> comp = completions(input, new_input);
153 if (comp.empty() && new_input == input) {
154 // show_info_suffix(qt_("[no match]"), input);
159 edit_->setText(toqstr(new_input));
160 // show_info_suffix(("[only completion]"), new_input + ' ');
164 edit_->setText(toqstr(new_input));
166 QTempListBox * list = new QTempListBox;
168 // For some reason the scrollview's contents are larger
169 // than the number of actual items...
170 vector<string>::const_iterator cit = comp.begin();
171 vector<string>::const_iterator end = comp.end();
172 for (; cit != end; ++cit)
173 list->addItem(toqstr(*cit));
175 list->resize(list->sizeHint());
176 QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
178 int const y = std::max(0, pos.y() - list->height());
180 list->move(pos.x(), y);
182 connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
183 this, SLOT(complete_selected(QListWidgetItem *)));
184 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
185 this, SLOT(complete_selected(QListWidgetItem *)));
192 void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
194 QWidget const * widget = static_cast<QWidget const *>(sender());
195 const_cast<QWidget *>(widget)->hide();
196 edit_->setText(item->text() + ' ');
197 edit_->activateWindow();
202 void GuiCommandBuffer::up()
204 string const input = fromqstr(edit_->text());
205 string const h = historyUp();
208 // show_info_suffix(qt_("[Beginning of history]"), input);
210 edit_->setText(toqstr(h));
215 void GuiCommandBuffer::down()
217 string const input = fromqstr(edit_->text());
218 string const h = historyDown();
221 // show_info_suffix(qt_("[End of history]"), input);
223 edit_->setText(toqstr(h));
228 void GuiCommandBuffer::hideParent()
231 edit_->setText(QString());
242 prefix_p(string const & s) : p(s) {}
243 bool operator()(string const & s) const { return prefixIs(s, p); }
246 } // end of anon namespace
249 string const GuiCommandBuffer::historyUp()
251 if (history_pos_ == history_.begin())
254 return *(--history_pos_);
258 string const GuiCommandBuffer::historyDown()
260 if (history_pos_ == history_.end())
262 if (history_pos_ + 1 == history_.end())
265 return *(++history_pos_);
269 docstring const GuiCommandBuffer::getCurrentState() const
271 return lv_.view()->cursor().currentState();
275 void GuiCommandBuffer::hide() const
277 lv_.showMiniBuffer(false);
282 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
286 copy_if(commands_.begin(), commands_.end(),
287 back_inserter(comp), prefix_p(prefix));
294 if (comp.size() == 1) {
295 new_prefix = comp[0];
296 return vector<string>();
299 // find maximal available prefix
300 string const tmp = comp[0];
301 string test = prefix;
302 if (tmp.length() > test.length())
303 test += tmp[test.length()];
304 while (test.length() < tmp.length()) {
306 copy_if(comp.begin(), comp.end(),
307 back_inserter(vtmp), prefix_p(test));
308 if (vtmp.size() != comp.size()) {
309 test.erase(test.length() - 1);
312 test += tmp[test.length()];
320 void GuiCommandBuffer::dispatch(string const & str)
325 history_.push_back(str);
326 history_pos_ = history_.end();
327 FuncRequest func = lyxaction.lookupFunc(str);
328 func.origin = FuncRequest::COMMANDBUFFER;
332 } // namespace frontend
335 #include "GuiCommandBuffer_moc.cpp"