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 <QVBoxLayout>
22 #include <QMouseEvent>
24 #include <QHBoxLayout>
27 #include "controllers/ControlCommandBuffer.h"
29 #include "support/filetools.h"
31 #include <QListWidget>
34 #include <QPushButton>
36 using lyx::support::libFileSearch;
46 class QTempListBox : public QListWidget {
49 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
50 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
51 setWindowModality(Qt::WindowModal);
52 setWindowFlags(Qt::Popup);
53 setAttribute(Qt::WA_DeleteOnClose);
56 void mouseReleaseEvent(QMouseEvent * e) {
57 if (e->x() < 0 || e->y() < 0
58 || e->x() > width() || e->y() > height()) {
61 emit itemPressed(currentItem());
65 void keyPressEvent(QKeyEvent * e) {
66 if (e->key() == Qt::Key_Escape) {
70 QListWidget::keyPressEvent(e);
77 QCommandBuffer::QCommandBuffer(GuiView * view, ControlCommandBuffer & control, QWidget * parent)
78 : QWidget(parent), view_(view), controller_(control)
80 QPixmap qpup(toqstr(libFileSearch("images", "up", "xpm")));
81 QPixmap qpdown(toqstr(libFileSearch("images", "down", "xpm")));
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 top->addLayout(layout);
111 void QCommandBuffer::focus_command()
117 void QCommandBuffer::cancel()
119 view_->centralWidget()->setFocus();
124 void QCommandBuffer::dispatch()
126 controller_.dispatch(fromqstr(edit_->text()));
127 view_->centralWidget()->setFocus();
133 void QCommandBuffer::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));
162 // width() is not big enough by a few pixels. Qt Sucks.
163 // list->setMinimumWidth(list->sizeHint().width() + 10);
165 list->resize(list->sizeHint());
166 QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
168 int y = std::max(0, pos.y() - list->height());
170 list->move(pos.x(), y);
172 connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
173 this, SLOT(complete_selected(QListWidgetItem *)));
180 void QCommandBuffer::complete_selected(QListWidgetItem * item)
182 QWidget const * widget = static_cast<QWidget const *>(sender());
183 const_cast<QWidget *>(widget)->hide();
184 edit_->setText(item->text() + ' ');
189 void QCommandBuffer::up()
191 string const input(fromqstr(edit_->text()));
192 string const h(controller_.historyUp());
195 // show_info_suffix(qt_("[Beginning of history]"), input);
197 edit_->setText(toqstr(h));
202 void QCommandBuffer::down()
204 string const input(fromqstr(edit_->text()));
205 string const h(controller_.historyDown());
208 // show_info_suffix(qt_("[End of history]"), input);
210 edit_->setText(toqstr(h));
216 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
218 stored_input_ = input;
219 info_suffix_shown_ = true;
220 set_input(input + ' ' + suffix);
221 suffix_timer_->start();
225 void XMiniBuffer::suffix_timeout()
227 info_suffix_shown_ = false;
228 set_input(stored_input_);
233 } // namespace frontend
236 #include "QCommandBuffer_moc.cpp"