mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-17 12:40:35 +01:00
Implemented QET::writeXmlFile() to handle every XML file generation in a single place.
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/branches/0.3@1640 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -763,16 +763,16 @@ void QETElementEditor::fromFile(const QString &filepath) {
|
||||
@return true en cas de reussite, false sinon
|
||||
*/
|
||||
bool QETElementEditor::toFile(const QString &fn) {
|
||||
QFile file(fn);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
QET::MessageBox::warning(this, tr("Erreur", "message box title"), tr("Impossible d'\351crire dans ce fichier", "message box content"));
|
||||
return(false);
|
||||
QDomDocument element_xml = ce_scene -> toXml();
|
||||
bool writing = QET::writeXmlFile(element_xml, fn);
|
||||
if (!writing) {
|
||||
QET::MessageBox::warning(
|
||||
this,
|
||||
tr("Erreur", "message box title"),
|
||||
tr("Impossible d'\351crire dans ce fichier", "message box content")
|
||||
);
|
||||
}
|
||||
QTextStream out(&file);
|
||||
out.setCodec("UTF-8");
|
||||
out << ce_scene -> toXml().toString(4);
|
||||
file.close();
|
||||
return(true);
|
||||
return(writing);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
#include "fileelementscategory.h"
|
||||
#include "fileelementscollection.h"
|
||||
#include "qetapp.h"
|
||||
#include "qet.h"
|
||||
|
||||
/**
|
||||
Constructeur
|
||||
@param uri Chemin du fichier contenant la definition de l'element
|
||||
@@ -74,16 +76,7 @@ bool FileElementDefinition::setXml(const QDomElement &xml_element) {
|
||||
@return true si l'operation a reussi, false sinon
|
||||
*/
|
||||
bool FileElementDefinition::write() {
|
||||
QFile file(file_path);
|
||||
|
||||
// le fichier doit etre accessible en ecriture
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return(false);
|
||||
|
||||
QTextStream out(&file);
|
||||
out.setCodec("UTF-8");
|
||||
out << xml_element_.toString(4);
|
||||
file.close();
|
||||
return(true);
|
||||
return(QET::writeXmlFile(xml_element_, file_path));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#include "fileelementscategory.h"
|
||||
#include "fileelementscollection.h"
|
||||
#include "fileelementdefinition.h"
|
||||
#include "qet.h"
|
||||
|
||||
/**
|
||||
Constructeur
|
||||
@param path Chemin du dossier de la categorie
|
||||
@@ -412,21 +414,8 @@ bool FileElementsCategory::write() {
|
||||
document.appendChild(root);
|
||||
root.appendChild(category_names.toXml(document));
|
||||
|
||||
// repere le chemin du fichier de configuration de la categorie
|
||||
QFile directory_conf(cat_dir.absolutePath() + "/qet_directory");
|
||||
|
||||
// ouvre le fichier
|
||||
if (!directory_conf.open(QIODevice::Text | QIODevice::WriteOnly)) return(false);
|
||||
|
||||
// ecrit le fichier
|
||||
QTextStream out(&directory_conf);
|
||||
out.setCodec("UTF-8");
|
||||
out << document.toString(4);
|
||||
|
||||
// ferme le fichier
|
||||
directory_conf.close();
|
||||
|
||||
return(true);
|
||||
QString filepath = cat_dir.absolutePath() + "/qet_directory";
|
||||
return(QET::writeXmlFile(document, filepath));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -528,3 +528,38 @@ QString QET::titleBlockColumnLengthToString(const TitleBlockColumnLength &icl)
|
||||
else if (icl == RelativeToRemainingLength) type_str = "relative to remaining";
|
||||
return(type_str);
|
||||
}
|
||||
|
||||
/**
|
||||
Export an XML document to an UTF-8 text file indented with 4 spaces, with LF
|
||||
end of lines and no BOM.
|
||||
@param xml_doc An XML document to be exported
|
||||
@param filepath Path to the file to be written
|
||||
@param error_message If non-zero, will contain an error message explaining
|
||||
what happened when this function returns false.
|
||||
@return false if an error occured, true otherwise
|
||||
*/
|
||||
bool QET::writeXmlFile(QDomDocument &xml_doc, const QString &filepath, QString *error_message) {
|
||||
QFile file(filepath);
|
||||
|
||||
// Note: we do not set QIODevice::Text to avoid generating CRLF end of lines
|
||||
bool file_opening = file.open(QIODevice::WriteOnly);
|
||||
if (!file_opening) {
|
||||
if (error_message) {
|
||||
*error_message = QString(
|
||||
QObject::tr(
|
||||
"Impossible d'ouvrir le fichier %1 en \351criture, erreur %2 rencontr\351e.",
|
||||
"error message when attempting to write an XML file"
|
||||
)
|
||||
).arg(filepath).arg(file.error());
|
||||
}
|
||||
return(false);
|
||||
}
|
||||
|
||||
QTextStream out(&file);
|
||||
out.setCodec("UTF-8");
|
||||
out.setGenerateByteOrderMark(false);
|
||||
out << xml_doc.toString(4);
|
||||
file.close();
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
@@ -148,5 +148,6 @@ namespace QET {
|
||||
qreal correctAngle(const qreal &);
|
||||
bool compareCanonicalFilePaths(const QString &, const QString &);
|
||||
QString titleBlockColumnLengthToString(const TitleBlockColumnLength &);
|
||||
bool writeXmlFile(QDomDocument &, const QString &, QString * = 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -464,26 +464,16 @@ bool QETProject::write() {
|
||||
return(true);
|
||||
}
|
||||
|
||||
// ouvre le fichier en ecriture
|
||||
QFile file(file_path_);
|
||||
bool file_opening = file.open(QIODevice::WriteOnly | QIODevice::Text);
|
||||
if (!file_opening) {
|
||||
qDebug() << qPrintable(QString("QETProject::write() : unable to open %1 with write access [%2]").arg(file_path_).arg(QET::pointerString(this)));
|
||||
return(false);
|
||||
}
|
||||
|
||||
qDebug() << qPrintable(QString("QETProject::write() : writing to file %1 [%2]").arg(file_path_).arg(QET::pointerString(this)));
|
||||
|
||||
// realise l'export en XML du projet dans le document XML interne
|
||||
document_root_.clear();
|
||||
document_root_.appendChild(document_root_.importNode(toXml().documentElement(), true));
|
||||
|
||||
QTextStream out(&file);
|
||||
out.setCodec("UTF-8");
|
||||
out << document_root_.toString(4);
|
||||
file.close();
|
||||
|
||||
return(true);
|
||||
QString error_message;
|
||||
bool writing = QET::writeXmlFile(document_root_, file_path_, &error_message);
|
||||
if (!writing) {
|
||||
qDebug() << qPrintable(QString("QETProject::write() : %1 [%2]").arg(error_message).arg(QET::pointerString(this)));
|
||||
}
|
||||
return(writing);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -417,17 +417,11 @@ bool TitleBlockTemplatesFilesCollection::setTemplateXmlDescription(const QString
|
||||
// prevent the watcher from emitting signals while we open and write to file
|
||||
blockSignals(true);
|
||||
|
||||
QFile xml_file(path(template_name));
|
||||
if (!xml_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
return(false);
|
||||
}
|
||||
QDomDocument doc;
|
||||
doc.appendChild(doc.importNode(xml_element, true));
|
||||
|
||||
QTextStream out(&xml_file);
|
||||
out.setCodec("UTF-8");
|
||||
out << doc.toString(4);
|
||||
xml_file.close();
|
||||
bool writing = QET::writeXmlFile(doc, path(template_name));
|
||||
if (!writing) return(false);
|
||||
|
||||
// emit a single signal for the change
|
||||
blockSignals(false);
|
||||
|
||||
@@ -126,12 +126,6 @@ bool TitleBlockTemplate::loadFromXmlElement(const QDomElement &xml_element) {
|
||||
bool TitleBlockTemplate::saveToXmlFile(const QString &filepath) {
|
||||
if (filepath.isEmpty()) return(false);
|
||||
|
||||
// open the file
|
||||
QFile xml_file(filepath);
|
||||
if (!xml_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
return(false);
|
||||
}
|
||||
|
||||
// generate the XML document
|
||||
QDomDocument doc;
|
||||
QDomElement e = doc.createElement("root");
|
||||
@@ -139,13 +133,7 @@ bool TitleBlockTemplate::saveToXmlFile(const QString &filepath) {
|
||||
if (!saving) return(false);
|
||||
doc.appendChild(e);
|
||||
|
||||
// write the file
|
||||
QTextStream out(&xml_file);
|
||||
out.setCodec("UTF-8");
|
||||
out << doc.toString(4);
|
||||
xml_file.close();
|
||||
|
||||
return(true);
|
||||
return(QET::writeXmlFile(doc, filepath));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user