From d60b1e5026e62ecd3eb9b08a24d4f3b834324677 Mon Sep 17 00:00:00 2001 From: ruglory Date: Tue, 10 Mar 2009 19:34:05 +0000 Subject: [PATCH] Menu items are enabled/disabled on events git-svn-id: https://svn.code.sf.net/p/qlscribe/svn/trunk@124 cac9541e-1b8d-4bfa-827e-589bba606050 --- src/mainwindow.cpp | 96 ++++++++++++++++++++++++++++++++-------------- src/mainwindow.h | 11 ++++++ 2 files changed, 78 insertions(+), 29 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3e03178..9e12f2a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -37,6 +37,8 @@ #include #include #include +#include +#include MainWindow::MainWindow( bool enablePrint ) @@ -74,54 +76,56 @@ MainWindow::MainWindow( bool enablePrint ) this, SLOT(onMenuOpen()) ); - m_menuFile->addAction( tr( "Save", "Menu item \"Save\"" ), - this, - SLOT(onMenuSave()), - QKeySequence( Qt::CTRL + Qt::Key_S ) ); + m_actionSave = m_menuFile->addAction( tr( "Save", "Menu item \"Save\"" ), + this, + SLOT(onMenuSave()), + QKeySequence( Qt::CTRL + Qt::Key_S ) ); - m_menuFile->addAction( tr( "Save as...", "Menu item \"Save as\"" ), - this, - SLOT(onMenuSaveAs()) ); + m_actionSaveAs = m_menuFile->addAction( tr( "Save as...", "Menu item \"Save as\"" ), + this, + SLOT(onMenuSaveAs()) ); m_menuFile->addSeparator(); - m_menuFile->addAction( tr( "Label properties...", "Menu item \"Label propeties...\"" ), - this, - SLOT(onMenuProperties()) ); + m_actionLabelProperties = + m_menuFile->addAction( tr( "Label properties...", "Menu item \"Label propeties...\"" ), + this, + SLOT(onMenuProperties()) ); m_menuFile->addSeparator(); - m_menuFile->addAction( tr( "Print preview...", "Menu item \"Print preview\"" ), - this, - SLOT(onMenuPrintPreview()) ); + m_actionPrintPreview = + m_menuFile->addAction( tr( "Print preview...", "Menu item \"Print preview\"" ), + this, + SLOT(onMenuPrintPreview()) ); - QAction *printAction = + m_actionPrint = m_menuFile->addAction( tr( "Print...", "Menu item \"Print\"" ), this, SLOT(onMenuPrint()) ); - if( !enablePrint ) - printAction->setEnabled( false ); m_menuFile->addAction( tr( "Exit", "Menu item \"Exit\"" ), this, - SLOT(close())); + SLOT(close()), + QKeySequence( Qt::CTRL + Qt::Key_X ) ); + m_menuEdit = menuBar()->addMenu( tr( "Edit", "Menu item \"Edit\"" ) ); - m_menuEdit->addAction( tr( "Copy", "Menu item \"Copy\"" ), - this, - SLOT(onMenuCopy()), - QKeySequence( Qt::CTRL + Qt::Key_C ) ); + m_actionCopy = m_menuEdit->addAction( tr( "Copy", "Menu item \"Copy\"" ), + this, + SLOT(onMenuCopy()), + QKeySequence( Qt::CTRL + Qt::Key_C ) ); - m_menuEdit->addAction( tr( "Cut", "Menu item \"Cut\"" ), - this, - SLOT(onMenuCut()), - QKeySequence( Qt::CTRL + Qt::Key_X ) ); + m_actionCut = m_menuEdit->addAction( tr( "Cut", "Menu item \"Cut\"" ), + this, + SLOT(onMenuCut()), + QKeySequence( Qt::CTRL + Qt::Key_X ) ); - m_menuEdit->addAction( tr( "Paste", "Menu item \"Paste\"" ), - this, - SLOT(onMenuPaste()), - QKeySequence( Qt::CTRL + Qt::Key_V ) ); + m_actionPaste = m_menuEdit->addAction( tr( "Paste", "Menu item \"Paste\"" ), + this, + SLOT(onMenuPaste()), + QKeySequence( Qt::CTRL + Qt::Key_V ) ); m_menuInsert = m_menuEdit->addMenu( tr( "Insert", "Menu item \"Insert\"" ) ); @@ -144,6 +148,13 @@ MainWindow::MainWindow( bool enablePrint ) connect( action, SIGNAL(triggered()), m_insertMapper, SLOT(map()) ); } connect( m_insertMapper, SIGNAL(mapped(int)), this, SLOT(onMenuInsert(int)) ); + + connect( QApplication::clipboard(), SIGNAL(changed(QClipboard::Mode)), + this, SLOT(updateMenu()) ); + connect( m_mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), + this, SLOT(updateMenu()) ); + + updateMenu(); } MainWindow::~MainWindow() @@ -236,6 +247,7 @@ void MainWindow::onMenuNewLabel( int mode ) scene->setLabelMode( LabelMode( mode ) ); newView->setScene( scene ); scene->setName(); + connect( scene, SIGNAL(selectionChanged()), this, SLOT(updateMenu()) ); QMdiSubWindow *subWindow = m_mdiArea->addSubWindow( newView ); subWindow->show(); @@ -298,6 +310,7 @@ void MainWindow::onMenuOpen() delete newView; return; } + connect( scene, SIGNAL(selectionChanged()), this, SLOT(updateMenu()) ); QMdiSubWindow *subWindow = m_mdiArea->addSubWindow( newView ); subWindow->show(); @@ -399,3 +412,28 @@ void MainWindow::onMenuQtAbout() { QMessageBox::aboutQt( this ); } + +void MainWindow::updateMenu() +{ + QCDScene *scene = getScene( m_mdiArea ); + + m_actionSave->setEnabled( scene ); + m_actionSaveAs->setEnabled( scene ); + m_actionLabelProperties->setEnabled( scene ); + m_actionPrint->setEnabled( scene ); + m_actionPrintPreview->setEnabled( scene ); + m_menuInsert->setEnabled( scene ); + + if( !scene ) { + m_actionCopy->setEnabled( false ); + m_actionCut->setEnabled( false ); + m_actionPaste->setEnabled( false ); + } else { + const QMimeData *mdata = QApplication::clipboard()->mimeData(); + m_actionPaste->setEnabled( mdata && mdata->hasFormat( itemMimeType ) ); + + bool selected = scene->selectedItems().count(); + m_actionCopy->setEnabled( selected ); + m_actionCut->setEnabled( selected ); + } +} diff --git a/src/mainwindow.h b/src/mainwindow.h index 5d3b908..56e54db 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -55,6 +55,8 @@ private slots: void onMenuAbout(); void onMenuQtAbout(); + void updateMenu(); + private: bool saveSceneAs( QCDScene *scene ); @@ -65,6 +67,15 @@ private: QMenu *m_menuInsert; QMenu *m_menuHelp; + QAction *m_actionSave; + QAction *m_actionSaveAs; + QAction *m_actionLabelProperties; + QAction *m_actionPrint; + QAction *m_actionPrintPreview; + QAction *m_actionCopy; + QAction *m_actionCut; + QAction *m_actionPaste; + QSignalMapper *m_insertMapper; QSignalMapper *m_newLabelMapper; };