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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/* This file is part of the KDE project
Copyright (C) 2005-2006 Jaroslaw Staniek <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kexidatasourcecombobox.h"
#include <kdebug.h>
#include <kiconloader.h>
#include <tdelistbox.h>
#include <kexi.h>
#include <kexiproject.h>
#include <keximainwindow.h>
#include <kexipart.h>
#include <kexipartmanager.h>
#include <kexipartinfo.h>
#include <kexipartitem.h>
#include <kexidb/connection.h>
#ifdef KEXI_SHOW_UNIMPLEMENTED
#define ADD_DEFINEQUERY_ROW
#endif
//! @internal
class KexiDataSourceComboBox::Private
{
public:
Private()
: tablesCount(0)
, prevIndex(-1)
, showTables(true)
, showQueries(true)
{
}
int firstTableIndex() const {
int index = 1; //skip empty row
#ifdef ADD_DEFINEQUERY_ROW
index++; /*skip 'define query' row*/
#endif
return index;
}
int firstQueryIndex() const {
return firstTableIndex() + tablesCount;
}
TQGuardedPtr<KexiProject> prj;
TQPixmap tableIcon, queryIcon;
int tablesCount;
int prevIndex; //!< Used in slotActivated()
bool showTables : 1;
bool showQueries : 1;
};
//------------------------
KexiDataSourceComboBox::KexiDataSourceComboBox(TQWidget *parent, const char *name)
: KComboBox(true/*rw*/, parent, name)
, d(new Private())
{
setInsertionPolicy(NoInsertion);
setCompletionMode(TDEGlobalSettings::CompletionPopupAuto);
setSizeLimit( 16 );
connect(this, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotActivated(int)));
connect(this, TQ_SIGNAL(returnPressed(const TQString &)), this, TQ_SLOT(slotReturnPressed(const TQString &)));
d->tableIcon = SmallIcon("table");
d->queryIcon = SmallIcon("query");
}
KexiDataSourceComboBox::~KexiDataSourceComboBox()
{
delete d;
}
KexiProject* KexiDataSourceComboBox::project() const
{
return d->prj;
}
void KexiDataSourceComboBox::setProject(KexiProject *prj, bool showTables, bool showQueries)
{
if ((KexiProject*)d->prj == prj)
return;
if (d->prj) {
disconnect(d->prj, 0, this, 0);
}
d->prj = prj;
d->showTables = showTables;
d->showQueries = showQueries;
clear();
d->tablesCount = 0;
if (!d->prj)
return;
//needed for updating contents of the combo box
connect(d->prj, TQ_SIGNAL(newItemStored(KexiPart::Item&)),
this, TQ_SLOT(slotNewItemStored(KexiPart::Item&)));
connect(d->prj, TQ_SIGNAL(itemRemoved(const KexiPart::Item&)),
this, TQ_SLOT(slotItemRemoved(const KexiPart::Item&)));
connect(d->prj, TQ_SIGNAL(itemRenamed(const KexiPart::Item&, const TQCString&)),
this, TQ_SLOT(slotItemRenamed(const KexiPart::Item&, const TQCString&)));
KexiDB::Connection *conn = d->prj->dbConnection();
if (!conn)
return;
//special item: empty
insertItem("");
#ifdef ADD_DEFINEQUERY_ROW
//special item: define query
insertItem(i18n("Define Query..."));
#endif
TDECompletion *comp = completionObject();
if (d->showTables) {
//tables
KexiPart::Info* partInfo = Kexi::partManager().infoForMimeType("kexi/table");
if (!partInfo)
return;
KexiPart::ItemList list;
prj->getSortedItems(list, partInfo);
list.sort();
d->tablesCount = 0;
for (KexiPart::ItemListIterator it(list); it.current(); ++it, d->tablesCount++) {
insertItem(d->tableIcon, it.current()->name()); //or caption()?
comp->addItem(it.current()->name());
}
}
if (d->showQueries) {
//queries
KexiPart::Info* partInfo = Kexi::partManager().infoForMimeType("kexi/query");
if (!partInfo)
return;
KexiPart::ItemList list;
prj->getSortedItems(list, partInfo);
list.sort();
for (KexiPart::ItemListIterator it(list); it.current(); ++it) {
insertItem(d->queryIcon, it.current()->name()); //or caption()?
comp->addItem(it.current()->name());
}
}
// setCurrentText("");
setCurrentItem(0);
}
void KexiDataSourceComboBox::setDataSource(const TQString& mimeType, const TQString& name)
{
if (name.isEmpty()) {
clearEdit();
setCurrentItem(0);
d->prevIndex = -1;
emit dataSourceChanged();
return;
}
TQString mt(mimeType);
if (mimeType.isEmpty())
mt="kexi/table";
int i = findItem(mt, name);
if (i==-1) {
if (mimeType.isEmpty())
i = findItem("kexi/query", name);
if (i==-1) {
setCurrentItem(0);
return;
}
}
setCurrentItem(i);
slotActivated(i);
}
void KexiDataSourceComboBox::slotNewItemStored(KexiPart::Item& item)
{
TQString name(item.name());
//insert a new item, maintaining sort order and splitting to tables and queries
if (item.mimeType()=="kexi/table") {
int i = 1; /*skip empty row*/
#ifdef ADD_DEFINEQUERY_ROW
i++; /*skip 'define query' row*/
#endif
for (; i < d->firstQueryIndex() && name>=text(i); i++)
;
insertItem(d->tableIcon, name, i);
completionObject()->addItem(name);
d->tablesCount++;
}
else if (item.mimeType()=="kexi/query") {
int i;
for (i=d->firstQueryIndex(); i<count() && name>=text(i); i++)
;
insertItem(d->queryIcon, name, i);
completionObject()->addItem(name);
}
}
int KexiDataSourceComboBox::findItem(const TQString& mimeType, const TQString& name)
{
int i, end;
if (mimeType=="kexi/table") {
i = 0;
#ifdef ADD_DEFINEQUERY_ROW
i++; //skip 'define query'
#endif
end = d->firstQueryIndex();
}
else if (mimeType=="kexi/query") {
i = d->firstQueryIndex();
end = count();
}
else
return -1;
TQString nameString(name);
for (; i<end; i++)
if (text(i)==nameString)
return i;
return -1;
}
void KexiDataSourceComboBox::slotItemRemoved(const KexiPart::Item& item)
{
const int i = findItem(item.mimeType(), item.name());
if (i==-1)
return;
removeItem(i);
completionObject()->removeItem(item.name());
if (item.mimeType()=="kexi/table")
d->tablesCount--;
#if 0 //disabled because even invalid data source can be set
if (currentItem()==i) {
if (i==(count()-1))
setCurrentItem(i-1);
else
setCurrentItem(i);
}
#endif
}
void KexiDataSourceComboBox::slotItemRenamed(const KexiPart::Item& item, const TQCString& oldName)
{
const int i = findItem(item.mimeType(), TQString(oldName));
if (i==-1)
return;
changeItem(item.name(), i);
completionObject()->removeItem(TQString(oldName));
completionObject()->addItem(item.name());
setCurrentText(oldName); //still keep old name
}
void KexiDataSourceComboBox::slotActivated( int index )
{
if (index >= d->firstTableIndex() && index < count() && d->prevIndex!=currentItem()) {
d->prevIndex = currentItem();
emit dataSourceChanged();
}
}
TQString KexiDataSourceComboBox::selectedMimeType() const
{
if (selectedName().isEmpty())
return "";
const int index = currentItem();
if (index >= d->firstTableIndex() && index < (int)d->firstQueryIndex())
return "kexi/table";
else if (index >= (int)d->firstQueryIndex() && index < count())
return "kexi/query";
return "";
}
TQString KexiDataSourceComboBox::selectedName() const
{
if (isSelectionValid())
return text(currentItem());
return currentText();
}
bool KexiDataSourceComboBox::isSelectionValid() const
{
const int index = currentItem();
return index >= d->firstTableIndex() && index < count() && text(index)==currentText();
}
void KexiDataSourceComboBox::slotReturnPressed(const TQString & text)
{
//text is available: select item for this text:
bool changed = false;
if (text.isEmpty() && 0!=currentItem()) {
setCurrentItem(0);
changed = true;
}
else {
TQListBoxItem *item = listBox()->findItem( text, TQt::ExactMatch );
if (item) {
int index = listBox()->index( item );
//if (index < d->firstTableIndex())
if (index>=0 && index!=currentItem()) {
setCurrentItem( index );
changed = true;
}
}
}
if (changed)
emit dataSourceChanged();
}
void KexiDataSourceComboBox::focusOutEvent( TQFocusEvent *e )
{
KComboBox::focusOutEvent( e );
slotReturnPressed(currentText());
}
#include "kexidatasourcecombobox.moc"
|