diff options
author | Steven Ceuppens <steven.ceuppens@cense.be> | 2011-10-25 12:19:19 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-10-27 13:32:01 +0200 |
commit | fca432c1c09081e1e5823d0dad6290e1519823b0 (patch) | |
tree | 6631209dc4583fa23102da030f497c7121f9dcab | |
parent | ddce31c12fa34350881df76d1516cd0af30037a8 (diff) |
Add QDataStream operators to QMargins, so it can be streamed
* QDataStream format documented
* Added Unit test for QDataStream operators
* Updated Unit test
Change-Id: Idbcfcb0b927e6369e8d31b57693c7aa0d1a154e7
Reviewed-by: Olivier Goffart <ogoffart@kde.org>
-rw-r--r-- | doc/src/network/files-and-resources/datastreamformat.qdoc | 7 | ||||
-rw-r--r-- | src/corelib/tools/qmargins.cpp | 41 | ||||
-rw-r--r-- | src/corelib/tools/qmargins.h | 8 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qmargins/tst_qmargins.cpp | 26 |
4 files changed, 82 insertions, 0 deletions
diff --git a/doc/src/network/files-and-resources/datastreamformat.qdoc b/doc/src/network/files-and-resources/datastreamformat.qdoc index 6fe888b52e..69f49d892f 100644 --- a/doc/src/network/files-and-resources/datastreamformat.qdoc +++ b/doc/src/network/files-and-resources/datastreamformat.qdoc @@ -196,6 +196,13 @@ \o The number of items (quint32) \o For all items, the key (Key) and value (T) \endlist + \row \o QMargins + \o \list + \o left (int) + \o top (int) + \o right (int) + \o bottom (int) + \endlist \row \o QMatrix \o \list \o m11 (double) diff --git a/src/corelib/tools/qmargins.cpp b/src/corelib/tools/qmargins.cpp index 7e62a62169..9c7dd038e7 100644 --- a/src/corelib/tools/qmargins.cpp +++ b/src/corelib/tools/qmargins.cpp @@ -156,6 +156,47 @@ QT_BEGIN_NAMESPACE Returns true if \a m1 and \a m2 are different; otherwise returns false. */ +/***************************************************************************** + QMargins stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \fn QDataStream &operator<<(QDataStream &stream, const QMargins &m) + \relates QMargins + + Writes the given \a margin to the given \a stream and returns a + reference to the stream. + + \sa {Serializing Qt Data Types} +*/ + +QDataStream &operator<<(QDataStream &s, const QMargins &m) +{ + s << m.left() << m.top() << m.right() << m.bottom(); + return s; +} + +/*! + \fn QDataStream &operator>>(QDataStream &stream, QMargins &m) + \relates QMargins + + Reads a margin from the given \a stream into the given \a margin + and returns a reference to the stream. + + \sa {Serializing Qt Data Types} +*/ + +QDataStream &operator>>(QDataStream &s, QMargins &m) +{ + int left, top, right, bottom; + s >> left; m.setLeft(left); + s >> top; m.setTop(top); + s >> right; m.setRight(right); + s >> bottom; m.setBottom(bottom); + return s; +} +#endif // QT_NO_DATASTREAM + #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QMargins &m) { dbg.nospace() << "QMargins(" << m.left() << ", " diff --git a/src/corelib/tools/qmargins.h b/src/corelib/tools/qmargins.h index f8404ca801..635ac56137 100644 --- a/src/corelib/tools/qmargins.h +++ b/src/corelib/tools/qmargins.h @@ -81,6 +81,14 @@ private: Q_DECLARE_TYPEINFO(QMargins, Q_MOVABLE_TYPE); /***************************************************************************** + QMargins stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QMargins &); +Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QMargins &); +#endif + +/***************************************************************************** QMargins inline functions *****************************************************************************/ diff --git a/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp b/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp index c7632d125a..cb0dd2d67d 100644 --- a/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp +++ b/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp @@ -61,6 +61,7 @@ public slots: void cleanup(); private slots: void getSetCheck(); + void dataStreamCheck(); }; // Testing get/set functions @@ -86,6 +87,31 @@ void tst_QMargins::getSetCheck() QCOMPARE(margins, QMargins(5, 0, 5, 0)); } +// Testing QDataStream operators +void tst_QMargins::dataStreamCheck() +{ + QByteArray buffer; + + // stream out + { + QMargins marginsOut(0,INT_MIN,INT_MAX,6852); + QDataStream streamOut(&buffer, QIODevice::WriteOnly); + streamOut << marginsOut; + } + + // stream in & compare + { + QMargins marginsIn; + QDataStream streamIn(&buffer, QIODevice::ReadOnly); + streamIn >> marginsIn; + + QCOMPARE(marginsIn.left(), 0); + QCOMPARE(marginsIn.top(), INT_MIN); + QCOMPARE(marginsIn.right(), INT_MAX); + QCOMPARE(marginsIn.bottom(), 6852); + } +} + tst_QMargins::tst_QMargins() { } |