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 <asset.h>
|
||||
#include <modelnodeoperations.h>
|
||||
#include <qmldesignerplugin.h>
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/asset.h>
|
||||
#include <utils/filepath.h>
|
||||
#include <utils/filesystemwatcher.h>
|
||||
#include <utils/uniquename.h>
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QFileSystemModel>
|
||||
@@ -151,16 +154,20 @@ bool AssetsLibraryModel::renameFolder(const QString &folderPath, const QString &
|
||||
|
||||
QString AssetsLibraryModel::addNewFolder(const QString &folderPath)
|
||||
{
|
||||
QString iterPath = folderPath;
|
||||
QDir dir{folderPath};
|
||||
Utils::FilePath dir = Utils::FilePath::fromString(folderPath);
|
||||
Utils::FilePath parentDir = dir.parentDir();
|
||||
|
||||
while (dir.exists()) {
|
||||
iterPath = getUniqueName(iterPath);
|
||||
QString uniqueFolderName = UniqueName::get(dir.fileName(), [&] (const QString &folderName) {
|
||||
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
|
||||
@@ -242,36 +249,6 @@ void AssetsLibraryModel::syncHasFiles()
|
||||
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)
|
||||
{
|
||||
beginResetModel();
|
||||
|
@@ -58,8 +58,6 @@ public:
|
||||
|
||||
bool hasFiles() const { return m_hasFiles; }
|
||||
|
||||
QString getUniqueName(const QString &oldName);
|
||||
|
||||
signals:
|
||||
void directoryLoaded(const QString &path);
|
||||
void rootPathChanged();
|
||||
|
@@ -7,7 +7,6 @@
|
||||
#include "assetslibrarymodel.h"
|
||||
#include "assetslibraryview.h"
|
||||
|
||||
#include <asset.h>
|
||||
#include <designeractionmanager.h>
|
||||
#include <designerpaths.h>
|
||||
#include <hdrimage.h>
|
||||
@@ -25,9 +24,11 @@
|
||||
#include <coreplugin/messagebox.h>
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/asset.h>
|
||||
#include <utils/environment.h>
|
||||
#include <utils/filepath.h>
|
||||
#include <utils/qtcassert.h>
|
||||
#include <utils/uniquename.h>
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QFileInfo>
|
||||
@@ -174,23 +175,14 @@ void AssetsLibraryWidget::deleteSelectedAssets()
|
||||
|
||||
QString AssetsLibraryWidget::getUniqueEffectPath(const QString &parentFolder, const QString &effectName)
|
||||
{
|
||||
auto genEffectPath = [&parentFolder](const QString &name) {
|
||||
QString effectsDir = ModelNodeOperations::getEffectsDefaultDirectory(parentFolder);
|
||||
return QLatin1String("%1/%2.qep").arg(effectsDir, name);
|
||||
};
|
||||
QString effectPathTemplate = effectsDir + QLatin1String("/%1.qep");
|
||||
|
||||
QString uniqueName = effectName;
|
||||
QString path = genEffectPath(uniqueName);
|
||||
QFileInfo file{path};
|
||||
QString uniqueName = UniqueName::get(effectName, [&] (const QString &name) {
|
||||
return !QFile::exists(effectPathTemplate.arg(name));
|
||||
});
|
||||
|
||||
while (file.exists()) {
|
||||
uniqueName = m_assetsModel->getUniqueName(uniqueName);
|
||||
|
||||
path = genEffectPath(uniqueName);
|
||||
file.setFile(path);
|
||||
}
|
||||
|
||||
return path;
|
||||
return effectPathTemplate.arg(uniqueName);
|
||||
}
|
||||
|
||||
bool AssetsLibraryWidget::createNewEffect(const QString &effectPath, bool openInEffectComposer)
|
||||
|
Reference in New Issue
Block a user