QmlDesigner : QRC File integration

Split resource file generation into two actions. Take file from
current project into account. .qrc generation should take existing
.qrc file into account.

Task-number: QDS-4516
Task-number: QDS-4517
Task-number: QDS-4519
Change-Id: Iee15880657e456fbd74df505c10ee83a80ee64a7
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Vikas Pachdha <vikas.pachdha@qt.io>
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Pekka Kaikkonen
2021-06-18 21:21:52 +03:00
committed by Tim Jenssen
parent f8a77287bf
commit 68ff37e630
2 changed files with 277 additions and 37 deletions

View File

@@ -63,7 +63,7 @@
namespace QmlDesigner { namespace QmlDesigner {
QTableWidget* GenerateResource::createFilesTable(const QStringList &fileNames) QTableWidget* GenerateResource::createFilesTable(const QList<ResourceFile> &fileNames)
{ {
auto table = new QTableWidget(0, 1); auto table = new QTableWidget(0, 1);
table->setSelectionMode(QAbstractItemView::SingleSelection); table->setSelectionMode(QAbstractItemView::SingleSelection);
@@ -74,12 +74,19 @@ QTableWidget* GenerateResource::createFilesTable(const QStringList &fileNames)
table->verticalHeader()->hide(); table->verticalHeader()->hide();
table->setShowGrid(false); table->setShowGrid(false);
for (const QString &filePath : fileNames) { QFont font;
font.setBold(true);
for (ResourceFile resource : fileNames){
QString filePath = resource.fileName;
auto checkboxItem = new QTableWidgetItem(); auto checkboxItem = new QTableWidgetItem();
checkboxItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); checkboxItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
checkboxItem->setCheckState(Qt::Checked); checkboxItem->setCheckState(Qt::Checked);
checkboxItem->setText(filePath); checkboxItem->setText(filePath);
if (resource.inProject)
checkboxItem->setFont(font);
int row = table->rowCount(); int row = table->rowCount();
table->insertRow(row); table->insertRow(row);
table->setItem(row, 0, checkboxItem); table->setItem(row, 0, checkboxItem);
@@ -88,7 +95,7 @@ QTableWidget* GenerateResource::createFilesTable(const QStringList &fileNames)
return table; return table;
} }
QStringList GenerateResource::getFileList(const QStringList &fileNames) 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());
@@ -107,21 +114,21 @@ QStringList GenerateResource::getFileList(const QStringList &fileNames)
mainLayout->addWidget(buttonBox, 3, 2, 1, 2); mainLayout->addWidget(buttonBox, 3, 2, 1, 2);
QObject::connect(buttonBox, &QDialogButtonBox::accepted, dialog, [dialog](){ QObject::connect(buttonBox, &QDialogButtonBox::accepted, dialog, [dialog]() {
dialog->accept(); dialog->accept();
dialog->deleteLater(); dialog->deleteLater();
}); });
QObject::connect(buttonBox, &QDialogButtonBox::rejected, dialog, [dialog](){ QObject::connect(buttonBox, &QDialogButtonBox::rejected, dialog, [dialog]() {
dialog->reject(); dialog->reject();
dialog->deleteLater(); dialog->deleteLater();
}); });
QObject::connect(dialog, &QDialog::accepted, [&result, &table](){ QObject::connect(dialog, &QDialog::accepted, [&result, &table]() {
QStringList fileList; QStringList fileList;
QString file; QString file;
for (int i = 0; i < table->rowCount(); ++i){ for (int i = 0; i < table->rowCount(); ++i) {
if (table->item(i,0)->checkState()){ if (table->item(i,0)->checkState()){
file = table->item(i,0)->text(); file = table->item(i,0)->text();
fileList.append(file); fileList.append(file);
@@ -141,19 +148,189 @@ void GenerateResource::generateMenuEntry()
Core::ActionContainer *buildMenu = Core::ActionContainer *buildMenu =
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT); Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
const Core::Context projectContext(QmlProjectManager::Constants::QML_PROJECT_ID); const Core::Context projectContext(QmlProjectManager::Constants::QML_PROJECT_ID);
// ToDo: move this to QtCreator and add tr to the string then // ToDo: move this to QtCreator and add tr to the string then
auto action = new QAction(QCoreApplication::translate("QmlDesigner::GenerateResource", "Generate Resource File")); auto action = new QAction(QCoreApplication::translate("QmlDesigner::GenerateResource",
"Generate QRC Resource File"));
action->setEnabled(ProjectExplorer::SessionManager::startupProject() != nullptr); action->setEnabled(ProjectExplorer::SessionManager::startupProject() != nullptr);
// todo make it more intelligent when it gets enabled // todo make it more intelligent when it gets enabled
QObject::connect(ProjectExplorer::SessionManager::instance(), &ProjectExplorer::SessionManager::startupProjectChanged, [action]() { QObject::connect(ProjectExplorer::SessionManager::instance(),
&ProjectExplorer::SessionManager::startupProjectChanged, [action]() {
action->setEnabled(ProjectExplorer::SessionManager::startupProject()); action->setEnabled(ProjectExplorer::SessionManager::startupProject());
}); });
Core::Command *cmd = Core::ActionManager::registerAction(action, "QmlProject.CreateResource"); Core::Command *cmd = Core::ActionManager::registerAction(action, "QmlProject.CreateResource");
QObject::connect(action, &QAction::triggered, [] () { QObject::connect(action, &QAction::triggered, [] () {
auto currentProject = ProjectExplorer::SessionManager::startupProject(); auto currentProject = ProjectExplorer::SessionManager::startupProject();
QTC_ASSERT(currentProject, return);
auto projectPath = currentProject->projectFilePath().parentDir().toString();
static QMap<QString, QString> lastUsedPathes;
auto saveLastUsedPath = [currentProject] (const QString &lastUsedPath) {
lastUsedPathes.insert(currentProject->displayName(), lastUsedPath);
};
saveLastUsedPath(lastUsedPathes.value(currentProject->displayName(),
currentProject->projectFilePath().parentDir().parentDir().toString()));
QString projectFileName = currentProject->displayName() + ".qrc";
QTemporaryFile temp(projectPath + "/XXXXXXX.create.resource.qrc");
QFile persistentFile(projectPath + "/" + projectFileName);
if (!temp.open())
return;
temp.close();
QtSupport::BaseQtVersion *qtVersion = QtSupport::QtKitAspect::qtVersion(
currentProject->activeTarget()->kit());
QString rccBinary = qtVersion->rccCommand();
Utils::QtcProcess rccProcess;
rccProcess.setWorkingDirectory(projectPath);
const QStringList arguments1 = {"--project", "--output", temp.fileName()};
for (const auto &arguments : {arguments1}) {
rccProcess.setCommand({rccBinary, arguments});
rccProcess.start();
if (!rccProcess.waitForStarted()) {
Core::MessageManager::writeDisrupting(
QCoreApplication::translate("QmlDesigner::GenerateResource",
"Unable to generate resource file: %1")
.arg(temp.fileName()));
return;
}
QByteArray stdOut;
QByteArray stdErr;
if (!rccProcess.readDataFromProcess(30, &stdOut, &stdErr, true)) {
rccProcess.stopProcess();
Core::MessageManager::writeDisrupting(
QCoreApplication::translate("QmlDesigner::GenerateResource",
"A timeout occurred running \"%1\"")
.arg(rccBinary + " " + arguments.join(" ")));
return;
}
if (!stdOut.trimmed().isEmpty())
Core::MessageManager::writeFlashing(QString::fromLocal8Bit(stdOut));
if (!stdErr.trimmed().isEmpty())
Core::MessageManager::writeFlashing(QString::fromLocal8Bit(stdErr));
if (rccProcess.exitStatus() != QProcess::NormalExit) {
Core::MessageManager::writeDisrupting(
QCoreApplication::translate("QmlDesigner::GenerateResource", "\"%1\" crashed.")
.arg(rccBinary + " " + arguments.join(" ")));
return;
}
if (rccProcess.exitCode() != 0) {
Core::MessageManager::writeDisrupting(
QCoreApplication::translate("QmlDesigner::GenerateResource",
"\"%1\" failed (exit code %2).")
.arg(rccBinary + " " + arguments.join(" "))
.arg(rccProcess.exitCode()));
return;
}
}
if (!temp.open())
return;
QXmlStreamReader reader(&temp);
QList<ResourceFile> fileList = {};
QByteArray firstLine = temp.readLine();
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"))
&& !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;
dir.setCurrent(projectPath);
Utils::FilePaths paths = currentProject->files(ProjectExplorer::Project::AllFiles);
QStringList projectFiles = {};
for (const Utils::FilePath &path : paths) {
QString relativepath = dir.relativeFilePath(path.toString());
if (!relativepath.endsWith(".qmlproject") && !relativepath.endsWith(".pri")
&& !relativepath.endsWith(".pro") && !relativepath.endsWith(".user")
&& !relativepath.endsWith(".qrc")) {
projectFiles.append(relativepath);
bool found = false;
QString compareString = "./" + relativepath.trimmed();
for (int i = 0; i < fileList.count(); ++i)
if (fileList.at(i).fileName == compareString) {
fileList[i].inProject = true;
found = true;
break;
}
if (!found) {
ResourceFile res;
res.fileName = "./" + relativepath.trimmed();
res.inProject = true;
fileList.append(res);
}
}
}
temp.close();
QStringList modifiedList = getFileList(fileList);
if (!persistentFile.open(QIODevice::ReadWrite | QIODevice::Truncate))
return;
QXmlStreamWriter writer(&persistentFile);
writer.setAutoFormatting(true);
writer.setAutoFormattingIndent(0);
persistentFile.write(firstLine.trimmed());
writer.writeStartElement("qresource");
for (QString file : modifiedList)
writer.writeTextElement("file", file.trimmed());
writer.writeEndElement();
persistentFile.write("\n</RCC>\n");
persistentFile.close();
saveLastUsedPath(Utils::FilePath::fromString(projectFileName).parentDir().toString());
});
// ToDo: move this to QtCreator and add tr to the string then
auto rccAction = new QAction(QCoreApplication::translate("QmlDesigner::GenerateResource",
"Generate RCC Resource File"));
rccAction->setEnabled(ProjectExplorer::SessionManager::startupProject() != nullptr);
QObject::connect(ProjectExplorer::SessionManager::instance(),
&ProjectExplorer::SessionManager::startupProjectChanged, [rccAction]() {
rccAction->setEnabled(ProjectExplorer::SessionManager::startupProject());
});
Core::Command *cmd2 = Core::ActionManager::registerAction(rccAction,
"QmlProject.CreateRCCResource");
QObject::connect(rccAction, &QAction::triggered, [] () {
auto currentProject = ProjectExplorer::SessionManager::startupProject();
QTC_ASSERT(currentProject, return);
auto projectPath = currentProject->projectFilePath().parentDir().toString(); auto projectPath = currentProject->projectFilePath().parentDir().toString();
static QMap<QString, QString> lastUsedPathes; static QMap<QString, QString> lastUsedPathes;
@@ -164,9 +341,11 @@ void GenerateResource::generateMenuEntry()
currentProject->projectFilePath().parentDir().parentDir().toString())); currentProject->projectFilePath().parentDir().parentDir().toString()));
auto resourceFileName = Core:: DocumentManager::getSaveFileName( auto resourceFileName = Core:: DocumentManager::getSaveFileName(
QCoreApplication::translate("QmlDesigner::GenerateResource", "Save Project as Resource"), QCoreApplication::translate("QmlDesigner::GenerateResource",
lastUsedPathes.value(currentProject->displayName()) + "/" + currentProject->displayName() + ".qmlrc", "Save Project as Resource"), lastUsedPathes.value(currentProject->displayName())
QCoreApplication::translate("QmlDesigner::GenerateResource", "QML Resource File (*.qmlrc)")); + "/" + currentProject->displayName() + ".qmlrc",
QCoreApplication::translate("QmlDesigner::GenerateResource",
"QML Resource File (*.qmlrc);;Resource File (*.rcc)"));
if (resourceFileName.isEmpty()) if (resourceFileName.isEmpty())
return; return;
@@ -175,10 +354,9 @@ void GenerateResource::generateMenuEntry()
"Generate a resource file out of project %1 to %2") "Generate a resource file out of project %1 to %2")
.arg(currentProject->displayName(), QDir::toNativeSeparators(resourceFileName))); .arg(currentProject->displayName(), QDir::toNativeSeparators(resourceFileName)));
QString projectFileName = currentProject->displayName() + ".qrc";
QFile persistentFile(projectPath + "/" + projectFileName);
QTemporaryFile temp(projectPath + "/XXXXXXX.create.resource.qrc"); QTemporaryFile temp(projectPath + "/XXXXXXX.create.resource.qrc");
if (!temp.open())
return;
temp.close();
QtSupport::BaseQtVersion *qtVersion = QtSupport::QtKitAspect::qtVersion( QtSupport::BaseQtVersion *qtVersion = QtSupport::QtKitAspect::qtVersion(
currentProject->activeTarget()->kit()); currentProject->activeTarget()->kit());
@@ -187,6 +365,14 @@ void GenerateResource::generateMenuEntry()
Utils::QtcProcess rccProcess; Utils::QtcProcess rccProcess;
rccProcess.setWorkingDirectory(projectPath); rccProcess.setWorkingDirectory(projectPath);
QXmlStreamReader reader;
QByteArray firstLine;
if (!QFileInfo(persistentFile).exists()) {
if (!temp.open())
return;
temp.close();
const QStringList arguments1 = {"--project", "--output", temp.fileName()}; const QStringList arguments1 = {"--project", "--output", temp.fileName()};
for (const auto &arguments : {arguments1}) { for (const auto &arguments : {arguments1}) {
@@ -209,9 +395,9 @@ void GenerateResource::generateMenuEntry()
.arg(rccBinary + " " + arguments.join(" "))); .arg(rccBinary + " " + arguments.join(" ")));
return; return;
} }
if (!stdOut.trimmed().isEmpty()) { if (!stdOut.trimmed().isEmpty())
Core::MessageManager::writeFlashing(QString::fromLocal8Bit(stdOut)); Core::MessageManager::writeFlashing(QString::fromLocal8Bit(stdOut));
}
if (!stdErr.trimmed().isEmpty()) if (!stdErr.trimmed().isEmpty())
Core::MessageManager::writeFlashing(QString::fromLocal8Bit(stdErr)); Core::MessageManager::writeFlashing(QString::fromLocal8Bit(stdErr));
@@ -229,15 +415,23 @@ void GenerateResource::generateMenuEntry()
.arg(rccProcess.exitCode())); .arg(rccProcess.exitCode()));
return; return;
} }
} }
reader.setDevice(&temp);
if (!temp.open()) if (!temp.open())
return; return;
firstLine = temp.readLine();
QXmlStreamReader reader(&temp); } else {
QStringList fileList = {}; reader.setDevice(&persistentFile);
QByteArray firstLine = temp.readLine(); if (!persistentFile.open(QIODevice::ReadWrite))
return;
firstLine = persistentFile.readLine();
}
QList<ResourceFile> fileList = {};
while (!reader.atEnd()) { while (!reader.atEnd()) {
const auto token = reader.readNext(); const auto token = reader.readNext();
@@ -247,12 +441,51 @@ 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"))
fileList.append(fileName); && !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;
dir.setCurrent(projectPath);
Utils::FilePaths paths = currentProject->files(ProjectExplorer::Project::AllFiles);
QStringList projectFiles = {};
for (const Utils::FilePath &path : paths) {
QString relativepath = dir.relativeFilePath(path.toString());
if (!relativepath.endsWith(".qmlproject") && !relativepath.endsWith(".pri")
&& !relativepath.endsWith(".pro") && !relativepath.endsWith(".user")
&& !relativepath.endsWith(".qrc")) {
projectFiles.append(relativepath);
bool found = false;
QString compareString = "./" + relativepath.trimmed();
for (int i = 0; i < fileList.count(); ++i)
if (fileList.at(i).fileName == compareString) {
fileList[i].inProject = true;
found = true;
}
if (!found) {
ResourceFile res;
res.fileName = "./" + relativepath.trimmed();
res.inProject = true;
fileList.append(res);
}
} }
} }
temp.close(); temp.close();
persistentFile.close();
QStringList modifiedList = getFileList(fileList); QStringList modifiedList = getFileList(fileList);
QTemporaryFile tempFile(projectPath + "/XXXXXXX.create.modifiedresource.qrc"); QTemporaryFile tempFile(projectPath + "/XXXXXXX.create.modifiedresource.qrc");
@@ -266,14 +499,15 @@ void GenerateResource::generateMenuEntry()
tempFile.write(firstLine.trimmed()); tempFile.write(firstLine.trimmed());
writer.writeStartElement("qresource"); writer.writeStartElement("qresource");
for (int i = 0; i < modifiedList.count(); ++i) for (QString file : modifiedList)
writer.writeTextElement("file", modifiedList.at(i).trimmed()); writer.writeTextElement("file", file.trimmed());
writer.writeEndElement(); writer.writeEndElement();
tempFile.write("\n</RCC>\n"); tempFile.write("\n</RCC>\n");
tempFile.close(); tempFile.close();
const QStringList arguments2 = {"--binary", "--output", resourceFileName, tempFile.fileName()}; const QStringList arguments2 = {"--binary", "--output", resourceFileName,
tempFile.fileName()};
for (const auto &arguments : {arguments2}) { for (const auto &arguments : {arguments2}) {
rccProcess.setCommand({rccBinary, arguments}); rccProcess.setCommand({rccBinary, arguments});
@@ -293,12 +527,12 @@ void GenerateResource::generateMenuEntry()
QCoreApplication::translate("QmlDesigner::GenerateResource", QCoreApplication::translate("QmlDesigner::GenerateResource",
"A timeout occurred running \"%1\"") "A timeout occurred running \"%1\"")
.arg(rccBinary + " " + arguments.join(" "))); .arg(rccBinary + " " + arguments.join(" ")));
return ; return;
} }
if (!stdOut.trimmed().isEmpty()) { if (!stdOut.trimmed().isEmpty())
Core::MessageManager::writeFlashing(QString::fromLocal8Bit(stdOut)); Core::MessageManager::writeFlashing(QString::fromLocal8Bit(stdOut));
}
if (!stdErr.trimmed().isEmpty()) if (!stdErr.trimmed().isEmpty())
Core::MessageManager::writeFlashing(QString::fromLocal8Bit(stdErr)); Core::MessageManager::writeFlashing(QString::fromLocal8Bit(stdErr));
@@ -322,6 +556,7 @@ void GenerateResource::generateMenuEntry()
saveLastUsedPath(Utils::FilePath::fromString(resourceFileName).parentDir().toString()); 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);
} }
} // namespace QmlDesigner } // namespace QmlDesigner

View File

@@ -28,8 +28,13 @@
namespace QmlDesigner { namespace QmlDesigner {
namespace GenerateResource { namespace GenerateResource {
struct ResourceFile
{
QString fileName;
bool inProject;
};
void generateMenuEntry(); void generateMenuEntry();
QStringList getFileList(const QStringList &); QStringList getFileList(const QList<ResourceFile> &);
QTableWidget* createFilesTable(const QStringList &); QTableWidget* createFilesTable(const QList<ResourceFile> &);
} }
} // namespace QmlDesigner } // namespace QmlDesigner