Skip to content

Recommend unique filename for new/default score #21029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/project/internal/notationproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static void setupScoreMetaTags(mu::engraving::MasterScore* masterScore, const Pr
}
}

static QString scoreDefaultTitle()
QString NotationProject::scoreDefaultTitle()
{
return qtrc("project", "Untitled score");
}
Expand Down
2 changes: 2 additions & 0 deletions src/project/internal/notationproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class NotationProject : public INotationProject, public async::Asyncable
public:
~NotationProject() override;

static QString scoreDefaultTitle();

Ret load(const io::path_t& path, const io::path_t& stylePath = io::path_t(), bool forceMode = false,
const std::string& format = "") override;
Ret createNew(const ProjectCreateOptions& projectInfo) override;
Expand Down
24 changes: 23 additions & 1 deletion src/project/internal/projectconfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "settings.h"

#include "engraving/infrastructure/mscio.h"
#include "project/internal/notationproject.h"

#include "log.h"

Expand Down Expand Up @@ -288,6 +289,7 @@ io::path_t ProjectConfiguration::defaultSavingFilePath(INotationProjectPtr proje
io::path_t folderPath;
io::path_t filename;
std::string theSuffix = suffix;
std::string theFilenameAddition = filenameAddition;

io::path_t projectPath = project->path();
bool isLocalProject = !project->isCloudProject();
Expand Down Expand Up @@ -334,11 +336,31 @@ io::path_t ProjectConfiguration::defaultSavingFilePath(INotationProjectPtr proje
theSuffix = DEFAULT_FILE_SUFFIX;
}

if (project->isNewlyCreated() && filename.toQString() == NotationProject::scoreDefaultTitle()) {
theFilenameAddition += uniqueFileNameAddition(filename + theFilenameAddition, folderPath, theSuffix);
}

return folderPath
.appendingComponent(filename + filenameAddition)
.appendingComponent(filename + theFilenameAddition)
.appendingSuffix(theSuffix);
}

std::string ProjectConfiguration::uniqueFileNameAddition(const io::path_t& filename, const io::path_t& folderPath,
const std::string& suffix) const
{
// Return a filename addition which would make filename unique if it wasn't already
const std::string theSuffix = suffix.empty() ? DEFAULT_FILE_SUFFIX : suffix;
std::string addition;
int id = 1;
while (fileSystem()->exists(folderPath
.appendingComponent(filename + addition)
.appendingSuffix(theSuffix))) {
addition = " (" + std::to_string(id) + ")";
id++;
}
return addition;
}

SaveLocationType ProjectConfiguration::lastUsedSaveLocationType() const
{
return settings()->value(LAST_USED_SAVE_LOCATION_TYPE).toEnum<SaveLocationType>();
Expand Down
1 change: 1 addition & 0 deletions src/project/internal/projectconfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class ProjectConfiguration : public IProjectConfiguration
io::path_t appTemplatesPath() const;
io::path_t legacyCloudProjectsPath() const;
io::path_t cloudProjectsPath() const;
std::string uniqueFileNameAddition(const io::path_t& filename, const io::path_t& folderPath, const std::string& suffix) const;

async::Channel<io::path_t> m_userTemplatesPathChanged;
async::Channel<io::path_t> m_userScoresPathChanged;
Expand Down