aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/generateresource.cpp
diff options
context:
space:
mode:
authorTim Jenssen <tim.jenssen@qt.io>2019-08-27 20:10:36 +0200
committerTim Jenssen <tim.jenssen@qt.io>2019-08-29 14:40:23 +0000
commitb77bce63e30669b9319f8570927a8789abf96240 (patch)
treedcac7d6ef5b3a75b489481a28a183d08bcd5689a /src/plugins/qmldesigner/generateresource.cpp
parent7fec4182059f2841239a06cc2402110aaa2a1ab4 (diff)
QmlDesigner: add a generate resource menu entry
The result can be used with the Webassembly qmlprojector. Change-Id: I69ed21561c05fcbb9552bf74217e3546c489d4a6 Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
Diffstat (limited to 'src/plugins/qmldesigner/generateresource.cpp')
-rw-r--r--src/plugins/qmldesigner/generateresource.cpp156
1 files changed, 156 insertions, 0 deletions
diff --git a/src/plugins/qmldesigner/generateresource.cpp b/src/plugins/qmldesigner/generateresource.cpp
new file mode 100644
index 0000000000..ca90783633
--- /dev/null
+++ b/src/plugins/qmldesigner/generateresource.cpp
@@ -0,0 +1,156 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt Design Tooling
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+****************************************************************************/
+
+#include <generateresource.h>
+
+#include <coreplugin/actionmanager/actionmanager.h>
+#include <coreplugin/actionmanager/actioncontainer.h>
+#include <coreplugin/actionmanager/command.h>
+#include <coreplugin/documentmanager.h>
+#include <coreplugin/messagemanager.h>
+
+#include <projectexplorer/project.h>
+#include <projectexplorer/session.h>
+#include <projectexplorer/projectexplorerconstants.h>
+#include <projectexplorer/target.h>
+
+#include <qmlprojectmanager/qmlprojectmanagerconstants.h>
+
+#include <qtsupport/baseqtversion.h>
+#include <qtsupport/qtkitinformation.h>
+
+#include <utils/fileutils.h>
+#include <utils/qtcassert.h>
+#include <utils/utilsicons.h>
+#include <utils/synchronousprocess.h>
+
+#include <QAction>
+#include <QTemporaryFile>
+#include <QMap>
+#include <QProcess>
+#include <QByteArray>
+#include <QObject>
+#include <QDebug>
+
+namespace QmlDesigner {
+void GenerateResource::generateMenuEntry()
+{
+ Core::ActionContainer *buildMenu =
+ Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
+
+
+ const Core::Context projectContext(QmlProjectManager::Constants::QML_PROJECT_ID);
+ // ToDo: move this to QtCreator and add tr to the string then
+ auto action = new QAction(QT_TRANSLATE_NOOP("GenerateResource", "Generate Resource File"));
+ action->setEnabled(ProjectExplorer::SessionManager::startupProject() != nullptr);
+ // todo make it more intelligent when it gets enabled
+ QObject::connect(ProjectExplorer::SessionManager::instance(), &ProjectExplorer::SessionManager::startupProjectChanged, [action]() {
+ action->setEnabled(ProjectExplorer::SessionManager::startupProject());
+ });
+
+ Core::Command *cmd = Core::ActionManager::registerAction(action, "QmlProject.CreateResource");
+ QObject::connect(action, &QAction::triggered, [] () {
+ auto currentProject = ProjectExplorer::SessionManager::startupProject();
+ 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().toString()));
+
+ auto resourceFileName = Core:: DocumentManager::getSaveFileName(
+ QT_TRANSLATE_NOOP("GenerateResource", "Save Project As Resource"),
+ lastUsedPathes.value(currentProject->displayName()) + currentProject->displayName() + ".qmlrc",
+ QT_TRANSLATE_NOOP("GenerateResource", "QML Resource File (*.qmlrc"));
+ if (resourceFileName.isEmpty())
+ return;
+
+ Core::MessageManager::write(QT_TRANSLATE_NOOP("GenerateResource",
+ QString("Generate a resource file out of project %1 to %2").arg(
+ currentProject->displayName(), QDir::toNativeSeparators(resourceFileName))));
+
+ QTemporaryFile temp(projectPath + "/XXXXXXX.create.resource.qrc");
+ if (!temp.open())
+ return;
+ temp.close();
+
+ auto rccBinary = QtSupport::QtKitAspect::qtVersion(currentProject->activeTarget()->kit())->binPath();
+#ifdef Q_OS_WIN
+ rccBinary = rccBinary.pathAppended("rcc.exe");
+#else
+ rccBinary = rccBinary.pathAppended("rcc");
+#endif
+ QProcess rccProcess;
+ rccProcess.setProgram(rccBinary.toString());
+ rccProcess.setWorkingDirectory(projectPath);
+
+ const QStringList arguments1 = {"--project", "--output", temp.fileName()};
+ const QStringList arguments2 = {"--binary", "--output", resourceFileName, temp.fileName()};
+
+ for (auto arguments : {arguments1, arguments2}) {
+ rccProcess.start(rccBinary.toString(), arguments);
+ if (!rccProcess.waitForStarted()) {
+ Core::MessageManager::write(QT_TRANSLATE_NOOP("GenerateResource", QString(
+ "Unable to generate resource file: %1").arg(resourceFileName)));
+ return;
+ }
+ QByteArray stdOut;
+ QByteArray stdErr;
+ if (!Utils::SynchronousProcess::readDataFromProcess(rccProcess, 30, &stdOut, &stdErr, true)) {
+ Utils::SynchronousProcess::stopProcess(rccProcess);
+ Core::MessageManager::write(QT_TRANSLATE_NOOP("GenerateResource", QString(
+ "A timeout occurred running \"%1\"").arg(rccBinary.toString() + arguments.join(" "))));
+ return ;
+
+ }
+ if (!stdOut.trimmed().isEmpty()) {
+ Core::MessageManager::write(QString::fromLocal8Bit(stdOut));
+ }
+ if (!stdErr.trimmed().isEmpty())
+ Core::MessageManager::write(QString::fromLocal8Bit(stdErr));
+
+ if (rccProcess.exitStatus() != QProcess::NormalExit) {
+ Core::MessageManager::write(QT_TRANSLATE_NOOP("GenerateResource", QString(
+ "\"%1\" crashed.").arg(rccBinary.toString() + arguments.join(" "))));
+ return;
+ }
+ if (rccProcess.exitCode() != 0) {
+ Core::MessageManager::write(QT_TRANSLATE_NOOP("GenerateResource", QString(
+ "\"%1\" failed (exit code %2).").arg(rccBinary.toString() +
+ " " + arguments.join(" ")).arg(rccProcess.exitCode())));
+ return;
+ }
+
+ }
+
+ saveLastUsedPath(Utils::FilePath::fromString(resourceFileName).parentDir().toString());
+ });
+ buildMenu->addAction(cmd, ProjectExplorer::Constants::G_BUILD_RUN);
+}
+
+} // namespace QmlDesigner
+