[qet] [3974] Bug fix : Qet crash when load a conductor without two terminal ( because the allocation of a new conductor fail)

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 3974
Author:   blacksun
Date:     2015-05-19 00:19:14 +0200 (Tue, 19 May 2015)
Log Message:
-----------
Bug fix : Qet crash when load a conductor without two terminal (because the allocation of a new conductor fail)

Modified Paths:
--------------
    trunk/sources/diagram.cpp
    trunk/sources/qetgraphicsitem/conductor.cpp
    trunk/sources/qetgraphicsitem/conductor.h

Modified: trunk/sources/diagram.cpp
===================================================================
--- trunk/sources/diagram.cpp	2015-05-17 17:47:50 UTC (rev 3973)
+++ trunk/sources/diagram.cpp	2015-05-18 22:19:14 UTC (rev 3974)
@@ -685,10 +685,11 @@
 		project_ -> state() == QETProject::ProjectParsingRunning
 	);
 	
-	// chargement de tous les elements du fichier XML
+		//Load all elements from the XML
 	QList<Element *> added_elements;
 	QHash<int, Terminal *> table_adr_id;
-	foreach (QDomElement element_xml, QET::findInDomElement(root, "elements", "element")) {
+	foreach (QDomElement element_xml, QET::findInDomElement(root, "elements", "element"))
+	{
 		if (!Element::valideXml(element_xml)) continue;
 		
 		// cree un element dont le type correspond a l'id type
@@ -698,7 +699,8 @@
 		
 		int state = 0;
 		Element *nvel_elmt = ElementFactory::Instance() -> createElement(element_location, 0, &state);
-		if (state) {
+		if (state)
+		{
 			QString debug_message = QString("Diagram::fromXml() : Le chargement de la description de l'element %1 a echoue avec le code d'erreur %2").arg(element_location.path()).arg(state);
 			qDebug() << qPrintable(debug_message);
 			delete nvel_elmt;
@@ -760,10 +762,15 @@
 			Terminal *p2 = table_adr_id.value(id_p2);
 			if (p1 != p2)
 			{
-				Conductor *c = new Conductor(table_adr_id.value(id_p1), table_adr_id.value(id_p2));
-				addItem(c);
-				c -> fromXml(f);
-				added_conductors << c;
+				Conductor *c = new Conductor(p1, p2);
+				if (c->isValid())
+				{
+					addItem(c);
+					c -> fromXml(f);
+					added_conductors << c;
+				}
+				else
+					delete c;
 			}
 		}
 		else qDebug() << "Diagram::fromXml() : Le chargement du conducteur" << id_p1 << id_p2 << "a echoue";

Modified: trunk/sources/qetgraphicsitem/conductor.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/conductor.cpp	2015-05-17 17:47:50 UTC (rev 3973)
+++ trunk/sources/qetgraphicsitem/conductor.cpp	2015-05-18 22:19:14 UTC (rev 3974)
@@ -59,13 +59,12 @@
 	setZValue(9);
 	previous_z_value = zValue();
 
-	// ajout du conducteur a la liste de conducteurs de chacune des deux bornes
+		//Add this conductor to the list of conductor of each of the two terminal
 	bool ajout_p1 = terminal1 -> addConductor(this);
 	bool ajout_p2 = terminal2 -> addConductor(this);
+		//m_valid become false if the conductor can't be added to terminal (conductor already exist)
+	m_valid = (!ajout_p1 || !ajout_p2) ? false : true;
 	
-	// en cas d'echec de l'ajout (conducteur deja existant notamment)
-	if (!ajout_p1 || !ajout_p2) return;
-	
 	// attributs de dessin par defaut (communs a tous les conducteurs)
 	if (!pen_and_brush_initialized) {
 		conductor_pen.setJoinStyle(Qt::MiterJoin);
@@ -114,6 +113,15 @@
 }
 
 /**
+ * @brief Conductor::isValid
+ * @return true if conductor is valid else false;
+ * A non valid conductor, is a conductor without two terminal
+ */
+bool Conductor::isValid() const {
+	return m_valid;
+}
+
+/**
 	Met a jour la representation graphique du conducteur en recalculant son
 	trace. Cette fonction est typiquement appelee lorsqu'une seule des bornes du
 	conducteur a change de position.

Modified: trunk/sources/qetgraphicsitem/conductor.h
===================================================================
--- trunk/sources/qetgraphicsitem/conductor.h	2015-05-17 17:47:50 UTC (rev 3973)
+++ trunk/sources/qetgraphicsitem/conductor.h	2015-05-18 22:19:14 UTC (rev 3974)
@@ -45,6 +45,8 @@
 	public:
 		Conductor(Terminal *, Terminal *);
 		virtual ~Conductor();
+
+		bool isValid() const;
 	
 	private:
 		Conductor(const Conductor &);
@@ -164,6 +166,7 @@
 	qreal segments_squares_scale_;
 	/// Define whether and how the conductor should be highlighted
 	Highlight must_highlight_;
+	bool m_valid;
 	
 	private:
 	void segmentsToPath();


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/