[qet] [3996] Element info widget : enable the use of live edit mode

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


Revision: 3996
Author:   blacksun
Date:     2015-05-31 17:02:41 +0200 (Sun, 31 May 2015)
Log Message:
-----------
Element info widget : enable the use of live edit mode

Modified Paths:
--------------
    trunk/sources/qetgraphicsitem/element.cpp
    trunk/sources/qetgraphicsitem/element.h
    trunk/sources/ui/elementinfopartwidget.cpp
    trunk/sources/ui/elementinfopartwidget.h
    trunk/sources/ui/elementinfowidget.cpp
    trunk/sources/ui/elementinfowidget.h
    trunk/sources/undocommand/changeelementinformationcommand.cpp
    trunk/sources/undocommand/changeelementinformationcommand.h

Added Paths:
-----------
    trunk/dev_doc/ID_of_QUndoCommand.txt

Added: trunk/dev_doc/ID_of_QUndoCommand.txt
===================================================================
--- trunk/dev_doc/ID_of_QUndoCommand.txt	                        (rev 0)
+++ trunk/dev_doc/ID_of_QUndoCommand.txt	2015-05-31 15:02:41 UTC (rev 3996)
@@ -0,0 +1 @@
+ChangeElementInformationCommand  = 1


Property changes on: trunk/dev_doc/ID_of_QUndoCommand.txt
___________________________________________________________________
Added: svn:executable
   + *

Modified: trunk/sources/qetgraphicsitem/element.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/element.cpp	2015-05-29 07:28:58 UTC (rev 3995)
+++ trunk/sources/qetgraphicsitem/element.cpp	2015-05-31 15:02:41 UTC (rev 3996)
@@ -415,7 +415,7 @@
 	uuid_= QUuid(e.attribute("uuid", QUuid::createUuid().toString()));
 
 	//load informations
-	element_informations_.fromXml(e.firstChildElement("elementInformations"), "elementInformation");
+	m_element_informations.fromXml(e.firstChildElement("elementInformations"), "elementInformation");
 
 		//Position and selection.
 		//We directly call setPos from QGraphicsObject, because QetGraphicsItem will snap to grid
@@ -498,9 +498,9 @@
 	}
 
 	//save information of this element
-	if (! element_informations_.keys().isEmpty()) {
+	if (! m_element_informations.keys().isEmpty()) {
 		QDomElement infos = document.createElement("elementInformations");
-		element_informations_.toXml(infos, "elementInformation");
+		m_element_informations.toXml(infos, "elementInformation");
 		element.appendChild(infos);
 	}
 
@@ -556,13 +556,15 @@
 /**
  * @brief Element::setElementInformations
  * Set new information for this element.
- * This method emit @elementInfoChange
+ * If new information is different of current infotmation emit @elementInfoChange
  * @param dc
  */
-void Element::setElementInformations(DiagramContext dc) {
-	DiagramContext old_info = element_informations_;
-	element_informations_ = dc;
-	emit elementInfoChange(old_info, element_informations_);
+void Element::setElementInformations(DiagramContext dc)
+{
+	if (m_element_informations == dc) return;
+	DiagramContext old_info = m_element_informations;
+	m_element_informations = dc;
+	emit elementInfoChange(old_info, m_element_informations);
 }
 
 /**

Modified: trunk/sources/qetgraphicsitem/element.h
===================================================================
--- trunk/sources/qetgraphicsitem/element.h	2015-05-29 07:28:58 UTC (rev 3995)
+++ trunk/sources/qetgraphicsitem/element.h	2015-05-31 15:02:41 UTC (rev 3996)
@@ -125,8 +125,8 @@
 
 		//METHODS related to information
 	public:
-		DiagramContext  elementInformations    ()const              {return element_informations_;}
-		DiagramContext& rElementInformations   ()                   {return element_informations_;}
+		DiagramContext  elementInformations    ()const              {return m_element_informations;}
+		DiagramContext& rElementInformations   ()                   {return m_element_informations;}
 		virtual void    setElementInformations (DiagramContext dc);
 		DiagramContext  kindInformations       () const             {return kind_informations_;}	//@kind_information_ is used to store more information
 																									//about the herited class like contactelement for know
@@ -134,7 +134,7 @@
 
 	//ATTRIBUTES
 	protected:
-		DiagramContext element_informations_, kind_informations_;
+		DiagramContext m_element_informations, kind_informations_;
 
 	/**
 		Draw this element

Modified: trunk/sources/ui/elementinfopartwidget.cpp
===================================================================
--- trunk/sources/ui/elementinfopartwidget.cpp	2015-05-29 07:28:58 UTC (rev 3995)
+++ trunk/sources/ui/elementinfopartwidget.cpp	2015-05-31 15:02:41 UTC (rev 3996)
@@ -34,6 +34,10 @@
 	ui->setupUi(this);
 	ui->label_->setText(translated_key);
 	if(key == "label") ui->checkBox->setChecked(true);
+
+	connect(ui->line_edit, &QLineEdit::textEdited, this, &ElementInfoPartWidget::textEdited);
+	connect(ui->line_edit, &QLineEdit::textChanged, this, &ElementInfoPartWidget::textChanged);
+
 }
 
 /**

Modified: trunk/sources/ui/elementinfopartwidget.h
===================================================================
--- trunk/sources/ui/elementinfopartwidget.h	2015-05-29 07:28:58 UTC (rev 3995)
+++ trunk/sources/ui/elementinfopartwidget.h	2015-05-31 15:02:41 UTC (rev 3996)
@@ -24,7 +24,8 @@
 	class ElementInfoPartWidget;
 }
 
-class ElementInfoPartWidget : public QWidget {
+class ElementInfoPartWidget : public QWidget
+{
 	Q_OBJECT
 
 	//METHODS
@@ -40,6 +41,10 @@
 		void    setHideShow (const bool &);
 		void    setFocusTolineEdit();
 
+	signals:
+		void textEdited (const QString & text);
+		void textChanged(const QString & text);
+
 	//ATTRIBUTES
 	private:
 		Ui::ElementInfoPartWidget *ui;

Modified: trunk/sources/ui/elementinfowidget.cpp
===================================================================
--- trunk/sources/ui/elementinfowidget.cpp	2015-05-29 07:28:58 UTC (rev 3995)
+++ trunk/sources/ui/elementinfowidget.cpp	2015-05-31 15:02:41 UTC (rev 3996)
@@ -45,7 +45,7 @@
  */
 ElementInfoWidget::~ElementInfoWidget()
 {
-	qDeleteAll(eipw_list);
+	qDeleteAll(m_eipw_list);
 	delete ui;
 }
 
@@ -57,9 +57,14 @@
 void ElementInfoWidget::setElement(Element *element)
 {
 	if (m_element == element) return;
+
+	if (m_element)
+		disconnect(m_element, &Element::elementInfoChange, this, &ElementInfoWidget::updateUi);
+
 	m_element = element;
-	m_element_info = m_element->elementInformations();
-	fillInfo();
+	updateUi();
+
+	connect(m_element, &Element::elementInfoChange, this, &ElementInfoWidget::updateUi);
 }
 
 /**
@@ -80,11 +85,13 @@
  * If no change return nullptr;
  * @return
  */
-QUndoCommand* ElementInfoWidget::associatedUndo() const {
+QUndoCommand* ElementInfoWidget::associatedUndo() const
+{
 	DiagramContext new_info;
 	DiagramContext old_info = m_element -> elementInformations();
 
-	foreach (ElementInfoPartWidget *eipw, eipw_list) {
+	foreach (ElementInfoPartWidget *eipw, m_eipw_list)
+	{
 			//add value only if they're something to store
 		if (!eipw->text().isEmpty())
 			new_info.addValue(eipw->key(),
@@ -92,13 +99,31 @@
 						eipw->mustShow());
 	}
 
-	if (old_info != new_info) {
+	if (old_info != new_info)
 		return (new ChangeElementInformationCommand(m_element, old_info, new_info));
-	}
+
 	return nullptr;
 }
 
 /**
+ * @brief ElementInfoWidget::setLiveEdit
+ * @param live_edit true : enable the live edit mode, false disable
+ * @return always true;
+ */
+bool ElementInfoWidget::setLiveEdit(bool live_edit)
+{
+	if (m_live_edit == live_edit) return true;
+	m_live_edit = live_edit;
+
+	if (m_live_edit)
+		enableLiveEdit();
+	else
+		disableLiveEdit();
+
+	return true;
+}
+
+/**
  * @brief ElementInfoWidget::event
  * Reimplemented from QWidget::event
  * Only give focus to the first line edit at first activation.
@@ -120,37 +145,67 @@
 }
 
 /**
+ * @brief ElementInfoWidget::enableLiveEdit
+ * Enable the live edit mode
+ */
+void ElementInfoWidget::enableLiveEdit()
+{
+	foreach (ElementInfoPartWidget *eipw, m_eipw_list)
+		connect(eipw, &ElementInfoPartWidget::textChanged, this, &ElementInfoWidget::apply);
+}
+
+/**
+ * @brief ElementInfoWidget::disableLiveEdit
+ * disable the live edit mode
+ */
+void ElementInfoWidget::disableLiveEdit()
+{
+	foreach (ElementInfoPartWidget *eipw, m_eipw_list)
+		disconnect(eipw, &ElementInfoPartWidget::textChanged, this, &ElementInfoWidget::apply);
+}
+
+/**
  * @brief ElementInfoWidget::buildInterface
  * Build the widget
  */
-void ElementInfoWidget::buildInterface() {
-	foreach (QString str, QETApp::elementInfoKeys()) {
+void ElementInfoWidget::buildInterface()
+{
+	foreach (QString str, QETApp::elementInfoKeys())
+	{
 		ElementInfoPartWidget *eipw = new ElementInfoPartWidget(str, QETApp::elementTranslatedInfoKey(str), this);
 		ui->scroll_vlayout->addWidget(eipw);
-		eipw_list << eipw;
+		m_eipw_list << eipw;
 	}
 }
 
 /**
- * @brief ElementInfoWidget::fillInfo
+ * @brief ElementInfoWidget::updateUi
  * fill information fetch in m_element_info to the
  * corresponding line edit
  */
-void ElementInfoWidget::fillInfo() {
-	foreach (ElementInfoPartWidget *eipw, eipw_list) {
+void ElementInfoWidget::updateUi()
+{
+		//We disable live edit to avoid wrong undo when we fill the line edit with new text
+	if (m_live_edit) disableLiveEdit();
 
-		eipw -> setText (m_element_info[eipw->key()].toString());
-		eipw -> setShow (m_element_info.keyMustShow(eipw->key()));
+	DiagramContext element_info = m_element->elementInformations();
+	foreach (ElementInfoPartWidget *eipw, m_eipw_list)
+	{
 
-		//If the current eipw is for label or comment and the text is empty
-		//we force the checkbox to ckecked
+		eipw -> setText (element_info[eipw->key()].toString());
+		eipw -> setShow (element_info.keyMustShow(eipw->key()));
+
+			//If the current eipw is for label or comment and the text is empty
+			//we force the checkbox to ckecked
 		if (eipw -> key() == "label" || eipw -> key() == "comment") {
-			if (m_element_info[eipw->key()].toString().isEmpty())
+			if (element_info[eipw->key()].toString().isEmpty())
 				eipw->setShow(true);
 		}
 		else //< for other eipw we hide the checkbox
 			eipw->setHideShow(true);
 	}
+
+	if (m_live_edit) enableLiveEdit();
 }
 
 /**
@@ -159,5 +214,5 @@
  * Set the focus to the first line edit provided by this widget
  */
 void ElementInfoWidget::firstActivated() {
-	eipw_list.first() -> setFocusTolineEdit();
+	m_eipw_list.first() -> setFocusTolineEdit();
 }

Modified: trunk/sources/ui/elementinfowidget.h
===================================================================
--- trunk/sources/ui/elementinfowidget.h	2015-05-29 07:28:58 UTC (rev 3995)
+++ trunk/sources/ui/elementinfowidget.h	2015-05-31 15:02:41 UTC (rev 3996)
@@ -19,7 +19,6 @@
 #define ELEMENTINFOWIDGET_H
 
 #include <QWidget>
-#include "diagramcontext.h"
 #include "abstractelementpropertieseditorwidget.h"
 
 class Element;
@@ -48,13 +47,16 @@
 		void apply();
 		QUndoCommand *associatedUndo () const;
 		QString title() const {return tr("Informations");}
+		bool setLiveEdit(bool live_edit);
+		virtual void updateUi();
 
 	protected:
 		virtual bool event(QEvent *event);
+		virtual void enableLiveEdit();
+		virtual void disableLiveEdit();
 
 	private:
 		void buildInterface();
-		void fillInfo();
 
 	private slots:
 		void firstActivated();
@@ -62,8 +64,7 @@
 		//ATTRIBUTES
 	private:
 		Ui::ElementInfoWidget           *ui;
-		DiagramContext                   m_element_info;
-		QList <ElementInfoPartWidget *>  eipw_list;
+		QList <ElementInfoPartWidget *>  m_eipw_list;
 		bool m_first_activation;
 };
 

Modified: trunk/sources/undocommand/changeelementinformationcommand.cpp
===================================================================
--- trunk/sources/undocommand/changeelementinformationcommand.cpp	2015-05-29 07:28:58 UTC (rev 3995)
+++ trunk/sources/undocommand/changeelementinformationcommand.cpp	2015-05-31 15:02:41 UTC (rev 3996)
@@ -35,6 +35,15 @@
 	setText(QObject::tr("Modifier les informations de l'élément : %1").arg(elmt -> name()));
 }
 
+bool ChangeElementInformationCommand::mergeWith(const QUndoCommand *other)
+{
+	if (id() != other->id()) return false;
+	ChangeElementInformationCommand const *undo = static_cast<const ChangeElementInformationCommand*>(other);
+	if (m_element != undo->m_element) return false;
+	m_new_info = undo->m_new_info;
+	return true;
+}
+
 /**
  * @brief ChangeElementInformationCommand::undo
  */

Modified: trunk/sources/undocommand/changeelementinformationcommand.h
===================================================================
--- trunk/sources/undocommand/changeelementinformationcommand.h	2015-05-29 07:28:58 UTC (rev 3995)
+++ trunk/sources/undocommand/changeelementinformationcommand.h	2015-05-31 15:02:41 UTC (rev 3996)
@@ -32,6 +32,8 @@
 	public:
 		ChangeElementInformationCommand(Element *elmt, DiagramContext &old_info, DiagramContext &new_info, QUndoCommand *parent = nullptr);
 
+		virtual int id() const {return 1;}
+		virtual bool mergeWith(const QUndoCommand *other);
 		virtual void undo();
 		virtual void redo();
 


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