From 78f6d144d6c1d0ed6880bbf99310fe03e8502567 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Mon, 31 Mar 2014 18:05:32 +0200 Subject: Serialize QPicture using the latest QDataStream version. The QDataStream version with which QPicture was previously serialized with was hard-coded to 11 (Qt 4.5). This prevents features of serializable types used by QPicture from being serialized if they were added after Qt 4.5; namely features of QFont like HintingPreference. [ChangeLog][QtGui][QPicture] QPicture now serializes its data properly by also accounting for QDataStream versions greater than Qt 4.5. Task-number: QTBUG-20578 Change-Id: Idd27682b187f4d4a3695c52bbf68e84853c024a8 Reviewed-by: Gunnar Sletta Reviewed-by: Eskil Abrahamsen Blomfeldt --- tests/auto/gui/image/qpicture/tst_qpicture.cpp | 107 +++++++++++++++++++------ 1 file changed, 83 insertions(+), 24 deletions(-) (limited to 'tests/auto/gui/image/qpicture') diff --git a/tests/auto/gui/image/qpicture/tst_qpicture.cpp b/tests/auto/gui/image/qpicture/tst_qpicture.cpp index 2e766c5bf5..986431941f 100644 --- a/tests/auto/gui/image/qpicture/tst_qpicture.cpp +++ b/tests/auto/gui/image/qpicture/tst_qpicture.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #ifndef QT_NO_WIDGETS #include #include @@ -64,7 +65,7 @@ private slots: void paintingActive(); void boundingRect(); void swap(); - void operator_lt_lt(); + void serialization(); #ifndef QT_NO_WIDGETS void save_restore(); @@ -169,36 +170,94 @@ void tst_QPicture::swap() QCOMPARE(p2.boundingRect(), QRect(0,0,5,5)); } -// operator<< and operator>> -void tst_QPicture::operator_lt_lt() +Q_DECLARE_METATYPE(QDataStream::Version) +Q_DECLARE_METATYPE(QPicture) + +void ensureSerializesCorrectly(const QPicture &picture, QDataStream::Version version) + { + QDataStream stream; + + QBuffer buffer; + buffer.open(QIODevice::WriteOnly); + stream.setDevice(&buffer); + stream.setVersion(version); + stream << picture; + buffer.close(); + + buffer.open(QIODevice::ReadOnly); + QPicture readpicture; + stream >> readpicture; + QVERIFY2(memcmp(picture.data(), readpicture.data(), picture.size()) == 0, + qPrintable(QString::fromLatin1("Picture data does not compare equal for QDataStream version %1").arg(version))); +} + +class PaintEngine : public QPaintEngine { - // streaming of null pictures - { - QPicture pic1, pic2; - QByteArray ba( 100, 0 ); - QDataStream str1( &ba, QIODevice::WriteOnly ); - str1 << pic1; - QDataStream str2( &ba, QIODevice::ReadOnly ); - str2 >> pic2; - QVERIFY( pic2.isNull() ); +public: + PaintEngine() : QPaintEngine() {} + bool begin(QPaintDevice *) { return true; } + bool end() { return true; } + void updateState(const QPaintEngineState &) {} + void drawPixmap(const QRectF &, const QPixmap &, const QRectF &) {} + Type type() const { return Raster; } + + QFont font() { return state->font(); } +}; + +class Picture : public QPicture +{ +public: + Picture() : QPicture() {} + QPaintEngine *paintEngine() const { return (QPaintEngine*)&mPaintEngine; } +private: + PaintEngine mPaintEngine; +}; + +void tst_QPicture::serialization() +{ + QDataStream stream; + const int thisVersion = stream.version(); + + for (int version = QDataStream::Qt_1_0; version <= thisVersion; ++version) { + const QDataStream::Version versionEnum = static_cast(version); + + { + // streaming of null pictures + ensureSerializesCorrectly(QPicture(), versionEnum); + } + { + // picture with a simple line, checking bitwise equality + QPicture picture; + QPainter painter(&picture); + painter.drawLine(10, 20, 30, 40); + ensureSerializesCorrectly(picture, versionEnum); + } } - // picture with a simple line, checking bitwise equality { - QPicture pic1, pic2; - QPainter p( &pic1 ); - p.drawLine( 10, 20, 30, 40 ); - p.end(); - QByteArray ba( 10 * pic1.size(), 0 ); - QDataStream str1( &ba, QIODevice::WriteOnly ); - str1 << pic1; - QDataStream str2( &ba, QIODevice::ReadOnly ); - str2 >> pic2; - QCOMPARE( pic1.size(), pic2.size() ); - QVERIFY( memcmp( pic1.data(), pic2.data(), pic1.size() ) == 0 ); + // Test features that were added after Qt 4.5, as that was hard-coded as the major + // version for a while, which was incorrect. In this case, we'll test font hints. + QPicture picture; + QPainter painter; + QFont font; + font.setStyleName("Blah"); + font.setHintingPreference(QFont::PreferFullHinting); + painter.begin(&picture); + painter.setFont(font); + painter.drawText(20, 20, "Hello"); + painter.end(); + + Picture customPicture; + painter.begin(&customPicture); + picture.play(&painter); + const QFont actualFont = ((PaintEngine*)customPicture.paintEngine())->font(); + painter.end(); + QCOMPARE(actualFont.styleName(), QStringLiteral("Blah")); + QCOMPARE(actualFont.hintingPreference(), QFont::PreferFullHinting); } } + #ifndef QT_NO_WIDGETS static QPointF scalePoint(const QPointF &point, QPaintDevice *sourceDevice, QPaintDevice *destDevice) { -- cgit v1.2.3