/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** 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 Nokia Corporation and its Subsidiary(-ies) 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 <QtGui>#include "variantdelegate.h"
VariantDelegate::VariantDelegate(QObject*parent)
: QItemDelegate(parent)
{
boolExp.setPattern("true|false");
boolExp.setCaseSensitivity(Qt::CaseInsensitive);
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,constQStyleOptionViewItem&option,constQModelIndex&index) const
{
if (index.column() ==2) {
QVariant value = index.model()->data(index,Qt::UserRole);
if (!isSupportedType(value.type())) {
QStyleOptionViewItem myOption = option;
myOption.state &=~QStyle::State_Enabled;
QItemDelegate::paint(painter, myOption, index);
return;
}
}
QItemDelegate::paint(painter, option, index);
}
QWidget*VariantDelegate::createEditor(QWidget*parent,constQStyleOptionViewItem&/* option */,constQModelIndex&index) const
{
if (index.column() !=2)
return0;
QVariant originalValue = index.model()->data(index,Qt::UserRole);
if (!isSupportedType(originalValue.type()))
return0;
QLineEdit*lineEdit =newQLineEdit(parent);
lineEdit->setFrame(false);
QRegExp regExp;
switch (originalValue.type()) {
caseQVariant::Bool:
regExp = boolExp;
break;
caseQVariant::ByteArray:
regExp = byteArrayExp;
break;
caseQVariant::Char:
regExp = charExp;
break;
caseQVariant::Color:
regExp = colorExp;
break;
caseQVariant::Date:
regExp = dateExp;
break;
caseQVariant::DateTime:
regExp = dateTimeExp;
break;
caseQVariant::Double:
regExp = doubleExp;
break;
caseQVariant::Int:
caseQVariant::LongLong:
regExp = signedIntegerExp;
break;
caseQVariant::Point:
regExp = pointExp;
break;
caseQVariant::Rect:
regExp = rectExp;
break;
caseQVariant::Size:
regExp = sizeExp;
break;
caseQVariant::Time:
regExp = timeExp;
break;
caseQVariant::UInt:
caseQVariant::ULongLong:
regExp = unsignedIntegerExp;
break;
default:
;
}
if (!regExp.isEmpty()) {
QValidator*validator =newQRegExpValidator(regExp, lineEdit);
lineEdit->setValidator(validator);
}
return lineEdit;
}
void VariantDelegate::setEditorData(QWidget*editor,constQModelIndex&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,constQModelIndex&index) const
{
QLineEdit*lineEdit = qobject_cast<QLineEdit*>(editor);
if (!lineEdit->isModified())
return;
QString text = lineEdit->text();
constQValidator*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;
switch (originalValue.type()) {
caseQVariant::Char:
value = text.at(0);
break;
caseQVariant::Color:
colorExp.exactMatch(text);
value =QColor(qMin(colorExp.cap(1).toInt(),255),qMin(colorExp.cap(2).toInt(),255),qMin(colorExp.cap(3).toInt(),255),qMin(colorExp.cap(4).toInt(),255));
break;
caseQVariant::Date:
{
QDate date =QDate::fromString(text,Qt::ISODate);
if (!date.isValid())
return;
value = date;
}
break;
caseQVariant::DateTime:
{
QDateTime dateTime =QDateTime::fromString(text,Qt::ISODate);
if (!dateTime.isValid())
return;
value = dateTime;
}
break;
caseQVariant::Point:
pointExp.exactMatch(text);
value =QPoint(pointExp.cap(1).toInt(), pointExp.cap(2).toInt());
break;
caseQVariant::Rect:
rectExp.exactMatch(text);
value =QRect(rectExp.cap(1).toInt(), rectExp.cap(2).toInt(),
rectExp.cap(3).toInt(), rectExp.cap(4).toInt());
break;
caseQVariant::Size:
sizeExp.exactMatch(text);
value =QSize(sizeExp.cap(1).toInt(), sizeExp.cap(2).toInt());
break;
caseQVariant::StringList:
value = text.split(",");
break;
caseQVariant::Time:
{
QTime time =QTime::fromString(text,Qt::ISODate);
if (!time.isValid())
return;
value = time;
}
break;
default:
value = text;
value.convert(originalValue.type());
}
model->setData(index, displayText(value),Qt::DisplayRole);
model->setData(index, value,Qt::UserRole);
}
bool VariantDelegate::isSupportedType(QVariant::Type type)
{
switch (type) {
caseQVariant::Bool:
caseQVariant::ByteArray:
caseQVariant::Char:
caseQVariant::Color:
caseQVariant::Date:
caseQVariant::DateTime:
caseQVariant::Double:
caseQVariant::Int:
caseQVariant::LongLong:
caseQVariant::Point:
caseQVariant::Rect:
caseQVariant::Size:
caseQVariant::String:
caseQVariant::StringList:
caseQVariant::Time:
caseQVariant::UInt:
caseQVariant::ULongLong:
returntrue;
default:
returnfalse;
}
}
QString VariantDelegate::displayText(constQVariant&value)
{
switch (value.type()) {
caseQVariant::Bool:
caseQVariant::ByteArray:
caseQVariant::Char:
caseQVariant::Double:
caseQVariant::Int:
caseQVariant::LongLong:
caseQVariant::String:
caseQVariant::UInt:
caseQVariant::ULongLong:
return value.toString();
caseQVariant::Color:
{
QColor color = qvariant_cast<QColor>(value);
returnQString("(%1,%2,%3,%4)")
.arg(color.red()).arg(color.green())
.arg(color.blue()).arg(color.alpha());
}
caseQVariant::Date:
return value.toDate().toString(Qt::ISODate);
caseQVariant::DateTime:
return value.toDateTime().toString(Qt::ISODate);
caseQVariant::Invalid:
return"<Invalid>";
caseQVariant::Point:
{
QPoint point = value.toPoint();
returnQString("(%1,%2)").arg(point.x()).arg(point.y());
}
caseQVariant::Rect:
{
QRect rect = value.toRect();
returnQString("(%1,%2,%3,%4)")
.arg(rect.x()).arg(rect.y())
.arg(rect.width()).arg(rect.height());
}
caseQVariant::Size:
{
QSize size = value.toSize();
returnQString("(%1,%2)").arg(size.width()).arg(size.height());
}
caseQVariant::StringList:
return value.toStringList().join(",");
caseQVariant::Time:
return value.toTime().toString(Qt::ISODate);
default:
break;
}
returnQString("<%1>").arg(value.typeName());
}
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter
ou par MP !