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