]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCommandBuffer.cpp
9789b40bb29ea73a34e3a3301778453ede433bf7
[lyx.git] / src / frontends / qt4 / QCommandBuffer.cpp
1 /**
2  * \file QCommandBuffer.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 // Qt defines a macro 'signals' that clashes with a boost namespace.
14 // All is well if the namespace is visible first.
15 #include "GuiView.h"
16
17 #include "QCommandBuffer.h"
18 #include "QCommandEdit.h"
19 #include "qt_helpers.h"
20
21 #include "support/filetools.h"
22
23 #include <QHBoxLayout>
24 #include <QKeyEvent>
25 #include <QLayout>
26 #include <QListWidget>
27 #include <QMouseEvent>
28 #include <QPixmap>
29 #include <QPushButton>
30 #include <QToolTip>
31 #include <QVBoxLayout>
32
33 using lyx::support::libFileSearch;
34
35 using std::vector;
36 using std::string;
37
38 namespace lyx {
39 namespace frontend {
40
41 namespace {
42
43 class QTempListBox : public QListWidget {
44 public:
45         QTempListBox() {
46                 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
47                 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
48                 setWindowModality(Qt::WindowModal);
49                 setWindowFlags(Qt::Popup);
50                 setAttribute(Qt::WA_DeleteOnClose);
51         }
52 protected:
53         void mouseReleaseEvent(QMouseEvent * ev) {
54                 if (ev->x() < 0 || ev->y() < 0
55                     || ev->x() > width() || ev->y() > height()) {
56                         hide();
57                 } else {
58                         // emit signal
59                         itemPressed(currentItem());
60                 }
61         }
62
63         void keyPressEvent(QKeyEvent * ev) {
64                 if (ev->key() == Qt::Key_Escape) {
65                         hide();
66                         return;
67                 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
68                         // emit signal
69                         itemPressed(currentItem());
70                 } else
71                         QListWidget::keyPressEvent(ev);
72         }
73 };
74
75 } // end of anon
76
77
78 QCommandBuffer::QCommandBuffer(GuiView * view)
79         : view_(view), controller_(*view)
80 {
81         QPixmap qpup(toqstr(libFileSearch("images", "up", "xpm").absFilename()));
82         QPixmap qpdown(toqstr(libFileSearch("images", "down", "xpm").absFilename()));
83
84         QVBoxLayout * top = new QVBoxLayout(this);
85         QHBoxLayout * layout = new QHBoxLayout(0);
86
87         QPushButton * up = new QPushButton(qpup, "", this);
88         up->setMaximumSize(24, 24);
89         up->setToolTip(qt_("Previous command"));
90         connect(up, SIGNAL(clicked()), this, SLOT(up()));
91         QPushButton * down = new QPushButton(qpdown, "", this);
92         down->setToolTip(qt_("Next command"));
93         down->setMaximumSize(24, 24);
94         connect(down, SIGNAL(clicked()), this, SLOT(down()));
95
96         edit_ = new QCommandEdit(this);
97         edit_->setMinimumSize(edit_->sizeHint());
98         edit_->setFocusPolicy(Qt::ClickFocus);
99
100         connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
101         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
102         connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
103         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
104         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
105         connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
106
107         layout->addWidget(up, 0);
108         layout->addWidget(down, 0);
109         layout->addWidget(edit_, 10);
110         layout->setMargin(0);
111         top->addLayout(layout);
112         top->setMargin(0);
113         setFocusProxy(edit_);
114 }
115
116
117 void QCommandBuffer::cancel()
118 {
119         view_->setFocus();
120         edit_->setText(QString());
121 }
122
123
124 void QCommandBuffer::dispatch()
125 {
126         controller_.dispatch(fromqstr(edit_->text()));
127         view_->setFocus();
128         edit_->setText(QString());
129         edit_->clearFocus();
130 }
131
132
133 void QCommandBuffer::complete()
134 {
135         string const input = fromqstr(edit_->text());
136         string new_input;
137         vector<string> comp = controller_.completions(input, new_input);
138
139         if (comp.empty() && new_input == input) {
140                 // show_info_suffix(qt_("[no match]"), input);
141                 return;
142         }
143
144         if (comp.empty()) {
145                 edit_->setText(toqstr(new_input));
146         //      show_info_suffix(("[only completion]"), new_input + ' ');
147                 return;
148         }
149
150         edit_->setText(toqstr(new_input));
151
152         QTempListBox * list = new QTempListBox;
153
154         // For some reason the scrollview's contents are larger
155         // than the number of actual items...
156         vector<string>::const_iterator cit = comp.begin();
157         vector<string>::const_iterator end = comp.end();
158         for (; cit != end; ++cit)
159                 list->addItem(toqstr(*cit));
160
161         list->resize(list->sizeHint());
162         QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
163
164         int const y = std::max(0, pos.y() - list->height());
165
166         list->move(pos.x(), y);
167
168         connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
169                 this, SLOT(complete_selected(QListWidgetItem *)));
170         connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
171                 this, SLOT(complete_selected(QListWidgetItem *)));
172
173         list->show();
174         list->setFocus();
175 }
176
177
178 void QCommandBuffer::complete_selected(QListWidgetItem * item)
179 {
180         QWidget const * widget = static_cast<QWidget const *>(sender());
181         const_cast<QWidget *>(widget)->hide();
182         edit_->setText(item->text() + ' ');
183         edit_->activateWindow();
184         edit_->setFocus();
185 }
186
187
188 void QCommandBuffer::up()
189 {
190         string const input = fromqstr(edit_->text());
191         string const h = controller_.historyUp();
192
193         if (h.empty()) {
194         //      show_info_suffix(qt_("[Beginning of history]"), input);
195         } else {
196                 edit_->setText(toqstr(h));
197         }
198 }
199
200
201 void QCommandBuffer::down()
202 {
203         string const input = fromqstr(edit_->text());
204         string const h = controller_.historyDown();
205
206         if (h.empty()) {
207         //      show_info_suffix(qt_("[End of history]"), input);
208         } else {
209                 edit_->setText(toqstr(h));
210         }
211 }
212
213
214 void QCommandBuffer::hideParent()
215 {
216         view_->setFocus();
217         edit_->setText(QString());
218         edit_->clearFocus();
219         controller_.hide();
220 }
221
222
223 #if 0
224 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
225 {
226         stored_input_ = input;
227         info_suffix_shown_ = true;
228         set_input(input + ' ' + suffix);
229         suffix_timer_->start();
230 }
231
232
233 void XMiniBuffer::suffix_timeout()
234 {
235         info_suffix_shown_ = false;
236         set_input(stored_input_);
237 }
238
239 #endif
240
241 } // namespace frontend
242 } // namespace lyx
243
244 #include "QCommandBuffer_moc.cpp"