Browse Source

Code refactoring: Clean up code of QXlsx::Package

We should merge the class Package with the class Document
master
Debao Zhang 11 years ago
parent
commit
d1818c7fd1
  1. 10
      src/xlsx/xlsxcontenttypes.cpp
  2. 1
      src/xlsx/xlsxcontenttypes_p.h
  3. 10
      src/xlsx/xlsxdrawing.cpp
  4. 1
      src/xlsx/xlsxdrawing_p.h
  5. 54
      src/xlsx/xlsxpackage.cpp
  6. 10
      src/xlsx/xlsxpackage_p.h
  7. 5
      src/xlsx/xlsxsharedstrings.cpp
  8. 1
      src/xlsx/xlsxsharedstrings_p.h

10
src/xlsx/xlsxcontenttypes.cpp

@ -26,6 +26,7 @@
#include <QXmlStreamWriter> #include <QXmlStreamWriter>
#include <QFile> #include <QFile>
#include <QMapIterator> #include <QMapIterator>
#include <QBuffer>
namespace QXlsx { namespace QXlsx {
@ -111,6 +112,15 @@ void ContentTypes::addVbaProject()
addOverride(QStringLiteral("bin"), QStringLiteral("application/vnd.ms-office.vbaProject")); addOverride(QStringLiteral("bin"), QStringLiteral("application/vnd.ms-office.vbaProject"));
} }
QByteArray ContentTypes::saveToXmlData() const
{
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
saveToXmlFile(&buffer);
return data;
}
void ContentTypes::saveToXmlFile(QIODevice *device) const void ContentTypes::saveToXmlFile(QIODevice *device) const
{ {
QXmlStreamWriter writer(device); QXmlStreamWriter writer(device);

1
src/xlsx/xlsxcontenttypes_p.h

@ -65,6 +65,7 @@ public:
void addCalcChain(); void addCalcChain();
void addVbaProject(); void addVbaProject();
QByteArray saveToXmlData() const;
void saveToXmlFile(QIODevice *device) const; void saveToXmlFile(QIODevice *device) const;
private: private:

10
src/xlsx/xlsxdrawing.cpp

@ -27,6 +27,7 @@
#include <QXmlStreamWriter> #include <QXmlStreamWriter>
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <QBuffer>
namespace QXlsx { namespace QXlsx {
@ -36,6 +37,15 @@ Drawing::Drawing()
orientation = 0; orientation = 0;
} }
QByteArray Drawing::saveToXmlData() const
{
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
saveToXmlFile(&buffer);
return data;
}
void Drawing::saveToXmlFile(QIODevice *device) const void Drawing::saveToXmlFile(QIODevice *device) const
{ {
QXmlStreamWriter writer(device); QXmlStreamWriter writer(device);

1
src/xlsx/xlsxdrawing_p.h

@ -69,6 +69,7 @@ class Drawing
public: public:
Drawing(); Drawing();
void saveToXmlFile(QIODevice *device) const; void saveToXmlFile(QIODevice *device) const;
QByteArray saveToXmlData() const;
bool embedded; bool embedded;
int orientation; int orientation;

54
src/xlsx/xlsxpackage.cpp

@ -46,6 +46,8 @@
namespace QXlsx { namespace QXlsx {
//: TODO This class should be merged to Document class.
/* /*
From Wikipedia: The Open Packaging Conventions (OPC) is a From Wikipedia: The Open Packaging Conventions (OPC) is a
container-file technology initially created by Microsoft to store container-file technology initially created by Microsoft to store
@ -202,24 +204,21 @@ bool Package::createPackage(QIODevice *package)
m_workbook->prepareDrawings(); m_workbook->prepareDrawings();
writeWorksheetFiles(zipWriter); writeWorksheetFiles(zipWriter);
// writeChartsheetFiles(zipWriter); zipWriter.addFile(QStringLiteral("xl/workbook.xml"), m_workbook->saveToXmlData());
writeWorkbookFile(zipWriter);
// writeChartFiles(zipWriter);
writeDrawingFiles(zipWriter); writeDrawingFiles(zipWriter);
// writeVmlFiles(zipWriter);
// writeCommentFiles(zipWriter);
// writeTableFiles(zipWriter);
writeSharedStringsFile(zipWriter);
writeDocPropsAppFile(zipWriter); writeDocPropsAppFile(zipWriter);
writeDocPropsCoreFile(zipWriter); writeDocPropsCoreFile(zipWriter);
writeContentTypesFile(zipWriter); writeContentTypesFile(zipWriter);
writeStylesFiles(zipWriter);
writeThemeFile(zipWriter); if (!m_workbook->sharedStrings()->isEmpty())
zipWriter.addFile(QStringLiteral("xl/sharedStrings.xml"), m_workbook->sharedStrings()->saveToXmlData());
zipWriter.addFile(QStringLiteral("xl/styles.xml"), m_workbook->styles()->saveToXmlData());
zipWriter.addFile(QStringLiteral("xl/theme/theme1.xml"), m_workbook->theme()->saveToXmlData());
writeRootRelsFile(zipWriter); writeRootRelsFile(zipWriter);
writeWorkbookRelsFile(zipWriter); writeWorkbookRelsFile(zipWriter);
writeDrawingRelsFiles(zipWriter); writeDrawingRelsFiles(zipWriter);
writeImageFiles(zipWriter); writeImageFiles(zipWriter);
// writeVbaProjectFiles(zipWriter);
zipWriter.close(); zipWriter.close();
return true; return true;
@ -239,21 +238,11 @@ void Package::writeWorksheetFiles(ZipWriter &zipWriter)
} }
} }
void Package::writeWorkbookFile(ZipWriter &zipWriter)
{
zipWriter.addFile(QStringLiteral("xl/workbook.xml"), m_workbook->saveToXmlData());
}
void Package::writeDrawingFiles(ZipWriter &zipWriter) void Package::writeDrawingFiles(ZipWriter &zipWriter)
{ {
for (int i=0; i<m_workbook->drawings().size(); ++i) { for (int i=0; i<m_workbook->drawings().size(); ++i) {
Drawing *drawing = m_workbook->drawings()[i]; Drawing *drawing = m_workbook->drawings()[i];
zipWriter.addFile(QStringLiteral("xl/drawings/drawing%1.xml").arg(i+1), drawing->saveToXmlData());
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
drawing->saveToXmlFile(&buffer);
zipWriter.addFile(QStringLiteral("xl/drawings/drawing%1.xml").arg(i+1), data);
} }
} }
@ -283,11 +272,7 @@ void Package::writeContentTypesFile(ZipWriter &zipWriter)
if (m_workbook->sharedStrings()->count()) if (m_workbook->sharedStrings()->count())
content.addSharedString(); content.addSharedString();
QByteArray data; zipWriter.addFile(QStringLiteral("[Content_Types].xml"), content.saveToXmlData());
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
content.saveToXmlFile(&buffer);
zipWriter.addFile(QStringLiteral("[Content_Types].xml"), data);
} }
void Package::writeDocPropsAppFile(ZipWriter &zipWriter) void Package::writeDocPropsAppFile(ZipWriter &zipWriter)
@ -329,21 +314,6 @@ void Package::writeDocPropsCoreFile(ZipWriter &zipWriter)
zipWriter.addFile(QStringLiteral("docProps/core.xml"), props.saveToXmlData()); zipWriter.addFile(QStringLiteral("docProps/core.xml"), props.saveToXmlData());
} }
void Package::writeSharedStringsFile(ZipWriter &zipWriter)
{
zipWriter.addFile(QStringLiteral("xl/sharedStrings.xml"), m_workbook->sharedStrings()->saveToXmlData());
}
void Package::writeStylesFiles(ZipWriter &zipWriter)
{
zipWriter.addFile(QStringLiteral("xl/styles.xml"), m_workbook->styles()->saveToXmlData());
}
void Package::writeThemeFile(ZipWriter &zipWriter)
{
zipWriter.addFile(QStringLiteral("xl/theme/theme1.xml"), m_workbook->theme()->saveToXmlData());
}
void Package::writeRootRelsFile(ZipWriter &zipWriter) void Package::writeRootRelsFile(ZipWriter &zipWriter)
{ {
Relationships rels; Relationships rels;
@ -374,7 +344,7 @@ void Package::writeWorkbookRelsFile(ZipWriter &zipWriter)
rels.addDocumentRelationship(QStringLiteral("/theme"), QStringLiteral("theme/theme1.xml")); rels.addDocumentRelationship(QStringLiteral("/theme"), QStringLiteral("theme/theme1.xml"));
rels.addDocumentRelationship(QStringLiteral("/styles"), QStringLiteral("styles.xml")); rels.addDocumentRelationship(QStringLiteral("/styles"), QStringLiteral("styles.xml"));
if (m_workbook->sharedStrings()->count()) if (!m_workbook->sharedStrings()->isEmpty())
rels.addDocumentRelationship(QStringLiteral("/sharedStrings"), QStringLiteral("sharedStrings.xml")); rels.addDocumentRelationship(QStringLiteral("/sharedStrings"), QStringLiteral("sharedStrings.xml"));
zipWriter.addFile(QStringLiteral("xl/_rels/workbook.xml.rels"), rels.saveToXmlData()); zipWriter.addFile(QStringLiteral("xl/_rels/workbook.xml.rels"), rels.saveToXmlData());

10
src/xlsx/xlsxpackage_p.h

@ -61,24 +61,14 @@ public:
private: private:
void writeWorksheetFiles(ZipWriter &zipWriter); void writeWorksheetFiles(ZipWriter &zipWriter);
// void writeChartsheetFiles(ZipWriter &zipWriter);
void writeWorkbookFile(ZipWriter &zipWriter);
// void writeChartFiles(ZipWriter &zipWriter);
void writeDrawingFiles(ZipWriter &zipWriter); void writeDrawingFiles(ZipWriter &zipWriter);
// void writeVmlFiles(ZipWriter &zipWriter);
// void writeCommentFiles(ZipWriter &zipWriter);
// void writeTableFiles(ZipWriter &zipWriter);
void writeSharedStringsFile(ZipWriter &zipWriter);
void writeDocPropsAppFile(ZipWriter &zipWriter); void writeDocPropsAppFile(ZipWriter &zipWriter);
void writeDocPropsCoreFile(ZipWriter &zipWriter); void writeDocPropsCoreFile(ZipWriter &zipWriter);
void writeContentTypesFile(ZipWriter &zipWriter); void writeContentTypesFile(ZipWriter &zipWriter);
void writeStylesFiles(ZipWriter &zipWriter);
void writeThemeFile(ZipWriter &zipWriter);
void writeRootRelsFile(ZipWriter &zipWriter); void writeRootRelsFile(ZipWriter &zipWriter);
void writeWorkbookRelsFile(ZipWriter &zipWriter); void writeWorkbookRelsFile(ZipWriter &zipWriter);
void writeDrawingRelsFiles(ZipWriter &zipWriter); void writeDrawingRelsFiles(ZipWriter &zipWriter);
void writeImageFiles(ZipWriter &zipWriter); void writeImageFiles(ZipWriter &zipWriter);
// void writeVbaProjectFiles(ZipWriter &zipWriter);
Document *m_document; Document *m_document;
Workbook *m_workbook; Workbook *m_workbook;

5
src/xlsx/xlsxsharedstrings.cpp

@ -47,6 +47,11 @@ int SharedStrings::count() const
return m_stringCount; return m_stringCount;
} }
bool SharedStrings::isEmpty() const
{
return m_stringList.isEmpty();
}
int SharedStrings::addSharedString(const QString &string) int SharedStrings::addSharedString(const QString &string)
{ {
return addSharedString(RichString(string)); return addSharedString(RichString(string));

1
src/xlsx/xlsxsharedstrings_p.h

@ -65,6 +65,7 @@ class XLSX_AUTOTEST_EXPORT SharedStrings
public: public:
SharedStrings(); SharedStrings();
int count() const; int count() const;
bool isEmpty() const;
int addSharedString(const QString &string); int addSharedString(const QString &string);
int addSharedString(const RichString &string); int addSharedString(const RichString &string);

Loading…
Cancel
Save