[qet] [4047] Revamp some code

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


Revision: 4047
Author:   blacksun
Date:     2015-07-12 17:05:59 +0200 (Sun, 12 Jul 2015)
Log Message:
-----------
Revamp some code

Modified Paths:
--------------
    trunk/sources/diagram.cpp
    trunk/sources/diagram.h
    trunk/sources/diagramcommands.cpp

Modified: trunk/sources/diagram.cpp
===================================================================
--- trunk/sources/diagram.cpp	2015-07-12 13:35:40 UTC (rev 4046)
+++ trunk/sources/diagram.cpp	2015-07-12 15:05:59 UTC (rev 4047)
@@ -861,131 +861,78 @@
 
 /**
  * @brief Diagram::addItem
- * Add element to diagram
- * @param element
+ * Réimplemented from QGraphicsScene::addItem(QGraphicsItem *item)
+ * Do some specific operation if item need it (for exemple an element)
+ * @param item
  */
-void Diagram::addItem(Element *element) {
-	if (!element || isReadOnly()) return;
+void Diagram::addItem(QGraphicsItem *item)
+{
+	if (!item || isReadOnly() || item->scene() == this) return;
+	QGraphicsScene::addItem(item);
 
-	if (element -> scene() != this)
-		QGraphicsScene::addItem(element);
-	foreach(ElementTextItem *eti, element -> texts()) {
-		connect(
-			eti,
-			SIGNAL(diagramTextChanged(DiagramTextItem *, const QString &, const QString &)),
-			this,
-			SLOT(diagramTextChanged(DiagramTextItem *, const QString &, const QString &))
-		);
-	}
-}
+	switch (item->type())
+	{
+		case Element::Type:
+		{
+			const Element *elmt = static_cast<const Element*>(item);
+			foreach(ElementTextItem *eti, elmt->texts())
+				connect (eti, &ElementTextItem::diagramTextChanged, this, &Diagram::diagramTextChanged);
+		}
+			break;
 
-/**
- * @brief Diagram::addItem
- * Add conductor to scene.
- * @param conductor
- */
-void Diagram::addItem(Conductor *conductor) {
-	if (!conductor || isReadOnly()) return;
-	
-	if (conductor -> scene() != this) {
-		QGraphicsScene::addItem(conductor);
-		conductor -> terminal1 -> addConductor(conductor);
-		conductor -> terminal2 -> addConductor(conductor);
-		conductor -> calculateTextItemPosition();
+		case Conductor::Type:
+		{
+			Conductor *conductor = static_cast<Conductor *>(item);
+			conductor->terminal1->addConductor(conductor);
+			conductor->terminal2->addConductor(conductor);
+			conductor->calculateTextItemPosition();
+		}
+			break;
+
+		case IndependentTextItem::Type:
+		{
+			const IndependentTextItem *text = static_cast<const IndependentTextItem *>(item);
+			connect(text, &IndependentTextItem::diagramTextChanged, this, &Diagram::diagramTextChanged);
+		}
 	}
 }
 
 /**
- * @brief Diagram::addItem
- * Add text item to diagram
- * @param iti
- */
-void Diagram::addItem(IndependentTextItem *iti) {
-	if (!iti || isReadOnly()) return;
-	
-	if (iti -> scene() != this)
-		QGraphicsScene::addItem(iti);
-	
-	connect(
-		iti,
-		SIGNAL(diagramTextChanged(DiagramTextItem *, const QString &, const QString &)),
-		this,
-		SLOT(diagramTextChanged(DiagramTextItem *, const QString &, const QString &))
-	);
-}
-
-/**
- * @brief Diagram::addItem
- * Generique method to add item to scene
+ * @brief Diagram::removeItem
+ * Réimplemented from QGraphicsScene::removeItem(QGraphicsItem *item)
+ * Do some specific operation if item need it (for exemple an element)
  * @param item
  */
-void Diagram::addItem(QGraphicsItem *item) {
+void Diagram::removeItem(QGraphicsItem *item)
+{
 	if (!item || isReadOnly()) return;
-	if (item -> scene() != this)
-		QGraphicsScene::addItem(item);
-}
 
-/**
- * @brief Diagram::removeItem
- * Remove an element from the scene
- * @param element
- */
-void Diagram::removeItem(Element *element) {
-	if (!element || isReadOnly()) return;
+	switch (item->type())
+	{
+		case Element::Type:
+		{
+			Element *elmt = static_cast<Element *>(item);
+			elmt->unlinkAllElements();
+			foreach(ElementTextItem *text, elmt->texts())
+				disconnect(text, &ElementTextItem::diagramTextChanged, this, &Diagram::diagramTextChanged);
+		}
+			break;
 
-	// remove all links of element
-	element->unlinkAllElements();
+		case Conductor::Type:
+		{
+			Conductor *conductor = static_cast<Conductor *>(item);
+			conductor->terminal1->removeConductor(conductor);
+			conductor->terminal2->removeConductor(conductor);
+		}
+			break;
 
-	QGraphicsScene::removeItem(element);
-
-	foreach(ElementTextItem *eti, element -> texts()) {
-		disconnect(
-			eti,
-			SIGNAL(diagramTextChanged(DiagramTextItem *, const QString &, const QString &)),
-			this,
-			SLOT(diagramTextChanged(DiagramTextItem *, const QString &, const QString &))
-		);
+		case IndependentTextItem::Type:
+		{
+			const IndependentTextItem *text = static_cast<const IndependentTextItem *>(item);
+			disconnect(text, &IndependentTextItem::diagramTextChanged, this, &Diagram::diagramTextChanged);
+		}
 	}
-}
 
-/**
- * @brief Diagram::removeItem
- * Remove a conductor from diagram
- * @param conductor
- */
-void Diagram::removeItem(Conductor *conductor) {
-	if (!conductor || isReadOnly()) return;
-
-	conductor -> terminal1 -> removeConductor(conductor);
-	conductor -> terminal2 -> removeConductor(conductor);
-
-	QGraphicsScene::removeItem(conductor);
-}
-
-/**
- * @brief Diagram::removeItem
- * Remove text field from diagram
- * @param iti
- */
-void Diagram::removeItem(IndependentTextItem *iti) {
-	if (!iti || isReadOnly()) return;
-	
-	QGraphicsScene::removeItem(iti);
-
-	disconnect(
-		iti,
-		SIGNAL(diagramTextChanged(DiagramTextItem *, const QString &, const QString &)),
-		this,
-		SLOT(diagramTextChanged(DiagramTextItem *, const QString &, const QString &))
-	);
-}
-
-/**
- * @brief Diagram::removeItem
- * Generique methode to remove QGraphicsItem from diagram
- * @param item
- */
-void Diagram::removeItem(QGraphicsItem *item) {
 	QGraphicsScene::removeItem(item);
 }
 

Modified: trunk/sources/diagram.h
===================================================================
--- trunk/sources/diagram.h	2015-07-12 13:35:40 UTC (rev 4046)
+++ trunk/sources/diagram.h	2015-07-12 15:05:59 UTC (rev 4047)
@@ -152,17 +152,10 @@
 	bool wasWritten() const;
 	QDomElement writeXml(QDomDocument &) const;
 	
-	// methods related to graphics items addition/removal on the diagram
+		// methods related to graphics items addition/removal on the diagram
 	void initElementsLinks();
-	virtual void addItem (Element             *element);
-	virtual void addItem (Conductor           *conductor);
-	virtual void addItem (IndependentTextItem *iti);
-	virtual void addItem (QGraphicsItem       *item);
-
-	virtual void removeItem (Element             *element);
-	virtual void removeItem (Conductor           *conductor);
-	virtual void removeItem (IndependentTextItem *iti);
-	virtual void removeItem (QGraphicsItem       *item);
+	virtual void addItem    (QGraphicsItem *item);
+	virtual void removeItem (QGraphicsItem *item);
 	
 	// methods related to graphics options
 	ExportProperties applyProperties(const ExportProperties &);

Modified: trunk/sources/diagramcommands.cpp
===================================================================
--- trunk/sources/diagramcommands.cpp	2015-07-12 13:35:40 UTC (rev 4046)
+++ trunk/sources/diagramcommands.cpp	2015-07-12 15:05:59 UTC (rev 4047)
@@ -74,140 +74,101 @@
 	diagram -> qgiManager().release(removed_content.items(DiagramContent::All));
 }
 
-/// annule les suppressions
-void DeleteElementsCommand::undo() {
+/**
+ * @brief DeleteElementsCommand::undo
+ * Undo this command
+ */
+void DeleteElementsCommand::undo()
+{
 	diagram -> showMe();
 
-	foreach(Element *e, removed_content.elements) {
-		diagram -> addItem(e);
-	}
+	foreach(QGraphicsItem *item, removed_content.items())
+		diagram->addItem(item);
 
-	//We relink element after every element was added to diagram
-	foreach(Element *e, removed_content.elements) {
-		foreach (Element *elmt, m_link_hash[e]) {
+		//We relink element after every element was added to diagram
+	foreach(Element *e, removed_content.elements)
+		foreach (Element *elmt, m_link_hash[e])
 				e -> linkToElement(elmt);
-		}
-	}
-	
-	foreach(Conductor *c, removed_content.conductors(DiagramContent::AnyConductor)) {
-		diagram -> addItem(c);
-	}
-	
-	foreach(IndependentTextItem *t, removed_content.textFields) {
-		diagram -> addItem(t);
-	}
-
-	foreach(DiagramImageItem *dii, removed_content.images) {
-		diagram -> addItem(dii);
-	}
-
-	foreach(QetShapeItem *dsi, removed_content.shapes) {
-		diagram -> addItem(dsi);
-	}
 }
 
 /**
  * @brief DeleteElementsCommand::redo
  * Redo the delete command
  */
-void DeleteElementsCommand::redo() {
+void DeleteElementsCommand::redo()
+{
 	diagram -> showMe();
 
-	// Remove Conductor
-	foreach(Conductor *c, removed_content.conductors(DiagramContent::AnyConductor)) {
-		diagram -> removeItem(c);
-
-		//If option one text per folio is enable, and the text item of
-		//current conductor is visible (that mean the conductor have the single displayed text)
-		//We call adjustTextItemPosition to other conductor at the same potential to keep
-		//a visible text on this potential.
-		if (diagram -> defaultConductorProperties.m_one_text_per_folio && c -> textItem() -> isVisible()) {
+	foreach(Conductor *c, removed_content.conductors(DiagramContent::AnyConductor))
+	{
+			//If option one text per folio is enable, and the text item of
+			//current conductor is visible (that mean the conductor have the single displayed text)
+			//We call adjustTextItemPosition to other conductor at the same potential to keep
+			//a visible text on this potential.
+		if (diagram -> defaultConductorProperties.m_one_text_per_folio && c -> textItem() -> isVisible())
+		{
 			QList <Conductor *> conductor_list;
 			conductor_list << c -> relatedPotentialConductors(false).toList();
-			if (conductor_list.count()) {
+			if (conductor_list.count())
 				conductor_list.first() -> calculateTextItemPosition();
-			}
 		}
 	}
 	
-	// Remove elements
-	foreach(Element *e, removed_content.elements) {
-		//Get linked element, for relink it at undo
+	foreach(Element *e, removed_content.elements)
+	{
+			//Get linked element, for relink it at undo
 		if (!e->linkedElements().isEmpty())
 			m_link_hash.insert(e, e->linkedElements());
-		diagram -> removeItem(e);
 	}
-	
-	// Remove texts
-	foreach(IndependentTextItem *t, removed_content.textFields) {
-		diagram -> removeItem(t);
-	}
 
-	// Remove images
-	foreach(DiagramImageItem *dii, removed_content.images) {
-		diagram -> removeItem(dii);
-	}
-
-	// Remove shapes
-	foreach(QetShapeItem *dsi, removed_content.shapes) {
-		diagram -> removeItem(dsi);
-	}
+	foreach(QGraphicsItem *item, removed_content.items())
+		diagram->removeItem(item);
 }
 
 /**
-	Constructeur
-	@param dia Schema sur lequel on colle les elements et conducteurs
-	@param c Contenu a coller sur le schema
-	@param parent QUndoCommand parent
-*/
-PasteDiagramCommand::PasteDiagramCommand(
-	Diagram *dia,
-	const DiagramContent &c,
-	QUndoCommand *parent
-) :
+ * @brief PasteDiagramCommand::PasteDiagramCommand
+ * Constructor
+ * @param dia : diagram where we must to paste
+ * @param c : content to past
+ * @param parent : parent undo command
+ */
+PasteDiagramCommand::PasteDiagramCommand( Diagram *dia, const DiagramContent &c, QUndoCommand *parent) :
 	QUndoCommand(parent),
 	content(c),
 	diagram(dia),
 	filter(DiagramContent::Elements|DiagramContent::TextFields|DiagramContent::Images|DiagramContent::ConductorsToMove | DiagramContent::Shapes),
 	first_redo(true)
 {
-	
-	setText(
-		QString(
-			QObject::tr(
-				"coller %1",
-				"undo caption - %1 is a sentence listing the content to paste"
-			).arg(content.sentence(filter))
-		)
-	);
+	setText(QObject::tr("coller %1", "undo caption - %1 is a sentence listing the content to paste").arg(content.sentence(filter)));
 	diagram -> qgiManager().manage(content.items(filter));
 }
 
-/// Destructeur
+/**
+ * @brief PasteDiagramCommand::~PasteDiagramCommand
+ * Destructor
+ */
 PasteDiagramCommand::~PasteDiagramCommand() {
 	diagram -> qgiManager().release(content.items(filter));
 }
 
-/// annule le coller
-void PasteDiagramCommand::undo() {
+/**
+ * @brief PasteDiagramCommand::undo
+ * Undo this command
+ */
+void PasteDiagramCommand::undo()
+{
 	diagram -> showMe();
-	// remove the conductors
-	foreach(Conductor *c, content.conductorsToMove) diagram -> removeItem(c);
-	
-	// remove the elements
-	foreach(Element *e, content.elements) diagram -> removeItem(e);
-	
-	// remove the texts
-	foreach(IndependentTextItem *t, content.textFields) diagram -> removeItem(t);
 
-	// remove the images and shapes
-	foreach(QGraphicsItem *qgi, content.items(DiagramContent::Images | DiagramContent::Shapes)) diagram -> removeItem(qgi);
+	foreach(QGraphicsItem *item, content.items(filter))
+		diagram->removeItem(item);
 }
 
 /**
  * @brief PasteDiagramCommand::redo
+ * Redo this commnand
  */
-void PasteDiagramCommand::redo() {
+void PasteDiagramCommand::redo()
+{
 	diagram -> showMe();
 
 	if (first_redo) {
@@ -240,18 +201,10 @@
 			c -> setProperties(cp);
 		}
 	}
-	else {
-		// paste the elements
-		foreach(Element *e, content.elements) diagram -> addItem(e);
-		
-		// paste the conductors
-		foreach(Conductor *c, content.conductorsToMove) diagram -> addItem(c);
-		
-		// paste the texts
-		foreach(IndependentTextItem *t, content.textFields) diagram -> addItem(t);
-
-		// paste the images and shapes
-		foreach(QGraphicsItem *qgi, content.items(DiagramContent::Images | DiagramContent::Shapes)) diagram -> addItem(qgi);
+	else
+	{
+		foreach (QGraphicsItem *item, content.items(filter))
+			diagram->addItem(item);
 	}
 	foreach (QGraphicsItem *qgi, content.items()) qgi -> setSelected(true);
 }


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