Extract RecentProjectsMenu class from MainWindow (#5148)

* Extract RecentProjectsMenu class from MainWindow
* Clean up updateRecentlyOpenedProjectsMenu
* Remove m_recentlyOpenedProjectsMenu from MainWindow
This commit is contained in:
Winnie
2019-08-24 18:55:30 +02:00
committed by Lukas W
parent a863830795
commit 2cdb9f2f38
5 changed files with 98 additions and 63 deletions

View File

@@ -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();

View File

@@ -0,0 +1,17 @@
#ifndef RECENTPROJECTSMENU_H
#define RECENTPROJECTSMENU_H
#include <QMenu>
class RecentProjectsMenu : public QMenu
{
Q_OBJECT
public:
RecentProjectsMenu(QWidget *parent = nullptr);
private slots:
void fillMenu();
void openProject(QAction * _action );
};
#endif // RECENTPROJECTSMENU_H

View File

@@ -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

View File

@@ -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() == "" )

View File

@@ -0,0 +1,77 @@
#include "RecentProjectsMenu.h"
#include <QFileInfo>
#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 );
}
}