1
/****************************************************************************
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
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.
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.
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.
21
** Contact info@trolltech.com if any conditions of this licensing are
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.
27
****************************************************************************/
31
#include "variantdelegate.h"
33
VariantDelegate::VariantDelegate(QObject *parent)
34
: QItemDelegate(parent)
36
boolExp.setPattern("true|false");
37
boolExp.setCaseSensitivity(Qt::CaseInsensitive);
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]*");
47
unsignedIntegerExp.setPattern("[0-9]*");
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());
54
void VariantDelegate::paint(QPainter *painter,
55
const QStyleOptionViewItem &option,
56
const QModelIndex &index) const
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);
68
QItemDelegate::paint(painter, option, index);
71
QWidget *VariantDelegate::createEditor(QWidget *parent,
72
const QStyleOptionViewItem & /* option */,
73
const QModelIndex &index) const
75
if (index.column() != 2)
78
QVariant originalValue = index.model()->data(index, Qt::UserRole);
79
if (!isSupportedType(originalValue.type()))
82
QLineEdit *lineEdit = new QLineEdit(parent);
83
lineEdit->setFrame(false);
87
switch (originalValue.type()) {
91
case QVariant::ByteArray:
92
regExp = byteArrayExp;
103
case QVariant::DateTime:
104
regExp = dateTimeExp;
106
case QVariant::Double:
110
case QVariant::LongLong:
111
regExp = signedIntegerExp;
113
case QVariant::Point:
126
case QVariant::ULongLong:
127
regExp = unsignedIntegerExp;
130
if (!regExp.isEmpty()) {
131
QValidator *validator = new QRegExpValidator(regExp, lineEdit);
132
lineEdit->setValidator(validator);
135
connect(lineEdit, SIGNAL(returnPressed()),
136
this, SLOT(commitAndCloseEditor()));
141
void VariantDelegate::setEditorData(QWidget *editor,
142
const QModelIndex &index) const
144
QVariant value = index.model()->data(index, Qt::UserRole);
145
if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor))
146
lineEdit->setText(displayText(value));
149
void VariantDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
150
const QModelIndex &index) const
152
QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor);
153
if (!lineEdit->isModified())
156
QString text = lineEdit->text();
157
const QValidator *validator = lineEdit->validator();
160
if (validator->validate(text, pos) != QValidator::Acceptable)
164
QVariant originalValue = index.model()->data(index, Qt::UserRole);
167
switch (originalValue.type()) {
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));
180
QDate date = QDate::fromString(text, Qt::ISODate);
186
case QVariant::DateTime:
188
QDateTime dateTime = QDateTime::fromString(text, Qt::ISODate);
189
if (!dateTime.isValid())
194
case QVariant::Point:
195
pointExp.exactMatch(text);
196
value = QPoint(pointExp.cap(1).toInt(), pointExp.cap(2).toInt());
199
rectExp.exactMatch(text);
200
value = QRect(rectExp.cap(1).toInt(), rectExp.cap(2).toInt(),
201
rectExp.cap(3).toInt(), rectExp.cap(4).toInt());
204
sizeExp.exactMatch(text);
205
value = QSize(sizeExp.cap(1).toInt(), sizeExp.cap(2).toInt());
207
case QVariant::StringList:
208
value = text.split(",");
212
QTime time = QTime::fromString(text, Qt::ISODate);
220
value.convert(originalValue.type());
223
model->setData(index, displayText(value), Qt::DisplayRole);
224
model->setData(index, value, Qt::UserRole);
227
bool VariantDelegate::isSupportedType(QVariant::Type type)
231
case QVariant::ByteArray:
233
case QVariant::Color:
235
case QVariant::DateTime:
236
case QVariant::Double:
238
case QVariant::LongLong:
239
case QVariant::Point:
242
case QVariant::String:
243
case QVariant::StringList:
246
case QVariant::ULongLong:
253
QString VariantDelegate::displayText(const QVariant &value)
255
switch (value.type()) {
257
case QVariant::ByteArray:
259
case QVariant::Double:
261
case QVariant::LongLong:
262
case QVariant::String:
264
case QVariant::ULongLong:
265
return value.toString();
266
case QVariant::Color:
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());
274
return value.toDate().toString(Qt::ISODate);
275
case QVariant::DateTime:
276
return value.toDateTime().toString(Qt::ISODate);
277
case QVariant::Invalid:
279
case QVariant::Point:
281
QPoint point = value.toPoint();
282
return QString("(%1,%2)").arg(point.x()).arg(point.y());
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());
293
QSize size = value.toSize();
294
return QString("(%1,%2)").arg(size.width()).arg(size.height());
296
case QVariant::StringList:
297
return value.toStringList().join(",");
299
return value.toTime().toString(Qt::ISODate);
301
return QString("<%1>").arg(value.typeName());
304
void VariantDelegate::commitAndCloseEditor()
306
QWidget *editor = qobject_cast<QWidget *>(sender());
307
emit commitData(editor);
308
emit closeEditor(editor);