mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-17 20:50:34 +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
|
@return true en cas de reussite, false sinon
|
||||||
*/
|
*/
|
||||||
bool QETElementEditor::toFile(const QString &fn) {
|
bool QETElementEditor::toFile(const QString &fn) {
|
||||||
QFile file(fn);
|
QDomDocument element_xml = ce_scene -> toXml();
|
||||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
bool writing = QET::writeXmlFile(element_xml, fn);
|
||||||
QET::MessageBox::warning(this, tr("Erreur", "message box title"), tr("Impossible d'\351crire dans ce fichier", "message box content"));
|
if (!writing) {
|
||||||
return(false);
|
QET::MessageBox::warning(
|
||||||
|
this,
|
||||||
|
tr("Erreur", "message box title"),
|
||||||
|
tr("Impossible d'\351crire dans ce fichier", "message box content")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
QTextStream out(&file);
|
return(writing);
|
||||||
out.setCodec("UTF-8");
|
|
||||||
out << ce_scene -> toXml().toString(4);
|
|
||||||
file.close();
|
|
||||||
return(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -20,6 +20,8 @@
|
|||||||
#include "fileelementscategory.h"
|
#include "fileelementscategory.h"
|
||||||
#include "fileelementscollection.h"
|
#include "fileelementscollection.h"
|
||||||
#include "qetapp.h"
|
#include "qetapp.h"
|
||||||
|
#include "qet.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Constructeur
|
Constructeur
|
||||||
@param uri Chemin du fichier contenant la definition de l'element
|
@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
|
@return true si l'operation a reussi, false sinon
|
||||||
*/
|
*/
|
||||||
bool FileElementDefinition::write() {
|
bool FileElementDefinition::write() {
|
||||||
QFile file(file_path);
|
return(QET::writeXmlFile(xml_element_, 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -18,6 +18,8 @@
|
|||||||
#include "fileelementscategory.h"
|
#include "fileelementscategory.h"
|
||||||
#include "fileelementscollection.h"
|
#include "fileelementscollection.h"
|
||||||
#include "fileelementdefinition.h"
|
#include "fileelementdefinition.h"
|
||||||
|
#include "qet.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Constructeur
|
Constructeur
|
||||||
@param path Chemin du dossier de la categorie
|
@param path Chemin du dossier de la categorie
|
||||||
@@ -412,21 +414,8 @@ bool FileElementsCategory::write() {
|
|||||||
document.appendChild(root);
|
document.appendChild(root);
|
||||||
root.appendChild(category_names.toXml(document));
|
root.appendChild(category_names.toXml(document));
|
||||||
|
|
||||||
// repere le chemin du fichier de configuration de la categorie
|
QString filepath = cat_dir.absolutePath() + "/qet_directory";
|
||||||
QFile directory_conf(cat_dir.absolutePath() + "/qet_directory");
|
return(QET::writeXmlFile(document, filepath));
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -528,3 +528,38 @@ QString QET::titleBlockColumnLengthToString(const TitleBlockColumnLength &icl)
|
|||||||
else if (icl == RelativeToRemainingLength) type_str = "relative to remaining";
|
else if (icl == RelativeToRemainingLength) type_str = "relative to remaining";
|
||||||
return(type_str);
|
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 &);
|
qreal correctAngle(const qreal &);
|
||||||
bool compareCanonicalFilePaths(const QString &, const QString &);
|
bool compareCanonicalFilePaths(const QString &, const QString &);
|
||||||
QString titleBlockColumnLengthToString(const TitleBlockColumnLength &);
|
QString titleBlockColumnLengthToString(const TitleBlockColumnLength &);
|
||||||
|
bool writeXmlFile(QDomDocument &, const QString &, QString * = 0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -464,26 +464,16 @@ bool QETProject::write() {
|
|||||||
return(true);
|
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
|
// realise l'export en XML du projet dans le document XML interne
|
||||||
document_root_.clear();
|
document_root_.clear();
|
||||||
document_root_.appendChild(document_root_.importNode(toXml().documentElement(), true));
|
document_root_.appendChild(document_root_.importNode(toXml().documentElement(), true));
|
||||||
|
|
||||||
QTextStream out(&file);
|
QString error_message;
|
||||||
out.setCodec("UTF-8");
|
bool writing = QET::writeXmlFile(document_root_, file_path_, &error_message);
|
||||||
out << document_root_.toString(4);
|
if (!writing) {
|
||||||
file.close();
|
qDebug() << qPrintable(QString("QETProject::write() : %1 [%2]").arg(error_message).arg(QET::pointerString(this)));
|
||||||
|
}
|
||||||
return(true);
|
return(writing);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -417,17 +417,11 @@ bool TitleBlockTemplatesFilesCollection::setTemplateXmlDescription(const QString
|
|||||||
// prevent the watcher from emitting signals while we open and write to file
|
// prevent the watcher from emitting signals while we open and write to file
|
||||||
blockSignals(true);
|
blockSignals(true);
|
||||||
|
|
||||||
QFile xml_file(path(template_name));
|
|
||||||
if (!xml_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
|
||||||
return(false);
|
|
||||||
}
|
|
||||||
QDomDocument doc;
|
QDomDocument doc;
|
||||||
doc.appendChild(doc.importNode(xml_element, true));
|
doc.appendChild(doc.importNode(xml_element, true));
|
||||||
|
|
||||||
QTextStream out(&xml_file);
|
bool writing = QET::writeXmlFile(doc, path(template_name));
|
||||||
out.setCodec("UTF-8");
|
if (!writing) return(false);
|
||||||
out << doc.toString(4);
|
|
||||||
xml_file.close();
|
|
||||||
|
|
||||||
// emit a single signal for the change
|
// emit a single signal for the change
|
||||||
blockSignals(false);
|
blockSignals(false);
|
||||||
|
|||||||
@@ -126,12 +126,6 @@ bool TitleBlockTemplate::loadFromXmlElement(const QDomElement &xml_element) {
|
|||||||
bool TitleBlockTemplate::saveToXmlFile(const QString &filepath) {
|
bool TitleBlockTemplate::saveToXmlFile(const QString &filepath) {
|
||||||
if (filepath.isEmpty()) return(false);
|
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
|
// generate the XML document
|
||||||
QDomDocument doc;
|
QDomDocument doc;
|
||||||
QDomElement e = doc.createElement("root");
|
QDomElement e = doc.createElement("root");
|
||||||
@@ -139,13 +133,7 @@ bool TitleBlockTemplate::saveToXmlFile(const QString &filepath) {
|
|||||||
if (!saving) return(false);
|
if (!saving) return(false);
|
||||||
doc.appendChild(e);
|
doc.appendChild(e);
|
||||||
|
|
||||||
// write the file
|
return(QET::writeXmlFile(doc, filepath));
|
||||||
QTextStream out(&xml_file);
|
|
||||||
out.setCodec("UTF-8");
|
|
||||||
out << doc.toString(4);
|
|
||||||
xml_file.close();
|
|
||||||
|
|
||||||
return(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user