Skip to content

Commit

Permalink
Extract TemplatesMenu class from MainWindow (LMMS#5125)
Browse files Browse the repository at this point in the history
  • Loading branch information
winniehell authored and artur-twardowski committed Dec 22, 2019
1 parent abf3530 commit 09e59d0
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 51 deletions.
14 changes: 9 additions & 5 deletions include/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ private slots:
void onExportProjectMidi();

protected:
void closeEvent( QCloseEvent * _ce ) override;
void focusOutEvent( QFocusEvent * _fe ) override;
void keyPressEvent( QKeyEvent * _ke ) override;
void keyReleaseEvent( QKeyEvent * _ke ) override;
void timerEvent( QTimerEvent * _ev ) override;
virtual void closeEvent( QCloseEvent * _ce );
virtual void focusOutEvent( QFocusEvent * _fe );
virtual void keyPressEvent( QKeyEvent * _ke );
virtual void keyReleaseEvent( QKeyEvent * _ke );
virtual void timerEvent( QTimerEvent * _ev );


private:
Expand All @@ -203,6 +203,8 @@ private slots:
QWidget * m_toolBar;
QGridLayout * m_toolBarLayout;

QMenu * m_recentlyOpenedProjectsMenu;

struct keyModifiers
{
keyModifiers() :
Expand Down Expand Up @@ -235,7 +237,9 @@ private slots:

private slots:
void browseHelp();
void openRecentlyOpenedProject( QAction * _action );
void showTool( QAction * _idx );
void updateRecentlyOpenedProjectsMenu();
void updateViewMenu( void );
void updateConfig( QAction * _who );
void onToggleMetronome();
Expand Down
6 changes: 4 additions & 2 deletions include/TemplatesMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class TemplatesMenu : public QMenu
virtual ~TemplatesMenu() = default;

private slots:
static void createNewProjectFromTemplate(QAction * _action);
void createNewProjectFromTemplate( QAction * _idx );
void fillTemplatesMenu();
void addTemplatesFromDir( const QDir& dir );
int addTemplatesFromDir( const QDir& dir );

private:
int m_customTemplatesCount;
};

#endif // TEMPLATESMENU_H
1 change: 0 additions & 1 deletion src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ SET(LMMS_SRCS
gui/editors/PianoRoll.cpp
gui/editors/SongEditor.cpp

gui/menus/RecentProjectsMenu.cpp
gui/menus/TemplatesMenu.cpp

gui/widgets/AutomatableButton.cpp
Expand Down
90 changes: 70 additions & 20 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
#include "ProjectJournal.h"
#include "ProjectNotes.h"
#include "ProjectRenderer.h"
#include "RecentProjectsMenu.h"
#include "RemotePlugin.h"
#include "SetupDialog.h"
#include "SideBar.h"
Expand Down Expand Up @@ -90,6 +89,7 @@ void disableAutoKeyAccelerators(QWidget* mainWindow)

MainWindow::MainWindow() :
m_workspace( NULL ),
m_recentlyOpenedProjectsMenu( NULL ),
m_toolsMenu( NULL ),
m_autoSaveTimer( this ),
m_viewMenu( NULL ),
Expand Down Expand Up @@ -174,16 +174,16 @@ MainWindow::MainWindow() :
m_workspace = new QMdiArea( splitter );

// Load background
emit initProgress(tr("Loading background picture"));
QString backgroundPicFile = ConfigManager::inst()->backgroundPicFile();
QImage backgroundPic;
if( !backgroundPicFile.isEmpty() )
emit initProgress(tr("Loading background artwork"));
QString bgArtwork = ConfigManager::inst()->backgroundArtwork();
QImage bgImage;
if( !bgArtwork.isEmpty() )
{
backgroundPic = QImage( backgroundPicFile );
bgImage = QImage( bgArtwork );
}
if( !backgroundPicFile.isNull() )
if( !bgImage.isNull() )
{
m_workspace->setBackground( backgroundPic );
m_workspace->setBackground( bgImage );
}
else
{
Expand Down Expand Up @@ -285,13 +285,19 @@ void MainWindow::finalize()
this, SLOT( openProject() ),
QKeySequence::Open );

project_menu->addMenu(new RecentProjectsMenu(this));
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->addAction( embed::getIconPixmap( "project_save" ),
tr( "&Save" ),
this, SLOT( saveProject() ),
QKeySequence::Save );
project_menu->addAction( embed::getIconPixmap( "project_save" ),
project_menu->addAction( embed::getIconPixmap( "project_saveas" ),
tr( "Save &As..." ),
this, SLOT( saveProjectAs() ),
Qt::CTRL + Qt::SHIFT + Qt::Key_S );
Expand All @@ -300,9 +306,8 @@ void MainWindow::finalize()
this, SLOT( saveProjectAsNewVersion() ),
Qt::CTRL + Qt::ALT + Qt::Key_S );

project_menu->addAction( embed::getIconPixmap( "project_save" ),
tr( "Save as default template" ),
this, SLOT( saveProjectAsDefaultTemplate() ) );
project_menu->addAction( tr( "Save as default template" ),
this, SLOT( saveProjectAsDefaultTemplate() ) );

project_menu->addSeparator();
project_menu->addAction( embed::getIconPixmap( "project_import" ),
Expand Down Expand Up @@ -434,7 +439,7 @@ void MainWindow::finalize()
embed::getIconPixmap( "project_open_recent" ),
tr( "Recently opened projects" ),
this, SLOT( emptySlot() ), m_toolBar );
project_open_recent->setMenu( new RecentProjectsMenu(this) );
project_open_recent->setMenu( m_recentlyOpenedProjectsMenu );
project_open_recent->setPopupMode( ToolButton::InstantPopup );

ToolButton * project_save = new ToolButton(
Expand Down Expand Up @@ -824,6 +829,56 @@ 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() == "" )
Expand Down Expand Up @@ -1518,12 +1573,7 @@ void MainWindow::exportProject(bool multiExport)
// Get first extension from selected dropdown.
// i.e. ".wav" from "WAV-File (*.wav), Dummy-File (*.dum)"
suffix = efd.selectedNameFilter().mid( stx + 2, etx - stx - 2 ).split( " " )[0].trimmed();

Qt::CaseSensitivity cs = Qt::CaseSensitive;
#if defined(LMMS_BUILD_APPLE) || defined(LMMS_BUILD_WIN32)
cs = Qt::CaseInsensitive;
#endif
exportFileName.remove( "." + suffix, cs );
exportFileName.remove( "." + suffix, Qt::CaseInsensitive );
if ( efd.selectedFiles()[0].endsWith( suffix ) )
{
if( VersionedSaveDialog::fileExistsQuery( exportFileName + suffix,
Expand Down
50 changes: 27 additions & 23 deletions src/gui/menus/TemplatesMenu.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#include "TemplatesMenu.h"

#include "GuiApplication.h"
#include "ConfigManager.h"
#include "Engine.h"
#include "Song.h"

#include "embed.h"
#include "GuiApplication.h"
#include "MainWindow.h"
#include "Song.h"

TemplatesMenu::TemplatesMenu(QWidget *parent) :
QMenu(tr("New from template"), parent)
QMenu(tr("New from template"), parent),
m_customTemplatesCount(0)
{
setIcon(embed::getIconPixmap("project_new"));

connect( this, SIGNAL( aboutToShow() ), SLOT( fillTemplatesMenu() ) );
connect( this, SIGNAL( triggered( QAction * ) ),
SLOT( createNewProjectFromTemplate( QAction * ) ) );
Expand All @@ -21,12 +18,18 @@ TemplatesMenu::TemplatesMenu(QWidget *parent) :



void TemplatesMenu::createNewProjectFromTemplate(QAction * _action)
void TemplatesMenu::createNewProjectFromTemplate( QAction * _idx )
{
if( gui->mainWindow()->mayChangeProject(true) )
{
const QString& templateFilePath = _action->data().toString();
Engine::getSong()->createNewProjectFromTemplate(templateFilePath);
int indexOfTemplate = actions().indexOf( _idx );
bool isFactoryTemplate = indexOfTemplate >= m_customTemplatesCount;
QString dirBase = isFactoryTemplate ?
ConfigManager::inst()->factoryTemplatesDir() :
ConfigManager::inst()->userTemplateDir();

const QString f = dirBase + _idx->text().replace("&&", "&") + ".mpt";
Engine::getSong()->createNewProjectFromTemplate(f);
}
}

Expand All @@ -38,32 +41,33 @@ void TemplatesMenu::fillTemplatesMenu()
{
clear();

addTemplatesFromDir(ConfigManager::inst()->userTemplateDir());
addTemplatesFromDir(ConfigManager::inst()->factoryProjectsDir() + "templates");
m_customTemplatesCount = addTemplatesFromDir(ConfigManager::inst()->userTemplateDir() );
addTemplatesFromDir( ConfigManager::inst()->factoryProjectsDir() + "templates" );
}




void TemplatesMenu::addTemplatesFromDir( const QDir& dir ) {
QFileInfoList templates = dir.entryInfoList( QStringList( "*.mpt" ),
int TemplatesMenu::addTemplatesFromDir( const QDir& dir ) {
QStringList templates = dir.entryList( QStringList( "*.mpt" ),
QDir::Files | QDir::Readable );

if (!templates.empty() && !actions().isEmpty())
if ( templates.size() && ! actions().isEmpty() )
{
addSeparator();
}

auto projectFileIcon = embed::getIconPixmap( "project_file" );

for(const QFileInfo& templateFile : templates)
for( QStringList::iterator it = templates.begin();
it != templates.end(); ++it )
{
auto action = addAction(projectFileIcon,
templateFile.completeBaseName().replace("&", "&&"));
action->setData(templateFile.absoluteFilePath());
addAction(
embed::getIconPixmap( "project_file" ),
( *it ).left( ( *it ).length() - 4 ).replace("&", "&&") );
#ifdef LMMS_BUILD_APPLE
action->setIconVisibleInMenu(false); // QTBUG-44565 workaround
action->setIconVisibleInMenu(true);
actions().last()->setIconVisibleInMenu(false); // QTBUG-44565 workaround
actions().last()->setIconVisibleInMenu(true);
#endif
}

return templates.size();
}

0 comments on commit 09e59d0

Please sign in to comment.