summaryrefslogtreecommitdiffstats
path: root/examples/widgets/tools/settingseditor/variantdelegate.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/tools/settingseditor/variantdelegate.cpp')
-rw-r--r--examples/widgets/tools/settingseditor/variantdelegate.cpp329
1 files changed, 0 insertions, 329 deletions
diff --git a/examples/widgets/tools/settingseditor/variantdelegate.cpp b/examples/widgets/tools/settingseditor/variantdelegate.cpp
deleted file mode 100644
index eb822f0dc2..0000000000
--- a/examples/widgets/tools/settingseditor/variantdelegate.cpp
+++ /dev/null
@@ -1,329 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the examples of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** 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:
-**
-** "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.
-** * 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.
-**
-**
-** 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."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#include "variantdelegate.h"
-
-#include <QDateTime>
-#include <QLineEdit>
-#include <QRegularExpressionValidator>
-
-VariantDelegate::VariantDelegate(QObject *parent)
- : QStyledItemDelegate(parent)
-{
- boolExp.setPattern("true|false");
- boolExp.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
-
- byteArrayExp.setPattern("[\\x00-\\xff]*");
- charExp.setPattern(".");
- colorExp.setPattern("^\\(([0-9]*),([0-9]*),([0-9]*),([0-9]*)\\)$");
- doubleExp.setPattern("");
- pointExp.setPattern("^\\((-?[0-9]*),(-?[0-9]*)\\)$");
- rectExp.setPattern("^\\((-?[0-9]*),(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)\\)$");
- 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})");
- dateTimeExp.setPattern(dateExp.pattern() + 'T' + timeExp.pattern());
-}
-
-void VariantDelegate::paint(QPainter *painter,
- const QStyleOptionViewItem &option,
- const QModelIndex &index) const
-{
- if (index.column() == 2) {
- QVariant value = index.model()->data(index, Qt::UserRole);
- if (!isSupportedType(value.userType())) {
- QStyleOptionViewItem myOption = option;
- myOption.state &= ~QStyle::State_Enabled;
- QStyledItemDelegate::paint(painter, myOption, index);
- return;
- }
- }
-
- QStyledItemDelegate::paint(painter, option, index);
-}
-
-QWidget *VariantDelegate::createEditor(QWidget *parent,
- const QStyleOptionViewItem & /* option */,
- const QModelIndex &index) const
-{
- if (index.column() != 2)
- return nullptr;
-
- QVariant originalValue = index.model()->data(index, Qt::UserRole);
- if (!isSupportedType(originalValue.userType()))
- return nullptr;
-
- QLineEdit *lineEdit = new QLineEdit(parent);
- lineEdit->setFrame(false);
-
- QRegularExpression regExp;
-
- switch (originalValue.userType()) {
- case QMetaType::Bool:
- regExp = boolExp;
- break;
- case QMetaType::QByteArray:
- regExp = byteArrayExp;
- break;
- case QMetaType::QChar:
- regExp = charExp;
- break;
- case QMetaType::QColor:
- regExp = colorExp;
- break;
- case QMetaType::QDate:
- regExp = dateExp;
- break;
- case QMetaType::QDateTime:
- regExp = dateTimeExp;
- break;
- case QMetaType::Double:
- regExp = doubleExp;
- break;
- case QMetaType::Int:
- case QMetaType::LongLong:
- regExp = signedIntegerExp;
- break;
- case QMetaType::QPoint:
- regExp = pointExp;
- break;
- case QMetaType::QRect:
- regExp = rectExp;
- break;
- case QMetaType::QSize:
- regExp = sizeExp;
- break;
- case QMetaType::QTime:
- regExp = timeExp;
- break;
- case QMetaType::UInt:
- case QMetaType::ULongLong:
- regExp = unsignedIntegerExp;
- break;
- default:
- break;
- }
-
- if (regExp.isValid()) {
- QValidator *validator = new QRegularExpressionValidator(regExp, lineEdit);
- 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;
- QRegularExpressionMatch match;
-
- switch (originalValue.userType()) {
- case QMetaType::QChar:
- value = text.at(0);
- break;
- case QMetaType::QColor:
- 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));
- break;
- case QMetaType::QDate:
- {
- QDate date = QDate::fromString(text, Qt::ISODate);
- if (!date.isValid())
- return;
- value = date;
- }
- break;
- case QMetaType::QDateTime:
- {
- QDateTime dateTime = QDateTime::fromString(text, Qt::ISODate);
- if (!dateTime.isValid())
- return;
- value = dateTime;
- }
- break;
- case QMetaType::QPoint:
- match = pointExp.match(text);
- value = QPoint(match.captured(1).toInt(), match.captured(2).toInt());
- break;
- case QMetaType::QRect:
- match = rectExp.match(text);
- value = QRect(match.captured(1).toInt(), match.captured(2).toInt(),
- match.captured(3).toInt(), match.captured(4).toInt());
- break;
- case QMetaType::QSize:
- match = sizeExp.match(text);
- value = QSize(match.captured(1).toInt(), match.captured(2).toInt());
- break;
- case QMetaType::QStringList:
- value = text.split(',');
- break;
- case QMetaType::QTime:
- {
- QTime time = QTime::fromString(text, Qt::ISODate);
- if (!time.isValid())
- return;
- value = time;
- }
- break;
- default:
- value = text;
- value.convert(originalValue.userType());
- }
-
- model->setData(index, displayText(value), Qt::DisplayRole);
- model->setData(index, value, Qt::UserRole);
-}
-
-bool VariantDelegate::isSupportedType(int type)
-{
- switch (type) {
- 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:
- return true;
- default:
- return false;
- }
-}
-
-QString VariantDelegate::displayText(const QVariant &value)
-{
- 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:
- return value.toString();
- case QMetaType::QColor:
- {
- QColor color = qvariant_cast<QColor>(value);
- return QString("(%1,%2,%3,%4)")
- .arg(color.red()).arg(color.green())
- .arg(color.blue()).arg(color.alpha());
- }
- case QMetaType::QDate:
- return value.toDate().toString(Qt::ISODate);
- case QMetaType::QDateTime:
- return value.toDateTime().toString(Qt::ISODate);
- case QMetaType::UnknownType:
- return "<Invalid>";
- case QMetaType::QPoint:
- {
- QPoint point = value.toPoint();
- return QString("(%1,%2)").arg(point.x()).arg(point.y());
- }
- case QMetaType::QRect:
- {
- QRect rect = value.toRect();
- return QString("(%1,%2,%3,%4)")
- .arg(rect.x()).arg(rect.y())
- .arg(rect.width()).arg(rect.height());
- }
- case QMetaType::QSize:
- {
- QSize size = value.toSize();
- return QString("(%1,%2)").arg(size.width()).arg(size.height());
- }
- case QMetaType::QStringList:
- return value.toStringList().join(',');
- case QMetaType::QTime:
- return value.toTime().toString(Qt::ISODate);
- default:
- break;
- }
- return QString("<%1>").arg(value.typeName());
-}