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->setMaximumSize(24, 24);
88 up->setToolTip(qt_("Previous command"));
89 connect(up, SIGNAL(clicked()), this, SLOT(up()));
90 QPushButton * down = new QPushButton(qpdown, "", this);
91 down->setToolTip(qt_("Next command"));
92 down->setMaximumSize(24, 24);
93 connect(down, SIGNAL(clicked()), this, SLOT(down()));
95 edit_ = new QCommandEdit(this);
96 edit_->setMinimumSize(edit_->sizeHint());
97 edit_->setFocusPolicy(Qt::ClickFocus);
99 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
100 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
101 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
102 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
103 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
105 layout->addWidget(up, 0);
106 layout->addWidget(down, 0);
107 layout->addWidget(edit_, 10);
108 layout->setMargin(0);
109 top->addLayout(layout);
115 void QCommandBuffer::focus_command()
121 void QCommandBuffer::cancel()
124 edit_->setText(QString());
128 void QCommandBuffer::dispatch()
130 controller_.dispatch(fromqstr(edit_->text()));
132 edit_->setText(QString());
137 void QCommandBuffer::complete()
139 string const input = fromqstr(edit_->text());
141 vector<string> comp = controller_.completions(input, new_input);
143 if (comp.empty() && new_input == input) {
144 // show_info_suffix(qt_("[no match]"), input);
149 edit_->setText(toqstr(new_input));
150 // show_info_suffix(("[only completion]"), new_input + ' ');
154 edit_->setText(toqstr(new_input));
156 QTempListBox * list = new QTempListBox;
158 // For some reason the scrollview's contents are larger
159 // than the number of actual items...
160 vector<string>::const_iterator cit = comp.begin();
161 vector<string>::const_iterator end = comp.end();
162 for (; cit != end; ++cit)
163 list->addItem(toqstr(*cit));
165 list->resize(list->sizeHint());
166 QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
168 int const 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"