2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-22 12:24:00 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the examples of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
2016-01-22 12:24:00 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
2015-02-13 11:15:33 +00:00
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
2012-01-24 06:17:24 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "variantdelegate.h"
|
|
|
|
|
2019-09-06 18:27:33 +00:00
|
|
|
#include <QDateTime>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QRegularExpressionValidator>
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
VariantDelegate::VariantDelegate(QObject *parent)
|
2019-09-06 18:27:33 +00:00
|
|
|
: QStyledItemDelegate(parent)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
boolExp.setPattern("true|false");
|
2017-01-21 21:57:20 +00:00
|
|
|
boolExp.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
byteArrayExp.setPattern("[\\x00-\\xff]*");
|
|
|
|
charExp.setPattern(".");
|
2017-01-21 21:57:20 +00:00
|
|
|
colorExp.setPattern("^\\(([0-9]*),([0-9]*),([0-9]*),([0-9]*)\\)$");
|
2011-04-27 10:05:43 +00:00
|
|
|
doubleExp.setPattern("");
|
2017-01-21 21:57:20 +00:00
|
|
|
pointExp.setPattern("^\\((-?[0-9]*),(-?[0-9]*)\\)$");
|
|
|
|
rectExp.setPattern("^\\((-?[0-9]*),(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)\\)$");
|
2011-04-27 10:05:43 +00:00
|
|
|
signedIntegerExp.setPattern("-?[0-9]*");
|
|
|
|
sizeExp = pointExp;
|
|
|
|
unsignedIntegerExp.setPattern("[0-9]*");
|
|
|
|
|
|
|
|
dateExp.setPattern("([0-9]{,4})-([0-9]{,2})-([0-9]{,2})");
|
|
|
|
timeExp.setPattern("([0-9]{,2}):([0-9]{,2}):([0-9]{,2})");
|
2015-10-13 07:06:58 +00:00
|
|
|
dateTimeExp.setPattern(dateExp.pattern() + 'T' + timeExp.pattern());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VariantDelegate::paint(QPainter *painter,
|
|
|
|
const QStyleOptionViewItem &option,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (index.column() == 2) {
|
|
|
|
QVariant value = index.model()->data(index, Qt::UserRole);
|
2019-12-02 16:54:48 +00:00
|
|
|
if (!isSupportedType(value.userType())) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QStyleOptionViewItem myOption = option;
|
|
|
|
myOption.state &= ~QStyle::State_Enabled;
|
2019-09-06 18:27:33 +00:00
|
|
|
QStyledItemDelegate::paint(painter, myOption, index);
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 18:27:33 +00:00
|
|
|
QStyledItemDelegate::paint(painter, option, index);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *VariantDelegate::createEditor(QWidget *parent,
|
|
|
|
const QStyleOptionViewItem & /* option */,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (index.column() != 2)
|
2019-09-06 18:27:33 +00:00
|
|
|
return nullptr;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QVariant originalValue = index.model()->data(index, Qt::UserRole);
|
2019-12-02 16:54:48 +00:00
|
|
|
if (!isSupportedType(originalValue.userType()))
|
2019-09-06 18:27:33 +00:00
|
|
|
return nullptr;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QLineEdit *lineEdit = new QLineEdit(parent);
|
|
|
|
lineEdit->setFrame(false);
|
|
|
|
|
2017-01-21 21:57:20 +00:00
|
|
|
QRegularExpression regExp;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-12-02 16:54:48 +00:00
|
|
|
switch (originalValue.userType()) {
|
|
|
|
case QMetaType::Bool:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = boolExp;
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QByteArray:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = byteArrayExp;
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QChar:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = charExp;
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QColor:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = colorExp;
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QDate:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = dateExp;
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QDateTime:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = dateTimeExp;
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::Double:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = doubleExp;
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::Int:
|
|
|
|
case QMetaType::LongLong:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = signedIntegerExp;
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QPoint:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = pointExp;
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QRect:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = rectExp;
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QSize:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = sizeExp;
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QTime:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = timeExp;
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::UInt:
|
|
|
|
case QMetaType::ULongLong:
|
2011-04-27 10:05:43 +00:00
|
|
|
regExp = unsignedIntegerExp;
|
|
|
|
break;
|
|
|
|
default:
|
2019-09-06 18:27:33 +00:00
|
|
|
break;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 21:57:20 +00:00
|
|
|
if (regExp.isValid()) {
|
|
|
|
QValidator *validator = new QRegularExpressionValidator(regExp, lineEdit);
|
2011-04-27 10:05:43 +00:00
|
|
|
lineEdit->setValidator(validator);
|
|
|
|
}
|
|
|
|
|
|
|
|
return lineEdit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VariantDelegate::setEditorData(QWidget *editor,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QVariant value = index.model()->data(index, Qt::UserRole);
|
|
|
|
if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor))
|
|
|
|
lineEdit->setText(displayText(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void VariantDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor);
|
|
|
|
if (!lineEdit->isModified())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString text = lineEdit->text();
|
|
|
|
const QValidator *validator = lineEdit->validator();
|
|
|
|
if (validator) {
|
|
|
|
int pos;
|
|
|
|
if (validator->validate(text, pos) != QValidator::Acceptable)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant originalValue = index.model()->data(index, Qt::UserRole);
|
|
|
|
QVariant value;
|
2017-01-21 21:57:20 +00:00
|
|
|
QRegularExpressionMatch match;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-12-02 16:54:48 +00:00
|
|
|
switch (originalValue.userType()) {
|
|
|
|
case QMetaType::QChar:
|
2011-04-27 10:05:43 +00:00
|
|
|
value = text.at(0);
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QColor:
|
2017-01-21 21:57:20 +00:00
|
|
|
match = colorExp.match(text);
|
|
|
|
value = QColor(qMin(match.captured(1).toInt(), 255),
|
|
|
|
qMin(match.captured(2).toInt(), 255),
|
|
|
|
qMin(match.captured(3).toInt(), 255),
|
|
|
|
qMin(match.captured(4).toInt(), 255));
|
2011-04-27 10:05:43 +00:00
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QDate:
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QDate date = QDate::fromString(text, Qt::ISODate);
|
|
|
|
if (!date.isValid())
|
|
|
|
return;
|
|
|
|
value = date;
|
|
|
|
}
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QDateTime:
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QDateTime dateTime = QDateTime::fromString(text, Qt::ISODate);
|
|
|
|
if (!dateTime.isValid())
|
|
|
|
return;
|
|
|
|
value = dateTime;
|
|
|
|
}
|
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QPoint:
|
2017-01-21 21:57:20 +00:00
|
|
|
match = pointExp.match(text);
|
|
|
|
value = QPoint(match.captured(1).toInt(), match.captured(2).toInt());
|
2011-04-27 10:05:43 +00:00
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QRect:
|
2017-01-21 21:57:20 +00:00
|
|
|
match = rectExp.match(text);
|
|
|
|
value = QRect(match.captured(1).toInt(), match.captured(2).toInt(),
|
|
|
|
match.captured(3).toInt(), match.captured(4).toInt());
|
2011-04-27 10:05:43 +00:00
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QSize:
|
2017-01-21 21:57:20 +00:00
|
|
|
match = sizeExp.match(text);
|
|
|
|
value = QSize(match.captured(1).toInt(), match.captured(2).toInt());
|
2011-04-27 10:05:43 +00:00
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QStringList:
|
2015-10-13 07:06:58 +00:00
|
|
|
value = text.split(',');
|
2011-04-27 10:05:43 +00:00
|
|
|
break;
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QTime:
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QTime time = QTime::fromString(text, Qt::ISODate);
|
|
|
|
if (!time.isValid())
|
|
|
|
return;
|
|
|
|
value = time;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = text;
|
2019-12-02 16:54:48 +00:00
|
|
|
value.convert(originalValue.userType());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
model->setData(index, displayText(value), Qt::DisplayRole);
|
|
|
|
model->setData(index, value, Qt::UserRole);
|
|
|
|
}
|
|
|
|
|
2019-12-02 16:54:48 +00:00
|
|
|
bool VariantDelegate::isSupportedType(int type)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
switch (type) {
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::Bool:
|
|
|
|
case QMetaType::QByteArray:
|
|
|
|
case QMetaType::QChar:
|
|
|
|
case QMetaType::QColor:
|
|
|
|
case QMetaType::QDate:
|
|
|
|
case QMetaType::QDateTime:
|
|
|
|
case QMetaType::Double:
|
|
|
|
case QMetaType::Int:
|
|
|
|
case QMetaType::LongLong:
|
|
|
|
case QMetaType::QPoint:
|
|
|
|
case QMetaType::QRect:
|
|
|
|
case QMetaType::QSize:
|
|
|
|
case QMetaType::QString:
|
|
|
|
case QMetaType::QStringList:
|
|
|
|
case QMetaType::QTime:
|
|
|
|
case QMetaType::UInt:
|
|
|
|
case QMetaType::ULongLong:
|
2011-04-27 10:05:43 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString VariantDelegate::displayText(const QVariant &value)
|
|
|
|
{
|
2019-12-02 16:54:48 +00:00
|
|
|
switch (value.userType()) {
|
|
|
|
case QMetaType::Bool:
|
|
|
|
case QMetaType::QByteArray:
|
|
|
|
case QMetaType::QChar:
|
|
|
|
case QMetaType::Double:
|
|
|
|
case QMetaType::Int:
|
|
|
|
case QMetaType::LongLong:
|
|
|
|
case QMetaType::QString:
|
|
|
|
case QMetaType::UInt:
|
|
|
|
case QMetaType::ULongLong:
|
2011-04-27 10:05:43 +00:00
|
|
|
return value.toString();
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QColor:
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QColor color = qvariant_cast<QColor>(value);
|
|
|
|
return QString("(%1,%2,%3,%4)")
|
|
|
|
.arg(color.red()).arg(color.green())
|
|
|
|
.arg(color.blue()).arg(color.alpha());
|
|
|
|
}
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QDate:
|
2011-04-27 10:05:43 +00:00
|
|
|
return value.toDate().toString(Qt::ISODate);
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QDateTime:
|
2011-04-27 10:05:43 +00:00
|
|
|
return value.toDateTime().toString(Qt::ISODate);
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::UnknownType:
|
2011-04-27 10:05:43 +00:00
|
|
|
return "<Invalid>";
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QPoint:
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QPoint point = value.toPoint();
|
|
|
|
return QString("(%1,%2)").arg(point.x()).arg(point.y());
|
|
|
|
}
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QRect:
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QRect rect = value.toRect();
|
|
|
|
return QString("(%1,%2,%3,%4)")
|
|
|
|
.arg(rect.x()).arg(rect.y())
|
|
|
|
.arg(rect.width()).arg(rect.height());
|
|
|
|
}
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QSize:
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QSize size = value.toSize();
|
|
|
|
return QString("(%1,%2)").arg(size.width()).arg(size.height());
|
|
|
|
}
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QStringList:
|
2012-05-18 18:00:23 +00:00
|
|
|
return value.toStringList().join(',');
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QTime:
|
2011-04-27 10:05:43 +00:00
|
|
|
return value.toTime().toString(Qt::ISODate);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QString("<%1>").arg(value.typeName());
|
|
|
|
}
|