[qet] qet/qet: [5272] Element editor : improve the behavior with the arrow keys (depending to the current selection : nothing / one / several)

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


Revision: 5272
Author:   blacksun
Date:     2018-03-20 11:59:39 +0100 (Tue, 20 Mar 2018)
Log Message:
-----------
Element editor : improve the behavior with the arrow keys (depending to the current selection : nothing / one / several)

Modified Paths:
--------------
    trunk/sources/editor/elementprimitivedecorator.cpp
    trunk/sources/editor/elementscene.cpp

Modified: trunk/sources/editor/elementprimitivedecorator.cpp
===================================================================
--- trunk/sources/editor/elementprimitivedecorator.cpp	2018-03-18 15:46:10 UTC (rev 5271)
+++ trunk/sources/editor/elementprimitivedecorator.cpp	2018-03-20 10:59:39 UTC (rev 5272)
@@ -246,26 +246,38 @@
 /**
 	@reimp QGraphicsItem::keyPressEvent
 */
-void ElementPrimitiveDecorator::keyPressEvent(QKeyEvent *e) {
+void ElementPrimitiveDecorator::keyPressEvent(QKeyEvent *e)
+{
 	const qreal movement_length = 1.0;
 	QPointF movement;
-	switch(e -> key()) {
+	
+	switch(e -> key())
+	{
 		case Qt::Key_Left:  movement = QPointF(-movement_length, 0.0); break;
 		case Qt::Key_Right: movement = QPointF(+movement_length, 0.0); break;
 		case Qt::Key_Up:    movement = QPointF(0.0, -movement_length); break;
 		case Qt::Key_Down:  movement = QPointF(0.0, +movement_length); break;
 	}
-	if (!movement.isNull() && focusItem() == this) {
-		if (!moving_by_keys_) {
+	if (!movement.isNull() && focusItem() == this)
+	{
+		if (!moving_by_keys_)
+		{
 			moving_by_keys_ = true;
 			keys_movement_ = movement;
-		} else {
+		}
+		else
+		{
 			keys_movement_ += movement;
 		}
-		foreach(QGraphicsItem *qgi, graphicsItems()) {
+		
+		for(QGraphicsItem *qgi : graphicsItems())
+		{
 			qgi -> setPos(qgi -> pos() + movement);
 			adjust();
 		}
+		
+		e->accept();
+		return;
 	}
 	
 	QGraphicsObject::keyPressEvent(e);
@@ -276,7 +288,7 @@
 */
 void ElementPrimitiveDecorator::keyReleaseEvent(QKeyEvent *e) {
 	// detecte le relachement d'une touche de direction ( = deplacement de parties)
-	if (
+	if(
 		(e -> key() == Qt::Key_Left  || e -> key() == Qt::Key_Right  ||\
 		 e -> key() == Qt::Key_Up    || e -> key() == Qt::Key_Down) &&\
 		moving_by_keys_  && !e -> isAutoRepeat()
@@ -285,6 +297,8 @@
 		emit(actionFinished(new MovePartsCommand(keys_movement_, nullptr, graphicsItems())));
 		keys_movement_ = QPointF();
 		moving_by_keys_ = false;
+		e->accept();
+		return;
 	}
 	QGraphicsObject::keyPressEvent(e);
 }

Modified: trunk/sources/editor/elementscene.cpp
===================================================================
--- trunk/sources/editor/elementscene.cpp	2018-03-18 15:46:10 UTC (rev 5271)
+++ trunk/sources/editor/elementscene.cpp	2018-03-20 10:59:39 UTC (rev 5272)
@@ -33,6 +33,7 @@
 #include "eseventinterface.h"
 #include "QetGraphicsItemModeler/qetgraphicshandleritem.h"
 #include "partdynamictextfield.h"
+#include "QPropertyUndoCommand/qpropertyundocommand.h"
 
 #include <algorithm>
 #include <QKeyEvent>
@@ -177,10 +178,14 @@
  * manage key press event
  * @param event
  */
-void ElementScene::keyPressEvent(QKeyEvent *event) {
-	if (m_event_interface) {
-		if (m_event_interface -> keyPressEvent(event)) {
-			if (m_event_interface->isFinish()) {
+void ElementScene::keyPressEvent(QKeyEvent *event)
+{
+	if (m_event_interface)
+	{
+		if (m_event_interface -> keyPressEvent(event))
+		{
+			if (m_event_interface->isFinish())
+			{
 				delete m_event_interface; m_event_interface = nullptr;
 				emit(partsAdded());
 			}
@@ -187,6 +192,34 @@
 			return;
 		}
 	}
+	
+	if(selectedItems().size() == 1)
+	{	
+		QGraphicsObject *qgo = selectedItems().first()->toGraphicsObject();
+		if(qgo)
+		{
+			QPointF original_pos = qgo->pos();
+			QPointF p = qgo->pos();
+			int k = event->key();
+			if(k == Qt::Key_Right)
+				p.rx() += 1;
+			else if (k == Qt::Key_Left)
+				p.rx() -= 1;
+			else if (k == Qt::Key_Up)
+				p.ry() -= 1;
+			else if (k == Qt::Key_Down)
+				p.ry() += 1;
+			
+			qgo->setPos(p);
+			QPropertyUndoCommand *undo = new QPropertyUndoCommand(qgo, "pos", QVariant(original_pos), QVariant(p));
+			undo->setText(tr("Déplacer une primitive"));
+			undo->enableAnimation();
+			undoStack().push(undo);
+			event->accept();
+			return;
+		}
+	}
+	
 	QGraphicsScene::keyPressEvent(event);
 }
 


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