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()) {
62 itemPressed(currentItem());
66 void keyPressEvent(QKeyEvent * e) {
67 if (e->key() == Qt::Key_Escape) {
71 QListWidget::keyPressEvent(e);
78 QCommandBuffer::QCommandBuffer(GuiView * view, ControlCommandBuffer & control, QWidget * parent)
79 : QWidget(parent), view_(view), controller_(control)
81 QPixmap qpup(toqstr(libFileSearch("images", "up", "xpm")));
82 QPixmap qpdown(toqstr(libFileSearch("images", "down", "xpm")));
84 QVBoxLayout * top = new QVBoxLayout(this);
85 QHBoxLayout * layout = new QHBoxLayout(0);
87 QPushButton * up = new QPushButton(qpup, "", this);
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 connect(down, SIGNAL(clicked()), this, SLOT(down()));
94 edit_ = new QCommandEdit(this);
95 edit_->setMinimumSize(edit_->sizeHint());
96 edit_->setFocusPolicy(Qt::ClickFocus);
98 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
99 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
100 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
101 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
102 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
104 layout->addWidget(up, 0);
105 layout->addWidget(down, 0);
106 layout->addWidget(edit_, 10);
107 top->addLayout(layout);
112 void QCommandBuffer::focus_command()
118 void QCommandBuffer::cancel()
120 view_->centralWidget()->setFocus();
125 void QCommandBuffer::dispatch()
127 controller_.dispatch(fromqstr(edit_->text()));
128 view_->centralWidget()->setFocus();
134 void QCommandBuffer::complete()
136 string const input = fromqstr(edit_->text());
138 vector<string> comp = controller_.completions(input, new_input);
140 if (comp.empty() && new_input == input) {
141 // show_info_suffix(qt_("[no match]"), input);
146 edit_->setText(toqstr(new_input));
147 // show_info_suffix(("[only completion]"), new_input + ' ');
151 edit_->setText(toqstr(new_input));
153 QTempListBox * list = new QTempListBox;
155 // For some reason the scrollview's contents are larger
156 // than the number of actual items...
157 vector<string>::const_iterator cit = comp.begin();
158 vector<string>::const_iterator end = comp.end();
159 for (; cit != end; ++cit) {
160 list->addItem(toqstr(*cit));
163 // width() is not big enough by a few pixels. Qt Sucks.
164 // list->setMinimumWidth(list->sizeHint().width() + 10);
166 list->resize(list->sizeHint());
167 QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
169 int y = std::max(0, pos.y() - list->height());
171 list->move(pos.x(), y);
173 connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
174 this, SLOT(complete_selected(QListWidgetItem *)));
181 void QCommandBuffer::complete_selected(QListWidgetItem * item)
183 QWidget const * widget = static_cast<QWidget const *>(sender());
184 const_cast<QWidget *>(widget)->hide();
185 edit_->setText(item->text() + ' ');
190 void QCommandBuffer::up()
192 string const input(fromqstr(edit_->text()));
193 string const h(controller_.historyUp());
196 // show_info_suffix(qt_("[Beginning of history]"), input);
198 edit_->setText(toqstr(h));
203 void QCommandBuffer::down()
205 string const input(fromqstr(edit_->text()));
206 string const h(controller_.historyDown());
209 // show_info_suffix(qt_("[End of history]"), input);
211 edit_->setText(toqstr(h));
217 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
219 stored_input_ = input;
220 info_suffix_shown_ = true;
221 set_input(input + ' ' + suffix);
222 suffix_timer_->start();
226 void XMiniBuffer::suffix_timeout()
228 info_suffix_shown_ = false;
229 set_input(stored_input_);
234 } // namespace frontend
237 #include "QCommandBuffer_moc.cpp"