1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the examples of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
44 #include "variantdelegate.h"
46 VariantDelegate::VariantDelegate(QObject
*parent
)
47 : QItemDelegate(parent
)
49 boolExp
.setPattern("true|false");
50 boolExp
.setCaseSensitivity(Qt::CaseInsensitive
);
52 byteArrayExp
.setPattern("[\\x00-\\xff]*");
53 charExp
.setPattern(".");
54 colorExp
.setPattern("\\(([0-9]*),([0-9]*),([0-9]*),([0-9]*)\\)");
55 doubleExp
.setPattern("");
56 pointExp
.setPattern("\\((-?[0-9]*),(-?[0-9]*)\\)");
57 rectExp
.setPattern("\\((-?[0-9]*),(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)\\)");
58 signedIntegerExp
.setPattern("-?[0-9]*");
60 unsignedIntegerExp
.setPattern("[0-9]*");
62 dateExp
.setPattern("([0-9]{,4})-([0-9]{,2})-([0-9]{,2})");
63 timeExp
.setPattern("([0-9]{,2}):([0-9]{,2}):([0-9]{,2})");
64 dateTimeExp
.setPattern(dateExp
.pattern() + "T" + timeExp
.pattern());
67 void VariantDelegate::paint(QPainter
*painter
,
68 const QStyleOptionViewItem
&option
,
69 const QModelIndex
&index
) const
71 if (index
.column() == 2) {
72 QVariant value
= index
.model()->data(index
, Qt::UserRole
);
73 if (!isSupportedType(value
.type())) {
74 QStyleOptionViewItem myOption
= option
;
75 myOption
.state
&= ~QStyle::State_Enabled
;
76 QItemDelegate::paint(painter
, myOption
, index
);
81 QItemDelegate::paint(painter
, option
, index
);
84 QWidget
*VariantDelegate::createEditor(QWidget
*parent
,
85 const QStyleOptionViewItem
& /* option */,
86 const QModelIndex
&index
) const
88 if (index
.column() != 2)
91 QVariant originalValue
= index
.model()->data(index
, Qt::UserRole
);
92 if (!isSupportedType(originalValue
.type()))
95 QLineEdit
*lineEdit
= new QLineEdit(parent
);
96 lineEdit
->setFrame(false);
100 switch (originalValue
.type()) {
104 case QVariant::ByteArray
:
105 regExp
= byteArrayExp
;
110 case QVariant::Color
:
116 case QVariant::DateTime
:
117 regExp
= dateTimeExp
;
119 case QVariant::Double
:
123 case QVariant::LongLong
:
124 regExp
= signedIntegerExp
;
126 case QVariant::Point
:
139 case QVariant::ULongLong
:
140 regExp
= unsignedIntegerExp
;
146 if (!regExp
.isEmpty()) {
147 QValidator
*validator
= new QRegExpValidator(regExp
, lineEdit
);
148 lineEdit
->setValidator(validator
);
154 void VariantDelegate::setEditorData(QWidget
*editor
,
155 const QModelIndex
&index
) const
157 QVariant value
= index
.model()->data(index
, Qt::UserRole
);
158 if (QLineEdit
*lineEdit
= qobject_cast
<QLineEdit
*>(editor
))
159 lineEdit
->setText(displayText(value
));
162 void VariantDelegate::setModelData(QWidget
*editor
, QAbstractItemModel
*model
,
163 const QModelIndex
&index
) const
165 QLineEdit
*lineEdit
= qobject_cast
<QLineEdit
*>(editor
);
166 if (!lineEdit
->isModified())
169 QString text
= lineEdit
->text();
170 const QValidator
*validator
= lineEdit
->validator();
173 if (validator
->validate(text
, pos
) != QValidator::Acceptable
)
177 QVariant originalValue
= index
.model()->data(index
, Qt::UserRole
);
180 switch (originalValue
.type()) {
184 case QVariant::Color
:
185 colorExp
.exactMatch(text
);
186 value
= QColor(qMin(colorExp
.cap(1).toInt(), 255),
187 qMin(colorExp
.cap(2).toInt(), 255),
188 qMin(colorExp
.cap(3).toInt(), 255),
189 qMin(colorExp
.cap(4).toInt(), 255));
193 QDate date
= QDate::fromString(text
, Qt::ISODate
);
199 case QVariant::DateTime
:
201 QDateTime dateTime
= QDateTime::fromString(text
, Qt::ISODate
);
202 if (!dateTime
.isValid())
207 case QVariant::Point
:
208 pointExp
.exactMatch(text
);
209 value
= QPoint(pointExp
.cap(1).toInt(), pointExp
.cap(2).toInt());
212 rectExp
.exactMatch(text
);
213 value
= QRect(rectExp
.cap(1).toInt(), rectExp
.cap(2).toInt(),
214 rectExp
.cap(3).toInt(), rectExp
.cap(4).toInt());
217 sizeExp
.exactMatch(text
);
218 value
= QSize(sizeExp
.cap(1).toInt(), sizeExp
.cap(2).toInt());
220 case QVariant::StringList
:
221 value
= text
.split(",");
225 QTime time
= QTime::fromString(text
, Qt::ISODate
);
233 value
.convert(originalValue
.type());
236 model
->setData(index
, displayText(value
), Qt::DisplayRole
);
237 model
->setData(index
, value
, Qt::UserRole
);
240 bool VariantDelegate::isSupportedType(QVariant::Type type
)
244 case QVariant::ByteArray
:
246 case QVariant::Color
:
248 case QVariant::DateTime
:
249 case QVariant::Double
:
251 case QVariant::LongLong
:
252 case QVariant::Point
:
255 case QVariant::String
:
256 case QVariant::StringList
:
259 case QVariant::ULongLong
:
266 QString
VariantDelegate::displayText(const QVariant
&value
)
268 switch (value
.type()) {
270 case QVariant::ByteArray
:
272 case QVariant::Double
:
274 case QVariant::LongLong
:
275 case QVariant::String
:
277 case QVariant::ULongLong
:
278 return value
.toString();
279 case QVariant::Color
:
281 QColor color
= qvariant_cast
<QColor
>(value
);
282 return QString("(%1,%2,%3,%4)")
283 .arg(color
.red()).arg(color
.green())
284 .arg(color
.blue()).arg(color
.alpha());
287 return value
.toDate().toString(Qt::ISODate
);
288 case QVariant::DateTime
:
289 return value
.toDateTime().toString(Qt::ISODate
);
290 case QVariant::Invalid
:
292 case QVariant::Point
:
294 QPoint point
= value
.toPoint();
295 return QString("(%1,%2)").arg(point
.x()).arg(point
.y());
299 QRect rect
= value
.toRect();
300 return QString("(%1,%2,%3,%4)")
301 .arg(rect
.x()).arg(rect
.y())
302 .arg(rect
.width()).arg(rect
.height());
306 QSize size
= value
.toSize();
307 return QString("(%1,%2)").arg(size
.width()).arg(size
.height());
309 case QVariant::StringList
:
310 return value
.toStringList().join(",");
312 return value
.toTime().toString(Qt::ISODate
);
316 return QString("<%1>").arg(value
.typeName());