Browse Source

Format: Use QMap instead of QHash

As the items of QHash are arbitrarily ordered, so the same formats may generate
different formatKey
master
Debao Zhang 11 years ago
parent
commit
e1d93af4b7
  1. 10
      src/xlsx/xlsxformat.cpp
  2. 5
      src/xlsx/xlsxformat.h
  3. 4
      src/xlsx/xlsxformat_p.h
  4. 82
      tests/auto/document/tst_documenttest.cpp

10
src/xlsx/xlsxformat.cpp

@ -903,7 +903,7 @@ QByteArray Format::formatKey() const
QByteArray key; QByteArray key;
QDataStream stream(&key, QIODevice::WriteOnly); QDataStream stream(&key, QIODevice::WriteOnly);
QHashIterator<int, QVariant> i(d->property); QMapIterator<int, QVariant> i(d->property);
while (i.hasNext()) { while (i.hasNext()) {
i.next(); i.next();
stream<<i.key()<<i.value(); stream<<i.key()<<i.value();
@ -1108,4 +1108,12 @@ QColor Format::colorProperty(int propertyId) const
return qvariant_cast<QColor>(prop); return qvariant_cast<QColor>(prop);
} }
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const Format &f)
{
dbg.nospace() << "QXlsx::Format(" << f.d->property << ")";
return dbg.space();
}
#endif
QT_END_NAMESPACE_XLSX QT_END_NAMESPACE_XLSX

5
src/xlsx/xlsxformat.h

@ -237,6 +237,7 @@ private:
friend class RichStringPrivate; friend class RichStringPrivate;
friend class SharedStrings; friend class SharedStrings;
friend class ::FormatTest; friend class ::FormatTest;
friend Q_XLSX_EXPORT QDebug operator<<(QDebug, const Format &f);
bool fontIndexValid() const; bool fontIndexValid() const;
int fontIndex() const; int fontIndex() const;
@ -274,6 +275,10 @@ private:
QExplicitlySharedDataPointer<FormatPrivate> d; QExplicitlySharedDataPointer<FormatPrivate> d;
}; };
#ifndef QT_NO_DEBUG_STREAM
Q_XLSX_EXPORT QDebug operator<<(QDebug dbg, const Format &f);
#endif
QT_END_NAMESPACE_XLSX QT_END_NAMESPACE_XLSX
#endif // QXLSX_FORMAT_H #endif // QXLSX_FORMAT_H

4
src/xlsx/xlsxformat_p.h

@ -26,7 +26,7 @@
#define XLSXFORMAT_P_H #define XLSXFORMAT_P_H
#include "xlsxformat.h" #include "xlsxformat.h"
#include <QSharedData> #include <QSharedData>
#include <QHash> #include <QMap>
namespace QXlsx { namespace QXlsx {
@ -148,7 +148,7 @@ public:
int theme; int theme;
QHash<int, QVariant> property; QMap<int, QVariant> property;
}; };
} }

82
tests/auto/document/tst_documenttest.cpp

@ -29,20 +29,25 @@ DocumentTest::DocumentTest()
void DocumentTest::testDocumentProperty() void DocumentTest::testDocumentProperty()
{ {
QBuffer device;
device.open(QIODevice::WriteOnly);
Document xlsx1; Document xlsx1;
xlsx1.setDocumentProperty("creator", "Debao"); xlsx1.setDocumentProperty("creator", "Debao");
xlsx1.setDocumentProperty("company", "Test"); xlsx1.setDocumentProperty("company", "Test");
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs(&device);
Document xlsx2("test.xlsx"); device.open(QIODevice::ReadOnly);
Document xlsx2(&device);
QCOMPARE(xlsx2.documentProperty("creator"), QString("Debao")); QCOMPARE(xlsx2.documentProperty("creator"), QString("Debao"));
QCOMPARE(xlsx2.documentProperty("company"), QString("Test")); QCOMPARE(xlsx2.documentProperty("company"), QString("Test"));
QFile::remove("test.xlsx");
} }
void DocumentTest::testReadWriteString() void DocumentTest::testReadWriteString()
{ {
QBuffer device;
device.open(QIODevice::WriteOnly);
Document xlsx1; Document xlsx1;
xlsx1.write("A1", "Hello Qt!"); xlsx1.write("A1", "Hello Qt!");
Format format; Format format;
@ -50,24 +55,26 @@ void DocumentTest::testReadWriteString()
format.setBorderStyle(Format::BorderDashDotDot); format.setBorderStyle(Format::BorderDashDotDot);
format.setFillPattern(Format::PatternSolid); format.setFillPattern(Format::PatternSolid);
xlsx1.write("A2", "Hello Qt again!", format); xlsx1.write("A2", "Hello Qt again!", format);
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs(&device);
Document xlsx2("test.xlsx"); device.open(QIODevice::ReadOnly);
Document xlsx2(&device);
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::String); QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::String);
QCOMPARE(xlsx2.cellAt("A1")->value().toString(), QString("Hello Qt!")); QCOMPARE(xlsx2.cellAt("A1")->value().toString(), QString("Hello Qt!"));
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::String); QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::String);
QCOMPARE(xlsx2.cellAt("A2")->value().toString(), QString("Hello Qt again!")); QCOMPARE(xlsx2.cellAt("A2")->value().toString(), QString("Hello Qt again!"));
QVERIFY(xlsx2.cellAt("A2")->format().isValid()); Format format2 = xlsx2.cellAt("A2")->format();
QCOMPARE(xlsx2.cellAt("A2")->format().fontColor(), format.fontColor()); QVERIFY(format2.isValid());
QCOMPARE(xlsx2.cellAt("A2")->format().leftBorderStyle(), format.leftBorderStyle()); // qDebug()<<format2;
QCOMPARE(xlsx2.cellAt("A2")->format().fillPattern(), format.fillPattern()); // qDebug()<<format;
// QCOMPARE(xlsx2.cellAt("A2")->format(), format); QCOMPARE(format2, format);
QFile::remove("test.xlsx");
} }
void DocumentTest::testReadWriteNumeric() void DocumentTest::testReadWriteNumeric()
{ {
QBuffer device;
device.open(QIODevice::WriteOnly);
Document xlsx1; Document xlsx1;
xlsx1.write("A1", 123); xlsx1.write("A1", 123);
Format format; Format format;
@ -76,21 +83,23 @@ void DocumentTest::testReadWriteNumeric()
format.setFillPattern(Format::PatternSolid); format.setFillPattern(Format::PatternSolid);
format.setNumberFormatIndex(10); format.setNumberFormatIndex(10);
xlsx1.write("A2", 12345, format); xlsx1.write("A2", 12345, format);
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs(&device);
Document xlsx2("test.xlsx"); device.open(QIODevice::ReadOnly);
Document xlsx2(&device);
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Numeric); QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Numeric);
QCOMPARE(xlsx2.cellAt("A1")->value().toDouble(), 123.0); QCOMPARE(xlsx2.cellAt("A1")->value().toDouble(), 123.0);
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Numeric); QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Numeric);
QCOMPARE(xlsx2.cellAt("A2")->value().toDouble(), 12345.0); QCOMPARE(xlsx2.cellAt("A2")->value().toDouble(), 12345.0);
QVERIFY(xlsx2.cellAt("A2")->format().isValid()); QVERIFY(xlsx2.cellAt("A2")->format().isValid());
QCOMPARE(xlsx2.cellAt("A2")->format(), format); QCOMPARE(xlsx2.cellAt("A2")->format(), format);
QFile::remove("test.xlsx");
} }
void DocumentTest::testReadWriteBool() void DocumentTest::testReadWriteBool()
{ {
QBuffer device;
device.open(QIODevice::WriteOnly);
Document xlsx1; Document xlsx1;
xlsx1.write("A1", true); xlsx1.write("A1", true);
Format format; Format format;
@ -98,21 +107,23 @@ void DocumentTest::testReadWriteBool()
format.setBorderStyle(Format::BorderDashDotDot); format.setBorderStyle(Format::BorderDashDotDot);
format.setFillPattern(Format::PatternSolid); format.setFillPattern(Format::PatternSolid);
xlsx1.write("A2", false, format); xlsx1.write("A2", false, format);
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs(&device);
Document xlsx2("test.xlsx"); device.open(QIODevice::ReadOnly);
Document xlsx2(&device);
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Boolean); QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Boolean);
QCOMPARE(xlsx2.cellAt("A1")->value().toBool(), true); QCOMPARE(xlsx2.cellAt("A1")->value().toBool(), true);
QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Boolean); QCOMPARE(xlsx2.cellAt("A2")->dataType(), Cell::Boolean);
QCOMPARE(xlsx2.cellAt("A2")->value().toBool(), false); QCOMPARE(xlsx2.cellAt("A2")->value().toBool(), false);
QVERIFY(xlsx2.cellAt("A2")->format().isValid()); QVERIFY(xlsx2.cellAt("A2")->format().isValid());
QCOMPARE(xlsx2.cellAt("A2")->format(), format); QCOMPARE(xlsx2.cellAt("A2")->format(), format);
QFile::remove("test.xlsx");
} }
void DocumentTest::testReadWriteBlank() void DocumentTest::testReadWriteBlank()
{ {
QBuffer device;
device.open(QIODevice::WriteOnly);
Document xlsx1; Document xlsx1;
xlsx1.write("A1", QVariant()); xlsx1.write("A1", QVariant());
Format format; Format format;
@ -120,9 +131,10 @@ void DocumentTest::testReadWriteBlank()
format.setBorderStyle(Format::BorderDashDotDot); format.setBorderStyle(Format::BorderDashDotDot);
format.setFillPattern(Format::PatternSolid); format.setFillPattern(Format::PatternSolid);
xlsx1.write("A2", QVariant(), format); xlsx1.write("A2", QVariant(), format);
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs(&device);
Document xlsx2("test.xlsx"); device.open(QIODevice::ReadOnly);
Document xlsx2(&device);
QVERIFY(xlsx2.cellAt("A1")); QVERIFY(xlsx2.cellAt("A1"));
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Blank); QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Blank);
QVERIFY(!xlsx2.cellAt("A1")->value().isValid()); QVERIFY(!xlsx2.cellAt("A1")->value().isValid());
@ -131,12 +143,13 @@ void DocumentTest::testReadWriteBlank()
QVERIFY(!xlsx2.cellAt("A2")->value().isValid()); QVERIFY(!xlsx2.cellAt("A2")->value().isValid());
QVERIFY(xlsx2.cellAt("A2")->format().isValid()); QVERIFY(xlsx2.cellAt("A2")->format().isValid());
QCOMPARE(xlsx2.cellAt("A2")->format(), format); QCOMPARE(xlsx2.cellAt("A2")->format(), format);
QFile::remove("test.xlsx");
} }
void DocumentTest::testReadWriteFormula() void DocumentTest::testReadWriteFormula()
{ {
QBuffer device;
device.open(QIODevice::WriteOnly);
Document xlsx1; Document xlsx1;
xlsx1.write("A1", "=11+22"); xlsx1.write("A1", "=11+22");
Format format; Format format;
@ -144,10 +157,11 @@ void DocumentTest::testReadWriteFormula()
format.setBorderStyle(Format::BorderDashDotDot); format.setBorderStyle(Format::BorderDashDotDot);
format.setFillPattern(Format::PatternSolid); format.setFillPattern(Format::PatternSolid);
xlsx1.write("A2", "=22+33", format); xlsx1.write("A2", "=22+33", format);
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs(&device);
Document xlsx2("test.xlsx"); device.open(QIODevice::ReadOnly);
Document xlsx2(&device);
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Formula); QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Formula);
// QCOMPARE(xlsx2.cellAt("A1")->value().toDouble(), 0.0); // QCOMPARE(xlsx2.cellAt("A1")->value().toDouble(), 0.0);
QCOMPARE(xlsx2.cellAt("A1")->formula(), QStringLiteral("11+22")); QCOMPARE(xlsx2.cellAt("A1")->formula(), QStringLiteral("11+22"));
@ -156,12 +170,13 @@ void DocumentTest::testReadWriteFormula()
QCOMPARE(xlsx2.cellAt("A2")->formula(), QStringLiteral("22+33")); QCOMPARE(xlsx2.cellAt("A2")->formula(), QStringLiteral("22+33"));
QVERIFY(xlsx2.cellAt("A2")->format().isValid()); QVERIFY(xlsx2.cellAt("A2")->format().isValid());
QCOMPARE(xlsx2.cellAt("A2")->format(), format); QCOMPARE(xlsx2.cellAt("A2")->format(), format);
QFile::remove("test.xlsx");
} }
void DocumentTest::testReadWriteDateTime() void DocumentTest::testReadWriteDateTime()
{ {
QBuffer device;
device.open(QIODevice::WriteOnly);
Document xlsx1; Document xlsx1;
QDateTime dt(QDate(2012, 11, 12), QTime(6, 0), Qt::UTC); QDateTime dt(QDate(2012, 11, 12), QTime(6, 0), Qt::UTC);
@ -177,10 +192,10 @@ void DocumentTest::testReadWriteDateTime()
format3.setNumberFormat("dd/mm/yyyy"); format3.setNumberFormat("dd/mm/yyyy");
xlsx1.write("A3", dt, format3); xlsx1.write("A3", dt, format3);
xlsx1.saveAs("test.xlsx"); xlsx1.saveAs(&device);
Document xlsx2("test.xlsx");
device.open(QIODevice::ReadOnly);
Document xlsx2(&device);
QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Numeric); QCOMPARE(xlsx2.cellAt("A1")->dataType(), Cell::Numeric);
QCOMPARE(xlsx2.cellAt("A1")->isDateTime(), true); QCOMPARE(xlsx2.cellAt("A1")->isDateTime(), true);
QCOMPARE(xlsx2.cellAt("A1")->dateTime(), dt); QCOMPARE(xlsx2.cellAt("A1")->dateTime(), dt);
@ -195,9 +210,6 @@ void DocumentTest::testReadWriteDateTime()
QCOMPARE(xlsx2.cellAt("A3")->isDateTime(), true); QCOMPARE(xlsx2.cellAt("A3")->isDateTime(), true);
QCOMPARE(xlsx2.cellAt("A3")->dateTime(), dt); QCOMPARE(xlsx2.cellAt("A3")->dateTime(), dt);
QCOMPARE(xlsx2.cellAt("A3")->format().numberFormat(), QString("dd/mm/yyyy")); QCOMPARE(xlsx2.cellAt("A3")->format().numberFormat(), QString("dd/mm/yyyy"));
QFile::remove("test.xlsx");
} }
QTEST_APPLESS_MAIN(DocumentTest) QTEST_APPLESS_MAIN(DocumentTest)

Loading…
Cancel
Save