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