Add navigation tool to scroll image by pressing-and-moving mouse over the image.

This commit is contained in:
Luis Lucas committed 2014-04-03 04:01:31 +01:00
1 parent 850b48771e
commit 7476093974
4 files changed
+46 -20

No files matched your search

+12 -2
View File
@@ -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
+2 -1
View File
@@ -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
+29 -16
View File
@@ -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;
}
+3 -1
View File
@@ -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;