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,
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 layout->setMargin(0);
108 top->addLayout(layout);
114 void QCommandBuffer::focus_command()
120 void QCommandBuffer::cancel()
122 view_->centralWidget()->setFocus();
123 edit_->setText(QString());
127 void QCommandBuffer::dispatch()
129 controller_.dispatch(fromqstr(edit_->text()));
130 view_->centralWidget()->setFocus();
131 edit_->setText(QString());
136 void QCommandBuffer::complete()
138 string const input = fromqstr(edit_->text());
140 vector<string> comp = controller_.completions(input, new_input);
142 if (comp.empty() && new_input == input) {
143 // show_info_suffix(qt_("[no match]"), input);
148 edit_->setText(toqstr(new_input));
149 // show_info_suffix(("[only completion]"), new_input + ' ');
153 edit_->setText(toqstr(new_input));
155 QTempListBox * list = new QTempListBox;
157 // For some reason the scrollview's contents are larger
158 // than the number of actual items...
159 vector<string>::const_iterator cit = comp.begin();
160 vector<string>::const_iterator end = comp.end();
161 for (; cit != end; ++cit)
162 list->addItem(toqstr(*cit));
164 list->resize(list->sizeHint());
165 QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
167 int const y = std::max(0, pos.y() - list->height());
169 list->move(pos.x(), y);
171 connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
172 this, SLOT(complete_selected(QListWidgetItem *)));
179 void QCommandBuffer::complete_selected(QListWidgetItem * item)
181 QWidget const * widget = static_cast<QWidget const *>(sender());
182 const_cast<QWidget *>(widget)->hide();
183 edit_->setText(item->text() + ' ');
188 void QCommandBuffer::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 QCommandBuffer::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));
215 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
217 stored_input_ = input;
218 info_suffix_shown_ = true;
219 set_input(input + ' ' + suffix);
220 suffix_timer_->start();
224 void XMiniBuffer::suffix_timeout()
226 info_suffix_shown_ = false;
227 set_input(stored_input_);
232 } // namespace frontend
235 #include "QCommandBuffer_moc.cpp"