Merge branch 'master' of scm.dev.nokia.troll.no:qt/oslo-staging-1 into master-integration
[qt-netbsd.git] / examples / tools / settingseditor / variantdelegate.cpp
blob330233792c155cbd4303f9352e13afa3fe092450
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 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: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
14 ** this package.
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.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include <QtGui>
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]*");
59 sizeExp = pointExp;
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);
77 return;
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)
89 return 0;
91 QVariant originalValue = index.model()->data(index, Qt::UserRole);
92 if (!isSupportedType(originalValue.type()))
93 return 0;
95 QLineEdit *lineEdit = new QLineEdit(parent);
96 lineEdit->setFrame(false);
98 QRegExp regExp;
100 switch (originalValue.type()) {
101 case QVariant::Bool:
102 regExp = boolExp;
103 break;
104 case QVariant::ByteArray:
105 regExp = byteArrayExp;
106 break;
107 case QVariant::Char:
108 regExp = charExp;
109 break;
110 case QVariant::Color:
111 regExp = colorExp;
112 break;
113 case QVariant::Date:
114 regExp = dateExp;
115 break;
116 case QVariant::DateTime:
117 regExp = dateTimeExp;
118 break;
119 case QVariant::Double:
120 regExp = doubleExp;
121 break;
122 case QVariant::Int:
123 case QVariant::LongLong:
124 regExp = signedIntegerExp;
125 break;
126 case QVariant::Point:
127 regExp = pointExp;
128 break;
129 case QVariant::Rect:
130 regExp = rectExp;
131 break;
132 case QVariant::Size:
133 regExp = sizeExp;
134 break;
135 case QVariant::Time:
136 regExp = timeExp;
137 break;
138 case QVariant::UInt:
139 case QVariant::ULongLong:
140 regExp = unsignedIntegerExp;
141 break;
142 default:
146 if (!regExp.isEmpty()) {
147 QValidator *validator = new QRegExpValidator(regExp, lineEdit);
148 lineEdit->setValidator(validator);
151 return lineEdit;
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())
167 return;
169 QString text = lineEdit->text();
170 const QValidator *validator = lineEdit->validator();
171 if (validator) {
172 int pos;
173 if (validator->validate(text, pos) != QValidator::Acceptable)
174 return;
177 QVariant originalValue = index.model()->data(index, Qt::UserRole);
178 QVariant value;
180 switch (originalValue.type()) {
181 case QVariant::Char:
182 value = text.at(0);
183 break;
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));
190 break;
191 case QVariant::Date:
193 QDate date = QDate::fromString(text, Qt::ISODate);
194 if (!date.isValid())
195 return;
196 value = date;
198 break;
199 case QVariant::DateTime:
201 QDateTime dateTime = QDateTime::fromString(text, Qt::ISODate);
202 if (!dateTime.isValid())
203 return;
204 value = dateTime;
206 break;
207 case QVariant::Point:
208 pointExp.exactMatch(text);
209 value = QPoint(pointExp.cap(1).toInt(), pointExp.cap(2).toInt());
210 break;
211 case QVariant::Rect:
212 rectExp.exactMatch(text);
213 value = QRect(rectExp.cap(1).toInt(), rectExp.cap(2).toInt(),
214 rectExp.cap(3).toInt(), rectExp.cap(4).toInt());
215 break;
216 case QVariant::Size:
217 sizeExp.exactMatch(text);
218 value = QSize(sizeExp.cap(1).toInt(), sizeExp.cap(2).toInt());
219 break;
220 case QVariant::StringList:
221 value = text.split(",");
222 break;
223 case QVariant::Time:
225 QTime time = QTime::fromString(text, Qt::ISODate);
226 if (!time.isValid())
227 return;
228 value = time;
230 break;
231 default:
232 value = text;
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)
242 switch (type) {
243 case QVariant::Bool:
244 case QVariant::ByteArray:
245 case QVariant::Char:
246 case QVariant::Color:
247 case QVariant::Date:
248 case QVariant::DateTime:
249 case QVariant::Double:
250 case QVariant::Int:
251 case QVariant::LongLong:
252 case QVariant::Point:
253 case QVariant::Rect:
254 case QVariant::Size:
255 case QVariant::String:
256 case QVariant::StringList:
257 case QVariant::Time:
258 case QVariant::UInt:
259 case QVariant::ULongLong:
260 return true;
261 default:
262 return false;
266 QString VariantDelegate::displayText(const QVariant &value)
268 switch (value.type()) {
269 case QVariant::Bool:
270 case QVariant::ByteArray:
271 case QVariant::Char:
272 case QVariant::Double:
273 case QVariant::Int:
274 case QVariant::LongLong:
275 case QVariant::String:
276 case QVariant::UInt:
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());
286 case QVariant::Date:
287 return value.toDate().toString(Qt::ISODate);
288 case QVariant::DateTime:
289 return value.toDateTime().toString(Qt::ISODate);
290 case QVariant::Invalid:
291 return "<Invalid>";
292 case QVariant::Point:
294 QPoint point = value.toPoint();
295 return QString("(%1,%2)").arg(point.x()).arg(point.y());
297 case QVariant::Rect:
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());
304 case QVariant::Size:
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(",");
311 case QVariant::Time:
312 return value.toTime().toString(Qt::ISODate);
313 default:
314 break;
316 return QString("<%1>").arg(value.typeName());