2 * \file QCommandBuffer.cpp
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) {
69 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
71 itemPressed(currentItem());
73 QListWidget::keyPressEvent(ev);
80 QCommandBuffer::QCommandBuffer(GuiView * view, ControlCommandBuffer & control)
81 : view_(view), controller_(control)
83 QPixmap qpup(toqstr(libFileSearch("images", "up", "xpm").absFilename()));
84 QPixmap qpdown(toqstr(libFileSearch("images", "down", "xpm").absFilename()));
86 QVBoxLayout * top = new QVBoxLayout(this);
87 QHBoxLayout * layout = new QHBoxLayout(0);
89 QPushButton * up = new QPushButton(qpup, "", this);
90 up->setMaximumSize(24, 24);
91 up->setToolTip(qt_("Previous command"));
92 connect(up, SIGNAL(clicked()), this, SLOT(up()));
93 QPushButton * down = new QPushButton(qpdown, "", this);
94 down->setToolTip(qt_("Next command"));
95 down->setMaximumSize(24, 24);
96 connect(down, SIGNAL(clicked()), this, SLOT(down()));
98 edit_ = new QCommandEdit(this);
99 edit_->setMinimumSize(edit_->sizeHint());
100 edit_->setFocusPolicy(Qt::ClickFocus);
102 connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
103 connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
104 connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
105 connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
106 connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
107 connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
109 layout->addWidget(up, 0);
110 layout->addWidget(down, 0);
111 layout->addWidget(edit_, 10);
112 layout->setMargin(0);
113 top->addLayout(layout);
119 void QCommandBuffer::focus_command()
125 void QCommandBuffer::cancel()
128 edit_->setText(QString());
132 void QCommandBuffer::dispatch()
134 controller_.dispatch(fromqstr(edit_->text()));
136 edit_->setText(QString());
141 void QCommandBuffer::complete()
143 string const input = fromqstr(edit_->text());
145 vector<string> comp = controller_.completions(input, new_input);
147 if (comp.empty() && new_input == input) {
148 // show_info_suffix(qt_("[no match]"), input);
153 edit_->setText(toqstr(new_input));
154 // show_info_suffix(("[only completion]"), new_input + ' ');
158 edit_->setText(toqstr(new_input));
160 QTempListBox * list = new QTempListBox;
162 // For some reason the scrollview's contents are larger
163 // than the number of actual items...
164 vector<string>::const_iterator cit = comp.begin();
165 vector<string>::const_iterator end = comp.end();
166 for (; cit != end; ++cit)
167 list->addItem(toqstr(*cit));
169 list->resize(list->sizeHint());
170 QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
172 int const y = std::max(0, pos.y() - list->height());
174 list->move(pos.x(), y);
176 connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
177 this, SLOT(complete_selected(QListWidgetItem *)));
178 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
179 this, SLOT(complete_selected(QListWidgetItem *)));
186 void QCommandBuffer::complete_selected(QListWidgetItem * item)
188 QWidget const * widget = static_cast<QWidget const *>(sender());
189 const_cast<QWidget *>(widget)->hide();
190 edit_->setText(item->text() + ' ');
191 edit_->activateWindow();
196 void QCommandBuffer::up()
198 string const input = fromqstr(edit_->text());
199 string const h = controller_.historyUp();
202 // show_info_suffix(qt_("[Beginning of history]"), input);
204 edit_->setText(toqstr(h));
209 void QCommandBuffer::down()
211 string const input = fromqstr(edit_->text());
212 string const h = controller_.historyDown();
215 // show_info_suffix(qt_("[End of history]"), input);
217 edit_->setText(toqstr(h));
222 void QCommandBuffer::hideParent()
225 edit_->setText(QString());
232 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
234 stored_input_ = input;
235 info_suffix_shown_ = true;
236 set_input(input + ' ' + suffix);
237 suffix_timer_->start();
241 void XMiniBuffer::suffix_timeout()
243 info_suffix_shown_ = false;
244 set_input(stored_input_);
249 } // namespace frontend
252 #include "QCommandBuffer_moc.cpp"