[qet] [3542] Change element information is now managed by an undo command

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


Revision: 3542
Author:   blacksun
Date:     2014-12-11 21:10:28 +0100 (Thu, 11 Dec 2014)
Log Message:
-----------
Change element information is now managed by an undo command

Modified Paths:
--------------
    trunk/qelectrotech.pro
    trunk/sources/ui/elementinfowidget.cpp
    trunk/sources/ui/elementinfowidget.h
    trunk/sources/ui/elementpropertieswidget.cpp
    trunk/sources/ui/elementpropertieswidget.h
    trunk/sources/ui/masterpropertieswidget.cpp
    trunk/sources/ui/masterpropertieswidget.h

Added Paths:
-----------
    trunk/sources/undocommand/
    trunk/sources/undocommand/changeelementinformationcommand.cpp
    trunk/sources/undocommand/changeelementinformationcommand.h

Modified: trunk/qelectrotech.pro
===================================================================
--- trunk/qelectrotech.pro	2014-12-10 17:00:08 UTC (rev 3541)
+++ trunk/qelectrotech.pro	2014-12-11 20:10:28 UTC (rev 3542)
@@ -71,7 +71,8 @@
                sources/properties \
                sources/dvevent \
                sources/editor \
-               sources/editor/esevent
+               sources/editor/esevent \
+               sources/undocommand
 
 
 # Fichiers sources
@@ -79,13 +80,15 @@
            $$files(sources/properties/*.h) \
            $$files(sources/editor/ui/*.h) \
            $$files(sources/editor/esevent/*.h) \
-           $$files(sources/dvevent/*.h)
+           $$files(sources/dvevent/*.h) \
+           $$files(sources/undocommand/*.h)
 
 SOURCES += $$files(sources/*.cpp) $$files(sources/editor/*.cpp) $$files(sources/titleblock/*.cpp) $$files(sources/richtext/*.cpp) $$files(sources/ui/*.cpp) $$files(sources/qetgraphicsitem/*.cpp) $$files(sources/factory/*.cpp) \
            $$files(sources/properties/*.cpp) \
            $$files(sources/editor/ui/*.cpp) \
            $$files(sources/editor/esevent/*.cpp) \
-           $$files(sources/dvevent/*.cpp)
+           $$files(sources/dvevent/*.cpp) \
+           $$files(sources/undocommand/*.cpp)
 
 # Liste des fichiers qui seront incorpores au binaire en tant que ressources Qt
 RESOURCES += qelectrotech.qrc

Modified: trunk/sources/ui/elementinfowidget.cpp
===================================================================
--- trunk/sources/ui/elementinfowidget.cpp	2014-12-10 17:00:08 UTC (rev 3541)
+++ trunk/sources/ui/elementinfowidget.cpp	2014-12-11 20:10:28 UTC (rev 3542)
@@ -18,6 +18,10 @@
 #include "elementinfowidget.h"
 #include "ui_elementinfowidget.h"
 #include "qetapp.h"
+#include "changeelementinformationcommand.h"
+#include "diagram.h"
+#include "elementinfopartwidget.h"
+#include "element.h"
 
 /**
  * @brief ElementInfoWidget::ElementInfoWidget
@@ -48,18 +52,41 @@
 
 /**
  * @brief ElementInfoWidget::apply
- * Apply the new information
+ * Apply the new information with a new undo command (got with method associatedUndo)
+ * pushed to the stack of element project.
+ * Return true if new info change, else false.
  */
-void ElementInfoWidget::apply() {
-	DiagramContext dc;
+bool ElementInfoWidget::apply() {
+	if (QUndoCommand *undo = associatedUndo()) {
+		element_ -> diagram() -> undoStack().push(undo);
+		return true;
+	}
+	return false;
+}
+
+/**
+ * @brief ElementInfoWidget::associatedUndo
+ * If the edited info is different of the actual element info,
+ * return a QUndoCommand with the change.
+ * If no change return nullptr;
+ * @return
+ */
+QUndoCommand* ElementInfoWidget::associatedUndo() const {
+	DiagramContext new_info;
+	DiagramContext old_info = element_ -> elementInformations();
+
 	foreach (ElementInfoPartWidget *eipw, eipw_list) {
-		//add value only if they're something to store
+			//add value only if they're something to store
 		if (!eipw->text().isEmpty())
-			dc.addValue(eipw->key(),
+			new_info.addValue(eipw->key(),
 						eipw->text(),
 						eipw->mustShow());
 	}
-	element_->setElementInformations(dc);
+
+	if (old_info != new_info) {
+		return (new ChangeElementInformationCommand(element_, old_info, new_info));
+	}
+	return nullptr;
 }
 
 /**

Modified: trunk/sources/ui/elementinfowidget.h
===================================================================
--- trunk/sources/ui/elementinfowidget.h	2014-12-10 17:00:08 UTC (rev 3541)
+++ trunk/sources/ui/elementinfowidget.h	2014-12-11 20:10:28 UTC (rev 3542)
@@ -19,10 +19,13 @@
 #define ELEMENTINFOWIDGET_H
 
 #include <QWidget>
-#include "qetgraphicsitem/element.h"
 #include "diagramcontext.h"
-#include "elementinfopartwidget.h"
 
+class Element;
+class QUndoCommand;
+class ElementInfoPartWidget;
+class ChangeElementInformationCommand;
+
 namespace Ui {
 	class ElementInfoWidget;
 }
@@ -35,20 +38,23 @@
 	Q_OBJECT
 
 		//METHODS
-		public:
-	explicit ElementInfoWidget(Element *elmt, QWidget *parent = 0);
-	~ElementInfoWidget();
-	void apply();
-		private:
-	void buildInterface();
-	void fillInfo();
+	public:
+		explicit ElementInfoWidget(Element *elmt, QWidget *parent = 0);
+		~ElementInfoWidget();
 
+		bool apply();
+		QUndoCommand* associatedUndo () const;
+
+	private:
+		void buildInterface();
+		void fillInfo();
+
 		//ATTRIBUTES
-		private:
-	Ui::ElementInfoWidget *ui;
-	Element *element_;
-	DiagramContext elmt_info;
-	QList <ElementInfoPartWidget *> eipw_list;
+	private:
+		Ui::ElementInfoWidget           *ui;
+		Element                         *element_;
+		DiagramContext                   elmt_info;
+		QList <ElementInfoPartWidget *>  eipw_list;
 };
 
 #endif // ELEMENTINFOWIDGET_H

Modified: trunk/sources/ui/elementpropertieswidget.cpp
===================================================================
--- trunk/sources/ui/elementpropertieswidget.cpp	2014-12-10 17:00:08 UTC (rev 3541)
+++ trunk/sources/ui/elementpropertieswidget.cpp	2014-12-11 20:10:28 UTC (rev 3542)
@@ -16,9 +16,13 @@
 	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
 */
 #include "elementpropertieswidget.h"
-#include "qetgraphicsitem/ghostelement.h"
+#include "ghostelement.h"
 #include "qeticons.h"
 #include "diagramposition.h"
+#include "diagram.h"
+#include "elementinfowidget.h"
+#include "masterpropertieswidget.h"
+#include "linksingleelementwidget.h"
 
 /**
  * @brief elementpropertieswidget::elementpropertieswidget
@@ -156,22 +160,47 @@
  * the cliked button
  */
 void elementpropertieswidget::standardButtonClicked(QAbstractButton *button) {
-	int answer = dbb -> buttonRole(button);
+	int  answer = dbb -> buttonRole(button);
+	bool accept = false;
 
 	switch (answer) {
 		case QDialogButtonBox::ResetRole:
 			if (mpw_) mpw_->reset();
 			break;
 		case QDialogButtonBox::ApplyRole:
-			if (eiw_) eiw_->apply();	//element information widget
-			if (mpw_) mpw_->apply();	//master property widget
-			if (lsew_) lsew_->apply();	//link sigle element widget
-			this->accept();
+			accept = true;
+			break;
 		case QDialogButtonBox::RejectRole:
-			this->reject();
+			this -> reject();
+			break;
 		default:
-			this->reject();
+			this -> reject();
+			break;
 	}
+
+	if (accept) {
+		QUndoCommand *a = nullptr;
+		QUndoCommand *b = nullptr;
+
+		if (eiw_) a = eiw_ -> associatedUndo();
+		if (mpw_) b = mpw_ -> associatedUndo();
+		if (lsew_) lsew_ -> apply();
+
+		 //If two undo, we push it in a macro
+		if (a && b) {
+			QUndoStack &stack = element_ -> diagram() -> undoStack();
+			stack.beginMacro(a -> text() + " + " + b -> text());
+			stack.push(a);
+			stack.push(b);
+			stack.endMacro();
+		}
+		else {
+			if (a) element_ -> diagram() -> undoStack().push(a);
+			if (b) element_ -> diagram() -> undoStack().push(b);
+		}
+
+		this -> accept();
+	}
 }
 
 /**

Modified: trunk/sources/ui/elementpropertieswidget.h
===================================================================
--- trunk/sources/ui/elementpropertieswidget.h	2014-12-10 17:00:08 UTC (rev 3541)
+++ trunk/sources/ui/elementpropertieswidget.h	2014-12-11 20:10:28 UTC (rev 3542)
@@ -18,44 +18,49 @@
 #ifndef ELEMENTPROPERTIESWIDGET_H
 #define ELEMENTPROPERTIESWIDGET_H
 
-#include <QtGui>
-#include "qetgraphicsitem/element.h"
-#include "diagram.h"
-#include "elementinfowidget.h"
-#include "masterpropertieswidget.h"
-#include "linksingleelementwidget.h"
+#include <QDialog>
 
-class elementpropertieswidget : public QDialog
-{
+class Diagram;
+class Element;
+class ElementsLocation;
+class ElementInfoWidget;
+class MasterPropertiesWidget;
+class LinkSingleElementWidget;
+class QAbstractButton;
+class QDialogButtonBox;
+class QTabWidget;
+
+class elementpropertieswidget : public QDialog {
 	Q_OBJECT
+
 	public:
-	explicit elementpropertieswidget(Element *elmt, QWidget *parent = 0);
+		explicit elementpropertieswidget(Element *elmt, QWidget *parent = 0);
 
 	private:
-	QWidget* generalWidget();
-	void buildInterface();
+		QWidget* generalWidget();
+		void     buildInterface();
 
 	signals:
-	/// Signal emitted when users wish to locate an element from the diagram within elements collection
-	void findElementRequired(const ElementsLocation &);
-	/// Signal emitted when users wish to edit an element from the diagram
-	void editElementRequired(const ElementsLocation &);
+			/// Signal emitted when users wish to locate an element from the diagram within elements collection
+		void findElementRequired(const ElementsLocation &);
+			/// Signal emitted when users wish to edit an element from the diagram
+		void editElementRequired(const ElementsLocation &);
 
 	public slots:
-	void standardButtonClicked (QAbstractButton *);
-	void findInPanel ();
-	void editElement ();
+		void standardButtonClicked (QAbstractButton *);
+		void findInPanel ();
+		void editElement ();
 
 	private:
-	ElementInfoWidget *eiw_;
-	MasterPropertiesWidget *mpw_;
-	LinkSingleElementWidget *lsew_;
-	QDialogButtonBox *dbb;
-	Element *element_;
-	Diagram *diagram_;
-	QTabWidget *tab_;
-	QPushButton *find_in_panel, *edit_element;
-
+		Element                 *element_;
+		Diagram                 *diagram_;
+		QTabWidget              *tab_;
+		QPushButton             *find_in_panel,
+								*edit_element;
+		QDialogButtonBox        *dbb;
+		ElementInfoWidget       *eiw_;
+		MasterPropertiesWidget  *mpw_;
+		LinkSingleElementWidget *lsew_;
 };
 
 #endif // ELEMENTPROPERTIESWIDGET_H

Modified: trunk/sources/ui/masterpropertieswidget.cpp
===================================================================
--- trunk/sources/ui/masterpropertieswidget.cpp	2014-12-10 17:00:08 UTC (rev 3541)
+++ trunk/sources/ui/masterpropertieswidget.cpp	2014-12-11 20:10:28 UTC (rev 3542)
@@ -53,9 +53,39 @@
 
 /**
  * @brief MasterPropertiesWidget::apply
- * Do what we need when apply new conf
+ * If link betwen edited element and other change,
+ * apply the change with a QUndoCommand (got with method associatedUndo)
+ * pushed to the stack of element project.
+ * Return true if link change, else false
  */
-void MasterPropertiesWidget::apply() {
+bool MasterPropertiesWidget::apply() {
+	if (QUndoCommand *undo = associatedUndo()) {
+		element_ -> diagram() -> undoStack().push(undo);
+		return true;
+	}
+	return false;
+}
+
+/**
+ * @brief MasterPropertiesWidget::reset
+ * Reset curent widget, clear eveything and rebuild widget.
+ */
+void MasterPropertiesWidget::reset() {
+	foreach (QListWidgetItem *lwi, lwi_hash.keys()) {
+		delete lwi;
+	}
+	lwi_hash.clear();
+	buildInterface();
+}
+
+/**
+ * @brief MasterPropertiesWidget::associatedUndo
+ * If link between the edited element and other change,
+ * return a QUndoCommand with this change.
+ * If no change return nullptr.
+ * @return
+ */
+QUndoCommand* MasterPropertiesWidget::associatedUndo() const {
 	QList <Element *> to_link;
 	QList <Element *> linked_ = element_->linkedElements();
 
@@ -63,10 +93,10 @@
 		to_link << lwi_hash[ui->linked_list->item(i)];
 	}
 
-	//If same element are find in to_link and linked, that means
-	// element are already linked, so we remove element on the two list
-	//if linked_ contains element at the end of the operation,
-	//that means this element must be unlinked from @element_
+		//If same element are find in to_link and linked, that means
+		// element are already linked, so we remove element on the two list
+		//if linked_ contains element at the end of the operation,
+		//that means this element must be unlinked from @element_
 	foreach (Element *elmt, to_link) {
 		if(linked_.contains(elmt)) {
 			to_link.removeAll(elmt);
@@ -74,35 +104,24 @@
 		}
 	}
 
-	// if two list, contain element, we link and unlink @element_ with corresponding
-	//undo command, and add first command for parent of the second, user see only one
-	//undo command
+		// if two list, contain element, we link and unlink @element_ with corresponding
+		//undo command, and add first command for parent of the second, user see only one
+		//undo command
 	if (linked_.count() && to_link.count()) {
 		LinkElementsCommand *lec = new LinkElementsCommand(element_, to_link);
 		new unlinkElementsCommand(element_, linked_, lec);
-		element_->diagram()->undoStack().push(lec);
+		return lec;
 	}
-	//Else do the single undo command corresponding to the link.
+		//Else do the single undo command corresponding to the link.
 	else if (to_link.count()) {
-		LinkElementsCommand *lec = new LinkElementsCommand(element_, to_link);
-		element_->diagram()->undoStack().push(lec);
+		return (new LinkElementsCommand(element_, to_link));
 	}
 	else if (linked_.count()) {
-		unlinkElementsCommand *uec = new unlinkElementsCommand(element_, linked_);
-		element_->diagram()->undoStack().push(uec);
+		return (new unlinkElementsCommand(element_, linked_));
 	}
-}
-
-/**
- * @brief MasterPropertiesWidget::reset
- * Reset curent widget, clear eveything and rebuild widget.
- */
-void MasterPropertiesWidget::reset() {
-	foreach (QListWidgetItem *lwi, lwi_hash.keys()) {
-		delete lwi;
+	else {
+		return nullptr;
 	}
-	lwi_hash.clear();
-	buildInterface();
 }
 
 /**

Modified: trunk/sources/ui/masterpropertieswidget.h
===================================================================
--- trunk/sources/ui/masterpropertieswidget.h	2014-12-10 17:00:08 UTC (rev 3541)
+++ trunk/sources/ui/masterpropertieswidget.h	2014-12-11 20:10:28 UTC (rev 3542)
@@ -23,6 +23,7 @@
 
 class QListWidgetItem;
 class Element;
+class QUndoCommand;
 
 namespace Ui {
 	class MasterPropertiesWidget;
@@ -42,8 +43,9 @@
 	explicit MasterPropertiesWidget(Element *elmt, QWidget *parent = 0);
 	~MasterPropertiesWidget();
 
-	void apply();
+	bool apply();
 	void reset();
+	QUndoCommand* associatedUndo() const;
 
 	private:
 	void buildInterface();

Added: trunk/sources/undocommand/changeelementinformationcommand.cpp
===================================================================
--- trunk/sources/undocommand/changeelementinformationcommand.cpp	                        (rev 0)
+++ trunk/sources/undocommand/changeelementinformationcommand.cpp	2014-12-11 20:10:28 UTC (rev 3542)
@@ -0,0 +1,50 @@
+/*
+	Copyright 2006-2014 The QElectroTech Team
+	This file is part of QElectroTech.
+
+	QElectroTech is free software: you can redistribute it and/or modify
+	it under the terms of the GNU General Public License as published by
+	the Free Software Foundation, either version 2 of the License, or
+	(at your option) any later version.
+
+	QElectroTech is distributed in the hope that it will be useful,
+	but WITHOUT ANY WARRANTY; without even the implied warranty of
+	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+	GNU General Public License for more details.
+
+	You should have received a copy of the GNU General Public License
+	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#include "changeelementinformationcommand.h"
+#include "element.h"
+#include <QObject>
+
+/**
+ * @brief ChangeElementInformationCommand::ChangeElementInformationCommand
+ * Default constructor
+ * @param elmt : element to change information
+ * @param old_info : old info of element
+ * @param new_info : new info of element
+ */
+ChangeElementInformationCommand::ChangeElementInformationCommand(Element *elmt, DiagramContext &old_info, DiagramContext &new_info, QUndoCommand *parent) :
+	QUndoCommand (parent),
+	m_element    (elmt),
+	m_old_info   (old_info),
+	m_new_info   (new_info)
+{
+	setText(QObject::tr("Modifier les informations de l'\351l\351ment : %1").arg(elmt -> name()));
+}
+
+/**
+ * @brief ChangeElementInformationCommand::undo
+ */
+void ChangeElementInformationCommand::undo() {
+	m_element -> setElementInformations(m_old_info);
+}
+
+/**
+ * @brief ChangeElementInformationCommand::redo
+ */
+void ChangeElementInformationCommand::redo() {
+	m_element -> setElementInformations(m_new_info);
+}

Added: trunk/sources/undocommand/changeelementinformationcommand.h
===================================================================
--- trunk/sources/undocommand/changeelementinformationcommand.h	                        (rev 0)
+++ trunk/sources/undocommand/changeelementinformationcommand.h	2014-12-11 20:10:28 UTC (rev 3542)
@@ -0,0 +1,44 @@
+/*
+	Copyright 2006-2014 The QElectroTech Team
+	This file is part of QElectroTech.
+
+	QElectroTech is free software: you can redistribute it and/or modify
+	it under the terms of the GNU General Public License as published by
+	the Free Software Foundation, either version 2 of the License, or
+	(at your option) any later version.
+
+	QElectroTech is distributed in the hope that it will be useful,
+	but WITHOUT ANY WARRANTY; without even the implied warranty of
+	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+	GNU General Public License for more details.
+
+	You should have received a copy of the GNU General Public License
+	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifndef CHANGEELEMENTINFORMATIONCOMMAND_H
+#define CHANGEELEMENTINFORMATIONCOMMAND_H
+
+#include <QUndoCommand>
+#include "diagramcontext.h"
+
+class Element;
+
+/**
+ * @brief The ChangeElementInformationCommand class
+ * This class manage undo/redo to change the element information.
+ */
+class ChangeElementInformationCommand : public QUndoCommand
+{
+	public:
+		ChangeElementInformationCommand(Element *elmt, DiagramContext &old_info, DiagramContext &new_info, QUndoCommand *parent = nullptr);
+
+		virtual void undo();
+		virtual void redo();
+
+	private:
+		Element       *m_element;
+		DiagramContext m_old_info,
+					   m_new_info;
+};
+
+#endif // CHANGEELEMENTINFORMATIONCOMMAND_H


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