]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCommandBuffer.cpp
delete unneeded Menubar virtual interface.
[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 "controllers/ControlCommandBuffer.h"
22
23 #include "support/filetools.h"
24
25 #include <QHBoxLayout>
26 #include <QKeyEvent>
27 #include <QLayout>
28 #include <QListWidget>
29 #include <QMouseEvent>
30 #include <QPixmap>
31 #include <QPushButton>
32 #include <QToolTip>
33 #include <QVBoxLayout>
34
35 using lyx::support::libFileSearch;
36
37 using std::vector;
38 using std::string;
39
40 namespace lyx {
41 namespace frontend {
42
43 namespace {
44
45 class QTempListBox : public QListWidget {
46 public:
47         QTempListBox() {
48                 //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
49                 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50                 setWindowModality(Qt::WindowModal);
51                 setWindowFlags(Qt::Popup);
52                 setAttribute(Qt::WA_DeleteOnClose);
53         }
54 protected:
55         void mouseReleaseEvent(QMouseEvent * ev) {
56                 if (ev->x() < 0 || ev->y() < 0
57                     || ev->x() > width() || ev->y() > height()) {
58                         hide();
59                 } else {
60                         // emit signal
61                         itemPressed(currentItem());
62                 }
63         }
64
65         void keyPressEvent(QKeyEvent * ev) {
66                 if (ev->key() == Qt::Key_Escape) {
67                         hide();
68                         return;
69                 } else if (ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Space) {
70                         // emit signal
71                         itemPressed(currentItem());
72                 } else
73                         QListWidget::keyPressEvent(ev);
74         }
75 };
76
77 } // end of anon
78
79
80 QCommandBuffer::QCommandBuffer(GuiView * view, ControlCommandBuffer & control)
81         : view_(view), controller_(control)
82 {
83         QPixmap qpup(toqstr(libFileSearch("images", "up", "xpm").absFilename()));
84         QPixmap qpdown(toqstr(libFileSearch("images", "down", "xpm").absFilename()));
85
86         QVBoxLayout * top = new QVBoxLayout(this);
87         QHBoxLayout * layout = new QHBoxLayout(0);
88
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()));
97
98         edit_ = new QCommandEdit(this);
99         edit_->setMinimumSize(edit_->sizeHint());
100         edit_->setFocusPolicy(Qt::ClickFocus);
101
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()));
108
109         layout->addWidget(up, 0);
110         layout->addWidget(down, 0);
111         layout->addWidget(edit_, 10);
112         layout->setMargin(0);
113         top->addLayout(layout);
114         top->setMargin(0);
115 }
116
117
118
119 void QCommandBuffer::focus_command()
120 {
121         edit_->setFocus();
122 }
123
124
125 void QCommandBuffer::cancel()
126 {
127         view_->setFocus();
128         edit_->setText(QString());
129 }
130
131
132 void QCommandBuffer::dispatch()
133 {
134         controller_.dispatch(fromqstr(edit_->text()));
135         view_->setFocus();
136         edit_->setText(QString());
137         edit_->clearFocus();
138 }
139
140
141 void QCommandBuffer::complete()
142 {
143         string const input = fromqstr(edit_->text());
144         string new_input;
145         vector<string> comp = controller_.completions(input, new_input);
146
147         if (comp.empty() && new_input == input) {
148                 // show_info_suffix(qt_("[no match]"), input);
149                 return;
150         }
151
152         if (comp.empty()) {
153                 edit_->setText(toqstr(new_input));
154         //      show_info_suffix(("[only completion]"), new_input + ' ');
155                 return;
156         }
157
158         edit_->setText(toqstr(new_input));
159
160         QTempListBox * list = new QTempListBox;
161
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));
168
169         list->resize(list->sizeHint());
170         QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
171
172         int const y = std::max(0, pos.y() - list->height());
173
174         list->move(pos.x(), y);
175
176         connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
177                 this, SLOT(complete_selected(QListWidgetItem *)));
178         connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
179                 this, SLOT(complete_selected(QListWidgetItem *)));
180
181         list->show();
182         list->setFocus();
183 }
184
185
186 void QCommandBuffer::complete_selected(QListWidgetItem * item)
187 {
188         QWidget const * widget = static_cast<QWidget const *>(sender());
189         const_cast<QWidget *>(widget)->hide();
190         edit_->setText(item->text() + ' ');
191         edit_->activateWindow();
192         edit_->setFocus();
193 }
194
195
196 void QCommandBuffer::up()
197 {
198         string const input = fromqstr(edit_->text());
199         string const h = controller_.historyUp();
200
201         if (h.empty()) {
202         //      show_info_suffix(qt_("[Beginning of history]"), input);
203         } else {
204                 edit_->setText(toqstr(h));
205         }
206 }
207
208
209 void QCommandBuffer::down()
210 {
211         string const input = fromqstr(edit_->text());
212         string const h = controller_.historyDown();
213
214         if (h.empty()) {
215         //      show_info_suffix(qt_("[End of history]"), input);
216         } else {
217                 edit_->setText(toqstr(h));
218         }
219 }
220
221
222 void QCommandBuffer::hideParent()
223 {
224         view_->setFocus();
225         edit_->setText(QString());
226         edit_->clearFocus();
227         controller_.hide();
228 }
229
230
231 #if 0
232 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
233 {
234         stored_input_ = input;
235         info_suffix_shown_ = true;
236         set_input(input + ' ' + suffix);
237         suffix_timer_->start();
238 }
239
240
241 void XMiniBuffer::suffix_timeout()
242 {
243         info_suffix_shown_ = false;
244         set_input(stored_input_);
245 }
246
247 #endif
248
249 } // namespace frontend
250 } // namespace lyx
251
252 #include "QCommandBuffer_moc.cpp"