| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 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.
|
|---|
| 23 | **
|
|---|
| 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.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 |
|
|---|
| 44 | #include "variantdelegate.h"
|
|---|
| 45 |
|
|---|
| 46 | VariantDelegate::VariantDelegate(QObject *parent)
|
|---|
| 47 | : QItemDelegate(parent)
|
|---|
| 48 | {
|
|---|
| 49 | boolExp.setPattern("true|false");
|
|---|
| 50 | boolExp.setCaseSensitivity(Qt::CaseInsensitive);
|
|---|
| 51 |
|
|---|
| 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]*");
|
|---|
| 61 |
|
|---|
| 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());
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | void VariantDelegate::paint(QPainter *painter,
|
|---|
| 68 | const QStyleOptionViewItem &option,
|
|---|
| 69 | const QModelIndex &index) const
|
|---|
| 70 | {
|
|---|
| 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;
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | QItemDelegate::paint(painter, option, index);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | QWidget *VariantDelegate::createEditor(QWidget *parent,
|
|---|
| 85 | const QStyleOptionViewItem & /* option */,
|
|---|
| 86 | const QModelIndex &index) const
|
|---|
| 87 | {
|
|---|
| 88 | if (index.column() != 2)
|
|---|
| 89 | return 0;
|
|---|
| 90 |
|
|---|
| 91 | QVariant originalValue = index.model()->data(index, Qt::UserRole);
|
|---|
| 92 | if (!isSupportedType(originalValue.type()))
|
|---|
| 93 | return 0;
|
|---|
| 94 |
|
|---|
| 95 | QLineEdit *lineEdit = new QLineEdit(parent);
|
|---|
| 96 | lineEdit->setFrame(false);
|
|---|
| 97 |
|
|---|
| 98 | QRegExp regExp;
|
|---|
| 99 |
|
|---|
| 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:
|
|---|
| 143 | ;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | if (!regExp.isEmpty()) {
|
|---|
| 147 | QValidator *validator = new QRegExpValidator(regExp, lineEdit);
|
|---|
| 148 | lineEdit->setValidator(validator);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | return lineEdit;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | void VariantDelegate::setEditorData(QWidget *editor,
|
|---|
| 155 | const QModelIndex &index) const
|
|---|
| 156 | {
|
|---|
| 157 | QVariant value = index.model()->data(index, Qt::UserRole);
|
|---|
| 158 | if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor))
|
|---|
| 159 | lineEdit->setText(displayText(value));
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | void VariantDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
|---|
| 163 | const QModelIndex &index) const
|
|---|
| 164 | {
|
|---|
| 165 | QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor);
|
|---|
| 166 | if (!lineEdit->isModified())
|
|---|
| 167 | return;
|
|---|
| 168 |
|
|---|
| 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;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | QVariant originalValue = index.model()->data(index, Qt::UserRole);
|
|---|
| 178 | QVariant value;
|
|---|
| 179 |
|
|---|
| 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:
|
|---|
| 192 | {
|
|---|
| 193 | QDate date = QDate::fromString(text, Qt::ISODate);
|
|---|
| 194 | if (!date.isValid())
|
|---|
| 195 | return;
|
|---|
| 196 | value = date;
|
|---|
| 197 | }
|
|---|
| 198 | break;
|
|---|
| 199 | case QVariant::DateTime:
|
|---|
| 200 | {
|
|---|
| 201 | QDateTime dateTime = QDateTime::fromString(text, Qt::ISODate);
|
|---|
| 202 | if (!dateTime.isValid())
|
|---|
| 203 | return;
|
|---|
| 204 | value = dateTime;
|
|---|
| 205 | }
|
|---|
| 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:
|
|---|
| 224 | {
|
|---|
| 225 | QTime time = QTime::fromString(text, Qt::ISODate);
|
|---|
| 226 | if (!time.isValid())
|
|---|
| 227 | return;
|
|---|
| 228 | value = time;
|
|---|
| 229 | }
|
|---|
| 230 | break;
|
|---|
| 231 | default:
|
|---|
| 232 | value = text;
|
|---|
| 233 | value.convert(originalValue.type());
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | model->setData(index, displayText(value), Qt::DisplayRole);
|
|---|
| 237 | model->setData(index, value, Qt::UserRole);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | bool VariantDelegate::isSupportedType(QVariant::Type type)
|
|---|
| 241 | {
|
|---|
| 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;
|
|---|
| 263 | }
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | QString VariantDelegate::displayText(const QVariant &value)
|
|---|
| 267 | {
|
|---|
| 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:
|
|---|
| 280 | {
|
|---|
| 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());
|
|---|
| 285 | }
|
|---|
| 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:
|
|---|
| 293 | {
|
|---|
| 294 | QPoint point = value.toPoint();
|
|---|
| 295 | return QString("(%1,%2)").arg(point.x()).arg(point.y());
|
|---|
| 296 | }
|
|---|
| 297 | case QVariant::Rect:
|
|---|
| 298 | {
|
|---|
| 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());
|
|---|
| 303 | }
|
|---|
| 304 | case QVariant::Size:
|
|---|
| 305 | {
|
|---|
| 306 | QSize size = value.toSize();
|
|---|
| 307 | return QString("(%1,%2)").arg(size.width()).arg(size.height());
|
|---|
| 308 | }
|
|---|
| 309 | case QVariant::StringList:
|
|---|
| 310 | return value.toStringList().join(",");
|
|---|
| 311 | case QVariant::Time:
|
|---|
| 312 | return value.toTime().toString(Qt::ISODate);
|
|---|
| 313 | default:
|
|---|
| 314 | break;
|
|---|
| 315 | }
|
|---|
| 316 | return QString("<%1>").arg(value.typeName());
|
|---|
| 317 | }
|
|---|