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) {
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()));
104 connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
106 layout->addWidget(up, 0);
107 layout->addWidget(down, 0);
108 layout->addWidget(edit_, 10);
109 layout->setMargin(0);
110 top->addLayout(layout);
116 void QCommandBuffer::focus_command()
122 void QCommandBuffer::cancel()
125 edit_->setText(QString());
129 void QCommandBuffer::dispatch()
131 controller_.dispatch(fromqstr(edit_->text()));
133 edit_->setText(QString());
138 void QCommandBuffer::complete()
140 string const input = fromqstr(edit_->text());
142 vector<string> comp = controller_.completions(input, new_input);
144 if (comp.empty() && new_input == input) {
145 // show_info_suffix(qt_("[no match]"), input);
150 edit_->setText(toqstr(new_input));
151 // show_info_suffix(("[only completion]"), new_input + ' ');
155 edit_->setText(toqstr(new_input));
157 QTempListBox * list = new QTempListBox;
159 // For some reason the scrollview's contents are larger
160 // than the number of actual items...
161 vector<string>::const_iterator cit = comp.begin();
162 vector<string>::const_iterator end = comp.end();
163 for (; cit != end; ++cit)
164 list->addItem(toqstr(*cit));
166 list->resize(list->sizeHint());
167 QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
169 int const 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 *)));
175 connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
176 this, SLOT(complete_selected(QListWidgetItem *)));
183 void QCommandBuffer::complete_selected(QListWidgetItem * item)
185 QWidget const * widget = static_cast<QWidget const *>(sender());
186 const_cast<QWidget *>(widget)->hide();
187 edit_->setText(item->text() + ' ');
192 void QCommandBuffer::up()
194 string const input = fromqstr(edit_->text());
195 string const h = controller_.historyUp();
198 // show_info_suffix(qt_("[Beginning of history]"), input);
200 edit_->setText(toqstr(h));
205 void QCommandBuffer::down()
207 string const input = fromqstr(edit_->text());
208 string const h = controller_.historyDown();
211 // show_info_suffix(qt_("[End of history]"), input);
213 edit_->setText(toqstr(h));
218 void QCommandBuffer::hideParent()
221 edit_->setText(QString());
228 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
230 stored_input_ = input;
231 info_suffix_shown_ = true;
232 set_input(input + ' ' + suffix);
233 suffix_timer_->start();
237 void XMiniBuffer::suffix_timeout()
239 info_suffix_shown_ = false;
240 set_input(stored_input_);
245 } // namespace frontend
248 #include "QCommandBuffer_moc.cpp"