QmlDesigner: Improve .qrc file generation

* Filter for more suffixes and avoid code duplication.
* Always use the project path for file dialogs.
* Allow choosing (existing?) .qrc file.

It is now possible to 'append' to and manage an existing .qrc file.

Change-Id: Ib66751adc49839fd3fae7ac3ee3ae5fa741b7d50
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Thomas Hartmann
2021-07-14 18:34:26 +02:00
parent c395e52c2b
commit a58686cf96

View File

@@ -102,6 +102,7 @@ QStringList GenerateResource::getFileList(const QList<ResourceFile> &fileNames)
QStringList result; QStringList result;
QDialog *dialog = new QDialog(Core::ICore::dialogParent()); QDialog *dialog = new QDialog(Core::ICore::dialogParent());
dialog->setMinimumWidth(480); dialog->setMinimumWidth(480);
dialog->setMinimumHeight(640);
dialog->setModal(true); dialog->setModal(true);
dialog->setWindowTitle(QCoreApplication::translate("AddImageToResources","Add Resources")); dialog->setWindowTitle(QCoreApplication::translate("AddImageToResources","Add Resources"));
@@ -145,6 +146,53 @@ QStringList GenerateResource::getFileList(const QList<ResourceFile> &fileNames)
return result; return result;
} }
bool skipSuffix(const QString &fileName)
{
const QStringList suffixes = {".qmlproject",
".pri",
".pro",
".user",
".qrc",
".qds",
"CMakeLists.txt",
".db",
".tmp",
".TMP",
".metainfo"};
for (const auto &suffix : suffixes)
if (fileName.endsWith(suffix))
return true;
return false;
}
QList<GenerateResource::ResourceFile> getFilesFromQrc(QFile *file, bool inProject = false)
{
QXmlStreamReader reader(file);
QList<GenerateResource::ResourceFile> fileList = {};
while (!reader.atEnd()) {
const auto token = reader.readNext();
if (token != QXmlStreamReader::StartElement)
continue;
if (reader.name() == QLatin1String("file")) {
QString fileName = reader.readElementText().trimmed();
if ((!fileName.startsWith("./.")) && (!fileName.startsWith("./XXXXXXX"))
&& !skipSuffix(fileName)) {
GenerateResource::ResourceFile file;
file.fileName = fileName;
file.inProject = inProject;
fileList.append(file);
}
}
}
return fileList;
}
void GenerateResource::generateMenuEntry() void GenerateResource::generateMenuEntry()
{ {
Core::ActionContainer *buildMenu = Core::ActionContainer *buildMenu =
@@ -167,16 +215,16 @@ void GenerateResource::generateMenuEntry()
QTC_ASSERT(currentProject, return); QTC_ASSERT(currentProject, return);
auto projectPath = currentProject->projectFilePath().parentDir().toString(); auto projectPath = currentProject->projectFilePath().parentDir().toString();
static QMap<QString, QString> lastUsedPathes; auto projectFileName = Core::DocumentManager::getSaveFileName(
auto saveLastUsedPath = [currentProject] (const QString &lastUsedPath) { QCoreApplication::translate("QmlDesigner::GenerateResource", "Save Project as QRC File"),
lastUsedPathes.insert(currentProject->displayName(), lastUsedPath); projectPath + "/" + currentProject->displayName() + ".qrc",
}; QCoreApplication::translate("QmlDesigner::GenerateResource",
saveLastUsedPath(lastUsedPathes.value(currentProject->displayName(), "QML Resource File (*.qrc)"));
currentProject->projectFilePath().parentDir().parentDir().toString())); if (projectFileName.isEmpty())
return;
QString projectFileName = currentProject->displayName() + ".qrc";
QTemporaryFile temp(projectPath + "/XXXXXXX.create.resource.qrc"); QTemporaryFile temp(projectPath + "/XXXXXXX.create.resource.qrc");
QFile persistentFile(projectPath + "/" + projectFileName); QFile persistentFile(projectFileName);
if (!temp.open()) if (!temp.open())
return; return;
@@ -238,29 +286,14 @@ void GenerateResource::generateMenuEntry()
if (!temp.open()) if (!temp.open())
return; return;
QXmlStreamReader reader(&temp);
QList<ResourceFile> fileList = {};
QByteArray firstLine = temp.readLine(); QByteArray firstLine = temp.readLine();
QList<ResourceFile> fileList = getFilesFromQrc(&temp);
while (!reader.atEnd()) { QFile existingQrcFile(projectFileName);
const auto token = reader.readNext(); if (existingQrcFile.exists()) {
existingQrcFile.open(QFile::ReadOnly);
if (token != QXmlStreamReader::StartElement) fileList = getFilesFromQrc(&existingQrcFile, true);
continue; existingQrcFile.close();
if (reader.name() == QLatin1String("file")) {
QString fileName = reader.readElementText().trimmed();
if ((!fileName.startsWith("./.")) && (!fileName.startsWith("./XXXXXXX"))
&& !fileName.endsWith(".qmlproject") && !fileName.endsWith(".pri")
&& !fileName.endsWith(".pro") && !fileName.endsWith(".user")
&& !fileName.endsWith(".qrc")) {
ResourceFile file;
file.fileName = fileName;
file.inProject = false;
fileList.append(file);
}
}
} }
QDir dir; QDir dir;
@@ -272,9 +305,7 @@ void GenerateResource::generateMenuEntry()
for (const Utils::FilePath &path : paths) { for (const Utils::FilePath &path : paths) {
QString relativepath = dir.relativeFilePath(path.toString()); QString relativepath = dir.relativeFilePath(path.toString());
if (!relativepath.endsWith(".qmlproject") && !relativepath.endsWith(".pri") if (!skipSuffix(relativepath)) {
&& !relativepath.endsWith(".pro") && !relativepath.endsWith(".user")
&& !relativepath.endsWith(".qrc")) {
projectFiles.append(relativepath); projectFiles.append(relativepath);
bool found = false; bool found = false;
@@ -316,7 +347,6 @@ void GenerateResource::generateMenuEntry()
persistentFile.write("\n</RCC>\n"); persistentFile.write("\n</RCC>\n");
persistentFile.close(); persistentFile.close();
saveLastUsedPath(Utils::FilePath::fromString(projectFileName).parentDir().toString());
}); });
// ToDo: move this to QtCreator and add tr to the string then // ToDo: move this to QtCreator and add tr to the string then
@@ -335,17 +365,9 @@ void GenerateResource::generateMenuEntry()
QTC_ASSERT(currentProject, return); QTC_ASSERT(currentProject, return);
auto projectPath = currentProject->projectFilePath().parentDir().toString(); auto projectPath = currentProject->projectFilePath().parentDir().toString();
static QMap<QString, QString> lastUsedPathes; auto resourceFileName = Core::DocumentManager::getSaveFileName(
auto saveLastUsedPath = [currentProject] (const QString &lastUsedPath) { QCoreApplication::translate("QmlDesigner::GenerateResource", "Save Project as Resource"),
lastUsedPathes.insert(currentProject->displayName(), lastUsedPath); projectPath + "/" + currentProject->displayName() + ".qmlrc",
};
saveLastUsedPath(lastUsedPathes.value(currentProject->displayName(),
currentProject->projectFilePath().parentDir().parentDir().toString()));
auto resourceFileName = Core:: DocumentManager::getSaveFileName(
QCoreApplication::translate("QmlDesigner::GenerateResource",
"Save Project as Resource"), lastUsedPathes.value(currentProject->displayName())
+ "/" + currentProject->displayName() + ".qmlrc",
QCoreApplication::translate("QmlDesigner::GenerateResource", QCoreApplication::translate("QmlDesigner::GenerateResource",
"QML Resource File (*.qmlrc);;Resource File (*.rcc)")); "QML Resource File (*.qmlrc);;Resource File (*.rcc)"));
if (resourceFileName.isEmpty()) if (resourceFileName.isEmpty())
@@ -358,6 +380,7 @@ void GenerateResource::generateMenuEntry()
QString projectFileName = currentProject->displayName() + ".qrc"; QString projectFileName = currentProject->displayName() + ".qrc";
QFile persistentFile(projectPath + "/" + projectFileName); QFile persistentFile(projectPath + "/" + projectFileName);
QTemporaryFile temp(projectPath + "/XXXXXXX.create.resource.qrc"); QTemporaryFile temp(projectPath + "/XXXXXXX.create.resource.qrc");
QtSupport::BaseQtVersion *qtVersion = QtSupport::QtKitAspect::qtVersion( QtSupport::BaseQtVersion *qtVersion = QtSupport::QtKitAspect::qtVersion(
@@ -444,9 +467,7 @@ void GenerateResource::generateMenuEntry()
if (reader.name() == QLatin1String("file")) { if (reader.name() == QLatin1String("file")) {
QString fileName = reader.readElementText().trimmed(); QString fileName = reader.readElementText().trimmed();
if ((!fileName.startsWith("./.")) && (!fileName.startsWith("./XXXXXXX")) if ((!fileName.startsWith("./.")) && (!fileName.startsWith("./XXXXXXX"))
&& !fileName.endsWith(".qmlproject") && !fileName.endsWith(".pri") && !skipSuffix(fileName)) {
&& !fileName.endsWith(".pro") && !fileName.endsWith(".user")
&& !fileName.endsWith(".qrc")) {
ResourceFile file; ResourceFile file;
file.fileName = fileName; file.fileName = fileName;
file.inProject = false; file.inProject = false;
@@ -464,9 +485,7 @@ void GenerateResource::generateMenuEntry()
for (const Utils::FilePath &path : paths) { for (const Utils::FilePath &path : paths) {
QString relativepath = dir.relativeFilePath(path.toString()); QString relativepath = dir.relativeFilePath(path.toString());
if (!relativepath.endsWith(".qmlproject") && !relativepath.endsWith(".pri") if (!skipSuffix(relativepath)) {
&& !relativepath.endsWith(".pro") && !relativepath.endsWith(".user")
&& !relativepath.endsWith(".qrc")) {
projectFiles.append(relativepath); projectFiles.append(relativepath);
bool found = false; bool found = false;
@@ -552,10 +571,7 @@ void GenerateResource::generateMenuEntry()
.arg(rccProcess.exitCode())); .arg(rccProcess.exitCode()));
return; return;
} }
} }
saveLastUsedPath(Utils::FilePath::fromString(resourceFileName).parentDir().toString());
}); });
buildMenu->addAction(cmd, ProjectExplorer::Constants::G_BUILD_RUN); buildMenu->addAction(cmd, ProjectExplorer::Constants::G_BUILD_RUN);
buildMenu->addAction(cmd2, ProjectExplorer::Constants::G_BUILD_RUN); buildMenu->addAction(cmd2, ProjectExplorer::Constants::G_BUILD_RUN);