Make QPersistentModelIndex an internal meta-type

It was already a user meta-type, so it only gets
promoted to internal.

[ChangeLog][QtCore] QPersistentModel index becomes an built-in
meta-type, including QVariant support.

Change-Id: I63d733d1eb66aa61691e7afce27fe7372a83ac00
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Gabriel de Dietrich 2015-03-02 13:22:02 +01:00
parent 9a3d7adaad
commit 374c60e046
8 changed files with 76 additions and 6 deletions

View File

@ -484,6 +484,5 @@ inline uint qHash(const QModelIndex &index) Q_DECL_NOTHROW
QT_END_NAMESPACE
Q_DECLARE_METATYPE(QModelIndexList)
Q_DECLARE_METATYPE(QPersistentModelIndex)
#endif // QABSTRACTITEMMODEL_H

View File

@ -277,6 +277,7 @@ struct DefinedTypesFilter {
\value QJsonArray QJsonArray
\value QJsonDocument QJsonDocument
\value QModelIndex QModelIndex
\value QPersistentModelIndex QPersistentModelIndex (since 5.5)
\value QUuid QUuid
\value QByteArrayList QByteArrayList
@ -1209,6 +1210,7 @@ bool QMetaType::save(QDataStream &stream, int type, const void *data)
case QMetaType::VoidStar:
case QMetaType::QObjectStar:
case QMetaType::QModelIndex:
case QMetaType::QPersistentModelIndex:
case QMetaType::QJsonValue:
case QMetaType::QJsonObject:
case QMetaType::QJsonArray:
@ -1429,6 +1431,7 @@ bool QMetaType::load(QDataStream &stream, int type, void *data)
case QMetaType::VoidStar:
case QMetaType::QObjectStar:
case QMetaType::QModelIndex:
case QMetaType::QPersistentModelIndex:
case QMetaType::QJsonValue:
case QMetaType::QJsonObject:
case QMetaType::QJsonArray:

View File

@ -113,6 +113,7 @@ inline Q_DECL_CONSTEXPR int qMetaTypeId();
F(QJsonObject, 46, QJsonObject) \
F(QJsonArray, 47, QJsonArray) \
F(QJsonDocument, 48, QJsonDocument) \
F(QPersistentModelIndex, 50, QPersistentModelIndex) \
#define QT_FOR_EACH_STATIC_CORE_POINTER(F)\
F(QObjectStar, 39, QObject*)
@ -407,7 +408,7 @@ public:
QT_FOR_EACH_STATIC_TYPE(QT_DEFINE_METATYPE_ID)
FirstCoreType = Bool,
LastCoreType = QByteArrayList,
LastCoreType = QPersistentModelIndex,
FirstGuiType = QFont,
LastGuiType = QPolygonF,
FirstWidgetsType = QSizePolicy,
@ -431,7 +432,7 @@ public:
QLocale = 18, QRect = 19, QRectF = 20, QSize = 21, QSizeF = 22,
QLine = 23, QLineF = 24, QPoint = 25, QPointF = 26, QRegExp = 27,
QEasingCurve = 29, QUuid = 30, QVariant = 41, QModelIndex = 42,
QRegularExpression = 44,
QPersistentModelIndex = 50, QRegularExpression = 44,
QJsonValue = 45, QJsonObject = 46, QJsonArray = 47, QJsonDocument = 48,
QByteArrayList = 49, QObjectStar = 39, SChar = 40,
Void = 43,

View File

@ -192,6 +192,7 @@ template<> struct TypeDefinition<QJsonDocument> { static const bool IsAvailable
template<> struct TypeDefinition<QJsonObject> { static const bool IsAvailable = false; };
template<> struct TypeDefinition<QJsonValue> { static const bool IsAvailable = false; };
template<> struct TypeDefinition<QModelIndex> { static const bool IsAvailable = false; };
template<> struct TypeDefinition<QPersistentModelIndex> { static const bool IsAvailable = false; };
template<> struct TypeDefinition<QUrl> { static const bool IsAvailable = false; };
template<> struct TypeDefinition<QByteArrayList> { static const bool IsAvailable = false; };
#endif

View File

@ -390,7 +390,25 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
return false;
}
break;
#endif
case QVariant::ModelIndex:
switch (d->type) {
case QVariant::PersistentModelIndex:
*static_cast<QModelIndex *>(result) = QModelIndex(*v_cast<QPersistentModelIndex>(d));
break;
default:
return false;
}
break;
case QVariant::PersistentModelIndex:
switch (d->type) {
case QVariant::ModelIndex:
*static_cast<QPersistentModelIndex *>(result) = QPersistentModelIndex(*v_cast<QModelIndex>(d));
break;
default:
return false;
}
break;
#endif // QT_BOOTSTRAPPED
case QVariant::String: {
QString *str = static_cast<QString *>(result);
switch (d->type) {
@ -1208,6 +1226,7 @@ Q_CORE_EXPORT void QVariantPrivate::registerHandler(const int /* Modules::Names
\value EasingCurve a QEasingCurve
\value Uuid a QUuid
\value ModelIndex a QModelIndex
\value PersistentModelIndex a QPersistentModelIndex (since 5.5)
\value Font a QFont
\value Hash a QVariantHash
\value Icon a QIcon
@ -1455,7 +1474,14 @@ QVariant::QVariant(const char *val)
\since 5.0
\fn QVariant::QVariant(const QModelIndex &val)
Constructs a new variant with an modelIndex value, \a val.
Constructs a new variant with a QModelIndex value, \a val.
*/
/*!
\since 5.5
\fn QVariant::QVariant(const QPersistentModelIndex &val)
Constructs a new variant with a QPersistentModelIndex value, \a val.
*/
/*!
@ -1763,6 +1789,9 @@ QVariant::QVariant(const QUuid &uuid)
QVariant::QVariant(const QModelIndex &modelIndex)
: d(ModelIndex)
{ v_construct<QModelIndex>(&d, modelIndex); }
QVariant::QVariant(const QPersistentModelIndex &modelIndex)
: d(PersistentModelIndex)
{ v_construct<QPersistentModelIndex>(&d, modelIndex); }
QVariant::QVariant(const QJsonValue &jsonValue)
: d(QMetaType::QJsonValue)
{ v_construct<QJsonValue>(&d, jsonValue); }
@ -2492,13 +2521,26 @@ QUuid QVariant::toUuid() const
Returns the variant as a QModelIndex if the variant has userType() \l
QModelIndex; otherwise returns a default constructed QModelIndex.
\sa canConvert(), convert()
\sa canConvert(), convert(), toPersistentModelIndex()
*/
QModelIndex QVariant::toModelIndex() const
{
return qVariantToHelper<QModelIndex>(d, handlerManager);
}
/*!
\since 5.5
Returns the variant as a QPersistentModelIndex if the variant has userType() \l
QPersistentModelIndex; otherwise returns a default constructed QPersistentModelIndex.
\sa canConvert(), convert(), toModelIndex()
*/
QModelIndex QVariant::toPersistentModelIndex() const
{
return qVariantToHelper<QPersistentModelIndex>(d, handlerManager);
}
/*!
\since 5.0
@ -2966,6 +3008,10 @@ static bool canConvertMetaObject(int fromId, int toId, QObject *fromObject)
*/
bool QVariant::canConvert(int targetTypeId) const
{
if ((targetTypeId == QMetaType::QModelIndex && d.type == QMetaType::QPersistentModelIndex)
|| (targetTypeId == QMetaType::QPersistentModelIndex && d.type == QMetaType::QModelIndex))
return true;
if (targetTypeId == QMetaType::QVariantList
&& (d.type == QMetaType::QVariantList
|| d.type == QMetaType::QStringList

View File

@ -156,6 +156,7 @@ class Q_CORE_EXPORT QVariant
EasingCurve = QMetaType::QEasingCurve,
Uuid = QMetaType::QUuid,
ModelIndex = QMetaType::QModelIndex,
PersistentModelIndex = QMetaType::QPersistentModelIndex,
LastCoreType = QMetaType::LastCoreType,
Font = QMetaType::QFont,
@ -245,6 +246,7 @@ class Q_CORE_EXPORT QVariant
QVariant(const QEasingCurve &easing);
QVariant(const QUuid &uuid);
QVariant(const QModelIndex &modelIndex);
QVariant(const QPersistentModelIndex &modelIndex);
QVariant(const QJsonValue &jsonValue);
QVariant(const QJsonObject &jsonObject);
QVariant(const QJsonArray &jsonArray);
@ -318,6 +320,7 @@ class Q_CORE_EXPORT QVariant
QEasingCurve toEasingCurve() const;
QUuid toUuid() const;
QModelIndex toModelIndex() const;
QModelIndex toPersistentModelIndex() const;
QJsonValue toJsonValue() const;
QJsonObject toJsonObject() const;
QJsonArray toJsonArray() const;

View File

@ -661,6 +661,9 @@ template<> struct TestValueFactory<QMetaType::QUuid> {
template<> struct TestValueFactory<QMetaType::QModelIndex> {
static QModelIndex *create() { return new QModelIndex(); }
};
template<> struct TestValueFactory<QMetaType::QPersistentModelIndex> {
static QPersistentModelIndex *create() { return new QPersistentModelIndex(); }
};
template<> struct TestValueFactory<QMetaType::QRegExp> {
static QRegExp *create()
{
@ -1735,6 +1738,7 @@ struct StreamingTraits
DECLARE_NONSTREAMABLE(void)
DECLARE_NONSTREAMABLE(void*)
DECLARE_NONSTREAMABLE(QModelIndex)
DECLARE_NONSTREAMABLE(QPersistentModelIndex)
DECLARE_NONSTREAMABLE(QJsonValue)
DECLARE_NONSTREAMABLE(QJsonObject)
DECLARE_NONSTREAMABLE(QJsonArray)

View File

@ -248,6 +248,7 @@ private slots:
void movabilityTest();
void variantInVariant();
void userConversion();
void modelIndexConversion();
void forwardDeclare();
void debugStream_data();
@ -3846,6 +3847,17 @@ void tst_QVariant::userConversion()
}
}
void tst_QVariant::modelIndexConversion()
{
QVariant modelIndexVariant = QModelIndex();
QVERIFY(modelIndexVariant.canConvert(QMetaType::QPersistentModelIndex));
QVERIFY(modelIndexVariant.convert(QMetaType::QPersistentModelIndex));
QCOMPARE(modelIndexVariant.type(), QVariant::PersistentModelIndex);
QVERIFY(modelIndexVariant.canConvert(QMetaType::QModelIndex));
QVERIFY(modelIndexVariant.convert(QMetaType::QModelIndex));
QCOMPARE(modelIndexVariant.type(), QVariant::ModelIndex);
}
class Forward;
Q_DECLARE_OPAQUE_POINTER(Forward*)
Q_DECLARE_METATYPE(Forward*)
@ -4073,6 +4085,7 @@ void tst_QVariant::implicitConstruction()
F(EasingCurve) \
F(Uuid) \
F(ModelIndex) \
F(PersistentModelIndex) \
F(RegularExpression) \
F(JsonValue) \
F(JsonObject) \