.
This commit is contained in:
+59
-11
@@ -3,6 +3,8 @@
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <JlCompress.h>
|
||||
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
|
||||
@@ -11,8 +13,9 @@
|
||||
#include <QDomNode>
|
||||
#include <QDomNamedNodeMap>
|
||||
|
||||
cDocumentReader::cDocumentReader(const QString &szFileName) :
|
||||
cDocumentReader::cDocumentReader(const QString &szFileName, bool bZip) :
|
||||
m_bFirstBlock(true),
|
||||
m_bZip(bZip),
|
||||
m_szFileName(szFileName)
|
||||
{
|
||||
}
|
||||
@@ -24,25 +27,56 @@ cDocumentReader::~cDocumentReader()
|
||||
|
||||
bool cDocumentReader::open()
|
||||
{
|
||||
m_file.setFileName(m_szFileName);
|
||||
if(m_bZip)
|
||||
{
|
||||
m_zip.setZipName(m_szFileName);
|
||||
|
||||
if(!m_file.open(QFile::ReadOnly | QFile::Text))
|
||||
return(false);
|
||||
if(!m_zip.open(QuaZip::mdUnzip))
|
||||
return(false);
|
||||
|
||||
m_zip.setCurrentFile("document.xml");
|
||||
m_zipFile.setZip(&m_zip);
|
||||
|
||||
if(!m_zipFile.open(QIODevice::ReadOnly))
|
||||
{
|
||||
m_zip.close();
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_file.setFileName(m_szFileName);
|
||||
|
||||
if(!m_file.open(QIODevice::ReadOnly))
|
||||
return(false);
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
bool cDocumentReader::close()
|
||||
{
|
||||
if(m_file.isOpen())
|
||||
m_file.close();;
|
||||
if(m_bZip)
|
||||
{
|
||||
if(m_zip.isOpen())
|
||||
{
|
||||
if(m_zipFile.isOpen())
|
||||
m_zipFile.close();
|
||||
m_zip.close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(m_file.isOpen())
|
||||
m_file.close();
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
cTextDocument* cDocumentReader::readDocument()
|
||||
{
|
||||
if(!m_file.isOpen())
|
||||
if(!open())
|
||||
return(0);
|
||||
|
||||
cTextDocument* lpDocument;
|
||||
@@ -50,10 +84,19 @@ cTextDocument* cDocumentReader::readDocument()
|
||||
QString errorStr;
|
||||
int errorLine;
|
||||
int errorColumn;
|
||||
if(!doc.setContent(&m_file, false, &errorStr, &errorLine, &errorColumn))
|
||||
return(0);
|
||||
|
||||
QDomElement root = doc.documentElement();
|
||||
if(m_bZip)
|
||||
{
|
||||
if(!doc.setContent(&m_zipFile, false, &errorStr, &errorLine, &errorColumn))
|
||||
return(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!doc.setContent(&m_file, false, &errorStr, &errorLine, &errorColumn))
|
||||
return(0);
|
||||
}
|
||||
|
||||
QDomElement root = doc.documentElement();
|
||||
if(root.tagName().compare("document", Qt::CaseInsensitive))
|
||||
return(0);
|
||||
|
||||
@@ -95,7 +138,7 @@ cTextDocument* cDocumentReader::readDocument()
|
||||
child = child.nextSibling();
|
||||
}
|
||||
|
||||
m_file.close();
|
||||
close();
|
||||
|
||||
return(lpDocument);
|
||||
}
|
||||
@@ -1710,7 +1753,12 @@ void cDocumentReader::parseTextBlock(const QDomElement& element, QTextCursor* lp
|
||||
int indent;
|
||||
|
||||
if(m_bFirstBlock)
|
||||
{
|
||||
m_bFirstBlock = false;
|
||||
lpCursor->setBlockFormat(allFormats[blockFormatIndex].blockFormat);
|
||||
lpCursor->setCharFormat(allFormats[charFormatIndex].charFormat);
|
||||
lpCursor->setBlockCharFormat(allFormats[charFormatIndex].charFormat);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(listFormatIndex != -1)
|
||||
|
||||
+6
-1
@@ -26,6 +26,8 @@
|
||||
#include <QTextBlock>
|
||||
#include <QTextCursor>
|
||||
|
||||
#include <JlCompress.h>
|
||||
|
||||
|
||||
class cTextDocument;
|
||||
|
||||
@@ -166,7 +168,7 @@ public:
|
||||
class cDocumentReader : public QXmlStreamReader
|
||||
{
|
||||
public:
|
||||
cDocumentReader(const QString& szFileName);
|
||||
cDocumentReader(const QString& szFileName, bool bZip = true);
|
||||
~cDocumentReader();
|
||||
|
||||
bool open();
|
||||
@@ -175,8 +177,11 @@ public:
|
||||
cTextDocument* readDocument();
|
||||
private:
|
||||
bool m_bFirstBlock;
|
||||
bool m_bZip;
|
||||
QString m_szFileName;
|
||||
QFile m_file;
|
||||
QuaZip m_zip;
|
||||
QuaZipFile m_zipFile;
|
||||
|
||||
QMap<int, QTextList*> m_textLists;
|
||||
|
||||
|
||||
+72
-10
@@ -1,12 +1,18 @@
|
||||
#include "cdocumentwriter.h"
|
||||
#include "ctextdocument.h"
|
||||
|
||||
#include <QFileInfo>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
cDocumentWriter::cDocumentWriter(const QString &szFileName) :
|
||||
m_szFileName(szFileName)
|
||||
cDocumentWriter::cDocumentWriter(const QString &szFileName, bool bZip) :
|
||||
m_bZip(bZip),
|
||||
m_szFileName(szFileName),
|
||||
m_lpDocument(0)
|
||||
{
|
||||
QFileInfo fileInfo(szFileName);
|
||||
m_szPath = fileInfo.absolutePath();
|
||||
}
|
||||
|
||||
cDocumentWriter::~cDocumentWriter()
|
||||
@@ -19,11 +25,31 @@ bool cDocumentWriter::open()
|
||||
if(m_szFileName.isEmpty())
|
||||
return(false);
|
||||
|
||||
m_file.setFileName(m_szFileName);
|
||||
if(!m_file.open(QFile::WriteOnly | QFile::Text))
|
||||
return(false);
|
||||
if(m_bZip)
|
||||
{
|
||||
m_zip.setZipName(m_szFileName);
|
||||
m_zip.open(QuaZip::mdCreate);
|
||||
|
||||
QuaZipNewInfo info("document.xml");
|
||||
m_zipFile.setZip(&m_zip);
|
||||
|
||||
if(!m_zipFile.open(QIODevice::WriteOnly, info))
|
||||
{
|
||||
m_zip.close();
|
||||
return(false);
|
||||
}
|
||||
|
||||
setDevice(&m_zipFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_file.setFileName(m_szFileName);
|
||||
if(!m_file.open(QIODevice::WriteOnly | QFile::Text))
|
||||
return(false);
|
||||
|
||||
setDevice(&m_file);
|
||||
}
|
||||
|
||||
setDevice(&m_file);
|
||||
setAutoFormatting(true);
|
||||
setAutoFormattingIndent(-1);
|
||||
writeStartDocument();
|
||||
@@ -34,11 +60,23 @@ bool cDocumentWriter::open()
|
||||
|
||||
bool cDocumentWriter::close()
|
||||
{
|
||||
if(m_file.isOpen())
|
||||
if(m_bZip)
|
||||
{
|
||||
writeEndElement();
|
||||
writeEndDocument();
|
||||
m_file.close();
|
||||
if(m_zipFile.isOpen())
|
||||
{
|
||||
writeEndElement();
|
||||
writeEndDocument();
|
||||
m_zipFile.close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(m_file.isOpen())
|
||||
{
|
||||
writeEndElement();
|
||||
writeEndDocument();
|
||||
m_file.close();
|
||||
}
|
||||
}
|
||||
|
||||
return(true);
|
||||
@@ -46,6 +84,8 @@ bool cDocumentWriter::close()
|
||||
|
||||
bool cDocumentWriter::writeDocument(cTextDocument *lpDocument)
|
||||
{
|
||||
m_lpDocument = lpDocument;
|
||||
|
||||
QList<QPair<int, QTextBlockFormat>> textBlockFormats;
|
||||
QTextBlock textBlock = lpDocument->firstBlock();
|
||||
|
||||
@@ -362,6 +402,7 @@ void cDocumentWriter::writeTextFormat(QTextFormat* lpTextFormat, int /*id*/, Wri
|
||||
while(property.hasNext())
|
||||
{
|
||||
property.next();
|
||||
|
||||
writeStartElement("property");
|
||||
writeAttribute("key", QString::number(property.key()));
|
||||
writeAttribute("type", QString::number(property.value().type()));
|
||||
@@ -529,6 +570,27 @@ void cDocumentWriter::writeTextImageFormat(QTextImageFormat* lpTextImageFormat,
|
||||
writeAttribute("height", QString::number(lpTextImageFormat->height()));
|
||||
writeAttribute("width", QString::number(lpTextImageFormat->width()));
|
||||
}
|
||||
|
||||
if(m_lpDocument)
|
||||
{
|
||||
QPixmap pixmap = m_lpDocument->resource(2, lpTextImageFormat->name()).value<QPixmap>();
|
||||
if(!pixmap.isNull())
|
||||
{
|
||||
QString name = lpTextImageFormat->name();
|
||||
if(name.left(1) == ':')
|
||||
name = name.mid(1);
|
||||
if(name.left(1) == '/')
|
||||
name = name.mid(1);
|
||||
|
||||
name = m_szPath + "/" + name;
|
||||
QString path = name.left(name.lastIndexOf("/"));
|
||||
|
||||
QDir dir;
|
||||
dir.mkpath(path);
|
||||
pixmap.save(name);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-1
@@ -19,6 +19,8 @@
|
||||
#include <QTextCharFormat>
|
||||
#include <QTextFormat>
|
||||
|
||||
#include <JlCompress.h>
|
||||
|
||||
|
||||
class cTextDocument;
|
||||
|
||||
@@ -33,7 +35,7 @@ public:
|
||||
WriteElements = 2,
|
||||
};
|
||||
|
||||
cDocumentWriter(const QString& szFileName);
|
||||
cDocumentWriter(const QString& szFileName, bool bZip = true);
|
||||
~cDocumentWriter();
|
||||
|
||||
bool open();
|
||||
@@ -42,8 +44,13 @@ public:
|
||||
bool writeDocument(cTextDocument* lpDocument);
|
||||
|
||||
private:
|
||||
bool m_bZip;
|
||||
QString m_szFileName;
|
||||
QString m_szPath;
|
||||
QFile m_file;
|
||||
QuaZip m_zip;
|
||||
QuaZipFile m_zipFile;
|
||||
cTextDocument* m_lpDocument;
|
||||
|
||||
void writeTextBlockFormats(const QList<QPair<int, QTextBlockFormat>> textBlockFormats);
|
||||
void writeTextBlocks(QTextBlock& textBlock);
|
||||
|
||||
+6
-5
@@ -267,12 +267,13 @@ void cMainWindow::on_actionTest1_triggered()
|
||||
|
||||
void cMainWindow::on_actionSave_triggered()
|
||||
{
|
||||
g_lpInputDocument->saveAs("C:/Temp/documentText.xml");
|
||||
// g_lpInputDocument->saveAs("C:/Temp/documentText.zip");
|
||||
// cDocumentReader reader("C:/Temp/documentText.zip");
|
||||
// g_lpOutputDocument = reader.readDocument();
|
||||
// ui->m_lpOutput->setDocument(g_lpOutputDocument);
|
||||
|
||||
cDocumentReader reader("C:/Temp/documentText.xml");
|
||||
reader.open();
|
||||
g_lpInputDocument->saveAs("C:/Temp/documentText.xml", false);
|
||||
cDocumentReader reader("C:/Temp/documentText.xml", false);
|
||||
g_lpOutputDocument = reader.readDocument();
|
||||
reader.close();
|
||||
|
||||
ui->m_lpOutput->setDocument(g_lpOutputDocument);
|
||||
}
|
||||
|
||||
+6
-6
@@ -79,22 +79,22 @@ void cTextDocument::init()
|
||||
*/
|
||||
}
|
||||
|
||||
bool cTextDocument::save()
|
||||
bool cTextDocument::save(bool bZip)
|
||||
{
|
||||
if(!m_szFileName.isEmpty())
|
||||
return(saveDocument());
|
||||
return(saveDocument(bZip));
|
||||
return(false);
|
||||
}
|
||||
|
||||
bool cTextDocument::saveAs(const QString& szFileName)
|
||||
bool cTextDocument::saveAs(const QString& szFileName, bool bZip)
|
||||
{
|
||||
m_szFileName = szFileName;
|
||||
return(saveDocument());
|
||||
return(saveDocument(bZip));
|
||||
}
|
||||
|
||||
bool cTextDocument::saveDocument()
|
||||
bool cTextDocument::saveDocument(bool bZip)
|
||||
{
|
||||
cDocumentWriter docWriter(m_szFileName);
|
||||
cDocumentWriter docWriter(m_szFileName, bZip);
|
||||
if(!docWriter.open())
|
||||
return(false);
|
||||
|
||||
|
||||
+3
-3
@@ -22,15 +22,15 @@ public:
|
||||
cTextDocument(QObject *parent = Q_NULLPTR);
|
||||
cTextDocument(const QString &text, QObject *parent = Q_NULLPTR);
|
||||
|
||||
bool save();
|
||||
bool saveAs(const QString& szFileName);
|
||||
bool save(bool bZip = true);
|
||||
bool saveAs(const QString& szFileName, bool bZip = true);
|
||||
|
||||
void printPreview(QPrinter* lpPrinter);
|
||||
private:
|
||||
QString m_szFileName;
|
||||
|
||||
void init();
|
||||
bool saveDocument();
|
||||
bool saveDocument(bool bZip = true);
|
||||
private slots:
|
||||
};
|
||||
|
||||
|
||||
@@ -47,3 +47,8 @@ DISTFILES += \
|
||||
|
||||
RESOURCES += \
|
||||
qtstorywriter.qrc
|
||||
|
||||
INCLUDEPATH += C:/dev/3rdParty/zlib
|
||||
LIBS += -LC:/dev/3rdParty/zlib -lz
|
||||
INCLUDEPATH += C:/dev/3rdParty/quazip
|
||||
LIBS += -LC:/dev/3rdParty/quazip/release -lquazip
|
||||
|
||||
Reference in New Issue
Block a user