]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCommandBuffer.C
renaming in frontends/qt4/ui: s/Q//g
[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->setMaximumSize(24, 24);
88         up->setToolTip(qt_("Previous command"));
89         connect(up, SIGNAL(clicked()), this, SLOT(up()));
90         QPushButton * down = new QPushButton(qpdown, "", this);
91         down->setToolTip(qt_("Next command"));
92         down->setMaximumSize(24, 24);
93         connect(down, SIGNAL(clicked()), this, SLOT(down()));
94
95         edit_ = new QCommandEdit(this);
96         edit_->setMinimumSize(edit_->sizeHint());
97         edit_->setFocusPolicy(Qt::ClickFocus);
98
99         connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
100         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
101         connect(edit_, SIGNAL(tabPressed()), this, SLOT(complete()));
102         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
103         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
104
105         layout->addWidget(up, 0);
106         layout->addWidget(down, 0);
107         layout->addWidget(edit_, 10);
108         layout->setMargin(0);
109         top->addLayout(layout);
110         top->setMargin(0);
111 }
112
113
114
115 void QCommandBuffer::focus_command()
116 {
117         edit_->setFocus();
118 }
119
120
121 void QCommandBuffer::cancel()
122 {
123         view_->setFocus();
124         edit_->setText(QString());
125 }
126
127
128 void QCommandBuffer::dispatch()
129 {
130         controller_.dispatch(fromqstr(edit_->text()));
131         view_->setFocus();
132         edit_->setText(QString());
133         edit_->clearFocus();
134 }
135
136
137 void QCommandBuffer::complete()
138 {
139         string const input = fromqstr(edit_->text());
140         string new_input;
141         vector<string> comp = controller_.completions(input, new_input);
142
143         if (comp.empty() && new_input == input) {
144                 // show_info_suffix(qt_("[no match]"), input);
145                 return;
146         }
147
148         if (comp.empty()) {
149                 edit_->setText(toqstr(new_input));
150         //      show_info_suffix(("[only completion]"), new_input + ' ');
151                 return;
152         }
153
154         edit_->setText(toqstr(new_input));
155
156         QTempListBox * list = new QTempListBox;
157
158         // For some reason the scrollview's contents are larger
159         // than the number of actual items...
160         vector<string>::const_iterator cit = comp.begin();
161         vector<string>::const_iterator end = comp.end();
162         for (; cit != end; ++cit)
163                 list->addItem(toqstr(*cit));
164
165         list->resize(list->sizeHint());
166         QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
167
168         int const 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         connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
175                 this, SLOT(complete_selected(QListWidgetItem *)));
176
177         list->show();
178         list->setFocus();
179 }
180
181
182 void QCommandBuffer::complete_selected(QListWidgetItem * item)
183 {
184         QWidget const * widget = static_cast<QWidget const *>(sender());
185         const_cast<QWidget *>(widget)->hide();
186         edit_->setText(item->text() + ' ');
187         edit_->setFocus();
188 }
189
190
191 void QCommandBuffer::up()
192 {
193         string const input = fromqstr(edit_->text());
194         string const h = controller_.historyUp();
195
196         if (h.empty()) {
197         //      show_info_suffix(qt_("[Beginning of history]"), input);
198         } else {
199                 edit_->setText(toqstr(h));
200         }
201 }
202
203
204 void QCommandBuffer::down()
205 {
206         string const input = fromqstr(edit_->text());
207         string const h = controller_.historyDown();
208
209         if (h.empty()) {
210         //      show_info_suffix(qt_("[End of history]"), input);
211         } else {
212                 edit_->setText(toqstr(h));
213         }
214 }
215
216
217 #if 0
218 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
219 {
220         stored_input_ = input;
221         info_suffix_shown_ = true;
222         set_input(input + ' ' + suffix);
223         suffix_timer_->start();
224 }
225
226
227 void XMiniBuffer::suffix_timeout()
228 {
229         info_suffix_shown_ = false;
230         set_input(stored_input_);
231 }
232
233 #endif
234
235 } // namespace frontend
236 } // namespace lyx
237
238 #include "QCommandBuffer_moc.cpp"