-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract TemplatesMenu class from MainWindow
- Loading branch information
1 parent
db200fb
commit ae95380
Showing
5 changed files
with
97 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef TEMPLATESMENU_H | ||
#define TEMPLATESMENU_H | ||
|
||
#include <QMenu> | ||
|
||
class TemplatesMenu : public QMenu | ||
{ | ||
Q_OBJECT | ||
public: | ||
TemplatesMenu(QWidget *parent = nullptr); | ||
virtual ~TemplatesMenu() = default; | ||
|
||
private slots: | ||
void createNewProjectFromTemplate( QAction * _idx ); | ||
void fillTemplatesMenu(); | ||
|
||
private: | ||
int m_custom_templates_count; | ||
}; | ||
|
||
#endif // TEMPLATESMENU_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <QDir> | ||
|
||
#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) | ||
{ | ||
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 = this->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 TemplatesMenu::fillTemplatesMenu() | ||
{ | ||
this->clear(); | ||
|
||
auto addTemplatesFromDir = [this]( QDir dir ) { | ||
QStringList templates = dir.entryList( QStringList( "*.mpt" ), | ||
QDir::Files | QDir::Readable ); | ||
|
||
if ( templates.size() && ! this->actions().isEmpty() ) | ||
{ | ||
this->addSeparator(); | ||
} | ||
|
||
for( QStringList::iterator it = templates.begin(); | ||
it != templates.end(); ++it ) | ||
{ | ||
this->addAction( | ||
embed::getIconPixmap( "project_file" ), | ||
( *it ).left( ( *it ).length() - 4 ).replace("&", "&&") ); | ||
#ifdef LMMS_BUILD_APPLE | ||
this->actions().last()->setIconVisibleInMenu(false); // QTBUG-44565 workaround | ||
this->actions().last()->setIconVisibleInMenu(true); | ||
#endif | ||
} | ||
|
||
return templates.size(); | ||
}; | ||
|
||
m_custom_templates_count = addTemplatesFromDir( ConfigManager::inst()->userTemplateDir() ); | ||
addTemplatesFromDir( ConfigManager::inst()->factoryProjectsDir() + "templates" ); | ||
} |