diff --git a/sources/editor/elementprimitivedecorator.cpp b/sources/editor/elementprimitivedecorator.cpp index 4ead8e817..4a9967c69 100644 --- a/sources/editor/elementprimitivedecorator.cpp +++ b/sources/editor/elementprimitivedecorator.cpp @@ -181,7 +181,6 @@ void ElementPrimitiveDecorator::hoverMoveEvent(QGraphicsSceneHoverEvent *event) @param event Object describing the mouse event */ void ElementPrimitiveDecorator::mousePressEvent(QGraphicsSceneMouseEvent *event) { - qDebug() << Q_FUNC_INFO << event << zValue(); QList rects = getResizingSquares(); QPointF pos = event -> pos(); @@ -336,6 +335,52 @@ void ElementPrimitiveDecorator::mouseReleaseEvent(QGraphicsSceneMouseEvent *even current_operation_square_ = QET::NoOperation; } +/** + @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 */ diff --git a/sources/editor/elementprimitivedecorator.h b/sources/editor/elementprimitivedecorator.h index 17f5a342c..c000ac362 100644 --- a/sources/editor/elementprimitivedecorator.h +++ b/sources/editor/elementprimitivedecorator.h @@ -47,6 +47,8 @@ class ElementPrimitiveDecorator : public QGraphicsObject { 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 @@ class ElementPrimitiveDecorator : public QGraphicsObject { 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 diff --git a/sources/editor/elementscene.cpp b/sources/editor/elementscene.cpp index 44bec8961..a596d5215 100644 --- a/sources/editor/elementscene.cpp +++ b/sources/editor/elementscene.cpp @@ -349,58 +349,6 @@ void ElementScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { } else QGraphicsScene::mouseReleaseEvent(e); } -/** - 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 diff --git a/sources/editor/elementscene.h b/sources/editor/elementscene.h index f13b187c9..d12ca0c90 100644 --- a/sources/editor/elementscene.h +++ b/sources/editor/elementscene.h @@ -149,8 +149,6 @@ class ElementScene : public QGraphicsScene { 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 *); diff --git a/sources/editor/parttext.cpp b/sources/editor/parttext.cpp index 3e89b0c3e..9ed25e351 100644 --- a/sources/editor/parttext.cpp +++ b/sources/editor/parttext.cpp @@ -403,7 +403,7 @@ bool PartText::sceneEventFilter(QGraphicsItem *watched, QEvent *event) { 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(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 @@ bool PartText::sceneEventFilter(QGraphicsItem *watched, QEvent *event) { } 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); } diff --git a/sources/editor/parttextfield.cpp b/sources/editor/parttextfield.cpp index 7e8306244..fb9eb604c 100644 --- a/sources/editor/parttextfield.cpp +++ b/sources/editor/parttextfield.cpp @@ -185,7 +185,7 @@ bool PartTextField::sceneEventFilter(QGraphicsItem *watched, QEvent *event) { 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(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 @@ bool PartTextField::sceneEventFilter(QGraphicsItem *watched, QEvent *event) { } 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); }