]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/QCommandBuffer.C
Extracted from r14281
[lyx.git] / src / frontends / qt3 / 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 "QtView.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 <qlistbox.h>
26 #include <qlayout.h>
27 #include <qtooltip.h>
28 #include <qpushbutton.h>
29
30 using lyx::support::libFileSearch;
31
32 using std::vector;
33 using std::string;
34
35 namespace lyx {
36 namespace frontend {
37
38 namespace {
39
40 class QTempListBox : public QListBox {
41 public:
42         QTempListBox()
43                 : QListBox(0, 0,
44                            WType_Modal | WType_Popup | WDestructiveClose) {
45                 setHScrollBarMode(AlwaysOff);
46         }
47 protected:
48         void mouseReleaseEvent(QMouseEvent * e) {
49                 if (e->x() < 0 || e->y() < 0
50                     || e->x() > width() || e->y() > height()) {
51                         hide();
52                 } else {
53                         // emit signal
54                         selected(currentText());
55                 }
56         }
57
58         void keyPressEvent(QKeyEvent * e) {
59                 if (e->key() == Key_Escape) {
60                         hide();
61                         return;
62                 }
63                 QListBox::keyPressEvent(e);
64         }
65 };
66
67 } // end of anon
68
69
70 QCommandBuffer::QCommandBuffer(QtView * view, QWidget * parent, ControlCommandBuffer & control)
71         : QWidget(parent), view_(view), controller_(control)
72 {
73         QPixmap qpup(toqstr(libFileSearch("images", "up", "xpm")));
74         QPixmap qpdown(toqstr(libFileSearch("images", "down", "xpm")));
75
76         QVBoxLayout * top = new QVBoxLayout(this);
77         QHBoxLayout * layout = new QHBoxLayout(0);
78
79         QPushButton * up = new QPushButton(qpup, "", this);
80         QToolTip::add(up, qt_("Previous command"));
81         connect(up, SIGNAL(clicked()), this, SLOT(up()));
82         QPushButton * down = new QPushButton(qpdown, "", this);
83         QToolTip::add(down, qt_("Next command"));
84         connect(down, SIGNAL(clicked()), this, SLOT(down()));
85
86         edit_ = new QCommandEdit(this);
87         edit_->setMinimumSize(edit_->sizeHint());
88         edit_->setFocusPolicy(ClickFocus);
89
90         connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
91         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
92         connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
93         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
94         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
95
96         layout->addWidget(up, 0);
97         layout->addWidget(down, 0);
98         layout->addWidget(edit_, 10);
99         top->addLayout(layout);
100 }
101
102
103
104 void QCommandBuffer::focus_command()
105 {
106         edit_->setFocus();
107 }
108
109
110 void QCommandBuffer::cancel()
111 {
112         view_->centralWidget()->setFocus();
113         edit_->setText("");
114 }
115
116
117 void QCommandBuffer::dispatch()
118 {
119         controller_.dispatch(fromqstr(edit_->text()));
120         view_->centralWidget()->setFocus();
121         edit_->setText("");
122         edit_->clearFocus();
123 }
124
125
126 void QCommandBuffer::complete()
127 {
128         string const input = fromqstr(edit_->text());
129         string new_input;
130         vector<string> comp = controller_.completions(input, new_input);
131
132         if (comp.empty() && new_input == input) {
133         //      show_info_suffix(qt_("[no match]"), input);
134                 return;
135         }
136
137         if (comp.empty()) {
138                 edit_->setText(toqstr(new_input));
139         //      show_info_suffix(("[only completion]"), new_input + ' ');
140                 return;
141         }
142
143         edit_->setText(toqstr(new_input));
144
145         QTempListBox * list = new QTempListBox;
146
147         // For some reason the scrollview's contents are larger
148         // than the number of actual items...
149         vector<string>::const_iterator cit = comp.begin();
150         vector<string>::const_iterator end = comp.end();
151         for (; cit != end; ++cit) {
152                 list->insertItem(toqstr(*cit));
153         }
154
155         // width() is not big enough by a few pixels. Qt Sucks.
156         list->setMinimumWidth(list->sizeHint().width() + 10);
157
158         list->resize(list->sizeHint());
159         QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
160
161         int y = std::max(0, pos.y() - list->height());
162
163         list->move(pos.x(), y);
164
165         connect(list, SIGNAL(selected(const QString &)),
166                 this, SLOT(complete_selected(const QString &)));
167
168         list->show();
169         list->setFocus();
170 }
171
172
173 void QCommandBuffer::complete_selected(QString const & str)
174 {
175         QWidget const * widget = static_cast<QWidget const *>(sender());
176         const_cast<QWidget *>(widget)->hide();
177         edit_->setText(str + ' ');
178         edit_->setFocus();
179 }
180
181
182 void QCommandBuffer::up()
183 {
184         string const input(fromqstr(edit_->text()));
185         string const h(controller_.historyUp());
186
187         if (h.empty()) {
188         //      show_info_suffix(qt_("[Beginning of history]"), input);
189         } else {
190                 edit_->setText(toqstr(h));
191         }
192 }
193
194
195 void QCommandBuffer::down()
196 {
197         string const input(fromqstr(edit_->text()));
198         string const h(controller_.historyDown());
199
200         if (h.empty()) {
201         //      show_info_suffix(qt_("[End of history]"), input);
202         } else {
203                 edit_->setText(toqstr(h));
204         }
205 }
206
207
208 #if 0
209 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
210 {
211         stored_input_ = input;
212         info_suffix_shown_ = true;
213         set_input(input + ' ' + suffix);
214         suffix_timer_->start();
215 }
216
217
218 void XMiniBuffer::suffix_timeout()
219 {
220         info_suffix_shown_ = false;
221         set_input(stored_input_);
222 }
223
224 #endif
225
226 } // namespace frontend
227 } // namespace lyx