]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCommandBuffer.C
* src/frontends/qt4/GuiWorkArea.[Ch]:
[lyx.git] / src / frontends / qt4 / QCommandBuffer.C
1 /**
2  * \file QCommandBuffer.C
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                 }
70                 QListWidget::keyPressEvent(ev);
71         }
72 };
73
74 } // end of anon
75
76
77 QCommandBuffer::QCommandBuffer(GuiView * view, ControlCommandBuffer & control)
78         : view_(view), controller_(control)
79 {
80         QPixmap qpup(toqstr(libFileSearch("images", "up", "xpm").absFilename()));
81         QPixmap qpdown(toqstr(libFileSearch("images", "down", "xpm").absFilename()));
82
83         QVBoxLayout * top = new QVBoxLayout(this);
84         QHBoxLayout * layout = new QHBoxLayout(0);
85
86         QPushButton * up = new QPushButton(qpup, "", this);
87         up->setToolTip(qt_("Previous command"));
88         connect(up, SIGNAL(clicked()), this, SLOT(up()));
89         QPushButton * down = new QPushButton(qpdown, "", this);
90         down->setToolTip(qt_("Next command"));
91         connect(down, SIGNAL(clicked()), this, SLOT(down()));
92
93         edit_ = new QCommandEdit(this);
94         edit_->setMinimumSize(edit_->sizeHint());
95         edit_->setFocusPolicy(Qt::ClickFocus);
96
97         connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
98         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
99         connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
100         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
101         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
102
103         layout->addWidget(up, 0);
104         layout->addWidget(down, 0);
105         layout->addWidget(edit_, 10);
106         layout->setMargin(0);
107         top->addLayout(layout);
108         top->setMargin(0);
109 }
110
111
112
113 void QCommandBuffer::focus_command()
114 {
115         edit_->setFocus();
116 }
117
118
119 void QCommandBuffer::cancel()
120 {
121         view_->setFocus();
122         edit_->setText(QString());
123 }
124
125
126 void QCommandBuffer::dispatch()
127 {
128         controller_.dispatch(fromqstr(edit_->text()));
129         view_->setFocus();
130         edit_->setText(QString());
131         edit_->clearFocus();
132 }
133
134
135 void QCommandBuffer::complete()
136 {
137         string const input = fromqstr(edit_->text());
138         string new_input;
139         vector<string> comp = controller_.completions(input, new_input);
140
141         if (comp.empty() && new_input == input) {
142                 // show_info_suffix(qt_("[no match]"), input);
143                 return;
144         }
145
146         if (comp.empty()) {
147                 edit_->setText(toqstr(new_input));
148         //      show_info_suffix(("[only completion]"), new_input + ' ');
149                 return;
150         }
151
152         edit_->setText(toqstr(new_input));
153
154         QTempListBox * list = new QTempListBox;
155
156         // For some reason the scrollview's contents are larger
157         // than the number of actual items...
158         vector<string>::const_iterator cit = comp.begin();
159         vector<string>::const_iterator end = comp.end();
160         for (; cit != end; ++cit)
161                 list->addItem(toqstr(*cit));
162
163         list->resize(list->sizeHint());
164         QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
165
166         int const y = std::max(0, pos.y() - list->height());
167
168         list->move(pos.x(), y);
169
170         connect(list, SIGNAL(itemPressed(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_->setFocus();
184 }
185
186
187 void QCommandBuffer::up()
188 {
189         string const input = fromqstr(edit_->text());
190         string const h = controller_.historyUp();
191
192         if (h.empty()) {
193         //      show_info_suffix(qt_("[Beginning of history]"), input);
194         } else {
195                 edit_->setText(toqstr(h));
196         }
197 }
198
199
200 void QCommandBuffer::down()
201 {
202         string const input = fromqstr(edit_->text());
203         string const h = controller_.historyDown();
204
205         if (h.empty()) {
206         //      show_info_suffix(qt_("[End of history]"), input);
207         } else {
208                 edit_->setText(toqstr(h));
209         }
210 }
211
212
213 #if 0
214 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
215 {
216         stored_input_ = input;
217         info_suffix_shown_ = true;
218         set_input(input + ' ' + suffix);
219         suffix_timer_->start();
220 }
221
222
223 void XMiniBuffer::suffix_timeout()
224 {
225         info_suffix_shown_ = false;
226         set_input(stored_input_);
227 }
228
229 #endif
230
231 } // namespace frontend
232 } // namespace lyx
233
234 #include "QCommandBuffer_moc.cpp"