1
/* This file is part of the KDE project
2
Copyright (C) 2005-2006 Jarosław Staniek <staniek@kde.org>
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this program; see the file COPYING. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
20
#include "KexiFieldComboBox.h"
21
#include <kexiutils/utils.h>
22
#include <kexiproject.h>
23
#include <kexi_global.h>
24
#include "KexiFieldListModel.h"
26
#include <KDbTableSchema>
27
#include <KDbQuerySchema>
28
#include <KDbTableOrQuerySchema>
31
#include <QPushButton>
36
class KexiFieldComboBox::Private
41
, insideSetFieldOrExpression(false)
46
QPointer<KexiProject> prj;
47
QPointer<KexiFieldListModel> model;
49
QString tableOrQueryName;
50
QString fieldOrExpression;
53
bool insideSetFieldOrExpression;
56
//------------------------
58
KexiFieldComboBox::KexiFieldComboBox(QWidget *parent)
59
: KComboBox(true/*rw*/, parent)
62
setInsertPolicy(NoInsert);
63
setCompletionMode(KCompletion::CompletionPopupAuto);
65
setMaxVisibleItems(16);
66
connect(this, SIGNAL(activated(int)),
67
this, SLOT(slotActivated(int)));
68
connect(this, SIGNAL(returnPressed(QString)),
69
this, SLOT(slotReturnPressed(QString)));
72
KexiFieldComboBox::~KexiFieldComboBox()
77
void KexiFieldComboBox::setProject(KexiProject *prj)
79
if ((KexiProject*)d->prj == prj)
82
setTableOrQuery(QString(), true);
85
KexiProject* KexiFieldComboBox::project() const
90
void KexiFieldComboBox::setTableOrQuery(const QString& name, bool table)
92
d->tableOrQueryName = name;
96
if (d->tableOrQueryName.isEmpty() || !d->prj)
99
KDbTableOrQuerySchema tableOrQuery(d->prj->dbConnection(), d->tableOrQueryName.toLatin1(), d->table);
100
if (!tableOrQuery.table() && !tableOrQuery.query())
104
d->model = new KexiFieldListModel(this, ShowEmptyItem);
106
d->model->setSchema(&tableOrQuery);
110
setFieldOrExpression(d->fieldOrExpression);
113
QString KexiFieldComboBox::tableOrQueryName() const
115
return d->tableOrQueryName;
118
bool KexiFieldComboBox::isTableAssigned() const
123
void KexiFieldComboBox::setFieldOrExpression(const QString& string)
125
if (d->insideSetFieldOrExpression) {
128
KexiUtils::BoolBlocker guard(&d->insideSetFieldOrExpression, true);
129
const QString name(string);
130
const int pos = name.indexOf('.');
132
d->fieldOrExpression = name;
134
QString objectName = name.left(pos);
135
if (d->tableOrQueryName != objectName) {
136
d->fieldOrExpression = name;
139
qWarning() << "invalid table/query name in" << name;
142
d->fieldOrExpression = name.mid(pos + 1);
145
//! @todo show 'the item doesn't match' info?
146
setEditText(d->fieldOrExpression);
149
void KexiFieldComboBox::setFieldOrExpression(int index)
152
index++; //skip 1st empty item
154
if (index >= count()) {
155
qWarning() << "index" << index << "out of range 0.." << (count() - 1);
160
d->fieldOrExpression.clear();
162
setCurrentIndex(index);
163
d->fieldOrExpression = itemData(currentIndex(), Qt::DisplayRole).toString();
164
lineEdit()->setText(d->fieldOrExpression);
168
QString KexiFieldComboBox::fieldOrExpression() const
170
return d->fieldOrExpression;
173
int KexiFieldComboBox::indexOfField() const
175
KDbTableOrQuerySchema tableOrQuery(d->prj->dbConnection(), d->tableOrQueryName.toLatin1(), d->table);
176
if (!tableOrQuery.table() && !tableOrQuery.query())
179
return currentIndex() > 0 ? (currentIndex() - 1) : -1;
182
QString KexiFieldComboBox::fieldOrExpressionCaption() const
184
return itemData(currentIndex()).toString();
187
void KexiFieldComboBox::slotActivated(int i)
189
d->fieldOrExpression = itemData(i, Qt::DisplayRole).toString();
190
setFieldOrExpression(d->fieldOrExpression);
194
void KexiFieldComboBox::slotReturnPressed(const QString & text)
196
//text is available: select item for this text:
198
if (text.isEmpty()) {
201
index = findText(text, Qt::MatchExactly);
205
setCurrentIndex(index);
206
slotActivated(index);
209
void KexiFieldComboBox::focusOutEvent(QFocusEvent *e)
211
KComboBox::focusOutEvent(e);
212
// accept changes if the focus is moved
213
if (!KDbUtils::hasParent(this, focusWidget())) {
214
//(a check needed because drop-down listbox also causes a focusout)
215
slotReturnPressed(currentText());