From 3811256fd9966d08300331a03e6b8d321544936c Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Fri, 7 Feb 2014 22:51:36 +0000 Subject: [PATCH] Use signal mapping to avoid using different functions to zoom in and zoom out --- source/plaYUVerApp.cpp | 278 ++++++++++++++++++++--------------------- source/plaYUVerApp.h | 61 ++++----- 2 files changed, 169 insertions(+), 170 deletions(-) diff --git a/source/plaYUVerApp.cpp b/source/plaYUVerApp.cpp index b607a04..15118ab 100644 --- a/source/plaYUVerApp.cpp +++ b/source/plaYUVerApp.cpp @@ -47,8 +47,9 @@ plaYUVerApp::plaYUVerApp() //mdiArea->setVerticalScrollBarPolicy( Qt::ScrollBarAsNeeded ); connect( mdiArea, SIGNAL( subWindowActivated(QMdiSubWindow*) ), this, SLOT( updateMenus() ) ); - windowMapper = new QSignalMapper( this ); - connect( windowMapper, SIGNAL( mapped(QWidget*) ), this, SLOT( setActiveSubWindow(QWidget*) ) ); + + mapperWindow = new QSignalMapper( this ); + connect( mapperWindow, SIGNAL( mapped(QWidget*) ), this, SLOT( setActiveSubWindow(QWidget*) ) ); createActions(); createMenus(); @@ -170,16 +171,6 @@ void plaYUVerApp::playEvent() // ----------------------- Zoom Functions ----------------------- -void plaYUVerApp::zoomIn() -{ - scaleImage( 1.25 ); -} - -void plaYUVerApp::zoomOut() -{ - scaleImage( 0.8 ); -} - void plaYUVerApp::normalSize() { if( activeSubWindow() ) @@ -192,15 +183,14 @@ void plaYUVerApp::zoomToFit() activeSubWindow()->zoomToFit(); } -void plaYUVerApp::scaleImage( double factor ) +void plaYUVerApp::scaleFrame( int ratio ) { - if( activeSubWindow() ) { - activeSubWindow()->scaleView( factor ); + activeSubWindow()->scaleView( (Double)(ratio)/100.0 ); - zoomInAct->setEnabled( activeSubWindow()->getScaleFactor() < 3.0 ); - zoomOutAct->setEnabled( activeSubWindow()->getScaleFactor() > 0.333 ); + actionZoomIn->setEnabled( activeSubWindow()->getScaleFactor() < 3.0 ); + actionZoomOut->setEnabled( activeSubWindow()->getScaleFactor() > 0.333 ); } } @@ -214,19 +204,19 @@ void plaYUVerApp::about() void plaYUVerApp::updateMenus() { Bool hasSubWindow = ( activeSubWindow() != 0 ); - saveAct->setEnabled( hasSubWindow ); - closeAct->setEnabled( hasSubWindow ); - closeAllAct->setEnabled( hasSubWindow ); - tileAct->setEnabled( hasSubWindow ); - cascadeAct->setEnabled( hasSubWindow ); - nextAct->setEnabled( hasSubWindow ); - previousAct->setEnabled( hasSubWindow ); - separatorAct->setVisible( hasSubWindow ); + actionSave->setEnabled( hasSubWindow ); + actionClose->setEnabled( hasSubWindow ); + actionCloseAll->setEnabled( hasSubWindow ); + actionTile->setEnabled( hasSubWindow ); + actionCascade->setEnabled( hasSubWindow ); + actionNext->setEnabled( hasSubWindow ); + actionPrevious->setEnabled( hasSubWindow ); + actionSeparator->setVisible( hasSubWindow ); - zoomInAct->setEnabled( hasSubWindow ); - zoomOutAct->setEnabled( hasSubWindow ); - normalSizeAct->setEnabled( hasSubWindow ); - zoomToFitAct->setEnabled( hasSubWindow ); + actionZoomIn->setEnabled( hasSubWindow ); + actionZoomOut->setEnabled( hasSubWindow ); + actionNormalSize->setEnabled( hasSubWindow ); + actionZoomToFit->setEnabled( hasSubWindow ); actionVideoPlay->setEnabled( hasSubWindow ); actionVideoPause->setEnabled( hasSubWindow ); @@ -237,21 +227,11 @@ void plaYUVerApp::updateMenus() void plaYUVerApp::updateWindowMenu() { - windowMenu->clear(); - windowMenu->addAction( closeAct ); - windowMenu->addAction( closeAllAct ); - windowMenu->addSeparator(); - windowMenu->addAction( tileAct ); - windowMenu->addAction( cascadeAct ); - windowMenu->addSeparator(); - windowMenu->addAction( nextAct ); - windowMenu->addAction( previousAct ); - windowMenu->addAction( separatorAct ); updateMenus(); QList windows = mdiArea->subWindowList(); - separatorAct->setVisible( !windows.isEmpty() ); + actionSeparator->setVisible( !windows.isEmpty() ); for( Int i = 0; i < windows.size(); ++i ) { @@ -266,11 +246,11 @@ void plaYUVerApp::updateWindowMenu() { text = tr( "%1 %2" ).arg( i + 1 ).arg( child->userFriendlyCurrentFile() ); } - QAction *action = windowMenu->addAction( text ); + QAction *action = menuWindow->addAction( text ); action->setCheckable( true ); action->setChecked( child == activeSubWindow() ); - connect( action, SIGNAL( triggered() ), windowMapper, SLOT( map() ) ); - windowMapper->setMapping( action, windows.at( i ) ); + connect( action, SIGNAL( triggered() ), mapperWindow, SLOT( map() ) ); + mapperWindow->setMapping( action, windows.at( i ) ); } } @@ -295,55 +275,60 @@ Void plaYUVerApp::createActions() { // ------------ File ------------ - openAct = new QAction( QIcon( ":/images/open.png" ), tr( "&Open..." ), this ); - openAct->setShortcuts( QKeySequence::Open ); - openAct->setStatusTip( tr( "Open an existing file" ) ); - connect( openAct, SIGNAL( triggered() ), this, SLOT( open() ) ); + actionOpen = new QAction( QIcon( ":/images/open.png" ), tr( "&Open..." ), this ); + actionOpen->setShortcuts( QKeySequence::Open ); + actionOpen->setStatusTip( tr( "Open an existing file" ) ); + connect( actionOpen, SIGNAL( triggered() ), this, SLOT( open() ) ); - saveAct = new QAction( QIcon( ":/images/save.png" ), tr( "&Save..." ), this ); - saveAct->setShortcuts( QKeySequence::SaveAs ); - saveAct->setStatusTip( tr( "Save the document under a new name" ) ); - connect( saveAct, SIGNAL( triggered() ), this, SLOT( save() ) ); + actionSave = new QAction( QIcon( ":/images/save.png" ), tr( "&Save..." ), this ); + actionSave->setShortcuts( QKeySequence::SaveAs ); + actionSave->setStatusTip( tr( "Save the document under a new name" ) ); + connect( actionSave, SIGNAL( triggered() ), this, SLOT( save() ) ); - exitAct = new QAction( tr( "E&xit" ), this ); - exitAct->setShortcuts( QKeySequence::Quit ); - exitAct->setStatusTip( tr( "Exit the application" ) ); - connect( exitAct, SIGNAL( triggered() ), qApp, SLOT( closeAllWindows() ) ); + actionExit = new QAction( tr( "E&xit" ), this ); + actionExit->setShortcuts( QKeySequence::Quit ); + actionExit->setStatusTip( tr( "Exit the application" ) ); + connect( actionExit, SIGNAL( triggered() ), qApp, SLOT( closeAllWindows() ) ); - closeAct = new QAction( tr( "Cl&ose" ), this ); - closeAct->setIcon( QIcon( ":/images/close.png" ) ); - closeAct->setStatusTip( tr( "Close the active window" ) ); - connect( closeAct, SIGNAL( triggered() ), mdiArea, SLOT( closeActiveSubWindow() ) ); + actionClose = new QAction( tr( "Cl&ose" ), this ); + actionClose->setIcon( QIcon( ":/images/close.png" ) ); + actionClose->setStatusTip( tr( "Close the active window" ) ); + connect( actionClose, SIGNAL( triggered() ), mdiArea, SLOT( closeActiveSubWindow() ) ); - closeAllAct = new QAction( tr( "Close &All" ), this ); - closeAllAct->setStatusTip( tr( "Close all the windows" ) ); - connect( closeAllAct, SIGNAL( triggered() ), mdiArea, SLOT( closeAllSubWindows() ) ); + actionCloseAll = new QAction( tr( "Close &All" ), this ); + actionCloseAll->setStatusTip( tr( "Close all the windows" ) ); + connect( actionCloseAll, SIGNAL( triggered() ), mdiArea, SLOT( closeAllSubWindows() ) ); // ------------ View ------------ - zoomInAct = new QAction( tr( "Zoom &In (25%)" ), this ); - zoomInAct->setIcon( QIcon( ":/images/zoomin.png" ) ); - zoomInAct->setShortcut( tr( "Ctrl++" ) ); - zoomInAct->setStatusTip( tr( "Scale the image up by 25%" ) ); - connect( zoomInAct, SIGNAL( triggered() ), this, SLOT( zoomIn() ) ); + mapperZoom = new QSignalMapper( this ); + connect( mapperZoom, SIGNAL( mapped(int) ), this, SLOT( scaleFrame(int) ) ); - zoomOutAct = new QAction( tr( "Zoom &Out (25%)" ), this ); - zoomOutAct->setIcon( QIcon( ":/images/zoomout.png" ) ); - zoomOutAct->setShortcut( tr( "Ctrl+-" ) ); - zoomOutAct->setStatusTip( tr( "Scale the image down by 25%" ) ); - connect( zoomOutAct, SIGNAL( triggered() ), this, SLOT( zoomOut() ) ); + actionZoomIn = new QAction( tr( "Zoom &In (25%)" ), this ); + actionZoomIn->setIcon( QIcon( ":/images/zoomin.png" ) ); + actionZoomIn->setShortcut( tr( "Ctrl++" ) ); + actionZoomIn->setStatusTip( tr( "Scale the image up by 25%" ) ); + connect (actionZoomIn, SIGNAL(triggered()), mapperZoom, SLOT(map())) ; + mapperZoom->setMapping(actionZoomIn, 125 ); - normalSizeAct = new QAction( tr( "&Normal Size" ), this ); - normalSizeAct->setIcon( QIcon( ":/images/zoomtonormal.png" ) ); - normalSizeAct->setShortcut( tr( "Ctrl+N" ) ); - normalSizeAct->setStatusTip( tr( "Show the image at its original size" ) ); - connect( normalSizeAct, SIGNAL( triggered() ), this, SLOT( normalSize() ) ); + actionZoomOut = new QAction( tr( "Zoom &Out (25%)" ), this ); + actionZoomOut->setIcon( QIcon( ":/images/zoomout.png" ) ); + actionZoomOut->setShortcut( tr( "Ctrl+-" ) ); + actionZoomOut->setStatusTip( tr( "Scale the image down by 25%" ) ); + connect (actionZoomOut, SIGNAL(triggered()), mapperZoom, SLOT(map())) ; + mapperZoom->setMapping(actionZoomOut, 80 ); - zoomToFitAct = new QAction( tr( "Zoom to &Fit" ), this ); - zoomToFitAct->setIcon( QIcon( ":/images/fittowindow.png" ) ); - zoomToFitAct->setStatusTip( tr( "Zoom in or out to fit on the window." ) ); - zoomToFitAct->setShortcut( tr( "Ctrl+F" ) ); - connect( zoomToFitAct, SIGNAL( triggered() ), this, SLOT( zoomToFit() ) ); + actionNormalSize = new QAction( tr( "&Normal Size" ), this ); + actionNormalSize->setIcon( QIcon( ":/images/zoomtonormal.png" ) ); + actionNormalSize->setShortcut( tr( "Ctrl+N" ) ); + actionNormalSize->setStatusTip( tr( "Show the image at its original size" ) ); + connect( actionNormalSize, SIGNAL( triggered() ), this, SLOT( normalSize() ) ); + + actionZoomToFit = new QAction( tr( "Zoom to &Fit" ), this ); + actionZoomToFit->setIcon( QIcon( ":/images/fittowindow.png" ) ); + actionZoomToFit->setStatusTip( tr( "Zoom in or out to fit on the window." ) ); + actionZoomToFit->setShortcut( tr( "Ctrl+F" ) ); + connect( actionZoomToFit, SIGNAL( triggered() ), this, SLOT( zoomToFit() ) ); // ------------ Playing ------------ @@ -375,89 +360,100 @@ Void plaYUVerApp::createActions() // ------------ Window ------------ - tileAct = new QAction( tr( "&Tile" ), this ); - tileAct->setStatusTip( tr( "Tile the windows" ) ); - connect( tileAct, SIGNAL( triggered() ), mdiArea, SLOT( tileSubWindows() ) ); + actionTile = new QAction( tr( "&Tile" ), this ); + actionTile->setStatusTip( tr( "Tile the windows" ) ); + connect( actionTile, SIGNAL( triggered() ), mdiArea, SLOT( tileSubWindows() ) ); - cascadeAct = new QAction( tr( "&Cascade" ), this ); - cascadeAct->setStatusTip( tr( "Cascade the windows" ) ); - connect( cascadeAct, SIGNAL( triggered() ), mdiArea, SLOT( cascadeSubWindows() ) ); + actionCascade = new QAction( tr( "&Cascade" ), this ); + actionCascade->setStatusTip( tr( "Cascade the windows" ) ); + connect( actionCascade, SIGNAL( triggered() ), mdiArea, SLOT( cascadeSubWindows() ) ); - nextAct = new QAction( tr( "Ne&xt" ), this ); - nextAct->setShortcuts( QKeySequence::NextChild ); - nextAct->setStatusTip( tr( "Move the focus to the next window" ) ); - connect( nextAct, SIGNAL( triggered() ), mdiArea, SLOT( activateNextSubWindow() ) ); + actionNext = new QAction( tr( "Ne&xt" ), this ); + actionNext->setShortcuts( QKeySequence::NextChild ); + actionNext->setStatusTip( tr( "Move the focus to the next window" ) ); + connect( actionNext, SIGNAL( triggered() ), mdiArea, SLOT( activateNextSubWindow() ) ); - previousAct = new QAction( tr( "Pre&vious" ), this ); - previousAct->setShortcuts( QKeySequence::PreviousChild ); - previousAct->setStatusTip( tr( "Move the focus to the previous window" ) ); - connect( previousAct, SIGNAL( triggered() ), mdiArea, SLOT( activatePreviousSubWindow() ) ); + actionPrevious = new QAction( tr( "Pre&vious" ), this ); + actionPrevious->setShortcuts( QKeySequence::PreviousChild ); + actionPrevious->setStatusTip( tr( "Move the focus to the previous window" ) ); + connect( actionPrevious, SIGNAL( triggered() ), mdiArea, SLOT( activatePreviousSubWindow() ) ); - separatorAct = new QAction( this ); - separatorAct->setSeparator( true ); + actionSeparator = new QAction( this ); + actionSeparator->setSeparator( true ); // ------------ About ------------ - aboutAct = new QAction( tr( "&About" ), this ); - aboutAct->setStatusTip( tr( "Show the application's About box" ) ); - connect( aboutAct, SIGNAL( triggered() ), this, SLOT( about() ) ); + actionAbout = new QAction( tr( "&About" ), this ); + actionAbout->setStatusTip( tr( "Show the application's About box" ) ); + connect( actionAbout, SIGNAL( triggered() ), this, SLOT( about() ) ); - aboutQtAct = new QAction( tr( "About &Qt" ), this ); - aboutQtAct->setIcon( QIcon( ":images/qt.png" ) ); - aboutQtAct->setStatusTip( tr( "Show the Qt library's About box" ) ); - connect( aboutQtAct, SIGNAL( triggered() ), qApp, SLOT( aboutQt() ) ); + actionAboutQt = new QAction( tr( "About &Qt" ), this ); + actionAboutQt->setIcon( QIcon( ":images/qt.png" ) ); + actionAboutQt->setStatusTip( tr( "Show the Qt library's About box" ) ); + connect( actionAboutQt, SIGNAL( triggered() ), qApp, SLOT( aboutQt() ) ); } Void plaYUVerApp::createMenus() { - fileMenu = menuBar()->addMenu( tr( "&File" ) ); - fileMenu->addAction( openAct ); - fileMenu->addSeparator(); - fileMenu->addAction( saveAct ); - fileMenu->addSeparator(); - fileMenu->addAction( closeAct ); - fileMenu->addAction( exitAct ); + menuFile = menuBar()->addMenu( tr( "&File" ) ); + menuFile->addAction( actionOpen ); + menuFile->addSeparator(); + menuFile->addAction( actionSave ); + menuFile->addSeparator(); + menuFile->addAction( actionClose ); + menuFile->addAction( actionExit ); - viewMenu = new QMenu( tr( "&View" ), this ); - viewMenu->addAction( zoomInAct ); - viewMenu->addAction( zoomOutAct ); - viewMenu->addAction( normalSizeAct ); - viewMenu->addAction( zoomToFitAct ); + menuView = new QMenu( tr( "&View" ), this ); + menuView->addAction( actionZoomIn ); + menuView->addAction( actionZoomOut ); + menuView->addAction( actionNormalSize ); + menuView->addAction( actionZoomToFit ); - windowMenu = menuBar()->addMenu( tr( "&Window" ) ); + menuWindow = menuBar()->addMenu( tr( "&Window" ) ); + menuWindow->clear(); + menuWindow->addAction( actionClose ); + menuWindow->addAction( actionCloseAll ); + menuWindow->addSeparator(); + menuWindow->addAction( actionTile ); + menuWindow->addAction( actionCascade ); + menuWindow->addSeparator(); + menuWindow->addAction( actionNext ); + menuWindow->addAction( actionPrevious ); + menuWindow->addAction( actionSeparator ); updateWindowMenu(); - connect( windowMenu, SIGNAL( aboutToShow() ), this, SLOT( updateWindowMenu() ) ); + connect( menuWindow, SIGNAL( aboutToShow() ), this, SLOT( updateWindowMenu() ) ); menuBar()->addSeparator(); - helpMenu = menuBar()->addMenu( tr( "&Help" ) ); - helpMenu->addAction( aboutAct ); - helpMenu->addAction( aboutQtAct ); + menuHelp = menuBar()->addMenu( tr( "&Help" ) ); + menuHelp->addAction( actionAbout ); + menuHelp->addAction( actionAboutQt ); } Void plaYUVerApp::createToolBars() { - fileToolBar = addToolBar( tr( "File" ) ); - fileToolBar->addAction( openAct ); - fileToolBar->addAction( saveAct ); - fileToolBar->addAction( closeAct ); + toolbarFile = addToolBar( tr( "File" ) ); + toolbarFile->addAction( actionOpen ); + toolbarFile->addAction( actionSave ); + toolbarFile->addAction( actionClose ); - viewToolBar = addToolBar( tr( "Zoom" ) ); - viewToolBar->addAction( zoomInAct ); - viewToolBar->addAction( zoomOutAct ); - viewToolBar->addAction( normalSizeAct ); - viewToolBar->addAction( zoomToFitAct ); + toolbarView = addToolBar( tr( "Zoom" ) ); + toolbarView->addAction( actionZoomIn ); + toolbarView->addAction( actionZoomOut ); + toolbarView->addAction( actionNormalSize ); + toolbarView->addAction( actionZoomToFit ); + + toolbarVideo = addToolBar( tr( "Video" ) ); + toolbarVideo->addAction( actionVideoPlay ); + toolbarVideo->addAction( actionVideoPause ); + toolbarVideo->addAction( actionVideoStop ); + toolbarVideo->addAction( actionVideoBackward ); + toolbarVideo->addAction( actionVideoForward ); + toolbarVideo->addAction( actionVideoLoop ); + toolbarVideo->addAction( actionVideoLock ); + toolbarVideo->addAction( actionVideoInterlace ); + toolbarVideo->addAction( actionVideoCenter ); - videoToolBar = addToolBar( tr( "Video" ) ); - videoToolBar->addAction( actionVideoPlay ); - videoToolBar->addAction( actionVideoPause ); - videoToolBar->addAction( actionVideoStop ); - videoToolBar->addAction( actionVideoBackward ); - videoToolBar->addAction( actionVideoForward ); - videoToolBar->addAction( actionVideoLoop ); - videoToolBar->addAction( actionVideoLock ); - videoToolBar->addAction( actionVideoInterlace ); - videoToolBar->addAction( actionVideoCenter ); } Void plaYUVerApp::createStatusBar() diff --git a/source/plaYUVerApp.h b/source/plaYUVerApp.h index 9705013..0025dad 100644 --- a/source/plaYUVerApp.h +++ b/source/plaYUVerApp.h @@ -62,7 +62,7 @@ private slots: void stop(); void playEvent(); - //! View functions + //! Zoom functions void zoomIn(); void zoomOut(); /** @@ -70,7 +70,7 @@ private slots: * @param factor factor of scale. Ex: 1.2 scale the image up by 20% and * 0.8 scale the image down by 25% */ - void scaleImage( double factor = 1 ); + void scaleFrame( int ratio = 100 ); void normalSize(); void zoomToFit(); @@ -92,6 +92,11 @@ private slots: void setActiveSubWindow( QWidget *window ); private: + QMdiArea *mdiArea; + + QString m_cLastOpenPath; + QTimer *playingTimer; + Void createActions(); Void createMenus(); Void createToolBars(); @@ -101,36 +106,34 @@ private: SubWindowHandle *activeSubWindow(); QMdiSubWindow *findSubWindow( const QString &fileName ); - QString m_cLastOpenPath; + QSignalMapper *mapperZoom; + QSignalMapper *mapperWindow; - QTimer *playingTimer; - QMdiArea *mdiArea; - QSignalMapper *windowMapper; + QMenu *menuFile; + QMenu *menuView; + QMenu *menuWindow; + QMenu *menuHelp; - QMenu *fileMenu; - QMenu *viewMenu; - QMenu *windowMenu; - QMenu *helpMenu; - QToolBar *fileToolBar; - QToolBar *viewToolBar; - QToolBar *videoToolBar; + QToolBar *toolbarFile; + QToolBar *toolbarView; + QToolBar *toolbarVideo; - QAction *openAct; - QAction *saveAct; - QAction *exitAct; - QAction *closeAct; - QAction *closeAllAct; + QAction *actionOpen; + QAction *actionSave; + QAction *actionExit; + QAction *actionClose; + QAction *actionCloseAll; - QAction *zoomInAct; - QAction *zoomOutAct; - QAction *normalSizeAct; - QAction *zoomToFitAct; + QAction *actionZoomIn; + QAction *actionZoomOut; + QAction *actionNormalSize; + QAction *actionZoomToFit; - QAction *tileAct; - QAction *cascadeAct; - QAction *nextAct; - QAction *previousAct; - QAction *separatorAct; + QAction *actionTile; + QAction *actionCascade; + QAction *actionNext; + QAction *actionPrevious; + QAction *actionSeparator; // Video actions. QAction *actionVideoPlay; @@ -143,8 +146,8 @@ private: QAction *actionVideoInterlace; QAction *actionVideoCenter; - QAction *aboutAct; - QAction *aboutQtAct; + QAction *actionAbout; + QAction *actionAboutQt; };