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