[qet] qet/qet: [5419] Element editor : add two new actions in context menu for insert or remove point of a selected polygon.

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


Revision: 5419
Author:   blacksun
Date:     2018-06-30 23:41:27 +0200 (Sat, 30 Jun 2018)
Log Message:
-----------
Element editor : add two new actions in context menu for insert or remove point of a selected polygon.
All parts items : remove the 'open hand cursor' when hover a selected part.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/sources/editor/elementscene.cpp
    trunk/sources/editor/elementscene.h
    trunk/sources/editor/graphicspart/customelementgraphicpart.cpp
    trunk/sources/editor/graphicspart/customelementgraphicpart.h
    trunk/sources/editor/graphicspart/partpolygon.cpp
    trunk/sources/editor/graphicspart/partpolygon.h
    trunk/sources/editor/qetelementeditor.cpp
    trunk/sources/editor/qetelementeditor.h

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2018-06-29 20:36:21 UTC (rev 5418)
+++ trunk/ChangeLog	2018-06-30 21:41:27 UTC (rev 5419)
@@ -8,6 +8,7 @@
 * Context menu display only enabled actions.
 * Add new feature -> alignment.
 * Alignment of text field can be edited.
+* Add two new actions in context menu for insert or remove point of a selected polygon.
 
 * Diagram editor : 
 * Conductors can now be drawn with two colors.

Modified: trunk/sources/editor/elementscene.cpp
===================================================================
--- trunk/sources/editor/elementscene.cpp	2018-06-29 20:36:21 UTC (rev 5418)
+++ trunk/sources/editor/elementscene.cpp	2018-06-30 21:41:27 UTC (rev 5419)
@@ -240,6 +240,10 @@
  */
 void ElementScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
 {
+	QGraphicsScene::contextMenuEvent(event);
+	if(event->isAccepted())
+		return;
+	
 	if (m_behavior == ElementScene::Normal)
 		m_element_editor -> contextMenu(event->screenPos());
 }
@@ -302,11 +306,14 @@
  * Modifie the current behavior of this scene
  * @param b
  */
-void ElementScene::setBehavior(ElementScene::Behavior b)
-{
+void ElementScene::setBehavior(ElementScene::Behavior b) {
 	m_behavior = b;
 }
 
+ElementScene::Behavior ElementScene::behavior() const {
+	return m_behavior;
+}
+
 /**
 	@return la taille horizontale de la grille
 */

Modified: trunk/sources/editor/elementscene.h
===================================================================
--- trunk/sources/editor/elementscene.h	2018-06-29 20:36:21 UTC (rev 5418)
+++ trunk/sources/editor/elementscene.h	2018-06-30 21:41:27 UTC (rev 5419)
@@ -91,6 +91,7 @@
 		void setEventInterface (ESEventInterface *event_interface);
 		void clearEventInterface();
 		void setBehavior (ElementScene::Behavior);
+		ElementScene::Behavior behavior() const;
 		QPointF snapToGrid(QPointF point);
 		void setNames(const NamesList &);
 		NamesList names() const;

Modified: trunk/sources/editor/graphicspart/customelementgraphicpart.cpp
===================================================================
--- trunk/sources/editor/graphicspart/customelementgraphicpart.cpp	2018-06-29 20:36:21 UTC (rev 5418)
+++ trunk/sources/editor/graphicspart/customelementgraphicpart.cpp	2018-06-30 21:41:27 UTC (rev 5419)
@@ -424,14 +424,6 @@
 	QGraphicsObject::hoverEnterEvent(event);
 }
 
-void CustomElementGraphicPart::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
-{
-	if (isSelected())
-		setCursor(Qt::OpenHandCursor);
-
-	QGraphicsObject::hoverMoveEvent(event);
-}
-
 /**
  * @brief CustomElementGraphicPart::hoverLeaveEvent
  * Reimplemented from QGraphicsObject.
@@ -441,7 +433,6 @@
 void CustomElementGraphicPart::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
 {
 	m_hovered = false;
-	unsetCursor();
 	QGraphicsObject::hoverLeaveEvent(event);
 }
 

Modified: trunk/sources/editor/graphicspart/customelementgraphicpart.h
===================================================================
--- trunk/sources/editor/graphicspart/customelementgraphicpart.h	2018-06-29 20:36:21 UTC (rev 5418)
+++ trunk/sources/editor/graphicspart/customelementgraphicpart.h	2018-06-30 21:41:27 UTC (rev 5419)
@@ -103,7 +103,6 @@
 
 		QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
 		void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
-		void hoverMoveEvent (QGraphicsSceneHoverEvent *event) override;
 		void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
 
 		void mousePressEvent(QGraphicsSceneMouseEvent *event) override;

Modified: trunk/sources/editor/graphicspart/partpolygon.cpp
===================================================================
--- trunk/sources/editor/graphicspart/partpolygon.cpp	2018-06-29 20:36:21 UTC (rev 5418)
+++ trunk/sources/editor/graphicspart/partpolygon.cpp	2018-06-30 21:41:27 UTC (rev 5419)
@@ -19,6 +19,8 @@
 #include "QPropertyUndoCommand/qpropertyundocommand.h"
 #include "elementscene.h"
 #include "QetGraphicsItemModeler/qetgraphicshandleritem.h"
+#include "qetelementeditor.h"
+#include "qeticons.h"
 
 
 /**
@@ -31,7 +33,14 @@
 	CustomElementGraphicPart(editor, parent),
 	m_closed(false),
 	m_undo_command(nullptr)
-{}
+{
+	m_insert_point = new QAction(tr("Ajouter un point"), this);
+	m_insert_point->setIcon(QET::Icons::Add);
+	connect(m_insert_point, &QAction::triggered, this, &PartPolygon::insertPoint);
+	m_remove_point = new QAction(tr("Supprimer ce point"), this);
+	m_remove_point->setIcon(QET::Icons::Remove);
+	connect(m_remove_point, &QAction::triggered, this, &PartPolygon::removePoint);
+}
 
 /**
  * @brief PartPolygon::~PartPolygon
@@ -327,6 +336,30 @@
 	return false;
 }
 
+void PartPolygon::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
+{
+	m_context_menu_pos = event->pos();
+	event->ignore();
+	if (isSelected() && elementScene() && (elementScene()->behavior() == ElementScene::Normal))
+	{
+		QList<QAction *> list;
+		list << m_insert_point;
+		if (m_handler_vector.count() > 2)
+		{
+			for (QetGraphicsHandlerItem *qghi : m_handler_vector)
+			{
+				if (qghi->contains(qghi->mapFromScene(event->scenePos())))
+				{
+					list << m_remove_point;
+					break;
+				}
+			}
+		}
+		elementScene()->editor()->contextMenu(event->screenPos(), list);
+		event->accept();
+	}
+}
+
 /**
  * @brief PartPolygon::adjusteHandlerPos
  */
@@ -341,6 +374,12 @@
 		for (int i = 0 ; i < points_vector.size() ; ++i)
 			m_handler_vector.at(i)->setPos(points_vector.at(i));
 	}
+	else
+	{
+		qDeleteAll(m_handler_vector);
+		m_handler_vector.clear();
+		addHandler();
+	}
 }
 
 /**
@@ -439,6 +478,95 @@
 }
 
 /**
+ * @brief PartPolygon::insertPoint
+ * Insert a point in this polygone
+ */
+void PartPolygon::insertPoint()
+{	
+	qreal max_angle = 0;
+	int index = 0;
+	
+	for (int i=1 ; i<m_polygon.size() ; i++)
+	{
+		QPointF A = m_polygon.at(i-1);
+		QPointF B = m_polygon.at(i);
+		QLineF line_a(A, m_context_menu_pos);
+		QLineF line_b(m_context_menu_pos, B);
+		qreal angle = line_a.angleTo(line_b);
+		if(angle<180)
+			angle = 360-angle;
+
+		if (i==1)
+		{
+			max_angle = angle;
+			index=i;
+		}
+		if (angle > max_angle)
+		{
+			max_angle = angle;
+			index=i;
+		}
+	}
+		//Special case when polygon is close
+	if (m_closed)
+	{
+		QLineF line_a(m_polygon.last(), m_context_menu_pos);
+		QLineF line_b(m_context_menu_pos, m_polygon.first());
+		
+		qreal angle = line_a.angleTo(line_b);
+		if (angle<180)
+			angle = 360-angle;
+
+		if (angle > max_angle)
+		{
+			max_angle = angle;
+			index=m_polygon.size();
+		}
+	}
+	
+	QPolygonF polygon = this->polygon();
+	polygon.insert(index, elementScene()->snapToGrid(m_context_menu_pos));
+	
+		//Wrap the undo for avoid to merge the undo commands when user add several points.
+	QUndoCommand *undo = new QUndoCommand(tr("Ajouter un point à un polygone"));
+	new QPropertyUndoCommand(this, "polygon", this->polygon(), polygon, undo);
+	elementScene()->undoStack().push(undo);
+}
+
+/**
+ * @brief PartPolygon::removePoint
+ * remove a point on this polygon
+ */
+void PartPolygon::removePoint()
+{
+	if (m_handler_vector.size() == 2)
+		return;
+	
+	QPointF point = mapToScene(m_context_menu_pos);
+	int index = -1;
+	for (int i=0 ; i<m_handler_vector.size() ; i++)
+	{
+		QetGraphicsHandlerItem *qghi = m_handler_vector.at(i);
+		if (qghi->contains(qghi->mapFromScene(point)))
+		{
+			index = i;
+			break;
+		}
+	}
+	if (index > -1 && index<m_handler_vector.count())
+	{
+		QPolygonF polygon = this->polygon();
+		polygon.removeAt(index);
+		
+			//Wrap the undo for avoid to merge the undo commands when user add several points.
+		QUndoCommand *undo = new QUndoCommand(tr("Supprimer un point d'un polygone"));
+		new QPropertyUndoCommand(this, "polygon", this->polygon(), polygon, undo);
+		elementScene()->undoStack().push(undo);
+	}
+	
+}
+
+/**
  * @brief PartPolygon::shape
  * @return the shape of this item
  */

Modified: trunk/sources/editor/graphicspart/partpolygon.h
===================================================================
--- trunk/sources/editor/graphicspart/partpolygon.h	2018-06-29 20:36:21 UTC (rev 5418)
+++ trunk/sources/editor/graphicspart/partpolygon.h	2018-06-30 21:41:27 UTC (rev 5419)
@@ -23,6 +23,7 @@
 
 class QPropertyUndoCommand;
 class QetGraphicsHandlerItem;
+class QAction;
 
 /**
  * @brief The PartPolygon class
@@ -86,6 +87,7 @@
 	protected:
 		QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
 		bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;
+		void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
 	
 	private:
 		void adjusteHandlerPos();
@@ -96,6 +98,8 @@
 		
 		void addHandler();
 		void removeHandler();
+		void insertPoint();
+		void removePoint();
 		
 		
 		bool m_closed;
@@ -104,5 +108,8 @@
 		QPropertyUndoCommand *m_undo_command;
 		int m_vector_index = -1;
 		QVector<QetGraphicsHandlerItem *> m_handler_vector;
+		QAction *m_insert_point,
+				*m_remove_point;
+		QPointF m_context_menu_pos;
 };
 #endif

Modified: trunk/sources/editor/qetelementeditor.cpp
===================================================================
--- trunk/sources/editor/qetelementeditor.cpp	2018-06-29 20:36:21 UTC (rev 5418)
+++ trunk/sources/editor/qetelementeditor.cpp	2018-06-30 21:41:27 UTC (rev 5419)
@@ -400,12 +400,16 @@
 }
 
 /**
- * @brief QETElementEditor::contextMenuEvent
- * @param event
+ * @brief QETElementEditor::contextMenu
+ * Display a context menu, with all available action.
+ * @param p, the pos of the menu, in screen coordinate
+ * @param actions, a list of actions who can be prepended to the context menu.
  */
-void QETElementEditor::contextMenu(QPoint p)
+void QETElementEditor::contextMenu(QPoint p, QList<QAction *> actions)
 {
 		QMenu menu(this);
+		menu.addActions(actions);
+		menu.addSeparator();
 		menu.addAction(undo);
 		menu.addAction(redo);
 		menu.addAction(selectall);
@@ -423,14 +427,14 @@
 		menu.addActions(m_depth_action_group -> actions());
 		
 			//Remove from the context menu the actions which are disabled.
-		const QList<QAction *>actions = menu.actions();
-		for(QAction *action : actions)
+		const QList<QAction *>menu_actions = menu.actions();
+		for(QAction *action : menu_actions)
 		{
 			if(!action->isEnabled())
 				menu.removeAction(action);
 		}
 		menu.exec(p);
- }
+}
 
 
 /**

Modified: trunk/sources/editor/qetelementeditor.h
===================================================================
--- trunk/sources/editor/qetelementeditor.h	2018-06-29 20:36:21 UTC (rev 5418)
+++ trunk/sources/editor/qetelementeditor.h	2018-06-30 21:41:27 UTC (rev 5419)
@@ -92,25 +92,25 @@
 	
 	// methods
 	public:
-	void setNames(const NamesList &);
-	void setLocation(const ElementsLocation &);
-	ElementsLocation location() const;
-	void setFileName(const QString &);
-	QString fileName() const;
-	void setReadOnly(bool);
-	bool isReadOnly() const;
-	void fromFile(const QString &);
-	void fromLocation(const ElementsLocation &);
-	bool toFile(const QString &);
-	bool toLocation(const ElementsLocation &location);
-	bool isEditing(const ElementsLocation &);
-	bool isEditing(const QString &);
-	ElementScene *elementScene() const;
-	void readSettings();
-	void writeSettings();
-	static QPointF pasteOffset();
-	static QString getOpenElementFileName(QWidget * = nullptr, const QString & = QString());
-	void contextMenu(QPoint p);
+		void setNames(const NamesList &);
+		void setLocation(const ElementsLocation &);
+		ElementsLocation location() const;
+		void setFileName(const QString &);
+		QString fileName() const;
+		void setReadOnly(bool);
+		bool isReadOnly() const;
+		void fromFile(const QString &);
+		void fromLocation(const ElementsLocation &);
+		bool toFile(const QString &);
+		bool toLocation(const ElementsLocation &location);
+		bool isEditing(const ElementsLocation &);
+		bool isEditing(const QString &);
+		ElementScene *elementScene() const;
+		void readSettings();
+		void writeSettings();
+		static QPointF pasteOffset();
+		static QString getOpenElementFileName(QWidget * = nullptr, const QString & = QString());
+		void contextMenu(QPoint p, QList<QAction *> actions = QList<QAction*>());
 
 	signals:
 		void saveToLocation(ElementsLocation loc);


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