mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-14 17:44:13 +02:00
Open and save projects as .qetz (zipped, opt-in)
Third stage of the zipped project file (#1440). A project is saved as a .qetz when its file name ends in .qetz -- Save As offers it as a second file type, and --resave, --set-titleblock, the script API's save() and the live-mode snapshot follow the name too. Saving a .qet project still writes a .qet; nothing changes unless the user picks .qetz. Opening recognises a .qetz by its content (a zip), not its name, and reads it as bytes: text mode would alter it on Windows. The project document is joined back and read by the same code as a .qet. Crash backups stay plain XML. Drag and drop, the open dialog and the numbering import accept .qetz; the database export names its file from the project's name without assuming ".qet". Parts are now written without added indentation (one line each): the symbol definitions are kept as read, whitespace included, and parts that gained indentation made the next save differ (five forum projects). Tests: tst_resaveunchanged::containerSavesTheSameProject (four examples saved as .qetz, then as .qet, equal the straight .qet; pictures checked to fail without the fix), tst_qetcontainer::noWhitespaceAdded. Outside ctest: 341 of 341 projects QET can open (examples, bugtracker, GitHub, forum) give the same .qet through a .qetz; 181.6 MB of .qet became 36.9 MB of .qetz. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019Wga9B92BNurDEtxxtCDxf
This commit is contained in:
@@ -34,7 +34,10 @@ void setError(QString *error, const QString &text)
|
||||
if (error) *error = text;
|
||||
}
|
||||
|
||||
//An element on its own, as a .qet is written (QET::writeXmlFile())
|
||||
//An element on its own, written without adding indentation: the
|
||||
//whitespace text already in the document (QETProject keeps the symbol
|
||||
//definitions as they were read, spacing included) must come back as it
|
||||
//was, or the next save writes it differently. Parts are one line.
|
||||
QByteArray serialize(const QDomNode &node)
|
||||
{
|
||||
QDomDocument document;
|
||||
@@ -42,7 +45,7 @@ QByteArray serialize(const QDomNode &node)
|
||||
QStringLiteral("xml"),
|
||||
QStringLiteral("version=\"1.0\" encoding=\"UTF-8\"")));
|
||||
document.appendChild(document.importNode(node, true));
|
||||
return document.toString(4).toUtf8();
|
||||
return document.toString(-1).toUtf8();
|
||||
}
|
||||
|
||||
//A file name from a name chosen by the user: no folders, nothing a zip
|
||||
@@ -136,6 +139,30 @@ bool QetContainer::parse(const QByteArray &xml, QDomDocument *document, QString
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@brief QetContainer::isZip
|
||||
A .qetz is recognised by what it holds, not its name: a crash backup
|
||||
of a .qetz project is plain XML, and a renamed file is still a zip.
|
||||
*/
|
||||
bool QetContainer::isZip(const QByteArray &content)
|
||||
{
|
||||
return content.startsWith("PK\x03\x04");
|
||||
}
|
||||
|
||||
/**
|
||||
@brief QetContainer::readDocument
|
||||
The project document held in @p content: unzipped and joined for a
|
||||
.qetz, parsed for a .qet.
|
||||
*/
|
||||
bool QetContainer::readDocument(const QByteArray &content, QDomDocument *document, QString *error)
|
||||
{
|
||||
if (!isZip(content))
|
||||
return parse(content, document, error);
|
||||
QList<QetZip::Entry> entries;
|
||||
return QetZip::fromBytes(content, &entries, error)
|
||||
&& join(entries, document, error);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief QetContainer::split
|
||||
@param project : the project document, as QETProject::toXml() makes it
|
||||
|
||||
@@ -54,6 +54,10 @@ namespace QetContainer
|
||||
|
||||
/// Parse @p xml as a .qet is read (QETProject::openFile)
|
||||
bool parse(const QByteArray &xml, QDomDocument *document, QString *error = nullptr);
|
||||
/// true if @p content is a zip, so a .qetz rather than a .qet
|
||||
bool isZip(const QByteArray &content);
|
||||
/// The project document in @p content, a .qet or a .qetz
|
||||
bool readDocument(const QByteArray &content, QDomDocument *document, QString *error = nullptr);
|
||||
}
|
||||
|
||||
#endif // QETCONTAINER_H
|
||||
|
||||
Reference in New Issue
Block a user