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"
21 #include "Validator.h"
23 #include "BufferView.h"
26 #include "LyXAction.h"
27 #include "FuncRequest.h"
30 #include "support/lyxalgo.h"
31 #include "support/lstrings.h"
33 #include <QHBoxLayout>
35 #include <QListWidget>
36 #include <QMouseEvent>
38 #include <QPushButton>
40 #include <QVBoxLayout>
43 using namespace lyx::support;
50 class QTempListBox : public QListWidget {
53 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
54 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
55 setWindowModality(Qt::WindowModal);
56 setWindowFlags(Qt::Popup);
57 setAttribute(Qt::WA_DeleteOnClose);
60 bool event(QEvent * ev) {
61 if (ev->type() == QEvent::MouseButtonPress) {
62 QMouseEvent * me = static_cast<QMouseEvent *>(ev);
63 if (me->x() < 0 || me->y() < 0
64 || me->x() > width() || me->y() > height())
68 return QListWidget::event(ev);
71 void keyPressEvent(QKeyEvent * ev) {
72 if (ev->key() == Qt::Key_Escape) {
75 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
77 itemClicked(currentItem());
79 QListWidget::keyPressEvent(ev);
86 GuiCommandBuffer::GuiCommandBuffer(GuiView * view)
89 transform(lyxaction.func_begin(), lyxaction.func_end(),
90 back_inserter(commands_), firster());
92 QPixmap qpup = getPixmap("images/", "up", "png");
93 QPixmap qpdown = getPixmap("images/", "down", "png");
95 QVBoxLayout * top = new QVBoxLayout(this);
96 QHBoxLayout * layout = new QHBoxLayout(0);
98 upPB = new QPushButton(qpup, "", this);
99 upPB->setToolTip(qt_("List of previous commands"));
100 upPB->setMaximumSize(24, 24);
101 downPB = new QPushButton(qpdown, "", this);
102 downPB->setToolTip(qt_("Next command"));
103 downPB->setMaximumSize(24, 24);
104 downPB->setEnabled(false);
105 connect(downPB, SIGNAL(clicked()), this, SLOT(down()));
106 connect(upPB, SIGNAL(pressed()), this, SLOT(listHistoryUp()));
108 edit_ = new GuiCommandEdit(this);
109 edit_->setMinimumSize(edit_->sizeHint());
110 edit_->setFocusPolicy(Qt::ClickFocus);
111 edit_->setValidator(new NoNewLineValidator(edit_));
113 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
114 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
115 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
116 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
117 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
118 connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
120 layout->addWidget(upPB, 0);
121 layout->addWidget(downPB, 0);
122 layout->addWidget(edit_, 10);
123 layout->setMargin(0);
124 top->addLayout(layout);
126 setFocusProxy(edit_);
128 LastCommandsSection::LastCommands last_commands
129 = theSession().lastCommands().getcommands();
130 LastCommandsSection::LastCommands::const_iterator it
131 = last_commands.begin();
132 LastCommandsSection::LastCommands::const_iterator end
133 = last_commands.end();
135 upPB->setEnabled(it != end);
137 for(; it != end; ++it)
138 history_.push_back(*it);
139 history_pos_ = history_.end();
143 void GuiCommandBuffer::cancel()
146 edit_->setText(QString());
150 void GuiCommandBuffer::dispatch()
152 QString cmd = edit_->text();
154 edit_->setText(QString());
156 std::string const cmd_ = fromqstr(cmd);
157 theSession().lastCommands().add(cmd_);
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 input = fromqstr(edit_->text());
235 string const h = historyUp();
238 edit_->setText(toqstr(h));
240 upPB->setEnabled(history_pos_ != history_.begin());
241 downPB->setEnabled(history_pos_ != history_.end());
245 void GuiCommandBuffer::down()
247 string const input = fromqstr(edit_->text());
248 string const h = historyDown();
251 edit_->setText(toqstr(h));
253 downPB->setEnabled(!history_.empty()
254 && history_pos_ != history_.end() - 1);
255 upPB->setEnabled(history_pos_ != history_.begin());
259 void GuiCommandBuffer::hideParent()
262 edit_->setText(QString());
273 prefix_p(string const & s) : p(s) {}
274 bool operator()(string const & s) const { return prefixIs(s, p); }
277 } // end of anon namespace
280 string const GuiCommandBuffer::historyUp()
282 if (history_pos_ == history_.begin())
285 return *(--history_pos_);
289 string const GuiCommandBuffer::historyDown()
291 if (history_pos_ == history_.end())
293 if (history_pos_ + 1 == history_.end())
296 return *(++history_pos_);
300 docstring const GuiCommandBuffer::getCurrentState() const
302 return view_->currentBufferView()->cursor().currentState();
306 void GuiCommandBuffer::hide() const
308 FuncRequest cmd(LFUN_COMMAND_EXECUTE, "off");
314 GuiCommandBuffer::completions(string const & prefix, string & new_prefix)
318 lyx::copy_if(commands_.begin(), commands_.end(),
319 back_inserter(comp), prefix_p(prefix));
326 if (comp.size() == 1) {
327 new_prefix = comp[0];
328 return vector<string>();
331 // find maximal available prefix
332 string const tmp = comp[0];
333 string test = prefix;
334 if (tmp.length() > test.length())
335 test += tmp[test.length()];
336 while (test.length() < tmp.length()) {
338 lyx::copy_if(comp.begin(), comp.end(),
339 back_inserter(vtmp), prefix_p(test));
340 if (vtmp.size() != comp.size()) {
341 test.erase(test.length() - 1);
344 test += tmp[test.length()];
352 void GuiCommandBuffer::dispatch(string const & str)
357 history_.push_back(trim(str));
358 history_pos_ = history_.end();
359 upPB->setEnabled(history_pos_ != history_.begin());
360 downPB->setEnabled(history_pos_ != history_.end());
361 FuncRequest func = lyxaction.lookupFunc(str);
362 func.setOrigin(FuncRequest::COMMANDBUFFER);
366 } // namespace frontend
369 #include "moc_GuiCommandBuffer.cpp"