]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QCommandBuffer.C
enable Font cache only for MacOSX and inline width() for other platform.
[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 //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 signal
62                         itemPressed(currentItem());
63                 }
64         }
65
66         void keyPressEvent(QKeyEvent * e) {
67                 if (e->key() == Qt::Key_Escape) {
68                         hide();
69                         return;
70                 }
71                 QListWidget::keyPressEvent(e);
72         }
73 };
74
75 } // end of anon
76
77
78 QCommandBuffer::QCommandBuffer(GuiView * view, ControlCommandBuffer & control, 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         top->addLayout(layout);
108 }
109
110
111
112 void QCommandBuffer::focus_command()
113 {
114         edit_->setFocus();
115 }
116
117
118 void QCommandBuffer::cancel()
119 {
120         view_->centralWidget()->setFocus();
121         edit_->setText("");
122 }
123
124
125 void QCommandBuffer::dispatch()
126 {
127         controller_.dispatch(fromqstr(edit_->text()));
128         view_->centralWidget()->setFocus();
129         edit_->setText("");
130         edit_->clearFocus();
131 }
132
133
134 void QCommandBuffer::complete()
135 {
136         string const input = fromqstr(edit_->text());
137         string new_input;
138         vector<string> comp = controller_.completions(input, new_input);
139
140         if (comp.empty() && new_input == input) {
141         //      show_info_suffix(qt_("[no match]"), input);
142                 return;
143         }
144
145         if (comp.empty()) {
146                 edit_->setText(toqstr(new_input));
147         //      show_info_suffix(("[only completion]"), new_input + ' ');
148                 return;
149         }
150
151         edit_->setText(toqstr(new_input));
152
153         QTempListBox * list = new QTempListBox;
154
155         // For some reason the scrollview's contents are larger
156         // than the number of actual items...
157         vector<string>::const_iterator cit = comp.begin();
158         vector<string>::const_iterator end = comp.end();
159         for (; cit != end; ++cit) {
160                 list->addItem(toqstr(*cit));
161         }
162
163         // width() is not big enough by a few pixels. Qt Sucks.
164 //      list->setMinimumWidth(list->sizeHint().width() + 10);
165
166         list->resize(list->sizeHint());
167         QPoint pos(edit_->mapToGlobal(QPoint(0, 0)));
168
169         int 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
176         list->show();
177         list->setFocus();
178 }
179
180
181 void QCommandBuffer::complete_selected(QListWidgetItem * item)
182 {
183         QWidget const * widget = static_cast<QWidget const *>(sender());
184         const_cast<QWidget *>(widget)->hide();
185         edit_->setText(item->text() + ' ');
186         edit_->setFocus();
187 }
188
189
190 void QCommandBuffer::up()
191 {
192         string const input(fromqstr(edit_->text()));
193         string const h(controller_.historyUp());
194
195         if (h.empty()) {
196         //      show_info_suffix(qt_("[Beginning of history]"), input);
197         } else {
198                 edit_->setText(toqstr(h));
199         }
200 }
201
202
203 void QCommandBuffer::down()
204 {
205         string const input(fromqstr(edit_->text()));
206         string const h(controller_.historyDown());
207
208         if (h.empty()) {
209         //      show_info_suffix(qt_("[End of history]"), input);
210         } else {
211                 edit_->setText(toqstr(h));
212         }
213 }
214
215
216 #if 0
217 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
218 {
219         stored_input_ = input;
220         info_suffix_shown_ = true;
221         set_input(input + ' ' + suffix);
222         suffix_timer_->start();
223 }
224
225
226 void XMiniBuffer::suffix_timeout()
227 {
228         info_suffix_shown_ = false;
229         set_input(stored_input_);
230 }
231
232 #endif
233
234 } // namespace frontend
235 } // namespace lyx
236
237 #include "QCommandBuffer_moc.cpp"