~ubuntu-branches/debian/sid/kexi/sid

« back to all changes in this revision

Viewing changes to src/widget/fields/KexiFieldComboBox.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2017-06-24 20:10:10 UTC
  • Revision ID: package-import@ubuntu.com-20170624201010-5lrzd5r2vwthwifp
Tags: upstream-3.0.1.1
Import upstream version 3.0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2005-2006 Jarosław Staniek <staniek@kde.org>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
18
*/
 
19
 
 
20
#include "KexiFieldComboBox.h"
 
21
#include <kexiutils/utils.h>
 
22
#include <kexiproject.h>
 
23
#include <kexi_global.h>
 
24
#include "KexiFieldListModel.h"
 
25
 
 
26
#include <KDbTableSchema>
 
27
#include <KDbQuerySchema>
 
28
#include <KDbTableOrQuerySchema>
 
29
#include <KDbUtils>
 
30
 
 
31
#include <QPushButton>
 
32
#include <QPoint>
 
33
#include <QLineEdit>
 
34
 
 
35
//! @internal
 
36
class KexiFieldComboBox::Private
 
37
{
 
38
public:
 
39
    Private()
 
40
            : table(true)
 
41
            , insideSetFieldOrExpression(false)
 
42
    {
 
43
    }
 
44
    ~Private() {
 
45
    }
 
46
    QPointer<KexiProject> prj;
 
47
    QPointer<KexiFieldListModel> model;
 
48
 
 
49
    QString tableOrQueryName;
 
50
    QString fieldOrExpression;
 
51
 
 
52
    bool table;
 
53
    bool insideSetFieldOrExpression;
 
54
};
 
55
 
 
56
//------------------------
 
57
 
 
58
KexiFieldComboBox::KexiFieldComboBox(QWidget *parent)
 
59
        : KComboBox(true/*rw*/, parent)
 
60
        , d(new Private())
 
61
{
 
62
    setInsertPolicy(NoInsert);
 
63
    setCompletionMode(KCompletion::CompletionPopupAuto);
 
64
 
 
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)));
 
70
}
 
71
 
 
72
KexiFieldComboBox::~KexiFieldComboBox()
 
73
{
 
74
    delete d;
 
75
}
 
76
 
 
77
void KexiFieldComboBox::setProject(KexiProject *prj)
 
78
{
 
79
    if ((KexiProject*)d->prj == prj)
 
80
        return;
 
81
    d->prj = prj;
 
82
    setTableOrQuery(QString(), true);
 
83
}
 
84
 
 
85
KexiProject* KexiFieldComboBox::project() const
 
86
{
 
87
    return d->prj;
 
88
}
 
89
 
 
90
void KexiFieldComboBox::setTableOrQuery(const QString& name, bool table)
 
91
{
 
92
    d->tableOrQueryName = name;
 
93
    d->table = table;
 
94
    clear();
 
95
 
 
96
    if (d->tableOrQueryName.isEmpty() || !d->prj)
 
97
        return;
 
98
 
 
99
    KDbTableOrQuerySchema tableOrQuery(d->prj->dbConnection(), d->tableOrQueryName.toLatin1(), d->table);
 
100
    if (!tableOrQuery.table() && !tableOrQuery.query())
 
101
        return;
 
102
 
 
103
    delete d->model;
 
104
    d->model = new KexiFieldListModel(this, ShowEmptyItem);
 
105
 
 
106
    d->model->setSchema(&tableOrQuery);
 
107
    setModel(d->model);
 
108
 
 
109
    //update selection
 
110
    setFieldOrExpression(d->fieldOrExpression);
 
111
}
 
112
 
 
113
QString KexiFieldComboBox::tableOrQueryName() const
 
114
{
 
115
    return d->tableOrQueryName;
 
116
}
 
117
 
 
118
bool KexiFieldComboBox::isTableAssigned() const
 
119
{
 
120
    return d->table;
 
121
}
 
122
 
 
123
void KexiFieldComboBox::setFieldOrExpression(const QString& string)
 
124
{
 
125
    if (d->insideSetFieldOrExpression) {
 
126
        return;
 
127
    }
 
128
    KexiUtils::BoolBlocker guard(&d->insideSetFieldOrExpression, true);
 
129
    const QString name(string);
 
130
    const int pos = name.indexOf('.');
 
131
    if (pos == -1) {
 
132
        d->fieldOrExpression = name;
 
133
    } else {
 
134
        QString objectName = name.left(pos);
 
135
        if (d->tableOrQueryName != objectName) {
 
136
            d->fieldOrExpression = name;
 
137
            setEditText(name);
 
138
//! @todo show error
 
139
            qWarning() << "invalid table/query name in" << name;
 
140
            return;
 
141
        }
 
142
        d->fieldOrExpression = name.mid(pos + 1);
 
143
    }
 
144
 
 
145
    //! @todo show 'the item doesn't match' info?
 
146
    setEditText(d->fieldOrExpression);
 
147
}
 
148
 
 
149
void KexiFieldComboBox::setFieldOrExpression(int index)
 
150
{
 
151
    if (index >= 0) {
 
152
        index++; //skip 1st empty item
 
153
    }
 
154
    if (index >= count()) {
 
155
        qWarning() << "index" << index << "out of range 0.." << (count() - 1);
 
156
        index = -1;
 
157
    }
 
158
    if (index <= 0) {
 
159
        setCurrentIndex(0);
 
160
        d->fieldOrExpression.clear();
 
161
    } else {
 
162
        setCurrentIndex(index);
 
163
        d->fieldOrExpression = itemData(currentIndex(), Qt::DisplayRole).toString();
 
164
        lineEdit()->setText(d->fieldOrExpression);
 
165
    }
 
166
}
 
167
 
 
168
QString KexiFieldComboBox::fieldOrExpression() const
 
169
{
 
170
    return d->fieldOrExpression;
 
171
}
 
172
 
 
173
int KexiFieldComboBox::indexOfField() const
 
174
{
 
175
    KDbTableOrQuerySchema tableOrQuery(d->prj->dbConnection(), d->tableOrQueryName.toLatin1(), d->table);
 
176
    if (!tableOrQuery.table() && !tableOrQuery.query())
 
177
        return -1;
 
178
 
 
179
    return currentIndex() > 0 ? (currentIndex() - 1) : -1;
 
180
}
 
181
 
 
182
QString KexiFieldComboBox::fieldOrExpressionCaption() const
 
183
{
 
184
    return itemData(currentIndex()).toString();
 
185
}
 
186
 
 
187
void KexiFieldComboBox::slotActivated(int i)
 
188
{
 
189
    d->fieldOrExpression = itemData(i, Qt::DisplayRole).toString();
 
190
    setFieldOrExpression(d->fieldOrExpression);
 
191
    emit selected();
 
192
}
 
193
 
 
194
void KexiFieldComboBox::slotReturnPressed(const QString & text)
 
195
{
 
196
    //text is available: select item for this text:
 
197
    int index;
 
198
    if (text.isEmpty()) {
 
199
        index = 0;
 
200
    } else {
 
201
        index = findText(text, Qt::MatchExactly);
 
202
        if (index < 1)
 
203
            return;
 
204
    }
 
205
    setCurrentIndex(index);
 
206
    slotActivated(index);
 
207
}
 
208
 
 
209
void KexiFieldComboBox::focusOutEvent(QFocusEvent *e)
 
210
{
 
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());
 
216
    }
 
217
}