]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QCommandBuffer.C
Some Qt graphics dialog improvements. Yet more work needed :/
[lyx.git] / src / frontends / qt2 / QCommandBuffer.C
1 /**
2  * \file QCommandBuffer.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon <moz@compsoc.man.ac.uk>
7  */
8
9 #include <config.h>
10  
11 #include "support/filetools.h"
12 #include "controllers/ControlCommandBuffer.h"
13 #include "gettext.h"
14  
15 #include "QtView.h"
16 #include "QCommandBuffer.h"
17 #include "QCommandEdit.h"
18  
19 #include <qcombobox.h>
20 #include <qtoolbutton.h>
21 #include <qpixmap.h>
22
23 using std::vector;
24
25 namespace {
26  
27 class QTempComboBox : public QComboBox {
28 public:
29         QTempComboBox(QWidget * parent) : QComboBox(parent) {
30                 setWFlags(WDestructiveClose);
31         }
32
33         void popup() { QComboBox::popup(); }
34 };
35
36 }
37  
38 QCommandBuffer::QCommandBuffer(QtView * view, ControlCommandBuffer & control)
39         : QToolBar(view), view_(view), controller_(control)
40 {
41         setHorizontalStretchable(true);
42  
43         QPixmap qp(LibFileSearch("images", "unknown", "xpm").c_str());
44
45         QToolButton * upb = new QToolButton(qp, _("Up"), "", this, SLOT(up()), this);
46         upb->show();
47  
48         QToolButton * downb = new QToolButton(qp, _("Down"), "", this, SLOT(down()), this);
49         downb->show();
50  
51         edit_ = new QCommandEdit(this);
52         edit_->setMinimumSize(edit_->sizeHint());
53         edit_->show();
54         setStretchableWidget(edit_);
55  
56         show();
57  
58         connect(edit_, SIGNAL(escapePressed()), this, SLOT(cancel()));
59         connect(edit_, SIGNAL(returnPressed()), this, SLOT(dispatch()));
60         connect(edit_, SIGNAL(rightPressed()), this, SLOT(complete()));
61         connect(edit_, SIGNAL(upPressed()), this, SLOT(up()));
62         connect(edit_, SIGNAL(downPressed()), this, SLOT(down()));
63 }
64
65
66
67 void QCommandBuffer::focus_command()
68 {
69         edit_->setFocus();
70 }
71
72  
73 void QCommandBuffer::cancel()
74 {
75         view_->centralWidget()->setFocus();
76         edit_->setText("");
77 }
78
79  
80 void QCommandBuffer::dispatch()
81 {
82         controller_.dispatch(edit_->text().latin1());
83         view_->centralWidget()->setFocus();
84         edit_->setText("");
85 }
86
87
88 void QCommandBuffer::complete()
89 {
90         string const input = edit_->text().latin1();
91         string new_input;
92         vector<string> comp = controller_.completions(input, new_input);
93                          
94         if (comp.empty() && new_input == input) {
95         //      show_info_suffix(_("[no match]"), input);
96                 return;
97         }
98
99         if (comp.empty()) {
100                 edit_->setText(new_input.c_str());
101         //      show_info_suffix(("[only completion]"), new_input + " ");
102                 return;
103         }
104                                  
105         edit_->setText(new_input.c_str());
106
107         QTempComboBox * combo = new QTempComboBox(view_);
108         combo->move(edit_->x() + x(), edit_->y() + y());
109
110         vector<string>::const_iterator cit = comp.begin();
111         vector<string>::const_iterator end = comp.end();
112         for (; cit != end; ++cit) {
113                 combo->insertItem(cit->c_str());
114         }
115
116         combo->setMinimumWidth(combo->sizeHint().width());
117         combo->resize(combo->width(), edit_->height());
118  
119         connect(combo, SIGNAL(activated(const QString &)),
120                 this, SLOT(complete_selected(const QString &))); 
121  
122         combo->show();
123         combo->setFocus();
124         combo->popup();
125 }
126
127
128 void QCommandBuffer::complete_selected(const QString & str)
129 {
130         edit_->setText(str + " ");
131         // FIXME
132         QWidget const * widget = static_cast<QWidget const *>(sender());
133         edit_->setFocus();
134         const_cast<QWidget *>(widget)->hide();
135 }
136
137  
138 void QCommandBuffer::up()
139 {
140         string const input = edit_->text().latin1();
141         string const h(controller_.historyUp());
142  
143         if (h.empty()) {
144         //      show_info_suffix(_("[Beginning of history]"), input);
145         } else {
146                 edit_->setText(h.c_str());
147         }
148 }
149
150
151 void QCommandBuffer::down()
152 {
153         string const input = edit_->text().latin1();
154         string const h(controller_.historyDown());
155  
156         if (h.empty()) {
157         //      show_info_suffix(_("[End of history]"), input);
158         } else {
159                 edit_->setText(h.c_str());
160         }
161 }
162
163
164 #if 0 
165 void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
166 {
167         stored_input_ = input;
168         info_suffix_shown_ = true;
169         set_input(input + " " + suffix); 
170         suffix_timer_->start();
171 }
172  
173
174 void XMiniBuffer::suffix_timeout()
175 {
176         info_suffix_shown_ = false;
177         set_input(stored_input_);
178 }
179
180 #endif