1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/***************************************************************************
kmymoneycompletion.cpp - description
-------------------
begin : Mon Apr 26 2004
copyright : (C) 2000-2004 by Michael Edwardes
email : mte@users.sourceforge.net
Javier Campos Morales <javi_c@users.sourceforge.net>
Felix Rodriguez <frodriguez@users.sourceforge.net>
John C <thetacoturtle@users.sourceforge.net>
Thomas Baumgart <ipwizard@users.sourceforge.net>
Kevin Tambascio <ktambascio@users.sourceforge.net>
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// ----------------------------------------------------------------------------
// QT Includes
#include <qapplication.h>
// ----------------------------------------------------------------------------
// KDE Includes
#include <klistview.h>
// ----------------------------------------------------------------------------
// Project Includes
#include "kmymoneycompletion.h"
#include <kmymoney/kmymoneyselector.h>
#include <kmymoney/kmymoneylistviewitem.h>
#include "./kmymoneycombo.h"
const int kMyMoneyCompletion::MAX_ITEMS = 16;
kMyMoneyCompletion::kMyMoneyCompletion(QWidget *parent, const char *name ) :
QVBox(parent, name, WType_Popup)
{
m_selector = new KMyMoneySelector(this);
m_selector->listView()->setFocusProxy(this);
m_parent = parent;
setFocusProxy((parent) ? parent : (QWidget*) NoFocus);
setFrameStyle(QFrame::PopupPanel | QFrame::Raised);
connectSignals(m_selector, m_selector->listView());
}
void kMyMoneyCompletion::connectSignals(QWidget* widget, KListView* lv)
{
m_widget = widget;
m_lv = lv;
connect(lv, SIGNAL(executed(QListViewItem*,const QPoint&,int)), this, SLOT(slotItemSelected(QListViewItem*,const QPoint&,int)));
}
kMyMoneyCompletion::~kMyMoneyCompletion()
{
}
void kMyMoneyCompletion::adjustSize(void)
{
QListViewItemIterator it(m_lv, QListViewItemIterator::Visible);
int count = 0;
while(it.current()) {
++count;
++it;
}
adjustSize(count);
}
void kMyMoneyCompletion::adjustSize(const int count)
{
int w = m_widget->sizeHint().width();
if(m_parent && w < m_parent->width())
w = m_parent->width();
QFontMetrics fm(font());
if(w < fm.maxWidth()*15)
w = fm.maxWidth()*15;
int h = 0;
QListViewItemIterator it(m_lv, QListViewItemIterator::Visible);
QListViewItem* item = it.current();
if(item)
h = item->height() * (count > MAX_ITEMS ? MAX_ITEMS : count);
// the offset of 4 in the next statement avoids the
// display of a scroll bar if count < MAX_ITEMS.
resize(w, h+4);
if(m_parent) {
// the code of this basic block is taken from KCompletionBox::show()
// and modified to our local needs
// this is probably better, once kde switches to requiring qt3.1
// QRect screenSize = QApplication::desktop()->availableGeometry(d->m_parent);
// for now use this since it's qt3.0.x-safe
QRect screenSize = QApplication::desktop()->screenGeometry(QApplication::desktop()->screenNumber(m_parent));
QPoint orig = m_parent->mapToGlobal( QPoint(0, m_parent->height()) );
int x = orig.x();
int y = orig.y();
if ( x + width() > screenSize.right() )
x = screenSize.right() - width();
// check for the maximum height here to avoid flipping
// of the completion box from top to bottom of the
// edit widget. The offset (y) is certainly based
// on the actual height.
if(item) {
if ((y + item->height()*MAX_ITEMS) > screenSize.bottom() )
y = y - height() - m_parent->height();
}
move( x, y);
}
}
void kMyMoneyCompletion::show(bool presetSelected)
{
if(!m_id.isEmpty() && presetSelected)
m_selector->setSelected(m_id);
adjustSize();
if(m_parent) {
m_parent->installEventFilter(this);
// make sure to install the filter for the combobox lineedit as well
// We have do this here because QObject::installEventFilter() is not
// declared virtual and we have no chance to override it in KMyMoneyCombo
KMyMoneyCombo* c = dynamic_cast<KMyMoneyCombo*>(m_parent);
if(c && c->lineEdit()) {
c->lineEdit()->installEventFilter(this);
}
}
QVBox::show();
}
void kMyMoneyCompletion::hide(void)
{
if(m_parent) {
m_parent->removeEventFilter(this);
// make sure to uninstall the filter for the combobox lineedit as well
// We have do this here because QObject::installEventFilter() is not
// declared virtual and we have no chance to override it in KMyMoneyCombo
KMyMoneyCombo* c = dynamic_cast<KMyMoneyCombo*>(m_parent);
if(c && c->lineEdit()) {
c->lineEdit()->removeEventFilter(this);
}
}
QVBox::hide();
}
bool kMyMoneyCompletion::eventFilter(QObject* o, QEvent* e)
{
int type = e->type();
KMyMoneyCombo *c = dynamic_cast<KMyMoneyCombo*>(m_parent);
QListViewItem* item;
if(o == m_parent || (c && o == c->lineEdit())) {
if(isVisible()) {
if(type == QEvent::KeyPress) {
QKeyEvent* ev = static_cast<QKeyEvent*> (e);
QKeyEvent evt(QEvent::KeyPress,
Key_Down, 0, ev->state(), QString::null,
ev->isAutoRepeat(), ev->count());
QKeyEvent evbt(QEvent::KeyPress,
Key_Up, 0, ev->state(), QString::null,
ev->isAutoRepeat(), ev->count());
switch(ev->key()) {
case Key_Tab:
case Key_BackTab:
slotItemSelected(m_lv->currentItem(), QPoint(0,0), 0);
break;
case Key_Down:
case Key_Next:
item = m_lv->currentItem();
while(item) {
item = item->itemBelow();
if(item && selector()->match(m_lastCompletion, item))
break;
}
if(item) {
m_lv->setCurrentItem(item);
selector()->ensureItemVisible(item);
}
ev->accept();
return true;
case Key_Up:
case Key_Prior:
item = m_lv->currentItem();
while(item) {
item = item->itemAbove();
if(item && selector()->match(m_lastCompletion, item))
break;
}
if(item) {
m_lv->setCurrentItem(item);
// make sure, we always see a possible (non-selectable) group item
if(item->itemAbove())
item = item->itemAbove();
selector()->ensureItemVisible(item);
}
ev->accept();
return true;
case Key_Escape:
hide();
ev->accept();
return true;
case Key_Enter:
case Key_Return:
slotItemSelected(m_lv->currentItem(), QPoint(0,0), 0);
ev->accept();
return true;
case Key_Home:
case Key_End:
if(ev->state() & ControlButton) {
item = m_lv->currentItem();
if(ev->key() == Key_Home) {
while(item && item->itemAbove()) {
item = item->itemAbove();
}
while(item && !selector()->match(m_lastCompletion, item)) {
item = item->itemBelow();
}
} else {
while(item && item->itemBelow()) {
item = item->itemBelow();
}
while(item && !selector()->match(m_lastCompletion, item)) {
item = item->itemAbove();
}
}
if(item) {
m_lv->setCurrentItem(item);
// make sure, we always see a possible (non-selectable) group item
if(item->itemAbove())
item = item->itemAbove();
selector()->ensureItemVisible(item);
}
ev->accept();
return true;
}
break;
default:
break;
}
}
}
}
return QVBox::eventFilter(o, e);
}
void kMyMoneyCompletion::slotMakeCompletion(const QString& txt)
{
int cnt = selector()->slotMakeCompletion(txt.stripWhiteSpace());
if(m_parent && m_parent->isVisible() && !isVisible() && cnt)
show(false);
else {
if(cnt != 0) {
adjustSize();
} else {
hide();
}
}
}
void kMyMoneyCompletion::slotItemSelected(QListViewItem *item, const QPoint&, int)
{
KMyMoneyListViewItem* it_v = static_cast<KMyMoneyListViewItem*>(item);
if(it_v && it_v->isSelectable()) {
QString id = it_v->id();
// hide the widget, so we can debug the slots that are connect
// to the signal we emit very soon
hide();
m_id = id;
emit itemSelected(id);
}
}
void kMyMoneyCompletion::setSelected(const QString& id)
{
m_id = id;
m_selector->setSelected(id, true);
}
#include "kmymoneycompletion.moc"
|