From a9afda4c164c9c91e565832057a716b94576ba4e Mon Sep 17 00:00:00 2001 From: ouv Date: Fri, 1 Apr 2011 08:34:37 +0000 Subject: [PATCH] 1) Processing key events (currently, do nothing, only handle events) 2) Small refactoring on GraphicsView_Viewer --- src/GraphicsView/GraphicsView_Scene.cxx | 20 ++ src/GraphicsView/GraphicsView_Scene.h | 3 + src/GraphicsView/GraphicsView_ViewFrame.cxx | 21 ++ src/GraphicsView/GraphicsView_ViewFrame.h | 3 + src/GraphicsView/GraphicsView_ViewPort.cxx | 11 + src/GraphicsView/GraphicsView_ViewPort.h | 2 + src/GraphicsView/GraphicsView_Viewer.cxx | 268 ++++++++++---------- src/GraphicsView/GraphicsView_Viewer.h | 9 +- 8 files changed, 192 insertions(+), 145 deletions(-) diff --git a/src/GraphicsView/GraphicsView_Scene.cxx b/src/GraphicsView/GraphicsView_Scene.cxx index 79fb11c29..39fe05733 100644 --- a/src/GraphicsView/GraphicsView_Scene.cxx +++ b/src/GraphicsView/GraphicsView_Scene.cxx @@ -73,6 +73,26 @@ void GraphicsView_Scene::onSceneRectChanged( const QRectF& theRect ) #endif } +//================================================================ +// Function : keyPressEvent +// Purpose : +//================================================================ +void GraphicsView_Scene::keyPressEvent( QKeyEvent* e ) +{ + emit gsKeyEvent( e ); + QGraphicsScene::keyPressEvent( e ); +} + +//================================================================ +// Function : keyReleaseEvent +// Purpose : +//================================================================ +void GraphicsView_Scene::keyReleaseEvent( QKeyEvent* e ) +{ + emit gsKeyEvent( e ); + QGraphicsScene::keyReleaseEvent( e ); +} + //================================================================ // Function : mousePressEvent // Purpose : diff --git a/src/GraphicsView/GraphicsView_Scene.h b/src/GraphicsView/GraphicsView_Scene.h index 94ac92f9f..bd9f8d6b1 100644 --- a/src/GraphicsView/GraphicsView_Scene.h +++ b/src/GraphicsView/GraphicsView_Scene.h @@ -48,6 +48,8 @@ protected slots: void onSceneRectChanged( const QRectF& theRect ); // for debug protected: + virtual void keyPressEvent( QKeyEvent* ); + virtual void keyReleaseEvent( QKeyEvent* ); virtual void mousePressEvent( QGraphicsSceneMouseEvent* ); virtual void mouseMoveEvent( QGraphicsSceneMouseEvent* ); virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent* ); @@ -61,6 +63,7 @@ protected: virtual void dropEvent( QGraphicsSceneDragDropEvent* ); signals: + void gsKeyEvent( QKeyEvent* ); void gsMouseEvent( QGraphicsSceneMouseEvent* ); void gsWheelEvent( QGraphicsSceneWheelEvent* ); void gsContextMenuEvent( QGraphicsSceneContextMenuEvent* ); diff --git a/src/GraphicsView/GraphicsView_ViewFrame.cxx b/src/GraphicsView/GraphicsView_ViewFrame.cxx index 77df4f75a..35c43f690 100644 --- a/src/GraphicsView/GraphicsView_ViewFrame.cxx +++ b/src/GraphicsView/GraphicsView_ViewFrame.cxx @@ -67,6 +67,8 @@ GraphicsView_ViewFrame::GraphicsView_ViewFrame( SUIT_Desktop* d, GraphicsView_Vi createActions(); createToolBar(); + connect( myViewPort, SIGNAL( vpKeyEvent( QKeyEvent* ) ), + this, SLOT( keyEvent( QKeyEvent* ) ) ); connect( myViewPort, SIGNAL( vpMouseEvent( QGraphicsSceneMouseEvent* ) ), this, SLOT( mouseEvent( QGraphicsSceneMouseEvent* ) ) ); connect( myViewPort, SIGNAL( vpWheelEvent( QGraphicsSceneWheelEvent* ) ), @@ -255,6 +257,25 @@ void GraphicsView_ViewFrame::onViewReset() myViewer->activateTransform( GraphicsView_Viewer::Reset ); } +//================================================================ +// Function : keyEvent +// Purpose : +//================================================================ +void GraphicsView_ViewFrame::keyEvent( QKeyEvent* e ) +{ + switch ( e->type() ) + { + case QEvent::KeyPress: + emit keyPressed( e ); + break; + case QEvent::KeyRelease: + emit keyReleased( e ); + break; + default: + break; + } +} + //================================================================ // Function : mouseEvent // Purpose : diff --git a/src/GraphicsView/GraphicsView_ViewFrame.h b/src/GraphicsView/GraphicsView_ViewFrame.h index 346b1f43a..6f9ab4be9 100644 --- a/src/GraphicsView/GraphicsView_ViewFrame.h +++ b/src/GraphicsView/GraphicsView_ViewFrame.h @@ -67,11 +67,14 @@ protected slots: void onViewReset(); private slots: + void keyEvent( QKeyEvent* ); void mouseEvent( QGraphicsSceneMouseEvent* ); void wheelEvent( QGraphicsSceneWheelEvent* ); void contextMenuEvent( QGraphicsSceneContextMenuEvent* ); signals: + void keyPressed( QKeyEvent* ); + void keyReleased( QKeyEvent* ); void mousePressed( QGraphicsSceneMouseEvent* ); void mouseMoving( QGraphicsSceneMouseEvent* ); void mouseReleased( QGraphicsSceneMouseEvent* ); diff --git a/src/GraphicsView/GraphicsView_ViewPort.cxx b/src/GraphicsView/GraphicsView_ViewPort.cxx index 442fc9281..82d36c8c5 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.cxx +++ b/src/GraphicsView/GraphicsView_ViewPort.cxx @@ -172,6 +172,8 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent ) QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing ); + connect( myScene, SIGNAL( gsKeyEvent( QKeyEvent* ) ), + this, SLOT( onKeyEvent( QKeyEvent* ) ) ); connect( myScene, SIGNAL( gsMouseEvent( QGraphicsSceneMouseEvent* ) ), this, SLOT( onMouseEvent( QGraphicsSceneMouseEvent* ) ) ); connect( myScene, SIGNAL( gsWheelEvent( QGraphicsSceneWheelEvent* ) ), @@ -1264,6 +1266,15 @@ void GraphicsView_ViewPort::onBoundingRectChanged() } } +//================================================================ +// Function : onKeyEvent +// Purpose : +//================================================================ +void GraphicsView_ViewPort::onKeyEvent( QKeyEvent* e ) +{ + emit vpKeyEvent( e ); +} + //================================================================ // Function : onMouseEvent // Purpose : diff --git a/src/GraphicsView/GraphicsView_ViewPort.h b/src/GraphicsView/GraphicsView_ViewPort.h index 0b97494f0..aac2af85f 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.h +++ b/src/GraphicsView/GraphicsView_ViewPort.h @@ -183,6 +183,7 @@ public slots: void onBoundingRectChanged(); protected slots: + void onKeyEvent( QKeyEvent* ); void onMouseEvent( QGraphicsSceneMouseEvent* ); void onWheelEvent( QGraphicsSceneWheelEvent* ); void onContextMenuEvent( QGraphicsSceneContextMenuEvent* ); @@ -191,6 +192,7 @@ protected: virtual void scrollContentsBy( int theDX, int theDY ); signals: + void vpKeyEvent( QKeyEvent* ); void vpMouseEvent( QGraphicsSceneMouseEvent* ); void vpWheelEvent( QGraphicsSceneWheelEvent* ); void vpContextMenuEvent( QGraphicsSceneContextMenuEvent* ); diff --git a/src/GraphicsView/GraphicsView_Viewer.cxx b/src/GraphicsView/GraphicsView_Viewer.cxx index 83d867ced..038fd8816 100644 --- a/src/GraphicsView/GraphicsView_Viewer.cxx +++ b/src/GraphicsView/GraphicsView_Viewer.cxx @@ -35,6 +35,7 @@ #include #include #include +#include #include //======================================================================= @@ -67,6 +68,12 @@ SUIT_ViewWindow* GraphicsView_Viewer::createView( SUIT_Desktop* theDesktop ) { GraphicsView_ViewFrame* aViewFrame = new GraphicsView_ViewFrame( theDesktop, this ); + connect( aViewFrame, SIGNAL( keyPressed( QKeyEvent* ) ), + this, SLOT( onKeyEvent( QKeyEvent* ) ) ); + + connect( aViewFrame, SIGNAL( keyReleased( QKeyEvent* ) ), + this, SLOT( onKeyEvent( QKeyEvent* ) ) ); + connect( aViewFrame, SIGNAL( mousePressed( QGraphicsSceneMouseEvent* ) ), this, SLOT( onMouseEvent( QGraphicsSceneMouseEvent* ) ) ); @@ -230,6 +237,24 @@ void GraphicsView_Viewer::onTransformationFinished() qApp->removeEventFilter( this ); } +//================================================================ +// Function : onKeyEvent +// Purpose : +//================================================================ +void GraphicsView_Viewer::onKeyEvent( QKeyEvent* e ) +{ + switch( e->type() ) + { + case QEvent::KeyPress: + handleKeyPress( e ); + break; + case QEvent::KeyRelease: + handleKeyRelease( e ); + break; + default: break; + } +} + //================================================================ // Function : onMouseEvent // Purpose : @@ -266,6 +291,22 @@ void GraphicsView_Viewer::onWheelEvent( QGraphicsSceneWheelEvent* e ) } } +//================================================================ +// Function : handleKeyPress +// Purpose : +//================================================================ +void GraphicsView_Viewer::handleKeyPress( QKeyEvent* e ) +{ +} + +//================================================================ +// Function : handleKeyRelease +// Purpose : +//================================================================ +void GraphicsView_Viewer::handleKeyRelease( QKeyEvent* e ) +{ +} + //================================================================ // Function : handleMousePress // Purpose : @@ -282,7 +323,36 @@ void GraphicsView_Viewer::handleMousePress( QGraphicsSceneMouseEvent* e ) activateTransform( Pan ); } else // checking for other operations before selection in release event - startOperations( e ); + { + if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) + { + if( e->button() == Qt::RightButton && + isImmediateSelectionEnabled() && + aViewPort->nbSelected() < 1 ) + { + // If the 'immediate selection' mode is enabled, + // try to perform selection before invoking context menu + bool append = bool ( e->modifiers() & GraphicsView_Selector::getAppendKey() ); + getSelector()->select( QRectF(), append ); + } + else if( e->button() == Qt::LeftButton && + !aViewPort->isSelectByRect() && + !aViewPort->isDragging() && + aViewPort->startPulling( e->scenePos() ) ) + { + // Try to start pulling if rectangular selection is performed + aViewPort->finishSelectByRect(); + } + else if( e->button() == Qt::LeftButton && + !( aViewPort->currentBlock() & GraphicsView_ViewPort::BS_Selection ) && + !aViewPort->getHighlightedObject() ) + { + // Start rectangular selection if pulling was not started + QPoint p = aViewPort->mapFromScene( e->scenePos() ); + aViewPort->startSelectByRect( p.x(), p.y() ); + } + } + } } //================================================================ @@ -299,7 +369,26 @@ void GraphicsView_Viewer::handleMouseMove( QGraphicsSceneMouseEvent* e ) getSelector()->detect( e->scenePos().x(), e->scenePos().y() ); } - updateOperations( e ); // try to activate other operations + // try to activate other operations + if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) + { + if( aViewPort->isPulling() ) + { + aViewPort->drawPulling( e->scenePos() ); + } + else if( e->button() == Qt::LeftButton && + !aViewPort->isSelectByRect() && + !aViewPort->isDragging() && + aViewPort->startPulling( e->scenePos() ) ) + { + aViewPort->finishSelectByRect(); + } + else if( !aViewPort->getHighlightedObject() ) + { + QPoint p = aViewPort->mapFromScene( e->scenePos() ); + aViewPort->drawSelectByRect( p.x(), p.y() ); + } + } } //================================================================ @@ -322,7 +411,25 @@ void GraphicsView_Viewer::handleMouseRelease( QGraphicsSceneMouseEvent* e ) } } - finishOperations( e ); // try to finish active operations + // try to finish active operations + if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) + { + if( aViewPort->isPulling() ) + { + aViewPort->finishPulling(); + } + else if( !aViewPort->getHighlightedObject() ) + { + QRect aSelRect = aViewPort->selectionRect(); + aViewPort->finishSelectByRect(); + if ( getSelector() && !aSelRect.isNull() ) + { + bool append = bool ( e->modifiers() & GraphicsView_Selector::getAppendKey() ); + QRectF aRect = aViewPort->mapToScene( aSelRect ).boundingRect(); + getSelector()->select( aRect, append ); + } + } + } } //================================================================ @@ -331,7 +438,22 @@ void GraphicsView_Viewer::handleMouseRelease( QGraphicsSceneMouseEvent* e ) //================================================================ void GraphicsView_Viewer::handleWheel( QGraphicsSceneWheelEvent* e ) { - startOperations( e ); + if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) + { + bool anIsScaleUp = e->delta() > 0; + bool anIsCtrl = e->modifiers() & Qt::ControlModifier; + + bool anIsScaleChanged = false; + for( aViewPort->initSelected(); aViewPort->moreSelected(); aViewPort->nextSelected() ) + if( GraphicsView_Object* anObject = aViewPort->selectedObject() ) + anIsScaleChanged = anObject->updateScale( anIsScaleUp, anIsCtrl ) || anIsScaleChanged; + + if( anIsScaleChanged ) + { + emit wheelScaleChanged(); + aViewPort->onBoundingRectChanged(); + } + } } //================================================================ @@ -351,7 +473,8 @@ void GraphicsView_Viewer::onChangeBgColor() { if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) { - QColor aColor = aViewPort->isForegroundEnabled() ? aViewPort->foregroundColor() : aViewPort->backgroundColor(); + QColor aColor = aViewPort->isForegroundEnabled() ? + aViewPort->foregroundColor() : aViewPort->backgroundColor(); aColor = QColorDialog::getColor( aColor, aViewPort ); if ( aColor.isValid() ) { @@ -374,138 +497,3 @@ void GraphicsView_Viewer::onSelectionCancel() { emit selectionChanged( GVSCS_Invalid ); } - -//================================================================ -// Function : startOperations -// Purpose : -//================================================================ -void GraphicsView_Viewer::startOperations( QGraphicsSceneMouseEvent* e ) -{ - GraphicsView_ViewPort* aViewPort = getActiveViewPort(); - if( !aViewPort ) - return; - - // If the 'immediate selection' mode is enabled, - // try to perform selection before invoking context menu - if( e->button() == Qt::RightButton && - isImmediateSelectionEnabled() && - aViewPort->nbSelected() < 1 ) - { - bool append = bool ( e->modifiers() & GraphicsView_Selector::getAppendKey() ); - getSelector()->select( QRectF(), append ); - return; - } - - // Try to start pulling if rectangular selection is performed - if( e->button() == Qt::LeftButton && - !aViewPort->isSelectByRect() && - !aViewPort->isDragging() && - aViewPort->startPulling( e->scenePos() ) ) - { - aViewPort->finishSelectByRect(); - return; - } - - // Start rectangular selection if pulling was not started - if( e->button() == Qt::LeftButton && - !( aViewPort->currentBlock() & GraphicsView_ViewPort::BS_Selection ) && - !aViewPort->getHighlightedObject() ) - { - QPoint p = aViewPort->mapFromScene( e->scenePos() ); - aViewPort->startSelectByRect( p.x(), p.y() ); - } -} - -//================================================================ -// Function : updateOperations -// Purpose : -//================================================================ -bool GraphicsView_Viewer::updateOperations( QGraphicsSceneMouseEvent* e ) -{ - GraphicsView_ViewPort* aViewPort = getActiveViewPort(); - if( !aViewPort ) - return false; - - if( aViewPort->isPulling() ) - { - aViewPort->drawPulling( e->scenePos() ); - return true; - } - - if( e->button() == Qt::LeftButton ) - { - if( !aViewPort->isSelectByRect() && !aViewPort->isDragging() && aViewPort->startPulling( e->scenePos() ) ) - { - aViewPort->finishSelectByRect(); - return true; - } - } - - if( !aViewPort->getHighlightedObject() ) - { - QPoint p = aViewPort->mapFromScene( e->scenePos() ); - aViewPort->drawSelectByRect( p.x(), p.y() ); - return true; - } - return false; -} - -//================================================================ -// Function : finishOperations -// Purpose : -//================================================================ -bool GraphicsView_Viewer::finishOperations( QGraphicsSceneMouseEvent* e ) -{ - GraphicsView_ViewPort* aViewPort = getActiveViewPort(); - if( !aViewPort ) - return false; - - if( aViewPort->isPulling() ) - { - aViewPort->finishPulling(); - // Although operation is finished, FALSE is returned because base class try to - // perform selection in this case. In the other case it is impossible to perform - // selection of pulled port - return false; - } - - if( !aViewPort->getHighlightedObject() ) - { - QRect aSelRect = aViewPort->selectionRect(); - aViewPort->finishSelectByRect(); - if ( getSelector() && !aSelRect.isNull() ) - { - bool append = bool ( e->modifiers() & GraphicsView_Selector::getAppendKey() ); - QRectF aRect = aViewPort->mapToScene( aSelRect ).boundingRect(); - getSelector()->select( aRect, append ); - return true; - } - } - - return false; -} - -//================================================================ -// Function : startOperations -// Purpose : -//================================================================ -void GraphicsView_Viewer::startOperations( QGraphicsSceneWheelEvent* e ) -{ - GraphicsView_ViewPort* aViewPort = getActiveViewPort(); - if( !aViewPort ) - return; - - bool anIsScaleUp = e->delta() > 0; - bool anIsCtrl = e->modifiers() & Qt::ControlModifier; - - bool anIsScaleChanged = false; - for( aViewPort->initSelected(); aViewPort->moreSelected(); aViewPort->nextSelected() ) - if( GraphicsView_Object* anObject = aViewPort->selectedObject() ) - anIsScaleChanged = anObject->updateScale( anIsScaleUp, anIsCtrl ) || anIsScaleChanged; - - if( anIsScaleChanged ) - { - emit wheelScaleChanged(); - aViewPort->onBoundingRectChanged(); - } -} diff --git a/src/GraphicsView/GraphicsView_Viewer.h b/src/GraphicsView/GraphicsView_Viewer.h index 4fb7943cc..100882000 100644 --- a/src/GraphicsView/GraphicsView_Viewer.h +++ b/src/GraphicsView/GraphicsView_Viewer.h @@ -31,6 +31,7 @@ class QGraphicsSceneMouseEvent; class QGraphicsSceneWheelEvent; +class QKeyEvent; class SUIT_ViewWindow; @@ -88,12 +89,8 @@ protected: virtual void onTransformationStarted(); virtual void onTransformationFinished(); - virtual void startOperations( QGraphicsSceneMouseEvent* ); - virtual bool updateOperations( QGraphicsSceneMouseEvent* ); - virtual bool finishOperations( QGraphicsSceneMouseEvent* ); - virtual void startOperations( QGraphicsSceneWheelEvent* ); - protected slots: + virtual void onKeyEvent( QKeyEvent* ); virtual void onMouseEvent( QGraphicsSceneMouseEvent* ); virtual void onWheelEvent( QGraphicsSceneWheelEvent* ); @@ -103,6 +100,8 @@ protected slots: virtual void onChangeBgColor(); private: + void handleKeyPress( QKeyEvent* ); + void handleKeyRelease( QKeyEvent* ); void handleMouseMove( QGraphicsSceneMouseEvent* ); void handleMousePress( QGraphicsSceneMouseEvent* ); void handleMouseRelease( QGraphicsSceneMouseEvent* ); -- 2.39.2