forked from qt-creator/qt-creator
QmlDesigner: Use UniqueName util in the assets view
Change-Id: I1e27cb55fd1085f36ba8b6b38829baef49040c29 Reviewed-by: Shrief Gabr <shrief.gabr@qt.io> Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
This commit is contained in:
@@ -3,13 +3,16 @@
|
|||||||
|
|
||||||
#include "assetslibrarymodel.h"
|
#include "assetslibrarymodel.h"
|
||||||
|
|
||||||
#include <asset.h>
|
|
||||||
#include <modelnodeoperations.h>
|
#include <modelnodeoperations.h>
|
||||||
#include <qmldesignerplugin.h>
|
#include <qmldesignerplugin.h>
|
||||||
|
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
|
#include <utils/asset.h>
|
||||||
|
#include <utils/filepath.h>
|
||||||
#include <utils/filesystemwatcher.h>
|
#include <utils/filesystemwatcher.h>
|
||||||
|
#include <utils/uniquename.h>
|
||||||
|
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QFileSystemModel>
|
#include <QFileSystemModel>
|
||||||
@@ -151,16 +154,20 @@ bool AssetsLibraryModel::renameFolder(const QString &folderPath, const QString &
|
|||||||
|
|
||||||
QString AssetsLibraryModel::addNewFolder(const QString &folderPath)
|
QString AssetsLibraryModel::addNewFolder(const QString &folderPath)
|
||||||
{
|
{
|
||||||
QString iterPath = folderPath;
|
Utils::FilePath dir = Utils::FilePath::fromString(folderPath);
|
||||||
QDir dir{folderPath};
|
Utils::FilePath parentDir = dir.parentDir();
|
||||||
|
|
||||||
while (dir.exists()) {
|
QString uniqueFolderName = UniqueName::get(dir.fileName(), [&] (const QString &folderName) {
|
||||||
iterPath = getUniqueName(iterPath);
|
return !parentDir.pathAppended(folderName).exists();
|
||||||
|
});
|
||||||
|
|
||||||
dir.setPath(iterPath);
|
auto res = parentDir.pathAppended(uniqueFolderName).ensureWritableDir();
|
||||||
|
if (!res.has_value()) {
|
||||||
|
qWarning() << __FUNCTION__ << res.error();
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
return dir.mkpath(iterPath) ? iterPath : "";
|
return uniqueFolderName;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AssetsLibraryModel::urlPathExistsInModel(const QUrl &url) const
|
bool AssetsLibraryModel::urlPathExistsInModel(const QUrl &url) const
|
||||||
@@ -242,36 +249,6 @@ void AssetsLibraryModel::syncHasFiles()
|
|||||||
setHasFiles(checkHasFiles());
|
setHasFiles(checkHasFiles());
|
||||||
}
|
}
|
||||||
|
|
||||||
QString AssetsLibraryModel::getUniqueName(const QString &oldName) {
|
|
||||||
static QRegularExpression rgx("\\d+$"); // matches a number at the end of a string
|
|
||||||
|
|
||||||
QString uniqueName = oldName;
|
|
||||||
// if the folder name ends with a number, increment it
|
|
||||||
QRegularExpressionMatch match = rgx.match(uniqueName);
|
|
||||||
if (match.hasMatch()) { // ends with a number
|
|
||||||
QString numStr = match.captured(0);
|
|
||||||
int num = match.captured(0).toInt();
|
|
||||||
|
|
||||||
// get number of padding zeros, ex: for "005" = 2
|
|
||||||
int nPaddingZeros = 0;
|
|
||||||
for (; nPaddingZeros < numStr.size() && numStr[nPaddingZeros] == '0'; ++nPaddingZeros);
|
|
||||||
|
|
||||||
++num;
|
|
||||||
|
|
||||||
// if the incremented number's digits increased, decrease the padding zeros
|
|
||||||
if (std::fmod(std::log10(num), 1.0) == 0)
|
|
||||||
--nPaddingZeros;
|
|
||||||
|
|
||||||
uniqueName = oldName.mid(0, match.capturedStart())
|
|
||||||
+ QString('0').repeated(nPaddingZeros)
|
|
||||||
+ QString::number(num);
|
|
||||||
} else {
|
|
||||||
uniqueName = oldName + '1';
|
|
||||||
}
|
|
||||||
|
|
||||||
return uniqueName;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AssetsLibraryModel::setRootPath(const QString &newPath)
|
void AssetsLibraryModel::setRootPath(const QString &newPath)
|
||||||
{
|
{
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
|
@@ -58,8 +58,6 @@ public:
|
|||||||
|
|
||||||
bool hasFiles() const { return m_hasFiles; }
|
bool hasFiles() const { return m_hasFiles; }
|
||||||
|
|
||||||
QString getUniqueName(const QString &oldName);
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void directoryLoaded(const QString &path);
|
void directoryLoaded(const QString &path);
|
||||||
void rootPathChanged();
|
void rootPathChanged();
|
||||||
|
@@ -7,7 +7,6 @@
|
|||||||
#include "assetslibrarymodel.h"
|
#include "assetslibrarymodel.h"
|
||||||
#include "assetslibraryview.h"
|
#include "assetslibraryview.h"
|
||||||
|
|
||||||
#include <asset.h>
|
|
||||||
#include <designeractionmanager.h>
|
#include <designeractionmanager.h>
|
||||||
#include <designerpaths.h>
|
#include <designerpaths.h>
|
||||||
#include <hdrimage.h>
|
#include <hdrimage.h>
|
||||||
@@ -25,9 +24,11 @@
|
|||||||
#include <coreplugin/messagebox.h>
|
#include <coreplugin/messagebox.h>
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
|
#include <utils/asset.h>
|
||||||
#include <utils/environment.h>
|
#include <utils/environment.h>
|
||||||
#include <utils/filepath.h>
|
#include <utils/filepath.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
#include <utils/uniquename.h>
|
||||||
|
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
@@ -174,23 +175,14 @@ void AssetsLibraryWidget::deleteSelectedAssets()
|
|||||||
|
|
||||||
QString AssetsLibraryWidget::getUniqueEffectPath(const QString &parentFolder, const QString &effectName)
|
QString AssetsLibraryWidget::getUniqueEffectPath(const QString &parentFolder, const QString &effectName)
|
||||||
{
|
{
|
||||||
auto genEffectPath = [&parentFolder](const QString &name) {
|
QString effectsDir = ModelNodeOperations::getEffectsDefaultDirectory(parentFolder);
|
||||||
QString effectsDir = ModelNodeOperations::getEffectsDefaultDirectory(parentFolder);
|
QString effectPathTemplate = effectsDir + QLatin1String("/%1.qep");
|
||||||
return QLatin1String("%1/%2.qep").arg(effectsDir, name);
|
|
||||||
};
|
|
||||||
|
|
||||||
QString uniqueName = effectName;
|
QString uniqueName = UniqueName::get(effectName, [&] (const QString &name) {
|
||||||
QString path = genEffectPath(uniqueName);
|
return !QFile::exists(effectPathTemplate.arg(name));
|
||||||
QFileInfo file{path};
|
});
|
||||||
|
|
||||||
while (file.exists()) {
|
return effectPathTemplate.arg(uniqueName);
|
||||||
uniqueName = m_assetsModel->getUniqueName(uniqueName);
|
|
||||||
|
|
||||||
path = genEffectPath(uniqueName);
|
|
||||||
file.setFile(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
return path;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AssetsLibraryWidget::createNewEffect(const QString &effectPath, bool openInEffectComposer)
|
bool AssetsLibraryWidget::createNewEffect(const QString &effectPath, bool openInEffectComposer)
|
||||||
|
Reference in New Issue
Block a user