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
|
/* This file is part of the KDE project
Copyright (C) 2005-2006 Jaroslaw Staniek <js@iidea.pl>
This program 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 program 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 program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kexifieldcombobox.h"
#include <tqheader.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqpushbutton.h>
#include <tqcursor.h>
#include <tqpoint.h>
#include <tqapplication.h>
#include <tqbitmap.h>
#include <tqstyle.h>
#include <tqlistbox.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <tdeversion.h>
#include <kconfig.h>
#include <kglobalsettings.h>
#include <klocale.h>
#include <kexidb/tableschema.h>
#include <kexidb/queryschema.h>
#include <kexidb/utils.h>
#include <kexiutils/utils.h>
#include <kexidragobjects.h>
#include <kexiproject.h>
//! @internal
class KexiFieldComboBox::Private
{
public:
Private()
// : schema(0)
: keyIcon( SmallIcon("key") )
, noIcon( KexiUtils::emptyIcon(KIcon::Small) )
, table(true)
{
}
~Private()
{
// delete schema;
}
TQGuardedPtr<KexiProject> prj;
// KexiDB::TableOrQuerySchema* schema;
TQPixmap keyIcon, noIcon;
TQString tableOrQueryName;
TQString fieldOrExpression;
TQMap<TQString, TQString> captions;
bool table : 1;
};
//------------------------
KexiFieldComboBox::KexiFieldComboBox(TQWidget *parent, const char *name)
: KComboBox(true/*rw*/, parent, name)
, d(new Private())
{
setInsertionPolicy(NoInsertion);
setCompletionMode(KGlobalSettings::CompletionPopupAuto);
setSizeLimit( 16 );
connect(this, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotActivated(int)));
connect(this, TQT_SIGNAL(returnPressed(const TQString &)), this, TQT_SLOT(slotReturnPressed(const TQString &)));
// setAcceptDrops(true);
// viewport()->setAcceptDrops(true);
}
KexiFieldComboBox::~KexiFieldComboBox()
{
delete d;
}
void KexiFieldComboBox::setProject(KexiProject *prj)
{
if ((KexiProject*)d->prj==prj)
return;
d->prj = prj;
setTableOrQuery("", true);
}
KexiProject* KexiFieldComboBox::project() const
{
return d->prj;
}
void KexiFieldComboBox::setTableOrQuery(const TQString& name, bool table)
{
d->tableOrQueryName = name;
d->table = table;
clear();
d->captions.clear();
insertItem("");
// delete d->schema;
if (d->tableOrQueryName.isEmpty() || !d->prj)
return;
KexiDB::TableOrQuerySchema tableOrQuery(d->prj->dbConnection(), d->tableOrQueryName.latin1(), d->table);
if (!tableOrQuery.table() && !tableOrQuery.query())
return;
// bool hasPKeys = true; //t->hasPrimaryKeys();
KexiDB::QueryColumnInfo::Vector columns = tableOrQuery.columns();
const int count = columns.count();
for(int i=0; i < count; i++)
{
KexiDB::QueryColumnInfo *colinfo = columns[i];
insertItem(
(colinfo && (colinfo->field->isPrimaryKey() || colinfo->field->isUniqueKey()))
? d->keyIcon
: d->noIcon
, TQString(colinfo->aliasOrName()));
completionObject()->addItem(colinfo->aliasOrName());
//store user-friendly caption (used by fieldOrExpressionCaption())
d->captions.insert( colinfo->aliasOrName(), colinfo->captionOrAliasOrName() );
}
//update selection
setFieldOrExpression(d->fieldOrExpression);
}
TQString KexiFieldComboBox::tableOrQueryName() const
{
return d->tableOrQueryName;
}
bool KexiFieldComboBox::isTableAssigned() const
{
return d->table;
}
void KexiFieldComboBox::setFieldOrExpression(const TQString& string)
{
const TQString name(string); //string.stripWhiteSpace().lower());
const int pos = name.find('.');
if (pos==-1) {
d->fieldOrExpression = name;
}
else {
TQString objectName = name.left(pos);
if (d->tableOrQueryName!=objectName) {
d->fieldOrExpression = name;
setCurrentItem(0);
setCurrentText(name);
//! @todo show error
kexiwarn << "KexiFieldComboBox::setField(): invalid table/query name in '" << name << "'" << endl;
return;
}
d->fieldOrExpression = name.mid(pos+1);
}
TQListBoxItem *item = listBox()->findItem(d->fieldOrExpression);
if (!item) {
setCurrentItem(0);
setCurrentText(d->fieldOrExpression);
//todo: show 'the item doesn't match' info?
return;
}
setCurrentItem( listBox()->index(item) );
}
void KexiFieldComboBox::setFieldOrExpression(int index)
{
index++; //skip 1st empty item
if (index>=count()) {
kexiwarn << TQString("KexiFieldComboBox::setFieldOrExpression(int index): index %1 "
"out of range (0..%2)").tqarg(index).tqarg(count()-1) << endl;
index = -1;
}
if (index<=0) {
setCurrentItem(0);
d->fieldOrExpression = TQString();
}
else {
setCurrentItem(index);
d->fieldOrExpression = currentText();
}
}
TQString KexiFieldComboBox::fieldOrExpression() const
{
return d->fieldOrExpression;
}
int KexiFieldComboBox::indexOfField() const
{
KexiDB::TableOrQuerySchema tableOrQuery(d->prj->dbConnection(), d->tableOrQueryName.latin1(), d->table);
if (!tableOrQuery.table() && !tableOrQuery.query())
return -1;
return currentItem()>0 ? (currentItem()-1) : -1;
}
TQString KexiFieldComboBox::fieldOrExpressionCaption() const
{
return d->captions[ d->fieldOrExpression ];
}
void KexiFieldComboBox::slotActivated(int i)
{
d->fieldOrExpression = text(i);
emit selected();
}
void KexiFieldComboBox::slotReturnPressed(const TQString & text)
{
//text is available: select item for this text:
int index;
if (text.isEmpty()) {
index = 0;
}
else {
TQListBoxItem *item = listBox()->findItem( text, TQt::ExactMatch );
if (!item)
return;
index = listBox()->index( item );
if (index < 1)
return;
}
setCurrentItem( index );
slotActivated( index );
}
void KexiFieldComboBox::focusOutEvent( TQFocusEvent *e )
{
KComboBox::focusOutEvent( e );
// accept changes if the focus is moved
if (!KexiUtils::hasParent(TQT_TQOBJECT(this), TQT_TQOBJECT(tqfocusWidget()))) //(a check needed because drop-down listbox also causes a focusout)
slotReturnPressed(currentText());
}
#include "kexifieldcombobox.moc"
|