2 * \file GuiCommandBuffer.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 // Qt defines a macro 'signals' that clashes with a boost namespace.
14 // All is well if the namespace is visible first.
17 #include "GuiCommandBuffer.h"
18 #include "GuiCommandEdit.h"
19 #include "qt_helpers.h"
21 #include "support/filetools.h"
23 #include <QHBoxLayout>
26 #include <QListWidget>
27 #include <QMouseEvent>
29 #include <QPushButton>
31 #include <QVBoxLayout>
33 using lyx::support::libFileSearch;
43 class QTempListBox : public QListWidget {
46 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
47 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
48 setWindowModality(Qt::WindowModal);
49 setWindowFlags(Qt::Popup);
50 setAttribute(Qt::WA_DeleteOnClose);
53 void mouseReleaseEvent(QMouseEvent * ev) {
54 if (ev->x() < 0 || ev->y() < 0
55 || ev->x() > width() || ev->y() > height()) {
59 itemPressed(currentItem());
63 void keyPressEvent(QKeyEvent * ev) {
64 if (ev->key() == Qt::Key_Escape) {
67 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
69 itemPressed(currentItem());
71 QListWidget::keyPressEvent(ev);
78 GuiCommandBuffer::GuiCommandBuffer(GuiViewBase * view)
79 : view_(view), controller_(*view)
81 QPixmap qpup(toqstr(libFileSearch("images", "up", "png").absFilename()));
82 QPixmap qpdown(toqstr(libFileSearch("images", "down", "png").absFilename()));
84 QVBoxLayout * top = new QVBoxLayout(this);
85 QHBoxLayout * layout = new QHBoxLayout(0);
87 QPushButton * up = new QPushButton(qpup, "", this);
88 up->setMaximumSize(24, 24);
89 up->setToolTip(qt_("Previous command"));
90 connect(up, SIGNAL(clicked()), this, SLOT(up()));
91 QPushButton * down = new QPushButton(qpdown, "", this);
92 down->setToolTip(qt_("Next command"));
93 down->setMaximumSize(24, 24);
94 connect(down, SIGNAL(clicked()), this, SLOT(down()));
96 edit_ = new GuiCommandEdit(this);
97 edit_->setMinimumSize(edit_->sizeHint());
98 edit_->setFocusPolicy(Qt::ClickFocus);
100 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
101 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
102 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
103 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
104 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
105 connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
107 layout->addWidget(up, 0);
108 layout->addWidget(down, 0);
109 layout->addWidget(edit_, 10);
110 layout->setMargin(0);
111 top->addLayout(layout);
113 setFocusProxy(edit_);
117 void GuiCommandBuffer::cancel()
120 edit_->setText(QString());
124 void GuiCommandBuffer::dispatch()
126 controller_.dispatch(fromqstr(edit_->text()));
128 edit_->setText(QString());
133 void GuiCommandBuffer::complete()
135 string const input = fromqstr(edit_->text());
137 vector<string> comp = controller_.completions(input, new_input);
139 if (comp.empty() && new_input == input) {
140 // show_info_suffix(qt_("[no match]"), input);
145 edit_->setText(toqstr(new_input));
146 // show_info_suffix(("[only completion]"), new_input + ' ');
150 edit_->setText(toqstr(new_input));
152 QTempListBox * list = new QTempListBox;
154 // For some reason the scrollview's contents are larger
155 // than the number of actual items...
156 vector<string>::const_iterator cit = comp.begin();
157 vector<string>::const_iterator end = comp.end();
158 for (; cit != end; ++cit)
159 list->addItem(toqstr(*cit));
161 list->resize(list->sizeHint());
162 QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
164 int const y = std::max(0, pos.y() - list->height());
166 list->move(pos.x(), y);
168 connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
169 this, SLOT(complete_selected(QListWidgetItem *)));
170 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
171 this, SLOT(complete_selected(QListWidgetItem *)));
178 void GuiCommandBuffer::complete_selected(QListWidgetItem * item)
180 QWidget const * widget = static_cast<QWidget const *>(sender());
181 const_cast<QWidget *>(widget)->hide();
182 edit_->setText(item->text() + ' ');
183 edit_->activateWindow();
188 void GuiCommandBuffer::up()
190 string const input = fromqstr(edit_->text());
191 string const h = controller_.historyUp();
194 // show_info_suffix(qt_("[Beginning of history]"), input);
196 edit_->setText(toqstr(h));
201 void GuiCommandBuffer::down()
203 string const input = fromqstr(edit_->text());
204 string const h = controller_.historyDown();
207 // show_info_suffix(qt_("[End of history]"), input);
209 edit_->setText(toqstr(h));
214 void GuiCommandBuffer::hideParent()
217 edit_->setText(QString());
224 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
226 stored_input_ = input;
227 info_suffix_shown_ = true;
228 set_input(input + ' ' + suffix);
229 suffix_timer_->start();
233 void XMiniBuffer::suffix_timeout()
235 info_suffix_shown_ = false;
236 set_input(stored_input_);
241 } // namespace frontend
244 #include "GuiCommandBuffer_moc.cpp"