diff --git a/sources/diagram.cpp b/sources/diagram.cpp index ba015c890..aba140684 100644 --- a/sources/diagram.cpp +++ b/sources/diagram.cpp @@ -85,7 +85,7 @@ Diagram::~Diagram() { if (qgraphicsitem_cast(qgi)) continue; deletable_items << qgi; } - + // suppression des items supprimables foreach(QGraphicsItem *qgi_d, deletable_items) { delete qgi_d; @@ -280,6 +280,9 @@ QDomDocument Diagram::toXml(bool whole_content) { racine.setAttribute("height", QString("%1").arg(border_and_inset.diagramHeight())); racine.setAttribute("displaycols", border_and_inset.columnsAreDisplayed() ? "true" : "false"); racine.setAttribute("displayrows", border_and_inset.rowsAreDisplayed() ? "true" : "false"); + if (!inset_template_name_.isEmpty()) { + racine.setAttribute("insettemplate", inset_template_name_); + } // type de conducteur par defaut QDomElement default_conductor = document.createElement("defaultconductor"); @@ -419,6 +422,14 @@ bool Diagram::fromXml(QDomElement &document, QPointF position, bool consider_inf border_and_inset.setDate(QDate::fromString(root.attribute("date"), "yyyyMMdd")); border_and_inset.setFileName(root.attribute("filename")); border_and_inset.setFolio(root.attribute("folio")); + if (root.hasAttribute("insettemplate") && project_) { + QString inset_template_name = root.attribute("insettemplate"); + const InsetTemplate *inset_template = project_ -> getTemplateByName(inset_template_name); + if (inset_template) { + inset_template_name_ = inset_template_name; + border_and_inset.setInsetTemplate(inset_template); + } + } bool ok; // nombre de colonnes diff --git a/sources/diagram.h b/sources/diagram.h index 1d9bd050d..dd47516a9 100644 --- a/sources/diagram.h +++ b/sources/diagram.h @@ -85,6 +85,7 @@ class Diagram : public QGraphicsScene { QDomDocument xml_document; QETProject *project_; bool read_only_; + QString inset_template_name_; // methodes protected: diff --git a/sources/qetproject.cpp b/sources/qetproject.cpp index 19da7358e..a76a20121 100644 --- a/sources/qetproject.cpp +++ b/sources/qetproject.cpp @@ -25,6 +25,7 @@ #include "integrationmoveelementshandler.h" #include "basicmoveelementshandler.h" #include "qetmessagebox.h" +#include "insettemplate.h" QString QETProject::integration_category_name = "import"; @@ -266,6 +267,51 @@ void QETProject::setTitle(const QString &title) { emit(projectInformationsChanged(this)); } +/** + @return the list of the inset templates embedded within this project +*/ +QList QETProject::embeddedInsetTemplates() const { + return(inset_templates_xml_.keys()); +} + +/** + @param template_name Name of the requested template + @return the requested template, or 0 if there is no vltaid template of this + name within the project +*/ +const InsetTemplate *QETProject::getTemplateByName(const QString &template_name) { + // Do we have already loaded this template? + if (inset_templates_.contains(template_name)) { + return(inset_templates_[template_name]); + } + + // No? Do we even know of it? + if (!inset_templates_xml_.contains(template_name)) { + return(0); + } + + // Ok, we have its XML description, we have to generate an InsetTemplate object + InsetTemplate *inset_template = new InsetTemplate(this); + if (inset_template -> loadFromXmlElement(inset_templates_xml_[template_name])) { + inset_templates_.insert(template_name, inset_template); + return(inset_template); + } else { + return(0); + } +} + +/** + @param template_name Name of the requested template + @return the XML description of the requested template, or a null QDomElement + if the project does not have such an inset template +*/ +QDomElement QETProject::getTemplateXmlDescriptionByName(const QString &template_name) { + if (inset_templates_xml_.contains(template_name)) { + return(inset_templates_xml_[template_name]); + } + return(QDomElement()); +} + /** @return les dimensions par defaut utilisees lors de la creation d'un nouveau schema dans ce projet. @@ -327,6 +373,16 @@ QDomDocument QETProject::toXml() { project_root.setAttribute("title", project_title_); xml_doc.appendChild(project_root); + // inset templates, if any + if (inset_templates_xml_.count()) { + qDebug() << qPrintable(QString("QETProject::toXml() : exporting %1 inset templates").arg(inset_templates_xml_.count())); + QDomElement insettemplates_elmt = xml_doc.createElement("insettemplates"); + foreach (QDomElement e, inset_templates_xml_) { + insettemplates_elmt.appendChild(e); + } + project_root.appendChild(insettemplates_elmt); + } + // proprietes pour les nouveaux schemas QDomElement new_diagrams_properties = xml_doc.createElement("newdiagrams"); writeDefaultPropertiesXml(new_diagrams_properties); @@ -734,6 +790,9 @@ void QETProject::readProjectXml() { // charge les proprietes par defaut pour les nouveaux schemas readDefaultPropertiesXml(); + // load the embedded inset templates + readEmbeddedTemplatesXml(); + // charge la collection embarquee readElementsCollectionXml(); @@ -776,6 +835,24 @@ void QETProject::readDiagramsXml() { } } +/** + Loads the embedded template from the XML description of the project +*/ +void QETProject::readEmbeddedTemplatesXml() { + foreach (QDomElement e, QET::findInDomElement(document_root_.documentElement(), "insettemplates", "insettemplate")) { + // each inset template must have a name + if (!e.hasAttribute("name")) continue; + QString inset_template_name = e.attribute("name"); + + // if several templates have the same name, we keep the first one encountered + if (inset_templates_xml_.contains(inset_template_name)) continue; + + // we simply store the XML element describing the inset template, + // without any further analysis for the moment + inset_templates_xml_.insert(inset_template_name, e); + } +} + /** Charge les schemas depuis la description XML du projet */ diff --git a/sources/qetproject.h b/sources/qetproject.h index 5794c43d3..26a9f3649 100644 --- a/sources/qetproject.h +++ b/sources/qetproject.h @@ -29,6 +29,7 @@ class ElementsCollection; class ElementsCategory; class ElementDefinition; class ElementsLocation; +class InsetTemplate; class XmlElementsCollection; class MoveElementsHandler; /** @@ -79,6 +80,9 @@ class QETProject : public QObject { QString title() const; qreal declaredQElectroTechVersion(); void setTitle(const QString &); + QList embeddedInsetTemplates() const; + const InsetTemplate *getTemplateByName(const QString &template_name); + QDomElement getTemplateXmlDescriptionByName(const QString &); BorderProperties defaultBorderProperties() const; void setDefaultBorderProperties(const BorderProperties &); InsetProperties defaultInsetProperties() const; @@ -124,6 +128,7 @@ class QETProject : public QObject { void readProjectXml(); void readDiagramsXml(); void readElementsCollectionXml(); + void readEmbeddedTemplatesXml(); void readDefaultPropertiesXml(); void writeDefaultPropertiesXml(QDomElement &); void addDiagram(Diagram *); @@ -158,5 +163,9 @@ class QETProject : public QObject { ConductorProperties default_conductor_properties_; /// Proprietes par defaut du cartouche pour les nouveaux schemas dans ce projet InsetProperties default_inset_properties_; + /// XML descriptions of embedded inset templates + QHash inset_templates_xml_; + /// Already parsed embedded inset templates + QHash inset_templates_; }; #endif