From 2cdb9f2f38a0b20f67d024e292fe06ee4f868c77 Mon Sep 17 00:00:00 2001 From: Winnie Date: Sat, 24 Aug 2019 18:55:30 +0200 Subject: [PATCH] Extract RecentProjectsMenu class from MainWindow (#5148) * Extract RecentProjectsMenu class from MainWindow * Clean up updateRecentlyOpenedProjectsMenu * Remove m_recentlyOpenedProjectsMenu from MainWindow --- include/MainWindow.h | 4 -- include/RecentProjectsMenu.h | 17 ++++++ src/gui/CMakeLists.txt | 1 + src/gui/MainWindow.cpp | 62 ++-------------------- src/gui/menus/RecentProjectsMenu.cpp | 77 ++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 63 deletions(-) create mode 100644 include/RecentProjectsMenu.h create mode 100644 src/gui/menus/RecentProjectsMenu.cpp diff --git a/include/MainWindow.h b/include/MainWindow.h index 22a29d0e2..74e569653 100644 --- a/include/MainWindow.h +++ b/include/MainWindow.h @@ -203,8 +203,6 @@ private: QWidget * m_toolBar; QGridLayout * m_toolBarLayout; - QMenu * m_recentlyOpenedProjectsMenu; - struct keyModifiers { keyModifiers() : @@ -237,9 +235,7 @@ private: private slots: void browseHelp(); - void openRecentlyOpenedProject( QAction * _action ); void showTool( QAction * _idx ); - void updateRecentlyOpenedProjectsMenu(); void updateViewMenu( void ); void updateConfig( QAction * _who ); void onToggleMetronome(); diff --git a/include/RecentProjectsMenu.h b/include/RecentProjectsMenu.h new file mode 100644 index 000000000..b3837ee2e --- /dev/null +++ b/include/RecentProjectsMenu.h @@ -0,0 +1,17 @@ +#ifndef RECENTPROJECTSMENU_H +#define RECENTPROJECTSMENU_H + +#include + +class RecentProjectsMenu : public QMenu +{ + Q_OBJECT +public: + RecentProjectsMenu(QWidget *parent = nullptr); + +private slots: + void fillMenu(); + void openProject(QAction * _action ); +}; + +#endif // RECENTPROJECTSMENU_H diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 84dc19935..af316fddb 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -43,6 +43,7 @@ SET(LMMS_SRCS gui/editors/PianoRoll.cpp gui/editors/SongEditor.cpp + gui/menus/RecentProjectsMenu.cpp gui/menus/TemplatesMenu.cpp gui/widgets/AutomatableButton.cpp diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 4021b68e0..0914d1685 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -57,6 +57,7 @@ #include "ProjectJournal.h" #include "ProjectNotes.h" #include "ProjectRenderer.h" +#include "RecentProjectsMenu.h" #include "RemotePlugin.h" #include "SetupDialog.h" #include "SideBar.h" @@ -89,7 +90,6 @@ void disableAutoKeyAccelerators(QWidget* mainWindow) MainWindow::MainWindow() : m_workspace( NULL ), - m_recentlyOpenedProjectsMenu( NULL ), m_toolsMenu( NULL ), m_autoSaveTimer( this ), m_viewMenu( NULL ), @@ -285,13 +285,7 @@ void MainWindow::finalize() this, SLOT( openProject() ), QKeySequence::Open ); - m_recentlyOpenedProjectsMenu = project_menu->addMenu( - embed::getIconPixmap( "project_open_recent" ), - tr( "&Recently Opened Projects" ) ); - connect( m_recentlyOpenedProjectsMenu, SIGNAL( aboutToShow() ), - this, SLOT( updateRecentlyOpenedProjectsMenu() ) ); - connect( m_recentlyOpenedProjectsMenu, SIGNAL( triggered( QAction * ) ), - this, SLOT( openRecentlyOpenedProject( QAction * ) ) ); + project_menu->addMenu(new RecentProjectsMenu(this)); project_menu->addAction( embed::getIconPixmap( "project_save" ), tr( "&Save" ), @@ -439,7 +433,7 @@ void MainWindow::finalize() embed::getIconPixmap( "project_open_recent" ), tr( "Recently opened projects" ), this, SLOT( emptySlot() ), m_toolBar ); - project_open_recent->setMenu( m_recentlyOpenedProjectsMenu ); + project_open_recent->setMenu( new RecentProjectsMenu(this) ); project_open_recent->setPopupMode( ToolButton::InstantPopup ); ToolButton * project_save = new ToolButton( @@ -829,56 +823,6 @@ void MainWindow::openProject() -void MainWindow::updateRecentlyOpenedProjectsMenu() -{ - m_recentlyOpenedProjectsMenu->clear(); - QStringList rup = ConfigManager::inst()->recentlyOpenedProjects(); - -// The file history goes 50 deep but we only show the 15 -// most recent ones that we can open and omit .mpt files. - int shownInMenu = 0; - for( QStringList::iterator it = rup.begin(); it != rup.end(); ++it ) - { - QFileInfo recentFile( *it ); - if ( recentFile.exists() && - *it != ConfigManager::inst()->recoveryFile() ) - { - if( recentFile.suffix().toLower() == "mpt" ) - { - continue; - } - - m_recentlyOpenedProjectsMenu->addAction( - embed::getIconPixmap( "project_file" ), it->replace("&", "&&") ); -#ifdef LMMS_BUILD_APPLE - m_recentlyOpenedProjectsMenu->actions().last()->setIconVisibleInMenu(false); // QTBUG-44565 workaround - m_recentlyOpenedProjectsMenu->actions().last()->setIconVisibleInMenu(true); -#endif - shownInMenu++; - if( shownInMenu >= 15 ) - { - return; - } - } - } -} - - - -void MainWindow::openRecentlyOpenedProject( QAction * _action ) -{ - if ( mayChangeProject(true) ) - { - const QString f = _action->text().replace("&&", "&"); - setCursor( Qt::WaitCursor ); - Engine::getSong()->loadProject( f ); - setCursor( Qt::ArrowCursor ); - } -} - - - - bool MainWindow::saveProject() { if( Engine::getSong()->projectFileName() == "" ) diff --git a/src/gui/menus/RecentProjectsMenu.cpp b/src/gui/menus/RecentProjectsMenu.cpp new file mode 100644 index 000000000..7c09c14aa --- /dev/null +++ b/src/gui/menus/RecentProjectsMenu.cpp @@ -0,0 +1,77 @@ +#include "RecentProjectsMenu.h" + +#include + +#include "ConfigManager.h" +#include "Engine.h" +#include "Song.h" + +#include "embed.h" +#include "GuiApplication.h" +#include "MainWindow.h" + +RecentProjectsMenu::RecentProjectsMenu(QWidget *parent) : + QMenu(tr( "&Recently Opened Projects" ), parent) +{ + setIcon(embed::getIconPixmap( "project_open_recent" )); + + connect( this, SIGNAL( aboutToShow() ), + this, SLOT(fillMenu() ) ); + connect( this, SIGNAL( triggered( QAction * ) ), + this, SLOT(openProject(QAction * ) ) ); +} + + + + +void RecentProjectsMenu::fillMenu() +{ + clear(); + QStringList rup = ConfigManager::inst()->recentlyOpenedProjects(); + + auto projectFileIcon = embed::getIconPixmap( "project_file" ); + + // The file history goes 50 deep but we only show the 15 + // most recent ones that we can open and omit .mpt files. + int shownInMenu = 0; + for(QString& fileName : rup) + { + QFileInfo recentFile(fileName); + if (!recentFile.exists() || + fileName == ConfigManager::inst()->recoveryFile() ) + { + continue; + } + + if( recentFile.suffix().toLower() == "mpt" ) + { + continue; + } + + addAction(projectFileIcon, fileName.replace("&", "&&") ); +#ifdef LMMS_BUILD_APPLE + actions().last()->setIconVisibleInMenu(false); // QTBUG-44565 workaround + actions().last()->setIconVisibleInMenu(true); +#endif + + shownInMenu++; + if( shownInMenu >= 15 ) + { + break; + } + } +} + + + + +void RecentProjectsMenu::openProject(QAction * _action ) +{ + if ( gui->mainWindow()->mayChangeProject(true) ) + { + const QString f = _action->text().replace("&&", "&"); + setCursor( Qt::WaitCursor ); + Engine::getSong()->loadProject( f ); + setCursor( Qt::ArrowCursor ); + } +}