[qet] [2028] The primitive decorator now handles keyboard-driven movements. |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/qet Archives
]
Revision: 2028
Author: xavier
Date: 2013-02-08 23:05:18 +0100 (Fri, 08 Feb 2013)
Log Message:
-----------
The primitive decorator now handles keyboard-driven movements.
Modified Paths:
--------------
trunk/sources/editor/elementprimitivedecorator.cpp
trunk/sources/editor/elementprimitivedecorator.h
trunk/sources/editor/elementscene.cpp
trunk/sources/editor/elementscene.h
trunk/sources/editor/parttext.cpp
trunk/sources/editor/parttextfield.cpp
Modified: trunk/sources/editor/elementprimitivedecorator.cpp
===================================================================
--- trunk/sources/editor/elementprimitivedecorator.cpp 2013-02-08 22:05:15 UTC (rev 2027)
+++ trunk/sources/editor/elementprimitivedecorator.cpp 2013-02-08 22:05:18 UTC (rev 2028)
@@ -181,7 +181,6 @@
@param event Object describing the mouse event
*/
void ElementPrimitiveDecorator::mousePressEvent(QGraphicsSceneMouseEvent *event) {
- qDebug() << Q_FUNC_INFO << event << zValue();
QList<QRectF> rects = getResizingSquares();
QPointF pos = event -> pos();
@@ -337,6 +336,52 @@
}
/**
+ @reimp QGraphicsItem::keyPressEvent
+*/
+void ElementPrimitiveDecorator::keyPressEvent(QKeyEvent *e) {
+ const qreal movement_length = 1.0;
+ QPointF movement;
+ 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()) {
+ if (!moving_by_keys_) {
+ moving_by_keys_ = true;
+ keys_movement_ = movement;
+ } else {
+ keys_movement_ += movement;
+ }
+ foreach(QGraphicsItem *qgi, graphicsItems()) {
+ qgi -> setPos(qgi -> pos() + movement);
+ adjust();
+ }
+ }
+
+ QGraphicsObject::keyPressEvent(e);
+}
+
+/**
+ @reimp QGraphicsItem::keyReleaseEvent
+*/
+void ElementPrimitiveDecorator::keyReleaseEvent(QKeyEvent *e) {
+ // detecte le relachement d'une touche de direction ( = deplacement de parties)
+ 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()
+ ) {
+ // cree un objet d'annulation pour le mouvement qui vient de se finir
+ emit(actionFinished(new MovePartsCommand(keys_movement_, 0, graphicsItems())));
+ keys_movement_ = QPointF();
+ moving_by_keys_ = false;
+ }
+ QGraphicsObject::keyPressEvent(e);
+}
+
+/**
Initialize an ElementPrimitiveDecorator
*/
void ElementPrimitiveDecorator::init() {
Modified: trunk/sources/editor/elementprimitivedecorator.h
===================================================================
--- trunk/sources/editor/elementprimitivedecorator.h 2013-02-08 22:05:15 UTC (rev 2027)
+++ trunk/sources/editor/elementprimitivedecorator.h 2013-02-08 22:05:18 UTC (rev 2028)
@@ -47,6 +47,8 @@
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *);
void mouseMoveEvent(QGraphicsSceneMouseEvent *);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *);
+ void keyPressEvent(QKeyEvent *);
+ void keyReleaseEvent(QKeyEvent *);
QPointF snapConstPointToGrid(const QPointF &) const;
void snapPointToGrid(QPointF &) const;
bool mustSnapToGrid(QGraphicsSceneMouseEvent *);
@@ -85,6 +87,8 @@
QPointF first_pos_; ///< First point involved within the current resizing operation
QPointF latest_pos_; ///< Latest point involved within the current resizing operation
QPointF mouse_offset_; ///< Offset between the mouse position and the point to be snapped to grid when moving selection
+ bool moving_by_keys_; ///< Whether we are currently moving our decorated items using the arrow keys
+ QPointF keys_movement_; ///< Movement applied to our decorated items using the arrow keys
};
#endif
Modified: trunk/sources/editor/elementscene.cpp
===================================================================
--- trunk/sources/editor/elementscene.cpp 2013-02-08 22:05:15 UTC (rev 2027)
+++ trunk/sources/editor/elementscene.cpp 2013-02-08 22:05:18 UTC (rev 2028)
@@ -350,58 +350,6 @@
}
/**
- Gere les enfoncements de touches du clavier
- @param e QKeyEvent decrivant l'evenement clavier
-*/
-void ElementScene::keyPressEvent(QKeyEvent *e) {
- bool is_read_only = element_editor && element_editor -> isReadOnly();
- if (!is_read_only) {
- const qreal movement_length = 1.0;
- QPointF movement;
- 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()) {
- if (!moving_parts_) {
- moving_parts_ = true;
- fsi_pos = movement;
- } else {
- fsi_pos += movement;
- }
- foreach(QGraphicsItem *qgi, selectedItems()) {
- qgi -> setPos(qgi -> pos() + movement);
- }
- }
- }
- QGraphicsScene::keyPressEvent(e);
-}
-
-/**
- Gere les relachements de touches du clavier
- @param e QKeyEvent decrivant l'evenement clavier
-*/
-void ElementScene::keyReleaseEvent(QKeyEvent *e) {
- bool is_read_only = element_editor && element_editor -> isReadOnly();
- if (!is_read_only) {
- // detecte le relachement d'une touche de direction ( = deplacement de parties)
- if (
- (e -> key() == Qt::Key_Left || e -> key() == Qt::Key_Right ||\
- e -> key() == Qt::Key_Up || e -> key() == Qt::Key_Down) &&\
- moving_parts_ && !e -> isAutoRepeat()
- ) {
- // cree un objet d'annulation pour le mouvement qui vient de se finir
- undo_stack.push(new MovePartsCommand(fsi_pos, this, selectedItems()));
- fsi_pos = QPointF();
- moving_parts_ = false;
- }
- }
- QGraphicsScene::keyReleaseEvent(e);
-}
-
-/**
Dessine l'arriere-plan de l'editeur, cad la grille.
@param p Le QPainter a utiliser pour dessiner
@param r Le rectangle de la zone a dessiner
Modified: trunk/sources/editor/elementscene.h
===================================================================
--- trunk/sources/editor/elementscene.h 2013-02-08 22:05:15 UTC (rev 2027)
+++ trunk/sources/editor/elementscene.h 2013-02-08 22:05:18 UTC (rev 2028)
@@ -149,8 +149,6 @@
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *);
virtual void mousePressEvent(QGraphicsSceneMouseEvent *);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *);
- virtual void keyPressEvent(QKeyEvent *);
- virtual void keyReleaseEvent(QKeyEvent *);
virtual void drawBackground(QPainter *, const QRectF &);
virtual void drawForeground(QPainter *, const QRectF &);
virtual void endCurrentBehavior(const QGraphicsSceneMouseEvent *);
Modified: trunk/sources/editor/parttext.cpp
===================================================================
--- trunk/sources/editor/parttext.cpp 2013-02-08 22:05:15 UTC (rev 2027)
+++ trunk/sources/editor/parttext.cpp 2013-02-08 22:05:18 UTC (rev 2028)
@@ -403,7 +403,7 @@
else if (event -> type() == QEvent::KeyRelease || event -> type() == QEvent::KeyPress) {
// Intercept F2 and escape keystrokes to focus in and out
QKeyEvent *key_event = static_cast<QKeyEvent *>(event);
- if (key_event -> key() == Qt::Key_F2) {
+ if (!hasFocus() && key_event -> key() == Qt::Key_F2) {
setEditable(true);
QTextCursor qtc = textCursor();
qtc.setPosition(qMax(0, document()->characterCount() - 1));
@@ -411,8 +411,10 @@
} else if (hasFocus() && key_event -> key() == Qt::Key_Escape) {
endEdition();
}
- sceneEvent(event); // manually deliver the event to this item
- return(true); // prevent this event from being delivered to any item
+ if (hasFocus()) {
+ sceneEvent(event); // manually deliver the event to this item
+ return(true); // prevent this event from being delivered to any item
+ }
}
return(false);
}
Modified: trunk/sources/editor/parttextfield.cpp
===================================================================
--- trunk/sources/editor/parttextfield.cpp 2013-02-08 22:05:15 UTC (rev 2027)
+++ trunk/sources/editor/parttextfield.cpp 2013-02-08 22:05:18 UTC (rev 2028)
@@ -185,7 +185,7 @@
else if (event -> type() == QEvent::KeyRelease || event -> type() == QEvent::KeyPress) {
// Intercept F2 and escape keystrokes to focus in and out
QKeyEvent *key_event = static_cast<QKeyEvent *>(event);
- if (key_event -> key() == Qt::Key_F2) {
+ if (!hasFocus() && key_event -> key() == Qt::Key_F2) {
setEditable(true);
QTextCursor qtc = textCursor();
qtc.setPosition(qMax(0, document()->characterCount() - 1));
@@ -193,8 +193,10 @@
} else if (hasFocus() && key_event -> key() == Qt::Key_Escape) {
endEdition();
}
- sceneEvent(event); // manually deliver the event to this item
- return(true); // prevent this event from being delivered to any item
+ if (hasFocus()) {
+ sceneEvent(event); // manually deliver the event to this item
+ return(true); // prevent this event from being delivered to any item
+ }
}
return(false);
}