~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/tools/settingseditor/variantdelegate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
 
 
31
#include "variantdelegate.h"
 
32
 
 
33
VariantDelegate::VariantDelegate(QObject *parent)
 
34
    : QItemDelegate(parent)
 
35
{
 
36
    boolExp.setPattern("true|false");
 
37
    boolExp.setCaseSensitivity(Qt::CaseInsensitive);
 
38
 
 
39
    byteArrayExp.setPattern("[\\x00-\\xff]*");
 
40
    charExp.setPattern(".");
 
41
    colorExp.setPattern("\\(([0-9]*),([0-9]*),([0-9]*),([0-9]*)\\)");
 
42
    doubleExp.setPattern("");
 
43
    pointExp.setPattern("\\((-?[0-9]*),(-?[0-9]*)\\)");
 
44
    rectExp.setPattern("\\((-?[0-9]*),(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)\\)");
 
45
    signedIntegerExp.setPattern("-?[0-9]*");
 
46
    sizeExp = pointExp;
 
47
    unsignedIntegerExp.setPattern("[0-9]*");
 
48
 
 
49
    dateExp.setPattern("([0-9]{,4})-([0-9]{,2})-([0-9]{,2})");
 
50
    timeExp.setPattern("([0-9]{,2}):([0-9]{,2}):([0-9]{,2})");
 
51
    dateTimeExp.setPattern(dateExp.pattern() + "T" + timeExp.pattern());
 
52
}
 
53
 
 
54
void VariantDelegate::paint(QPainter *painter,
 
55
                            const QStyleOptionViewItem &option,
 
56
                            const QModelIndex &index) const
 
57
{
 
58
    if (index.column() == 2) {
 
59
        QVariant value = index.model()->data(index, Qt::UserRole);
 
60
        if (!isSupportedType(value.type())) {
 
61
            QStyleOptionViewItem myOption = option;
 
62
            myOption.state &= ~QStyle::State_Enabled;
 
63
            QItemDelegate::paint(painter, myOption, index);
 
64
            return;
 
65
        }
 
66
    }
 
67
 
 
68
    QItemDelegate::paint(painter, option, index);
 
69
}
 
70
 
 
71
QWidget *VariantDelegate::createEditor(QWidget *parent,
 
72
        const QStyleOptionViewItem & /* option */,
 
73
        const QModelIndex &index) const
 
74
{
 
75
    if (index.column() != 2)
 
76
        return 0;
 
77
 
 
78
    QVariant originalValue = index.model()->data(index, Qt::UserRole);
 
79
    if (!isSupportedType(originalValue.type()))
 
80
        return 0;
 
81
 
 
82
    QLineEdit *lineEdit = new QLineEdit(parent);
 
83
    lineEdit->setFrame(false);
 
84
 
 
85
    QRegExp regExp;
 
86
 
 
87
    switch (originalValue.type()) {
 
88
    case QVariant::Bool:
 
89
        regExp = boolExp;
 
90
        break;
 
91
    case QVariant::ByteArray:
 
92
        regExp = byteArrayExp;
 
93
        break;
 
94
    case QVariant::Char:
 
95
        regExp = charExp;
 
96
        break;
 
97
    case QVariant::Color:
 
98
        regExp = colorExp;
 
99
        break;
 
100
    case QVariant::Date:
 
101
        regExp = dateExp;
 
102
        break;
 
103
    case QVariant::DateTime:
 
104
        regExp = dateTimeExp;
 
105
        break;
 
106
    case QVariant::Double:
 
107
        regExp = doubleExp;
 
108
        break;
 
109
    case QVariant::Int:
 
110
    case QVariant::LongLong:
 
111
        regExp = signedIntegerExp;
 
112
        break;
 
113
    case QVariant::Point:
 
114
        regExp = pointExp;
 
115
        break;
 
116
    case QVariant::Rect:
 
117
        regExp = rectExp;
 
118
        break;
 
119
    case QVariant::Size:
 
120
        regExp = sizeExp;
 
121
        break;
 
122
    case QVariant::Time:
 
123
        regExp = timeExp;
 
124
        break;
 
125
    case QVariant::UInt:
 
126
    case QVariant::ULongLong:
 
127
        regExp = unsignedIntegerExp;
 
128
    }
 
129
 
 
130
    if (!regExp.isEmpty()) {
 
131
        QValidator *validator = new QRegExpValidator(regExp, lineEdit);
 
132
        lineEdit->setValidator(validator);
 
133
    }
 
134
 
 
135
    connect(lineEdit, SIGNAL(returnPressed()),
 
136
            this, SLOT(commitAndCloseEditor()));
 
137
 
 
138
    return lineEdit;
 
139
}
 
140
 
 
141
void VariantDelegate::setEditorData(QWidget *editor,
 
142
                                    const QModelIndex &index) const
 
143
{
 
144
    QVariant value = index.model()->data(index, Qt::UserRole);
 
145
    if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor))
 
146
        lineEdit->setText(displayText(value));
 
147
}
 
148
 
 
149
void VariantDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
 
150
                                   const QModelIndex &index) const
 
151
{
 
152
    QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor);
 
153
    if (!lineEdit->isModified())
 
154
        return;
 
155
 
 
156
    QString text = lineEdit->text();
 
157
    const QValidator *validator = lineEdit->validator();
 
158
    if (validator) {
 
159
        int pos;
 
160
        if (validator->validate(text, pos) != QValidator::Acceptable)
 
161
            return;
 
162
    }
 
163
 
 
164
    QVariant originalValue = index.model()->data(index, Qt::UserRole);
 
165
    QVariant value;
 
166
 
 
167
    switch (originalValue.type()) {
 
168
    case QVariant::Char:
 
169
        value = text.at(0);
 
170
        break;
 
171
    case QVariant::Color:
 
172
        colorExp.exactMatch(text);
 
173
        value = QColor(qMin(colorExp.cap(1).toInt(), 255),
 
174
                       qMin(colorExp.cap(2).toInt(), 255),
 
175
                       qMin(colorExp.cap(3).toInt(), 255),
 
176
                       qMin(colorExp.cap(4).toInt(), 255));
 
177
        break;
 
178
    case QVariant::Date:
 
179
        {
 
180
            QDate date = QDate::fromString(text, Qt::ISODate);
 
181
            if (!date.isValid())
 
182
                return;
 
183
            value = date;
 
184
        }
 
185
        break;
 
186
    case QVariant::DateTime:
 
187
        {
 
188
            QDateTime dateTime = QDateTime::fromString(text, Qt::ISODate);
 
189
            if (!dateTime.isValid())
 
190
                return;
 
191
            value = dateTime;
 
192
        }
 
193
        break;
 
194
    case QVariant::Point:
 
195
        pointExp.exactMatch(text);
 
196
        value = QPoint(pointExp.cap(1).toInt(), pointExp.cap(2).toInt());
 
197
        break;
 
198
    case QVariant::Rect:
 
199
        rectExp.exactMatch(text);
 
200
        value = QRect(rectExp.cap(1).toInt(), rectExp.cap(2).toInt(),
 
201
                      rectExp.cap(3).toInt(), rectExp.cap(4).toInt());
 
202
        break;
 
203
    case QVariant::Size:
 
204
        sizeExp.exactMatch(text);
 
205
        value = QSize(sizeExp.cap(1).toInt(), sizeExp.cap(2).toInt());
 
206
        break;
 
207
    case QVariant::StringList:
 
208
        value = text.split(",");
 
209
        break;
 
210
    case QVariant::Time:
 
211
        {
 
212
            QTime time = QTime::fromString(text, Qt::ISODate);
 
213
            if (!time.isValid())
 
214
                return;
 
215
            value = time;
 
216
        }
 
217
        break;
 
218
    default:
 
219
        value = text;
 
220
        value.convert(originalValue.type());
 
221
    }
 
222
 
 
223
    model->setData(index, displayText(value), Qt::DisplayRole);
 
224
    model->setData(index, value, Qt::UserRole);
 
225
}
 
226
 
 
227
bool VariantDelegate::isSupportedType(QVariant::Type type)
 
228
{
 
229
    switch (type) {
 
230
    case QVariant::Bool:
 
231
    case QVariant::ByteArray:
 
232
    case QVariant::Char:
 
233
    case QVariant::Color:
 
234
    case QVariant::Date:
 
235
    case QVariant::DateTime:
 
236
    case QVariant::Double:
 
237
    case QVariant::Int:
 
238
    case QVariant::LongLong:
 
239
    case QVariant::Point:
 
240
    case QVariant::Rect:
 
241
    case QVariant::Size:
 
242
    case QVariant::String:
 
243
    case QVariant::StringList:
 
244
    case QVariant::Time:
 
245
    case QVariant::UInt:
 
246
    case QVariant::ULongLong:
 
247
        return true;
 
248
    default:
 
249
        return false;
 
250
    }
 
251
}
 
252
 
 
253
QString VariantDelegate::displayText(const QVariant &value)
 
254
{
 
255
    switch (value.type()) {
 
256
    case QVariant::Bool:
 
257
    case QVariant::ByteArray:
 
258
    case QVariant::Char:
 
259
    case QVariant::Double:
 
260
    case QVariant::Int:
 
261
    case QVariant::LongLong:
 
262
    case QVariant::String:
 
263
    case QVariant::UInt:
 
264
    case QVariant::ULongLong:
 
265
        return value.toString();
 
266
    case QVariant::Color:
 
267
        {
 
268
            QColor color = qvariant_cast<QColor>(value);
 
269
            return QString("(%1,%2,%3,%4)")
 
270
                   .arg(color.red()).arg(color.green())
 
271
                   .arg(color.blue()).arg(color.alpha());
 
272
        }
 
273
    case QVariant::Date:
 
274
        return value.toDate().toString(Qt::ISODate);
 
275
    case QVariant::DateTime:
 
276
        return value.toDateTime().toString(Qt::ISODate);
 
277
    case QVariant::Invalid:
 
278
        return "<Invalid>";
 
279
    case QVariant::Point:
 
280
        {
 
281
            QPoint point = value.toPoint();
 
282
            return QString("(%1,%2)").arg(point.x()).arg(point.y());
 
283
        }
 
284
    case QVariant::Rect:
 
285
        {
 
286
            QRect rect = value.toRect();
 
287
            return QString("(%1,%2,%3,%4)")
 
288
                   .arg(rect.x()).arg(rect.y())
 
289
                   .arg(rect.width()).arg(rect.height());
 
290
        }
 
291
    case QVariant::Size:
 
292
        {
 
293
            QSize size = value.toSize();
 
294
            return QString("(%1,%2)").arg(size.width()).arg(size.height());
 
295
        }
 
296
    case QVariant::StringList:
 
297
        return value.toStringList().join(",");
 
298
    case QVariant::Time:
 
299
        return value.toTime().toString(Qt::ISODate);
 
300
    }
 
301
    return QString("<%1>").arg(value.typeName());
 
302
}
 
303
 
 
304
void VariantDelegate::commitAndCloseEditor()
 
305
{
 
306
    QWidget *editor = qobject_cast<QWidget *>(sender());
 
307
    emit commitData(editor);
 
308
    emit closeEditor(editor);
 
309
}