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 // Qt defines a macro 'signals' that clashes with a boost namespace.
16 // All is well if the namespace is visible first.
19 #include "GuiCommandBuffer.h"
20 #include "GuiCommandEdit.h"
21 #include "qt_helpers.h"
23 #include "BufferView.h"
26 #include "LyXAction.h"
27 #include "FuncRequest.h"
29 #include "frontends/LyXView.h"
31 #include "support/lyxalgo.h"
32 #include "support/lstrings.h"
33 #include "support/filetools.h"
35 #include <QHBoxLayout>
38 #include <QListWidget>
39 #include <QMouseEvent>
41 #include <QPushButton>
43 #include <QVBoxLayout>
45 using std::back_inserter;
54 using support::prefixIs;
55 using support::libFileSearch;
60 class QTempListBox : public QListWidget {
63 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
64 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65 setWindowModality(Qt::WindowModal);
66 setWindowFlags(Qt::Popup);
67 setAttribute(Qt::WA_DeleteOnClose);
70 void mouseReleaseEvent(QMouseEvent * ev) {
71 if (ev->x() < 0 || ev->y() < 0
72 || ev->x() > width() || ev->y() > height()) {
76 itemPressed(currentItem());
80 void keyPressEvent(QKeyEvent * ev) {
81 if (ev->key() == Qt::Key_Escape) {
84 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
86 itemPressed(currentItem());
88 QListWidget::keyPressEvent(ev);
95 GuiCommandBuffer::GuiCommandBuffer(GuiViewBase * view)
96 : view_(view), lv_(*view), history_pos_(history_.end())
98 transform(lyxaction.func_begin(), lyxaction.func_end(),
99 back_inserter(commands_), firster());
101 QPixmap qpup(toqstr(libFileSearch("images", "up", "png").absFilename()));
102 QPixmap qpdown(toqstr(libFileSearch("images", "down", "png").absFilename()));
104 QVBoxLayout * top = new QVBoxLayout(this);
105 QHBoxLayout * layout = new QHBoxLayout(0);
107 QPushButton * up = new QPushButton(qpup, "", this);
108 up->setMaximumSize(24, 24);
109 QPushButton * down = new QPushButton(qpdown, "", this);
110 down->setToolTip(qt_("Next command"));
111 down->setMaximumSize(24, 24);
112 connect(down, SIGNAL(clicked()), this, SLOT(down()));
114 edit_ = new GuiCommandEdit(this);
115 edit_->setMinimumSize(edit_->sizeHint());
116 edit_->setFocusPolicy(Qt::ClickFocus);
118 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
119 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
120 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
121 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
122 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
123 connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
125 layout->addWidget(up, 0);
126 layout->addWidget(down, 0);
127 layout->addWidget(edit_, 10);
128 layout->setMargin(0);
129 top->addLayout(layout);
131 setFocusProxy(edit_);
135 void GuiCommandBuffer::cancel()
138 edit_->setText(QString());
142 void GuiCommandBuffer::dispatch()
144 dispatch(fromqstr(edit_->text()));
146 edit_->setText(QString());
151 void GuiCommandBuffer::complete()
153 string const input = fromqstr(edit_->text());
155 vector<string> comp = completions(input, new_input);
157 if (comp.empty() && new_input == input) {
158 // show_info_suffix(qt_("[no match]"), input);
163 edit_->setText(toqstr(new_input));
164 // show_info_suffix(("[only completion]"), new_input + ' ');
168 edit_->setText(toqstr(new_input));
170 QTempListBox * list = new QTempListBox;
172 // For some reason the scrollview's contents are larger
173 // than the number of actual items...
174 vector<string>::const_iterator cit = comp.begin();
175 vector<string>::const_iterator end = comp.end();
176 for (; cit != end; ++cit)
177 list->addItem(toqstr(*cit));
179 list->resize(list->sizeHint());
180 QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
182 int const y = std::max(0, pos.y() - list->height());
184 list->move(pos.x(), y);
186 connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
187 this, SLOT(complete_selected(QListWidgetItem *)));
188 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
189 this, SLOT(complete_selected(QListWidgetItem *)));
196 void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
198 QWidget const * widget = static_cast<QWidget const *>(sender());
199 const_cast<QWidget *>(widget)->hide();
200 edit_->setText(item->text() + ' ');
201 edit_->activateWindow();
206 void GuiCommandBuffer::up()
208 string const input = fromqstr(edit_->text());
209 string const h = historyUp();
212 // show_info_suffix(qt_("[Beginning of history]"), input);
214 edit_->setText(toqstr(h));
219 void GuiCommandBuffer::down()
221 string const input = fromqstr(edit_->text());
222 string const h = historyDown();
225 // show_info_suffix(qt_("[End of history]"), input);
227 edit_->setText(toqstr(h));
232 void GuiCommandBuffer::hideParent()
235 edit_->setText(QString());
246 prefix_p(string const & s) : p(s) {}
247 bool operator()(string const & s) const { return prefixIs(s, p); }
250 } // end of anon namespace
253 string const GuiCommandBuffer::historyUp()
255 if (history_pos_ == history_.begin())
258 return *(--history_pos_);
262 string const GuiCommandBuffer::historyDown()
264 if (history_pos_ == history_.end())
266 if (history_pos_ + 1 == history_.end())
269 return *(++history_pos_);
273 docstring const GuiCommandBuffer::getCurrentState() const
275 return lv_.view()->cursor().currentState();
279 void GuiCommandBuffer::hide() const
281 lv_.showMiniBuffer(false);
286 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
290 copy_if(commands_.begin(), commands_.end(),
291 back_inserter(comp), prefix_p(prefix));
298 if (comp.size() == 1) {
299 new_prefix = comp[0];
300 return vector<string>();
303 // find maximal available prefix
304 string const tmp = comp[0];
305 string test = prefix;
306 if (tmp.length() > test.length())
307 test += tmp[test.length()];
308 while (test.length() < tmp.length()) {
310 copy_if(comp.begin(), comp.end(),
311 back_inserter(vtmp), prefix_p(test));
312 if (vtmp.size() != comp.size()) {
313 test.erase(test.length() - 1);
316 test += tmp[test.length()];
324 void GuiCommandBuffer::dispatch(string const & str)
329 history_.push_back(str);
330 history_pos_ = history_.end();
331 FuncRequest func = lyxaction.lookupFunc(str);
332 func.origin = FuncRequest::COMMANDBUFFER;
336 } // namespace frontend
339 #include "GuiCommandBuffer_moc.cpp"