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

« back to all changes in this revision

Viewing changes to src/widget/kexiqueryparameters.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) 2006-2015 Jarosław Staniek <staniek@kde.org>
 
3
 
 
4
   This library 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 library 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 library; see the file COPYING.LIB.  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 "kexiqueryparameters.h"
 
21
#include <kexi_global.h>
 
22
#include "utils/kexidatetimeformatter.h"
 
23
 
 
24
#include <KDbQuerySchemaParameter>
 
25
#include <KDbUtils>
 
26
#include <KDbDriver>
 
27
 
 
28
#include <KLocalizedString>
 
29
 
 
30
#include <QDebug>
 
31
#include <QInputDialog>
 
32
 
 
33
//static
 
34
QList<QVariant> KexiQueryParameters::getParameters(QWidget *parent,
 
35
        const KDbDriver &driver, KDbQuerySchema* querySchema, bool *ok)
 
36
{
 
37
    Q_ASSERT(ok);
 
38
    Q_ASSERT(querySchema);
 
39
    Q_UNUSED(driver);
 
40
    *ok = false;
 
41
    const QList<KDbQuerySchemaParameter> params(querySchema->parameters());
 
42
    QList<QVariant> values;
 
43
    const QString caption(xi18nc("@title:window Enter Query Parameter Value", "Enter Parameter Value"));
 
44
    foreach(const KDbQuerySchemaParameter &parameter, params) {
 
45
        switch (parameter.type) {
 
46
        case KDbField::Byte:
 
47
        case KDbField::ShortInteger:
 
48
        case KDbField::Integer:
 
49
        case KDbField::BigInteger: {
 
50
//! @todo problem for ranges in case of BigInteger - will disappear when we remove use of QInputDialog
 
51
            qlonglong minValue, maxValue;
 
52
//! @todo add support for unsigned parameter here
 
53
            KDb::getLimitsForFieldType(parameter.type, &minValue, &maxValue);
 
54
            const int result = QInputDialog::getInt(parent, caption, parameter.message,
 
55
                                                    0, minValue, maxValue, 1/*step*/, ok);
 
56
            if (!*ok)
 
57
                return QList<QVariant>(); //cancelled
 
58
            values.append(result);
 
59
            break;
 
60
        }
 
61
        case KDbField::Boolean: {
 
62
            QStringList list;
 
63
            list << xi18nc("Boolean True - Yes", "Yes") << xi18nc("Boolean False - No", "No");
 
64
            const QString result = QInputDialog::getItem(parent, caption, parameter.message,
 
65
                                                         list, 0/*current*/, false /* !editable*/,
 
66
                                                         ok);
 
67
            if (!*ok || result.isEmpty())
 
68
                return QList<QVariant>(); //cancelled
 
69
            values.append(result == list.first());
 
70
            break;
 
71
        }
 
72
        case KDbField::Date: {
 
73
            //! @todo KEXI3 re-add the KexiDateFormatter's inputMask for QInputDialog
 
74
            KexiDateFormatter df;
 
75
            const QString result = QInputDialog::getText(parent, caption, parameter.message,
 
76
                                                         QLineEdit::Normal, QString(), ok);
 
77
            //! @todo KEXI3 add time validator
 
78
            // 0/*validator*/, df.inputMask());
 
79
            if (!*ok)
 
80
                return QList<QVariant>(); //cancelled
 
81
            values.append(df.fromString(result));
 
82
            break;
 
83
        }
 
84
        case KDbField::DateTime: {
 
85
            //! @todo KEXI3 re-add the KexiDateTimeFormatter's inputMask for QInputDialog
 
86
            KexiDateFormatter df;
 
87
            KexiTimeFormatter tf;
 
88
            const QString result = QInputDialog::getText(parent, caption, parameter.message,
 
89
                                                         QLineEdit::Normal, QString(), ok);
 
90
            //! @todo KEXI3 add date time validator
 
91
            // 0/*validator*/, KexiDateTimeFormatter::inputMask(df, tf));
 
92
            if (!*ok)
 
93
                return QList<QVariant>(); //cancelled
 
94
            values.append(KexiDateTimeFormatter::fromString(df, tf, result));
 
95
            break;
 
96
        }
 
97
        case KDbField::Time: {
 
98
            //! @todo KEXI3 re-add the KexiTimeFormatter's inputMask for QInputDialog
 
99
            KexiTimeFormatter tf;
 
100
            const QString result = QInputDialog::getText(parent, caption, parameter.message,
 
101
                                                         QLineEdit::Normal, QString(), ok);
 
102
//! @todo add validator
 
103
            // 0/*validator*/, tf.inputMask());
 
104
            if (!*ok)
 
105
                return QList<QVariant>(); //cancelled
 
106
            values.append(tf.fromString(result));
 
107
            break;
 
108
        }
 
109
        case KDbField::Float:
 
110
        case KDbField::Double: {
 
111
            // QInputDialog::getDouble() does not work well, use getText and double validator
 
112
            //! @todo KEXI3 re-add double validator
 
113
            // QDoubleValidator validator(0);
 
114
            const QString textResult
 
115
                = QInputDialog::getText(parent, caption, parameter.message, QLineEdit::Normal,
 
116
                                        QString(), ok);
 
117
            if (!*ok || textResult.isEmpty())
 
118
                return QList<QVariant>(); //cancelled
 
119
//! @todo this value will be still rounded: consider storing them as a decimal type
 
120
//!    (e.g. using a special qint64+decimalplace class)
 
121
            const double result = textResult.toDouble(ok); //this is also good for float (to avoid rounding)
 
122
            if (!*ok)
 
123
                return QList<QVariant>();
 
124
            values.append(result);
 
125
            break;
 
126
        }
 
127
        case KDbField::Text:
 
128
        case KDbField::LongText: {
 
129
            const QString result = QInputDialog::getText(parent, caption, parameter.message,
 
130
                                                         QLineEdit::Normal, QString(), ok);
 
131
            if (!*ok)
 
132
                return QList<QVariant>(); //cancelled
 
133
            values.append(result);
 
134
            break;
 
135
        }
 
136
        case KDbField::BLOB: {
 
137
//! @todo BLOB input unsupported
 
138
            values.append(QByteArray());
 
139
            break;
 
140
        }
 
141
        default:
 
142
            qWarning() << "unsupported type " << KDbField::typeName(parameter.type)
 
143
                << "for parameter \"" << parameter.message << "\" - aborting query execution!";
 
144
            return QList<QVariant>();
 
145
        }
 
146
    }
 
147
    *ok = true;
 
148
    return values;
 
149
}