From 083f38e8bf35c838438d1a99bc0e00bd0c802535 Mon Sep 17 00:00:00 2001 From: Luis Lucas Date: Sat, 17 Jan 2015 23:55:42 +0000 Subject: [PATCH] Reorganize image zoom functions. New zoomToFactor function is available from SubWindowHandle. --- app/SubWindowHandle.cpp | 16 ++++++++-------- app/SubWindowHandle.h | 11 ++++++++--- app/VideoSubWindow.cpp | 28 ++++++++++++++++++++-------- app/VideoSubWindow.h | 1 + app/ViewArea.cpp | 24 +++++++++++++----------- app/ViewArea.h | 4 ++-- app/plaYUVerApp.cpp | 16 +++++++--------- app/plaYUVerApp.h | 2 +- 8 files changed, 60 insertions(+), 42 deletions(-) diff --git a/app/SubWindowHandle.cpp b/app/SubWindowHandle.cpp index 750f169..f6ecf82 100644 --- a/app/SubWindowHandle.cpp +++ b/app/SubWindowHandle.cpp @@ -58,10 +58,10 @@ Void SubWindowHandle::setMainWidget( QWidget *widget ) m_cScrollArea->setWidget( widget ); } -Void SubWindowHandle::processZoomChanged( Double factor, QPoint center ) +Void SubWindowHandle::processZoomChange( Double scale, QPoint center ) { - adjustScrollBarByZoom( factor, center ); - emit zoomChanged(); + adjustScrollBarByScale( scale, center ); + emit zoomChangedOnSubWindow(); } QSize SubWindowHandle::getScrollSize() @@ -82,17 +82,17 @@ Void SubWindowHandle::adjustScrollBarByOffset( QPoint Offset ) // This function was developed with help of the schematics presented in // http://stackoverflow.com/questions/13155382/jscrollpane-zoom-relative-to-mouse-position -Void SubWindowHandle::adjustScrollBarByZoom( Double factor, QPoint center ) +Void SubWindowHandle::adjustScrollBarByScale( Double scale, QPoint center ) { QScrollBar *scrollBar = m_cScrollArea->horizontalScrollBar(); if( center.isNull() ) { - scrollBar->setValue( int( factor * scrollBar->value() + ( ( factor - 1 ) * scrollBar->pageStep() / 2 ) ) ); + scrollBar->setValue( int( scale * scrollBar->value() + ( ( scale - 1 ) * scrollBar->pageStep() / 2 ) ) ); } else { Int x = center.x() - m_cLastScroll.x(); - Int value = int( factor * m_cLastScroll.x() + ( ( factor - 1 ) * x ) ); + Int value = int( scale * m_cLastScroll.x() + ( ( scale - 1 ) * x ) ); if( value > scrollBar->maximum() ) value = scrollBar->maximum(); if( value < scrollBar->minimum() ) @@ -103,12 +103,12 @@ Void SubWindowHandle::adjustScrollBarByZoom( Double factor, QPoint center ) scrollBar = m_cScrollArea->verticalScrollBar(); if( center.isNull() ) { - scrollBar->setValue( int( factor * scrollBar->value() + ( ( factor - 1 ) * scrollBar->pageStep() / 2 ) ) ); + scrollBar->setValue( int( scale * scrollBar->value() + ( ( scale - 1 ) * scrollBar->pageStep() / 2 ) ) ); } else { Int y = center.y() - m_cLastScroll.y(); - Int value = int( factor * m_cLastScroll.y() + ( ( factor - 1 ) * y ) ); + Int value = int( scale * m_cLastScroll.y() + ( ( scale - 1 ) * y ) ); if( value > scrollBar->maximum() ) value = scrollBar->maximum(); if( value < scrollBar->minimum() ) diff --git a/app/SubWindowHandle.h b/app/SubWindowHandle.h index 7d8beff..a8059b6 100644 --- a/app/SubWindowHandle.h +++ b/app/SubWindowHandle.h @@ -47,7 +47,7 @@ private: UInt m_uiCategory; QString m_cWindowName; - Void adjustScrollBarByZoom( Double factor, QPoint center ); + Void adjustScrollBarByScale( Double scale, QPoint center ); public: enum SubWindowCategories @@ -68,6 +68,10 @@ public: * Scale the image (zoomed in or out) to fit on the window. */ virtual Void zoomToFit() = 0; + /* + * Scale the image (zoomed in or out) to speficied absolute zoom value (1.0 = original size). + */ + virtual Void zoomToFactor( Double ) = 0; /** * Scale the image by a given factor * @param factor factor of scale. Ex: 1.2 scale the image up by 20% and @@ -75,6 +79,7 @@ public: */ virtual Void scaleView( Double ) = 0; + virtual Double getScaleFactor() = 0; Void setWindowName( QString ); @@ -93,11 +98,11 @@ protected: QSize getScrollSize(); Q_SIGNALS: - void zoomChanged(); + void zoomChangedOnSubWindow(); void updateStatusBar( const QString& ); public Q_SLOTS: - void processZoomChanged( double factor, QPoint center ); + void processZoomChange( double factor, QPoint center ); void adjustScrollBarByOffset( QPoint Offset ); void updateLastScrollValue(); diff --git a/app/VideoSubWindow.cpp b/app/VideoSubWindow.cpp index 3b6eeb6..e06a3fc 100644 --- a/app/VideoSubWindow.cpp +++ b/app/VideoSubWindow.cpp @@ -83,7 +83,7 @@ VideoSubWindow::VideoSubWindow( QWidget * parent, Bool isModule ) : // Create a new interface to show images m_cViewArea = new ViewArea( this ); - connect( m_cViewArea, SIGNAL( zoomFactorChanged( double , QPoint) ), this, SLOT( processZoomChanged(double, QPoint) ) ); + connect( m_cViewArea, SIGNAL( zoomFactorChanged( double , QPoint) ), this, SLOT( processZoomChange(double, QPoint) ) ); connect( m_cViewArea, SIGNAL( moveScroll( QPoint ) ), this, SLOT( adjustScrollBarByOffset(QPoint) ) ); connect( m_cViewArea, SIGNAL( selectionChanged( QRect ) ), this, SLOT( updateSelectedArea( QRect ) ) ); @@ -339,9 +339,10 @@ Void VideoSubWindow::stop() Void VideoSubWindow::normalSize() { - Double scaleFactor = 1.0; - m_cViewArea->setZoomFactor( scaleFactor ); - updateLastScrollValue(); + Double factor = 1.0; + Double curFactor = getScaleFactor() ; + if( factor != curFactor ) + m_cViewArea->scaleZoomFactor( factor/curFactor, QPoint() ); } Void VideoSubWindow::zoomToFit() @@ -350,10 +351,18 @@ Void VideoSubWindow::zoomToFit() scaleView( getScrollSize() ); } +Void VideoSubWindow::zoomToFactor(Double factor) +{ + Double curFactor; + curFactor = getScaleFactor() ; + if( factor != curFactor ) + m_cViewArea->scaleZoomFactor( factor/curFactor, QPoint() ); +} + Void VideoSubWindow::scaleView( Double scale ) { Q_ASSERT( m_cViewArea->image() ); - m_cViewArea->zoomChangeEvent( scale, QPoint() ); + m_cViewArea->scaleZoomFactor( scale, QPoint() ); } Void VideoSubWindow::scaleView( const QSize & size ) @@ -369,16 +378,19 @@ Void VideoSubWindow::scaleView( const QSize & size ) // Calc the zoom factor Double wfactor = 1; Double hfactor = 1; + Double factor; wfactor = ( Double )newSize.width() / imgViewSize.width(); hfactor = ( Double )newSize.height() / imgViewSize.height(); if( wfactor < hfactor ) - m_cViewArea->setZoomFactor( wfactor ); + factor = wfactor; else - m_cViewArea->setZoomFactor( hfactor ); + factor = hfactor; - updateLastScrollValue(); + Double curFactor = getScaleFactor() ; + if( factor != curFactor ) + m_cViewArea->scaleZoomFactor( factor/curFactor, QPoint() ); } diff --git a/app/VideoSubWindow.h b/app/VideoSubWindow.h index ba74036..fcafe0d 100644 --- a/app/VideoSubWindow.h +++ b/app/VideoSubWindow.h @@ -165,6 +165,7 @@ public: Void normalSize(); Void zoomToFit(); Void scaleView( Double scale ); + Void zoomToFactor( Double factor ); Double getScaleFactor() { diff --git a/app/ViewArea.cpp b/app/ViewArea.cpp index 711821e..36436c0 100644 --- a/app/ViewArea.cpp +++ b/app/ViewArea.cpp @@ -109,19 +109,19 @@ Void ViewArea::setZoomFactor( Double f ) update(); } -Void ViewArea::zoomChangeEvent( Double factor, QPoint center ) +Void ViewArea::scaleZoomFactor( Double scale, QPoint center ) { Double zoomFactor; Double maxZoom = 100.0; Double minZoom = 0.01; //( 1.0 / m_pixmap.width() ); - if( ( m_zoomFactor == minZoom ) && ( factor < 1 ) ) + if( ( m_zoomFactor == minZoom ) && ( scale < 1 ) ) return; - if( ( m_zoomFactor == maxZoom ) && ( factor > 1 ) ) + if( ( m_zoomFactor == maxZoom ) && ( scale > 1 ) ) return; - zoomFactor = m_zoomFactor * factor; + zoomFactor = m_zoomFactor * scale; if( zoomFactor < minZoom ) zoomFactor = minZoom; @@ -130,8 +130,8 @@ Void ViewArea::zoomChangeEvent( Double factor, QPoint center ) zoomFactor = maxZoom; setZoomFactor( zoomFactor ); - emit zoomFactorChanged( factor, center ); + emit zoomFactorChanged( scale, center ); } void ViewArea::setMode( ViewMode mode ) @@ -544,16 +544,17 @@ void ViewArea::paintEvent( QPaintEvent *event ) //////////////////////////////////////////////////////////////////////////////// void ViewArea::wheelEvent( QWheelEvent *event ) { - Double factor; + Double scale; if( event->modifiers() & Qt::ControlModifier ) { - factor = 0.001 * event->delta(); - if(factor>0) - factor=1.25; + scale = 0.001 * event->delta(); + if(scale>0) + scale=1.25; else - factor=0.8; - zoomChangeEvent( factor , event->pos() ); + scale=0.8; + + scaleZoomFactor( scale , event->pos() ); } } @@ -810,6 +811,7 @@ void ViewArea::mouseReleaseEvent( QMouseEvent *event ) } } + //////////////////////////////////////////////////////////////////////////////// bool ViewArea::isPosValid( const QPoint &pos ) const diff --git a/app/ViewArea.h b/app/ViewArea.h index 1dda297..e66f5ec 100644 --- a/app/ViewArea.h +++ b/app/ViewArea.h @@ -115,7 +115,7 @@ public: void setInputStream( PlaYUVerStream *stream ); PlaYUVerStream* getInputStream(); - Void zoomChangeEvent(Double factor, QPoint center); + Void scaleZoomFactor(Double scale, QPoint center); void setTool( eTool tool ); @@ -137,7 +137,6 @@ public Q_SLOTS: void setGridVisible( bool enable ); void setSnapToGrid( bool enable ); void setSelectedArea( QRect &rect ); - void setZoomFactor( double ); protected: void paintEvent( QPaintEvent *event ); @@ -154,6 +153,7 @@ private: bool isPosValid( const QPoint &pos ) const; void updateMask( const QRect &rect ); + void setZoomFactor( double ); QPoint windowToView( const QPoint& pt ) const; QRect windowToView( const QRect& rc ) const; diff --git a/app/plaYUVerApp.cpp b/app/plaYUVerApp.cpp index 519adbe..c6d9a3c 100644 --- a/app/plaYUVerApp.cpp +++ b/app/plaYUVerApp.cpp @@ -195,7 +195,7 @@ Void plaYUVerApp::loadFile( QString fileName, PlaYUVerStreamInfo* pStreamInfo ) connect( videoSubWindow->getViewArea(), SIGNAL( selectionChanged( QRect ) ), m_appModuleVideo, SLOT( updateSelectionArea( QRect ) ) ); connect( subWindow, SIGNAL( updateStatusBar( const QString& ) ), this, SLOT( updateStatusBar( const QString& ) ) ); - connect( subWindow, SIGNAL( zoomChanged() ), this, SLOT( updateZoomFactorSBox() ) ); + connect( subWindow, SIGNAL( zoomChangedOnSubWindow() ), this, SLOT( updateZoomFactorSBox() ) ); videoSubWindow->zoomToFit(); videoSubWindow->getViewArea()->setTool( m_appTool ); @@ -399,7 +399,6 @@ Void plaYUVerApp::zoomToFit() { if( m_pcCurrentSubWindow ) { - m_pcCurrentSubWindow->zoomToFit(); updateZoomFactorSBox(); } @@ -413,7 +412,8 @@ Void plaYUVerApp::zoomToFitAll() subWindow = qobject_cast( mdiArea->subWindowList().at( i ) ); subWindow->zoomToFit(); } - updateZoomFactorSBox(); + if( m_pcCurrentSubWindow ) + updateZoomFactorSBox(); } Void plaYUVerApp::scaleFrame( int ratio ) @@ -421,17 +421,15 @@ Void plaYUVerApp::scaleFrame( int ratio ) if( m_pcCurrentSubWindow ) { m_pcCurrentSubWindow->scaleView( ( Double )( ratio ) / 100.0 ); + updateZoomFactorSBox(); } } -Void plaYUVerApp::setZoomFromSBox( double zoom ) +Void plaYUVerApp::zoomFromSBox( double zoom ) { - Double lastZoom; if( activeSubWindow() ) { - lastZoom = activeSubWindow()->getScaleFactor() * 100; - if( zoom / lastZoom != 1.0 ) - activeSubWindow()->scaleView( zoom / lastZoom ); + activeSubWindow()->zoomToFactor( zoom/100 ); } } @@ -924,7 +922,7 @@ Void plaYUVerApp::createToolBars() m_pcZoomFactorSBox->setSingleStep( 10.0 ); m_pcZoomFactorSBox->setValue( 100.0 ); m_pcZoomFactorSBox->setSuffix( "%" ); - connect( m_pcZoomFactorSBox, SIGNAL( valueChanged(double) ), this, SLOT( setZoomFromSBox(double) ) ); + connect( m_pcZoomFactorSBox, SIGNAL( valueChanged(double) ), this, SLOT( zoomFromSBox(double) ) ); m_arrayToolBars[VIEW_TOOLBAR]->addWidget( m_pcZoomFactorSBox ); m_arrayToolBars[VIEW_TOOLBAR]->addAction( m_arrayActions[ZOOM_IN_ACT] ); m_arrayToolBars[VIEW_TOOLBAR]->addAction( m_arrayActions[ZOOM_OUT_ACT] ); diff --git a/app/plaYUVerApp.h b/app/plaYUVerApp.h index 9354cac..b843276 100644 --- a/app/plaYUVerApp.h +++ b/app/plaYUVerApp.h @@ -85,7 +85,7 @@ private Q_SLOTS: * 80 scale the image down by 25% */ void scaleFrame( int ratio = 100.0 ); - void setZoomFromSBox( double ratio ); + void zoomFromSBox( double ratio ); void normalSize(); void zoomToFit(); void zoomToFitAll();