| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 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
|
|---|
|
|---|