1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "variantdelegate.h"
|
---|
44 |
|
---|
45 | VariantDelegate::VariantDelegate(QObject *parent)
|
---|
46 | : QItemDelegate(parent)
|
---|
47 | {
|
---|
48 | boolExp.setPattern("true|false");
|
---|
49 | boolExp.setCaseSensitivity(Qt::CaseInsensitive);
|
---|
50 |
|
---|
51 | byteArrayExp.setPattern("[\\x00-\\xff]*");
|
---|
52 | charExp.setPattern(".");
|
---|
53 | colorExp.setPattern("\\(([0-9]*),([0-9]*),([0-9]*),([0-9]*)\\)");
|
---|
54 | doubleExp.setPattern("");
|
---|
55 | pointExp.setPattern("\\((-?[0-9]*),(-?[0-9]*)\\)");
|
---|
56 | rectExp.setPattern("\\((-?[0-9]*),(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)\\)");
|
---|
57 | signedIntegerExp.setPattern("-?[0-9]*");
|
---|
58 | sizeExp = pointExp;
|
---|
59 | unsignedIntegerExp.setPattern("[0-9]*");
|
---|
60 |
|
---|
61 | dateExp.setPattern("([0-9]{,4})-([0-9]{,2})-([0-9]{,2})");
|
---|
62 | timeExp.setPattern("([0-9]{,2}):([0-9]{,2}):([0-9]{,2})");
|
---|
63 | dateTimeExp.setPattern(dateExp.pattern() + "T" + timeExp.pattern());
|
---|
64 | }
|
---|
65 |
|
---|
66 | void VariantDelegate::paint(QPainter *painter,
|
---|
67 | const QStyleOptionViewItem &option,
|
---|
68 | const QModelIndex &index) const
|
---|
69 | {
|
---|
70 | if (index.column() == 2) {
|
---|
71 | QVariant value = index.model()->data(index, Qt::UserRole);
|
---|
72 | if (!isSupportedType(value.type())) {
|
---|
73 | QStyleOptionViewItem myOption = option;
|
---|
74 | myOption.state &= ~QStyle::State_Enabled;
|
---|
75 | QItemDelegate::paint(painter, myOption, index);
|
---|
76 | return;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | QItemDelegate::paint(painter, option, index);
|
---|
81 | }
|
---|
82 |
|
---|
83 | QWidget *VariantDelegate::createEditor(QWidget *parent,
|
---|
84 | const QStyleOptionViewItem & /* option */,
|
---|
85 | const QModelIndex &index) const
|
---|
86 | {
|
---|
87 | if (index.column() != 2)
|
---|
88 | return 0;
|
---|
89 |
|
---|
90 | QVariant originalValue = index.model()->data(index, Qt::UserRole);
|
---|
91 | if (!isSupportedType(originalValue.type()))
|
---|
92 | return 0;
|
---|
93 |
|
---|
94 | QLineEdit *lineEdit = new QLineEdit(parent);
|
---|
95 | lineEdit->setFrame(false);
|
---|
96 |
|
---|
97 | QRegExp regExp;
|
---|
98 |
|
---|
99 | switch (originalValue.type()) {
|
---|
100 | case QVariant::Bool:
|
---|
101 | regExp = boolExp;
|
---|
102 | break;
|
---|
103 | case QVariant::ByteArray:
|
---|
104 | regExp = byteArrayExp;
|
---|
105 | break;
|
---|
106 | case QVariant::Char:
|
---|
107 | regExp = charExp;
|
---|
108 | break;
|
---|
109 | case QVariant::Color:
|
---|
110 | regExp = colorExp;
|
---|
111 | break;
|
---|
112 | case QVariant::Date:
|
---|
113 | regExp = dateExp;
|
---|
114 | break;
|
---|
115 | case QVariant::DateTime:
|
---|
116 | regExp = dateTimeExp;
|
---|
117 | break;
|
---|
118 | case QVariant::Double:
|
---|
119 | regExp = doubleExp;
|
---|
120 | break;
|
---|
121 | case QVariant::Int:
|
---|
122 | case QVariant::LongLong:
|
---|
123 | regExp = signedIntegerExp;
|
---|
124 | break;
|
---|
125 | case QVariant::Point:
|
---|
126 | regExp = pointExp;
|
---|
127 | break;
|
---|
128 | case QVariant::Rect:
|
---|
129 | regExp = rectExp;
|
---|
130 | break;
|
---|
131 | case QVariant::Size:
|
---|
132 | regExp = sizeExp;
|
---|
133 | break;
|
---|
134 | case QVariant::Time:
|
---|
135 | regExp = timeExp;
|
---|
136 | break;
|
---|
137 | case QVariant::UInt:
|
---|
138 | case QVariant::ULongLong:
|
---|
139 | regExp = unsignedIntegerExp;
|
---|
140 | break;
|
---|
141 | default:
|
---|
142 | ;
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (!regExp.isEmpty()) {
|
---|
146 | QValidator *validator = new QRegExpValidator(regExp, lineEdit);
|
---|
147 | lineEdit->setValidator(validator);
|
---|
148 | }
|
---|
149 |
|
---|
150 | return lineEdit;
|
---|
151 | }
|
---|
152 |
|
---|
153 | void VariantDelegate::setEditorData(QWidget *editor,
|
---|
154 | const QModelIndex &index) const
|
---|
155 | {
|
---|
156 | QVariant value = index.model()->data(index, Qt::UserRole);
|
---|
157 | if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor))
|
---|
158 | lineEdit->setText(displayText(value));
|
---|
159 | }
|
---|
160 |
|
---|
161 | void VariantDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
---|
162 | const QModelIndex &index) const
|
---|
163 | {
|
---|
164 | QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor);
|
---|
165 | if (!lineEdit->isModified())
|
---|
166 | return;
|
---|
167 |
|
---|
168 | QString text = lineEdit->text();
|
---|
169 | const QValidator *validator = lineEdit->validator();
|
---|
170 | if (validator) {
|
---|
171 | int pos;
|
---|
172 | if (validator->validate(text, pos) != QValidator::Acceptable)
|
---|
173 | return;
|
---|
174 | }
|
---|
175 |
|
---|
176 | QVariant originalValue = index.model()->data(index, Qt::UserRole);
|
---|
177 | QVariant value;
|
---|
178 |
|
---|
179 | switch (originalValue.type()) {
|
---|
180 | case QVariant::Char:
|
---|
181 | value = text.at(0);
|
---|
182 | break;
|
---|
183 | case QVariant::Color:
|
---|
184 | colorExp.exactMatch(text);
|
---|
185 | value = QColor(qMin(colorExp.cap(1).toInt(), 255),
|
---|
186 | qMin(colorExp.cap(2).toInt(), 255),
|
---|
187 | qMin(colorExp.cap(3).toInt(), 255),
|
---|
188 | qMin(colorExp.cap(4).toInt(), 255));
|
---|
189 | break;
|
---|
190 | case QVariant::Date:
|
---|
191 | {
|
---|
192 | QDate date = QDate::fromString(text, Qt::ISODate);
|
---|
193 | if (!date.isValid())
|
---|
194 | return;
|
---|
195 | value = date;
|
---|
196 | }
|
---|
197 | break;
|
---|
198 | case QVariant::DateTime:
|
---|
199 | {
|
---|
200 | QDateTime dateTime = QDateTime::fromString(text, Qt::ISODate);
|
---|
201 | if (!dateTime.isValid())
|
---|
202 | return;
|
---|
203 | value = dateTime;
|
---|
204 | }
|
---|
205 | break;
|
---|
206 | case QVariant::Point:
|
---|
207 | pointExp.exactMatch(text);
|
---|
208 | value = QPoint(pointExp.cap(1).toInt(), pointExp.cap(2).toInt());
|
---|
209 | break;
|
---|
210 | case QVariant::Rect:
|
---|
211 | rectExp.exactMatch(text);
|
---|
212 | value = QRect(rectExp.cap(1).toInt(), rectExp.cap(2).toInt(),
|
---|
213 | rectExp.cap(3).toInt(), rectExp.cap(4).toInt());
|
---|
214 | break;
|
---|
215 | case QVariant::Size:
|
---|
216 | sizeExp.exactMatch(text);
|
---|
217 | value = QSize(sizeExp.cap(1).toInt(), sizeExp.cap(2).toInt());
|
---|
218 | break;
|
---|
219 | case QVariant::StringList:
|
---|
220 | value = text.split(",");
|
---|
221 | break;
|
---|
222 | case QVariant::Time:
|
---|
223 | {
|
---|
224 | QTime time = QTime::fromString(text, Qt::ISODate);
|
---|
225 | if (!time.isValid())
|
---|
226 | return;
|
---|
227 | value = time;
|
---|
228 | }
|
---|
229 | break;
|
---|
230 | default:
|
---|
231 | value = text;
|
---|
232 | value.convert(originalValue.type());
|
---|
233 | }
|
---|
234 |
|
---|
235 | model->setData(index, displayText(value), Qt::DisplayRole);
|
---|
236 | model->setData(index, value, Qt::UserRole);
|
---|
237 | }
|
---|
238 |
|
---|
239 | bool VariantDelegate::isSupportedType(QVariant::Type type)
|
---|
240 | {
|
---|
241 | switch (type) {
|
---|
242 | case QVariant::Bool:
|
---|
243 | case QVariant::ByteArray:
|
---|
244 | case QVariant::Char:
|
---|
245 | case QVariant::Color:
|
---|
246 | case QVariant::Date:
|
---|
247 | case QVariant::DateTime:
|
---|
248 | case QVariant::Double:
|
---|
249 | case QVariant::Int:
|
---|
250 | case QVariant::LongLong:
|
---|
251 | case QVariant::Point:
|
---|
252 | case QVariant::Rect:
|
---|
253 | case QVariant::Size:
|
---|
254 | case QVariant::String:
|
---|
255 | case QVariant::StringList:
|
---|
256 | case QVariant::Time:
|
---|
257 | case QVariant::UInt:
|
---|
258 | case QVariant::ULongLong:
|
---|
259 | return true;
|
---|
260 | default:
|
---|
261 | return false;
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | QString VariantDelegate::displayText(const QVariant &value)
|
---|
266 | {
|
---|
267 | switch (value.type()) {
|
---|
268 | case QVariant::Bool:
|
---|
269 | case QVariant::ByteArray:
|
---|
270 | case QVariant::Char:
|
---|
271 | case QVariant::Double:
|
---|
272 | case QVariant::Int:
|
---|
273 | case QVariant::LongLong:
|
---|
274 | case QVariant::String:
|
---|
275 | case QVariant::UInt:
|
---|
276 | case QVariant::ULongLong:
|
---|
277 | return value.toString();
|
---|
278 | case QVariant::Color:
|
---|
279 | {
|
---|
280 | QColor color = qvariant_cast<QColor>(value);
|
---|
281 | return QString("(%1,%2,%3,%4)")
|
---|
282 | .arg(color.red()).arg(color.green())
|
---|
283 | .arg(color.blue()).arg(color.alpha());
|
---|
284 | }
|
---|
285 | case QVariant::Date:
|
---|
286 | return value.toDate().toString(Qt::ISODate);
|
---|
287 | case QVariant::DateTime:
|
---|
288 | return value.toDateTime().toString(Qt::ISODate);
|
---|
289 | case QVariant::Invalid:
|
---|
290 | return "<Invalid>";
|
---|
291 | case QVariant::Point:
|
---|
292 | {
|
---|
293 | QPoint point = value.toPoint();
|
---|
294 | return QString("(%1,%2)").arg(point.x()).arg(point.y());
|
---|
295 | }
|
---|
296 | case QVariant::Rect:
|
---|
297 | {
|
---|
298 | QRect rect = value.toRect();
|
---|
299 | return QString("(%1,%2,%3,%4)")
|
---|
300 | .arg(rect.x()).arg(rect.y())
|
---|
301 | .arg(rect.width()).arg(rect.height());
|
---|
302 | }
|
---|
303 | case QVariant::Size:
|
---|
304 | {
|
---|
305 | QSize size = value.toSize();
|
---|
306 | return QString("(%1,%2)").arg(size.width()).arg(size.height());
|
---|
307 | }
|
---|
308 | case QVariant::StringList:
|
---|
309 | return value.toStringList().join(",");
|
---|
310 | case QVariant::Time:
|
---|
311 | return value.toTime().toString(Qt::ISODate);
|
---|
312 | default:
|
---|
313 | break;
|
---|
314 | }
|
---|
315 | return QString("<%1>").arg(value.typeName());
|
---|
316 | }
|
---|