Extract TemplatesMenu class from MainWindow (#5125)
This commit is contained in:
@@ -148,7 +148,6 @@ public slots:
|
||||
|
||||
void emptySlot();
|
||||
void createNewProject();
|
||||
void createNewProjectFromTemplate( QAction * _idx );
|
||||
void openProject();
|
||||
bool saveProject();
|
||||
bool saveProjectAs();
|
||||
@@ -204,9 +203,7 @@ private:
|
||||
QWidget * m_toolBar;
|
||||
QGridLayout * m_toolBarLayout;
|
||||
|
||||
QMenu * m_templatesMenu;
|
||||
QMenu * m_recentlyOpenedProjectsMenu;
|
||||
int m_custom_templates_count;
|
||||
|
||||
struct keyModifiers
|
||||
{
|
||||
@@ -240,7 +237,6 @@ private:
|
||||
|
||||
private slots:
|
||||
void browseHelp();
|
||||
void fillTemplatesMenu();
|
||||
void openRecentlyOpenedProject( QAction * _action );
|
||||
void showTool( QAction * _idx );
|
||||
void updateRecentlyOpenedProjectsMenu();
|
||||
|
||||
23
include/TemplatesMenu.h
Normal file
23
include/TemplatesMenu.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef TEMPLATESMENU_H
|
||||
#define TEMPLATESMENU_H
|
||||
|
||||
#include <QDir>
|
||||
#include <QMenu>
|
||||
|
||||
class TemplatesMenu : public QMenu
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
TemplatesMenu(QWidget *parent = nullptr);
|
||||
virtual ~TemplatesMenu() = default;
|
||||
|
||||
private slots:
|
||||
void createNewProjectFromTemplate( QAction * _idx );
|
||||
void fillTemplatesMenu();
|
||||
int addTemplatesFromDir( const QDir& dir );
|
||||
|
||||
private:
|
||||
int m_customTemplatesCount;
|
||||
};
|
||||
|
||||
#endif // TEMPLATESMENU_H
|
||||
@@ -43,6 +43,8 @@ SET(LMMS_SRCS
|
||||
gui/editors/PianoRoll.cpp
|
||||
gui/editors/SongEditor.cpp
|
||||
|
||||
gui/menus/TemplatesMenu.cpp
|
||||
|
||||
gui/widgets/AutomatableButton.cpp
|
||||
gui/widgets/AutomatableSlider.cpp
|
||||
gui/widgets/CaptionMenu.cpp
|
||||
|
||||
@@ -61,6 +61,7 @@
|
||||
#include "SetupDialog.h"
|
||||
#include "SideBar.h"
|
||||
#include "SongEditor.h"
|
||||
#include "TemplatesMenu.h"
|
||||
#include "TextFloat.h"
|
||||
#include "TimeLineWidget.h"
|
||||
#include "ToolButton.h"
|
||||
@@ -88,7 +89,6 @@ void disableAutoKeyAccelerators(QWidget* mainWindow)
|
||||
|
||||
MainWindow::MainWindow() :
|
||||
m_workspace( NULL ),
|
||||
m_templatesMenu( NULL ),
|
||||
m_recentlyOpenedProjectsMenu( NULL ),
|
||||
m_toolsMenu( NULL ),
|
||||
m_autoSaveTimer( this ),
|
||||
@@ -277,12 +277,8 @@ void MainWindow::finalize()
|
||||
this, SLOT( createNewProject() ),
|
||||
QKeySequence::New );
|
||||
|
||||
m_templatesMenu = new QMenu( tr("New from template"), this );
|
||||
connect( m_templatesMenu, SIGNAL( aboutToShow() ), SLOT( fillTemplatesMenu() ) );
|
||||
connect( m_templatesMenu, SIGNAL( triggered( QAction * ) ),
|
||||
SLOT( createNewProjectFromTemplate( QAction * ) ) );
|
||||
|
||||
project_menu->addMenu(m_templatesMenu);
|
||||
auto templates_menu = new TemplatesMenu( this );
|
||||
project_menu->addMenu(templates_menu);
|
||||
|
||||
project_menu->addAction( embed::getIconPixmap( "project_open" ),
|
||||
tr( "&Open..." ),
|
||||
@@ -429,7 +425,7 @@ void MainWindow::finalize()
|
||||
tr( "Create new project from template" ),
|
||||
this, SLOT( emptySlot() ),
|
||||
m_toolBar );
|
||||
project_new_from_template->setMenu( m_templatesMenu );
|
||||
project_new_from_template->setMenu( templates_menu );
|
||||
project_new_from_template->setPopupMode( ToolButton::InstantPopup );
|
||||
|
||||
ToolButton * project_open = new ToolButton(
|
||||
@@ -809,24 +805,6 @@ void MainWindow::createNewProject()
|
||||
|
||||
|
||||
|
||||
void MainWindow::createNewProjectFromTemplate( QAction * _idx )
|
||||
{
|
||||
if( m_templatesMenu && mayChangeProject(true) )
|
||||
{
|
||||
int indexOfTemplate = m_templatesMenu->actions().indexOf( _idx );
|
||||
bool isFactoryTemplate = indexOfTemplate >= m_custom_templates_count;
|
||||
QString dirBase = isFactoryTemplate ?
|
||||
ConfigManager::inst()->factoryTemplatesDir() :
|
||||
ConfigManager::inst()->userTemplateDir();
|
||||
|
||||
const QString f = dirBase + _idx->text().replace("&&", "&") + ".mpt";
|
||||
Engine::getSong()->createNewProjectFromTemplate(f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void MainWindow::openProject()
|
||||
{
|
||||
if( mayChangeProject(false) )
|
||||
@@ -1448,40 +1426,6 @@ void MainWindow::timerEvent( QTimerEvent * _te)
|
||||
|
||||
|
||||
|
||||
void MainWindow::fillTemplatesMenu()
|
||||
{
|
||||
m_templatesMenu->clear();
|
||||
|
||||
auto addTemplatesFromDir = [this]( QDir dir ) {
|
||||
QStringList templates = dir.entryList( QStringList( "*.mpt" ),
|
||||
QDir::Files | QDir::Readable );
|
||||
|
||||
if ( templates.size() && ! m_templatesMenu->actions().isEmpty() )
|
||||
{
|
||||
m_templatesMenu->addSeparator();
|
||||
}
|
||||
|
||||
for( QStringList::iterator it = templates.begin();
|
||||
it != templates.end(); ++it )
|
||||
{
|
||||
m_templatesMenu->addAction(
|
||||
embed::getIconPixmap( "project_file" ),
|
||||
( *it ).left( ( *it ).length() - 4 ).replace("&", "&&") );
|
||||
#ifdef LMMS_BUILD_APPLE
|
||||
m_templatesMenu->actions().last()->setIconVisibleInMenu(false); // QTBUG-44565 workaround
|
||||
m_templatesMenu->actions().last()->setIconVisibleInMenu(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
return templates.size();
|
||||
};
|
||||
|
||||
m_custom_templates_count = addTemplatesFromDir( ConfigManager::inst()->userTemplateDir() );
|
||||
addTemplatesFromDir( ConfigManager::inst()->factoryProjectsDir() + "templates" );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void MainWindow::showTool( QAction * _idx )
|
||||
{
|
||||
|
||||
73
src/gui/menus/TemplatesMenu.cpp
Normal file
73
src/gui/menus/TemplatesMenu.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#include "TemplatesMenu.h"
|
||||
#include "GuiApplication.h"
|
||||
#include "ConfigManager.h"
|
||||
#include "Engine.h"
|
||||
#include "embed.h"
|
||||
#include "MainWindow.h"
|
||||
#include "Song.h"
|
||||
|
||||
TemplatesMenu::TemplatesMenu(QWidget *parent) :
|
||||
QMenu(tr("New from template"), parent),
|
||||
m_customTemplatesCount(0)
|
||||
{
|
||||
connect( this, SIGNAL( aboutToShow() ), SLOT( fillTemplatesMenu() ) );
|
||||
connect( this, SIGNAL( triggered( QAction * ) ),
|
||||
SLOT( createNewProjectFromTemplate( QAction * ) ) );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void TemplatesMenu::createNewProjectFromTemplate( QAction * _idx )
|
||||
{
|
||||
if( gui->mainWindow()->mayChangeProject(true) )
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void TemplatesMenu::fillTemplatesMenu()
|
||||
{
|
||||
clear();
|
||||
|
||||
m_customTemplatesCount = addTemplatesFromDir(ConfigManager::inst()->userTemplateDir() );
|
||||
addTemplatesFromDir( ConfigManager::inst()->factoryProjectsDir() + "templates" );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int TemplatesMenu::addTemplatesFromDir( const QDir& dir ) {
|
||||
QStringList templates = dir.entryList( QStringList( "*.mpt" ),
|
||||
QDir::Files | QDir::Readable );
|
||||
|
||||
if ( templates.size() && ! actions().isEmpty() )
|
||||
{
|
||||
addSeparator();
|
||||
}
|
||||
|
||||
for( QStringList::iterator it = templates.begin();
|
||||
it != templates.end(); ++it )
|
||||
{
|
||||
addAction(
|
||||
embed::getIconPixmap( "project_file" ),
|
||||
( *it ).left( ( *it ).length() - 4 ).replace("&", "&&") );
|
||||
#ifdef LMMS_BUILD_APPLE
|
||||
actions().last()->setIconVisibleInMenu(false); // QTBUG-44565 workaround
|
||||
actions().last()->setIconVisibleInMenu(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
return templates.size();
|
||||
}
|
||||
Reference in New Issue
Block a user