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