]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCommandBuffer.cpp
* src/frontends/qt4/ui/TextLayoutUi.ui:
[lyx.git] / src / frontends / qt4 / QCommandBuffer.cpp
1 /**
2  * \file QCommandBuffer.cpp
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         connect(edit_, SIGNAL(hidePressed()), this, SLOT(hideParent()));
105
106         layout->addWidget(up, 0);
107         layout->addWidget(down, 0);
108         layout->addWidget(edit_, 10);
109         layout->setMargin(0);
110         top->addLayout(layout);
111         top->setMargin(0);
112 }
113
114
115
116 void QCommandBuffer::focus_command()
117 {
118         edit_->setFocus();
119 }
120
121
122 void QCommandBuffer::cancel()
123 {
124         view_->setFocus();
125         edit_->setText(QString());
126 }
127
128
129 void QCommandBuffer::dispatch()
130 {
131         controller_.dispatch(fromqstr(edit_->text()));
132         view_->setFocus();
133         edit_->setText(QString());
134         edit_->clearFocus();
135 }
136
137
138 void QCommandBuffer::complete()
139 {
140         string const input = fromqstr(edit_->text());
141         string new_input;
142         vector<string> comp = controller_.completions(input, new_input);
143
144         if (comp.empty() && new_input == input) {
145                 // show_info_suffix(qt_("[no match]"), input);
146                 return;
147         }
148
149         if (comp.empty()) {
150                 edit_->setText(toqstr(new_input));
151         //      show_info_suffix(("[only completion]"), new_input + ' ');
152                 return;
153         }
154
155         edit_->setText(toqstr(new_input));
156
157         QTempListBox * list = new QTempListBox;
158
159         // For some reason the scrollview's contents are larger
160         // than the number of actual items...
161         vector<string>::const_iterator cit = comp.begin();
162         vector<string>::const_iterator end = comp.end();
163         for (; cit != end; ++cit)
164                 list->addItem(toqstr(*cit));
165
166         list->resize(list->sizeHint());
167         QPoint const pos = edit_->mapToGlobal(QPoint(0, 0));
168
169         int const y = std::max(0, pos.y() - list->height());
170
171         list->move(pos.x(), y);
172
173         connect(list, SIGNAL(itemPressed(QListWidgetItem *)),
174                 this, SLOT(complete_selected(QListWidgetItem *)));
175         connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
176                 this, SLOT(complete_selected(QListWidgetItem *)));
177
178         list->show();
179         list->setFocus();
180 }
181
182
183 void QCommandBuffer::complete_selected(QListWidgetItem * item)
184 {
185         QWidget const * widget = static_cast<QWidget const *>(sender());
186         const_cast<QWidget *>(widget)->hide();
187         edit_->setText(item->text() + ' ');
188         edit_->setFocus();
189 }
190
191
192 void QCommandBuffer::up()
193 {
194         string const input = fromqstr(edit_->text());
195         string const h = controller_.historyUp();
196
197         if (h.empty()) {
198         //      show_info_suffix(qt_("[Beginning of history]"), input);
199         } else {
200                 edit_->setText(toqstr(h));
201         }
202 }
203
204
205 void QCommandBuffer::down()
206 {
207         string const input = fromqstr(edit_->text());
208         string const h = controller_.historyDown();
209
210         if (h.empty()) {
211         //      show_info_suffix(qt_("[End of history]"), input);
212         } else {
213                 edit_->setText(toqstr(h));
214         }
215 }
216
217
218 void QCommandBuffer::hideParent()
219 {
220         view_->setFocus();
221         edit_->setText(QString());
222         edit_->clearFocus();
223         controller_.hide();
224 }
225
226
227 #if 0
228 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
229 {
230         stored_input_ = input;
231         info_suffix_shown_ = true;
232         set_input(input + ' ' + suffix);
233         suffix_timer_->start();
234 }
235
236
237 void XMiniBuffer::suffix_timeout()
238 {
239         info_suffix_shown_ = false;
240         set_input(stored_input_);
241 }
242
243 #endif
244
245 } // namespace frontend
246 } // namespace lyx
247
248 #include "QCommandBuffer_moc.cpp"