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"
28 #include "support/lyxalgo.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) {
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) {
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 transform(lyxaction.func_begin(), lyxaction.func_end(),
88 back_inserter(commands_), firster());
90 QPixmap qpup(":/images/up.png");
91 QPixmap qpdown(":/images/down.png");
93 QVBoxLayout * top = new QVBoxLayout(this);
94 QHBoxLayout * layout = new QHBoxLayout(0);
96 upPB = new QPushButton(qpup, "", this);
97 upPB->setToolTip(qt_("List of previous commands"));
98 upPB->setMaximumSize(24, 24);
99 downPB = new QPushButton(qpdown, "", this);
100 downPB->setToolTip(qt_("Next command"));
101 downPB->setMaximumSize(24, 24);
102 downPB->setEnabled(false);
103 connect(downPB, SIGNAL(clicked()), this, SLOT(down()));
104 connect(upPB, SIGNAL(pressed()), this, SLOT(listHistoryUp()));
106 edit_ = new GuiCommandEdit(this);
107 edit_->setMinimumSize(edit_->sizeHint());
108 edit_->setFocusPolicy(Qt::ClickFocus);
110 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
111 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
112 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
113 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
114 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
115 connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
117 layout->addWidget(upPB, 0);
118 layout->addWidget(downPB, 0);
119 layout->addWidget(edit_, 10);
120 layout->setMargin(0);
121 top->addLayout(layout);
123 setFocusProxy(edit_);
125 LastCommandsSection::LastCommands last_commands
126 = theSession().lastCommands().getcommands();
127 LastCommandsSection::LastCommands::const_iterator it
128 = last_commands.begin();
129 LastCommandsSection::LastCommands::const_iterator end
130 = last_commands.end();
132 upPB->setEnabled(it != end);
134 for(; it != end; ++it)
135 history_.push_back(*it);
136 history_pos_ = history_.end();
140 void GuiCommandBuffer::cancel()
143 edit_->setText(QString());
147 void GuiCommandBuffer::dispatch()
149 QString cmd = edit_->text();
151 edit_->setText(QString());
153 std::string const cmd_ = fromqstr(cmd);
154 theSession().lastCommands().add(cmd_);
159 void GuiCommandBuffer::listHistoryUp()
161 if (history_.size()==1) {
162 edit_->setText(toqstr(history_.back()));
163 upPB->setEnabled(false);
166 QPoint const & pos = upPB->mapToGlobal(QPoint(0, 0));
167 showList(history_, pos, true);
171 void GuiCommandBuffer::complete()
173 string const input = fromqstr(edit_->text());
175 vector<string> comp = completions(input, new_input);
178 if (new_input != input)
179 edit_->setText(toqstr(new_input));
183 edit_->setText(toqstr(new_input));
184 QPoint const & pos = edit_->mapToGlobal(QPoint(0, 0));
188 void GuiCommandBuffer::showList(vector<string> const & list,
189 QPoint const & pos, bool reversed) const
191 QTempListBox * listBox = new QTempListBox;
193 // For some reason the scrollview's contents are larger
194 // than the number of actual items...
195 vector<string>::const_iterator cit = list.begin();
196 vector<string>::const_iterator end = list.end();
198 for (; cit != end; ++cit)
199 listBox ->addItem(toqstr(*cit));
201 for (--end; end != cit; --end)
202 listBox ->addItem(toqstr(*end));
205 listBox->resize(listBox ->sizeHint());
207 int const y = max(0, pos.y() - listBox->height());
208 listBox->move(pos.x(), y);
210 connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
211 this, SLOT(item_selected(QListWidgetItem *)));
212 connect(listBox, SIGNAL(itemActivated(QListWidgetItem *)),
213 this, SLOT(item_selected(QListWidgetItem *)));
220 void GuiCommandBuffer::item_selected(QListWidgetItem * item)
222 QWidget const * widget = static_cast<QWidget const *>(sender());
223 const_cast<QWidget *>(widget)->hide();
224 edit_->setText(item->text()+ ' ');
225 edit_->activateWindow();
230 void GuiCommandBuffer::up()
232 string const input = fromqstr(edit_->text());
233 string const h = historyUp();
236 edit_->setText(toqstr(h));
238 upPB->setEnabled(history_pos_ != history_.begin());
239 downPB->setEnabled(history_pos_ != history_.end());
243 void GuiCommandBuffer::down()
245 string const input = fromqstr(edit_->text());
246 string const h = historyDown();
249 edit_->setText(toqstr(h));
251 downPB->setEnabled(history_pos_ != history_.end()-1);
252 upPB->setEnabled(history_pos_ != history_.begin());
256 void GuiCommandBuffer::hideParent()
259 edit_->setText(QString());
270 prefix_p(string const & s) : p(s) {}
271 bool operator()(string const & s) const { return prefixIs(s, p); }
274 } // end of anon namespace
277 string const GuiCommandBuffer::historyUp()
279 if (history_pos_ == history_.begin())
282 return *(--history_pos_);
286 string const GuiCommandBuffer::historyDown()
288 if (history_pos_ == history_.end())
290 if (history_pos_ + 1 == history_.end())
293 return *(++history_pos_);
297 docstring const GuiCommandBuffer::getCurrentState() const
299 return view_->view()->cursor().currentState();
303 void GuiCommandBuffer::hide() const
305 FuncRequest cmd(LFUN_COMMAND_EXECUTE, "off");
306 theLyXFunc().setLyXView(view_);
312 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
316 copy_if(commands_.begin(), commands_.end(),
317 back_inserter(comp), prefix_p(prefix));
324 if (comp.size() == 1) {
325 new_prefix = comp[0];
326 return vector<string>();
329 // find maximal available prefix
330 string const tmp = comp[0];
331 string test = prefix;
332 if (tmp.length() > test.length())
333 test += tmp[test.length()];
334 while (test.length() < tmp.length()) {
336 copy_if(comp.begin(), comp.end(),
337 back_inserter(vtmp), prefix_p(test));
338 if (vtmp.size() != comp.size()) {
339 test.erase(test.length() - 1);
342 test += tmp[test.length()];
350 void GuiCommandBuffer::dispatch(string const & str)
355 history_.push_back(trim(str));
356 history_pos_ = history_.end();
357 upPB->setEnabled(history_pos_ != history_.begin());
358 downPB->setEnabled(history_pos_ != history_.end());
359 FuncRequest func = lyxaction.lookupFunc(str);
360 func.origin = FuncRequest::COMMANDBUFFER;
361 theLyXFunc().setLyXView(view_);
365 } // namespace frontend
368 #include "moc_GuiCommandBuffer.cpp"