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 <QFile>
#include <QMapIterator>
#include <QBuffer>
namespace QXlsx {
@ -111,6 +112,15 @@ void ContentTypes::addVbaProject()
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
{
QXmlStreamWriter writer(device);

1
src/xlsx/xlsxcontenttypes_p.h

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

10
src/xlsx/xlsxdrawing.cpp

@ -27,6 +27,7 @@
#include <QXmlStreamWriter>
#include <QXmlStreamReader>
#include <QBuffer>
namespace QXlsx {
@ -36,6 +37,15 @@ Drawing::Drawing()
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
{
QXmlStreamWriter writer(device);

1
src/xlsx/xlsxdrawing_p.h

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

54
src/xlsx/xlsxpackage.cpp

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

10
src/xlsx/xlsxpackage_p.h

@ -61,24 +61,14 @@ public:
private:
void writeWorksheetFiles(ZipWriter &zipWriter);
// void writeChartsheetFiles(ZipWriter &zipWriter);
void writeWorkbookFile(ZipWriter &zipWriter);
// void writeChartFiles(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 writeDocPropsCoreFile(ZipWriter &zipWriter);
void writeContentTypesFile(ZipWriter &zipWriter);
void writeStylesFiles(ZipWriter &zipWriter);
void writeThemeFile(ZipWriter &zipWriter);
void writeRootRelsFile(ZipWriter &zipWriter);
void writeWorkbookRelsFile(ZipWriter &zipWriter);
void writeDrawingRelsFiles(ZipWriter &zipWriter);
void writeImageFiles(ZipWriter &zipWriter);
// void writeVbaProjectFiles(ZipWriter &zipWriter);
Document *m_document;
Workbook *m_workbook;

5
src/xlsx/xlsxsharedstrings.cpp

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

1
src/xlsx/xlsxsharedstrings_p.h

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

Loading…
Cancel
Save