From 7476093974849996b043ef17ab94e4d2ccf19388 Mon Sep 17 00:00:00 2001 From: Luis Lucas Date: Thu, 3 Apr 2014 04:01:31 +0100 Subject: [PATCH] Add navigation tool to scroll image by pressing-and-moving mouse over the image. --- source/app/SubWindowHandle.cpp | 14 +++++++++-- source/app/SubWindowHandle.h | 3 ++- source/app/ViewArea.cpp | 45 ++++++++++++++++++++++------------ source/app/ViewArea.h | 4 ++- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/source/app/SubWindowHandle.cpp b/source/app/SubWindowHandle.cpp index e9ca957..4617c63 100644 --- a/source/app/SubWindowHandle.cpp +++ b/source/app/SubWindowHandle.cpp @@ -43,7 +43,8 @@ SubWindowHandle::SubWindowHandle( QWidget * parent ) : // Create a new interface to show images m_cViewArea = new ViewArea( this ); - connect( m_cViewArea, SIGNAL( zoomFactorChanged(double) ), this, SLOT( adjustScrollBar(double) ) ); + connect( m_cViewArea, SIGNAL( zoomFactorChanged( double ) ), this, SLOT( adjustScrollBarByZoom(double) ) ); + connect( m_cViewArea, SIGNAL( moveScroll( QPoint ) ), this, SLOT( adjustScrollBarByOffset(QPoint) ) ); // Define the cViewArea as the widget inside the scroll area m_cScrollArea->setWidget( m_cViewArea ); @@ -268,7 +269,15 @@ Void SubWindowHandle::scaleView( const QSize & size ) scaleView( hfactor ); } -void SubWindowHandle::adjustScrollBar( double factor ) +void SubWindowHandle::adjustScrollBarByOffset( QPoint Offset ) +{ + QScrollBar *scrollBar = m_cScrollArea->horizontalScrollBar(); + scrollBar->setValue( int( scrollBar->value() + Offset.x() ) ); + scrollBar = m_cScrollArea->verticalScrollBar(); + scrollBar->setValue( int( scrollBar->value() + Offset.y() ) ); +} + +void SubWindowHandle::adjustScrollBarByZoom( double factor ) { QScrollBar *scrollBar = m_cScrollArea->horizontalScrollBar(); scrollBar->setValue( int( factor * scrollBar->value() + ( ( factor - 1 ) * scrollBar->pageStep() / 2 ) ) ); @@ -276,6 +285,7 @@ void SubWindowHandle::adjustScrollBar( double factor ) scrollBar->setValue( int( factor * scrollBar->value() + ( ( factor - 1 ) * scrollBar->pageStep() / 2 ) ) ); } + QSize SubWindowHandle::sizeHint() const { QSize maxSize; // The size of the parent (viewport widget diff --git a/source/app/SubWindowHandle.h b/source/app/SubWindowHandle.h index cbde86e..3d38701 100644 --- a/source/app/SubWindowHandle.h +++ b/source/app/SubWindowHandle.h @@ -145,7 +145,8 @@ protected: void closeEvent( QCloseEvent *event ); public Q_SLOTS: - void adjustScrollBar( double factor ); + void adjustScrollBarByZoom( double factor ); + void adjustScrollBarByOffset( QPoint Offset ); }; } // NAMESPACE diff --git a/source/app/ViewArea.cpp b/source/app/ViewArea.cpp index 07190eb..bab0f5e 100755 --- a/source/app/ViewArea.cpp +++ b/source/app/ViewArea.cpp @@ -62,7 +62,7 @@ ViewArea::ViewArea( QWidget *parent ) : m_xOffset = 0; m_yOffset = 0; m_mode = NormalMode; - m_tool = NormalSelectionTool; + m_tool = NavigationTool; m_gridVisible = false; m_snapToGrid = false; m_blockTrackEnable = false; @@ -529,6 +529,10 @@ void ViewArea::mousePressEvent( QMouseEvent *event ) if( event->button() == Qt::LeftButton ) { + if( tool() == NavigationTool ) + { + m_lastWindowPos = event->pos(); + } // Is the mouse over the image? If yes, save the mouse position; // otherwise, QPoint( -1, -1 ) indicates an invalid position. if( isPosValid( vpos ) ) @@ -569,7 +573,7 @@ void ViewArea::mousePressEvent( QMouseEvent *event ) } // Block selection - m_selectedArea = m_grid.rectContains( m_lastPos ); +// m_selectedArea = m_grid.rectContains( m_lastPos ); QRect updateRect = viewToWindow( m_selectedArea ); updateRect.adjust( 0, 0, 1, 1 ); @@ -602,6 +606,12 @@ void ViewArea::mouseMoveEvent( QMouseEvent *event ) } } + if( tool() == NavigationTool ) + { + QPoint offset = m_lastWindowPos - event->pos() ; + emit moveScroll( offset ); + } + updateRect = viewToWindow( m_selectedArea ); if( tool() == NormalSelectionTool ) @@ -648,7 +658,7 @@ void ViewArea::mouseMoveEvent( QMouseEvent *event ) } } } - else // if tool() == BlockSelectionTool || MaskTool || EraserTool +/* else // if tool() == BlockSelectionTool || MaskTool || EraserTool { m_blockTrackEnable = false; // If cursor is inside the selected area, we most redraw @@ -660,22 +670,25 @@ void ViewArea::mouseMoveEvent( QMouseEvent *event ) m_selectedArea = m_selectedArea.united( m_grid.rectContains( actualPos ) ); } +*/ + if( tool() == NormalSelectionTool ) + { + // Intercept the selected area with the image area to limit the + // selection only to the image area, preventing it to come outside + // the image. + m_selectedArea &= QRect( 0, 0, m_pixmap.width(), m_pixmap.height() ); - // Intercept the selected area with the image area to limit the - // selection only to the image area, preventing it to come outside - // the image. - m_selectedArea &= QRect( 0, 0, m_pixmap.width(), m_pixmap.height() ); + // Update only the united area + updateRect = updateRect.united( viewToWindow( m_selectedArea ) ); - // Update only the united area - updateRect = updateRect.united( viewToWindow( m_selectedArea ) ); + // "When rendering with a one pixel wide pen the QRect's + // boundary line will be rendered to the right and below the + // mathematical rectangle's boundary line.", in QT4 doc. + // Our selection pen width is 1, let's adjust the rendering area. + updateRect.adjust( 0, 0, 1, 1 ); - // "When rendering with a one pixel wide pen the QRect's - // boundary line will be rendered to the right and below the - // mathematical rectangle's boundary line.", in QT4 doc. - // Our selection pen width is 1, let's adjust the rendering area. - updateRect.adjust( 0, 0, 1, 1 ); - - update( updateRect.normalized() ); + update( updateRect.normalized() ); + } return; } diff --git a/source/app/ViewArea.h b/source/app/ViewArea.h index 6444bff..daec08b 100755 --- a/source/app/ViewArea.h +++ b/source/app/ViewArea.h @@ -55,7 +55,7 @@ public: enum Tool { - NormalSelectionTool, BlockSelectionTool, MaskTool, EraserTool + NavigationTool, NormalSelectionTool, BlockSelectionTool, MaskTool, EraserTool }; ViewArea( QWidget *parent = 0 ); @@ -114,6 +114,7 @@ public: signals: void selectionChanged( const QRect &rect ); void positionChanged( const QPoint &pos, InputStream *stream ); + void moveScroll( QPoint offset ); void zoomFactorChanged( double ); public slots: @@ -154,6 +155,7 @@ private: QBitmap m_mask; QRect m_selectedArea; QPoint m_lastPos; + QPoint m_lastWindowPos; GridManager m_grid; ViewMode m_mode; Tool m_tool;