2 * \file QCommandBuffer.C
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 "QCommandBuffer.h"
18 #include "QCommandEdit.h"
19 #include "qt_helpers.h"
21 #include "controllers/ControlCommandBuffer.h"
23 #include "support/filetools.h"
25 #include <QHBoxLayout>
28 #include <QListWidget>
29 #include <QMouseEvent>
31 #include <QPushButton>
33 #include <QVBoxLayout>
35 using lyx::support::libFileSearch;
45 class QTempListBox : public QListWidget {
48 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
49 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50 setWindowModality(Qt::WindowModal);
51 setWindowFlags(Qt::Popup);
52 setAttribute(Qt::WA_DeleteOnClose);
55 void mouseReleaseEvent(QMouseEvent * ev) {
56 if (ev->x() < 0 || ev->y() < 0
57 || ev->x() > width() || ev->y() > height()) {
61 itemPressed(currentItem());
65 void keyPressEvent(QKeyEvent * ev) {
66 if (ev->key() == Qt::Key_Escape) {
70 QListWidget::keyPressEvent(ev);
77 QCommandBuffer::QCommandBuffer(GuiView * view, ControlCommandBuffer & control)
78 : view_(view), controller_(control)
80 QPixmap qpup(toqstr(libFileSearch("images", "up", "xpm").absFilename()));
81 QPixmap qpdown(toqstr(libFileSearch("images", "down", "xpm").absFilename()));
83 QVBoxLayout * top = new QVBoxLayout(this);
84 QHBoxLayout * layout = new QHBoxLayout(0);
86 QPushButton * up = new QPushButton(qpup, "", this);
87 up->setToolTip(qt_("Previous command"));
88 connect(up, SIGNAL(clicked()), this, SLOT(up()));
89 QPushButton * down = new QPushButton(qpdown, "", this);
90 down->setToolTip(qt_("Next command"));
91 connect(down, SIGNAL(clicked()), this, SLOT(down()));
93 edit_ = new QCommandEdit(this);
94 edit_->setMinimumSize(edit_->sizeHint());
95 edit_->setFocusPolicy(Qt::ClickFocus);
97 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
98 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
99 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
100 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
101 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
103 layout->addWidget(up, 0);
104 layout->addWidget(down, 0);
105 layout->addWidget(edit_, 10);
106 layout->setMargin(0);
107 top->addLayout(layout);
113 void QCommandBuffer::focus_command()
119 void QCommandBuffer::cancel()
122 edit_->setText(QString());
126 void QCommandBuffer::dispatch()
128 controller_.dispatch(fromqstr(edit_->text()));
130 edit_->setText(QString());
135 void QCommandBuffer::complete()
137 string const input = fromqstr(edit_->text());
139 vector<string> comp = controller_.completions(input, new_input);
141 if (comp.empty() && new_input == input) {
142 // show_info_suffix(qt_("[no match]"), input);
147 edit_->setText(toqstr(new_input));
148 // show_info_suffix(("[only completion]"), new_input + ' ');
152 edit_->setText(toqstr(new_input));
154 QTempListBox * list = new QTempListBox;
156 // For some reason the scrollview's contents are larger
157 // than the number of actual items...
158 vector<string>::const_iterator cit = comp.begin();
159 vector<string>::const_iterator end = comp.end();
160 for (; cit != end; ++cit)
161 list->addItem(toqstr(*cit));
163 list->resize(list->sizeHint());
164 QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
166 int const y = std::max(0, pos.y() - list->height());
168 list->move(pos.x(), y);
170 connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
171 this, SLOT(complete_selected(QListWidgetItem *)));
178 void QCommandBuffer::complete_selected(QListWidgetItem * item)
180 QWidget const * widget = static_cast<QWidget const *>(sender());
181 const_cast<QWidget *>(widget)->hide();
182 edit_->setText(item->text() + ' ');
187 void QCommandBuffer::up()
189 string const input = fromqstr(edit_->text());
190 string const h = controller_.historyUp();
193 // show_info_suffix(qt_("[Beginning of history]"), input);
195 edit_->setText(toqstr(h));
200 void QCommandBuffer::down()
202 string const input = fromqstr(edit_->text());
203 string const h = controller_.historyDown();
206 // show_info_suffix(qt_("[End of history]"), input);
208 edit_->setText(toqstr(h));
214 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
216 stored_input_ = input;
217 info_suffix_shown_ = true;
218 set_input(input + ' ' + suffix);
219 suffix_timer_->start();
223 void XMiniBuffer::suffix_timeout()
225 info_suffix_shown_ = false;
226 set_input(stored_input_);
231 } // namespace frontend
234 #include "QCommandBuffer_moc.cpp"