Browse Source

Make constructor of Workbook private

master
Debao Zhang 12 years ago
parent
commit
6ee507b0d8
  1. 26
      src/xlsx/xlsxworkbook.cpp
  2. 15
      src/xlsx/xlsxworkbook.h
  3. 29
      tests/auto/mergecell/tst_mergecelltest.cpp
  4. 10
      tests/auto/relationships/tst_relationshipstest.cpp

26
src/xlsx/xlsxworkbook.cpp

@ -33,6 +33,7 @@
#include "xlsxworksheet_p.h" #include "xlsxworksheet_p.h"
#include <QFile> #include <QFile>
#include <QBuffer>
namespace QXlsx { namespace QXlsx {
@ -281,4 +282,29 @@ void Workbook::saveToXmlFile(QIODevice *device)
writer.writeEndDocument(); writer.writeEndDocument();
} }
QByteArray Workbook::saveToXmlData()
{
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
saveToXmlFile(&buffer);
return data;
}
QSharedPointer<Workbook> Workbook::loadFromXmlFile(QIODevice *device)
{
return QSharedPointer<Workbook>(new Workbook);
}
QSharedPointer<Workbook> Workbook::loadFromXmlData(const QByteArray &data)
{
QBuffer buffer;
buffer.setData(data);
buffer.open(QIODevice::ReadOnly);
return loadFromXmlFile(&buffer);
}
} //namespace } //namespace

15
src/xlsx/xlsxworkbook.h

@ -29,6 +29,8 @@
#include <QObject> #include <QObject>
#include <QList> #include <QList>
#include <QImage> #include <QImage>
#include <QSharedPointer>
class QIODevice; class QIODevice;
namespace QXlsx { namespace QXlsx {
@ -39,6 +41,8 @@ class SharedStrings;
class Styles; class Styles;
class Package; class Package;
class Drawing; class Drawing;
class Document;
class DocumentPrivate;
class WorkbookPrivate; class WorkbookPrivate;
class Q_XLSX_EXPORT Workbook : public QObject class Q_XLSX_EXPORT Workbook : public QObject
@ -46,7 +50,6 @@ class Q_XLSX_EXPORT Workbook : public QObject
Q_OBJECT Q_OBJECT
Q_DECLARE_PRIVATE(Workbook) Q_DECLARE_PRIVATE(Workbook)
public: public:
Workbook(QObject *parent=0);
~Workbook(); ~Workbook();
QList<Worksheet *> worksheets() const; QList<Worksheet *> worksheets() const;
@ -66,13 +69,21 @@ public:
private: private:
friend class Package; friend class Package;
friend class Worksheet; friend class Worksheet;
friend class Document;
friend class DocumentPrivate;
Workbook(QObject *parent=0);
void saveToXmlFile(QIODevice *device);
QByteArray saveToXmlData();
static QSharedPointer<Workbook> loadFromXmlFile(QIODevice *device);
static QSharedPointer<Workbook> loadFromXmlData(const QByteArray &data);
SharedStrings *sharedStrings(); SharedStrings *sharedStrings();
Styles *styles(); Styles *styles();
QList<QImage> images(); QList<QImage> images();
QList<Drawing *> drawings(); QList<Drawing *> drawings();
void prepareDrawings(); void prepareDrawings();
void saveToXmlFile(QIODevice *device);
WorkbookPrivate * const d_ptr; WorkbookPrivate * const d_ptr;
}; };

29
tests/auto/mergecell/tst_mergecelltest.cpp

@ -2,7 +2,7 @@
#include <QtTest> #include <QtTest>
#include "xlsxworksheet.h" #include "xlsxworksheet.h"
#include "xlsxworkbook.h" #include "xlsxdocument.h"
class MergeCellTest : public QObject class MergeCellTest : public QObject
{ {
@ -23,45 +23,42 @@ MergeCellTest::MergeCellTest()
void MergeCellTest::testWithoutMerge() void MergeCellTest::testWithoutMerge()
{ {
QXlsx::Workbook book; QXlsx::Document xlsx;
QXlsx::Worksheet *sheet = book.addWorksheet("Sheet1"); xlsx.write("B1", "Hello");
sheet->write("B1", "Hello");
QByteArray xmldata; QByteArray xmldata;
QBuffer buffer(&xmldata); QBuffer buffer(&xmldata);
buffer.open(QIODevice::WriteOnly); buffer.open(QIODevice::WriteOnly);
sheet->saveToXmlFile(&buffer); xlsx.activedWorksheet()->saveToXmlFile(&buffer);
QVERIFY2(!xmldata.contains("<mergeCell"), ""); QVERIFY2(!xmldata.contains("<mergeCell"), "");
} }
void MergeCellTest::testMerge() void MergeCellTest::testMerge()
{ {
QXlsx::Workbook book; QXlsx::Document xlsx;
QXlsx::Worksheet *sheet = book.addWorksheet("Sheet1"); xlsx.write("B1", "Test Merged Cell");
sheet->write("B1", "Test Merged Cell"); xlsx.mergeCells("B1:B5");
sheet->mergeCells("B1:B5");
QByteArray xmldata; QByteArray xmldata;
QBuffer buffer(&xmldata); QBuffer buffer(&xmldata);
buffer.open(QIODevice::WriteOnly); buffer.open(QIODevice::WriteOnly);
sheet->saveToXmlFile(&buffer); xlsx.activedWorksheet()->saveToXmlFile(&buffer);
QVERIFY2(xmldata.contains("<mergeCells count=\"1\"><mergeCell ref=\"B1:B5\"/></mergeCells>"), ""); QVERIFY2(xmldata.contains("<mergeCells count=\"1\"><mergeCell ref=\"B1:B5\"/></mergeCells>"), "");
} }
void MergeCellTest::testUnMerge() void MergeCellTest::testUnMerge()
{ {
QXlsx::Workbook book; QXlsx::Document xlsx;
QXlsx::Worksheet *sheet = book.addWorksheet("Sheet1"); xlsx.write("B1", "Test Merged Cell");
sheet->write("B1", "Test Merged Cell"); xlsx.mergeCells("B1:B5");
sheet->mergeCells("B1:B5"); xlsx.unmergeCells("B1:B5");
sheet->unmergeCells("B1:B5");
QByteArray xmldata; QByteArray xmldata;
QBuffer buffer(&xmldata); QBuffer buffer(&xmldata);
buffer.open(QIODevice::WriteOnly); buffer.open(QIODevice::WriteOnly);
sheet->saveToXmlFile(&buffer); xlsx.activedWorksheet()->saveToXmlFile(&buffer);
QVERIFY2(!xmldata.contains("<mergeCell"), ""); QVERIFY2(!xmldata.contains("<mergeCell"), "");
} }

10
tests/auto/relationships/tst_relationshipstest.cpp

@ -24,10 +24,7 @@ void RelationshipsTest::testSaveXml()
QXlsx::Relationships rels; QXlsx::Relationships rels;
rels.addDocumentRelationship("/officeDocument", "xl/workbook.xml"); rels.addDocumentRelationship("/officeDocument", "xl/workbook.xml");
QByteArray xmldata; QByteArray xmldata = rels.saveToXmlData();
QBuffer buffer(&xmldata);
buffer.open(QIODevice::WriteOnly);
rels.saveToXmlFile(&buffer);
QVERIFY2(xmldata.contains("<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"xl/workbook.xml\"/>"), ""); QVERIFY2(xmldata.contains("<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"xl/workbook.xml\"/>"), "");
} }
@ -38,11 +35,8 @@ void RelationshipsTest::testLoadXml()
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">" "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">"
"<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"xl/workbook.xml\"/>" "<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"xl/workbook.xml\"/>"
"</Relationships>"); "</Relationships>");
QBuffer buffer(&xmldata);
buffer.open(QIODevice::ReadOnly);
QXlsx::Relationships rels; QXlsx::Relationships rels = QXlsx::Relationships::loadFromXmlData(xmldata);
rels.loadFromXmlFile(&buffer);
QCOMPARE(rels.documentRelationships("/officeDocument").size(), 1); QCOMPARE(rels.documentRelationships("/officeDocument").size(), 1);
} }

Loading…
Cancel
Save