1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-05 10:32:28 +01:00

Refactor wizards to use qcoro

This commit is contained in:
Elias Steurer 2024-01-05 14:09:05 +01:00
parent 1a4a14ae8a
commit b41242a938
11 changed files with 290 additions and 240 deletions

View File

@ -6,26 +6,33 @@
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFont>
#include <QFuture>
#include <QFutureWatcher>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QLinearGradient>
#include <QObject>
#include <QPainter>
#include <QProcess>
#include <QQmlEngine>
#include <QScopedPointer>
#include <QString>
#include <QStringList>
#include <QTextOption>
#include <QTime>
#include <QTimer>
#include <QUrl>
#include <QtMath>
#include "ScreenPlay/createimportvideo.h"
#include "ScreenPlay/CMakeVariables.h"
#include "ScreenPlay/globalvariables.h"
#include "ScreenPlayUtil/util.h"
#include "qcorotask.h"
#include "qml/qcoroqml.h"
#include "qml/qcoroqmltask.h"
#include <memory>
#include <optional>
@ -36,6 +43,8 @@ class Wizards : public QObject {
Q_OBJECT
QML_ELEMENT
QML_UNCREATABLE("CPP ONLY")
Q_CLASSINFO("RegisterEnumClassesUnscoped", "false")
public:
explicit Wizards(
const std::shared_ptr<GlobalVariables>& globalVariables,
@ -44,6 +53,7 @@ public:
enum class WizardResult {
Ok,
CopyError,
WriteFileError,
WriteProjectFileError,
WriteLicenseFileError,
CreateProjectFolderError,
@ -52,8 +62,7 @@ public:
};
Q_ENUM(WizardResult)
public slots:
void createQMLWidget(
Q_INVOKABLE QCoro::QmlTask createQMLWidget(
const QString& title,
const QString& licenseName,
const QString& licenseFile,
@ -61,7 +70,7 @@ public slots:
const QString& previewThumbnail,
const QVector<QString>& tags);
void createHTMLWidget(
Q_INVOKABLE QCoro::QmlTask createHTMLWidget(
const QString& title,
const QString& licenseName,
const QString& licenseFile,
@ -69,7 +78,7 @@ public slots:
const QString& previewThumbnail,
const QVector<QString>& tags);
void createHTMLWallpaper(
Q_INVOKABLE QCoro::QmlTask createHTMLWallpaper(
const QString& title,
const QString& licenseName,
const QString& licenseFile,
@ -77,7 +86,7 @@ public slots:
const QString& previewThumbnail,
const QVector<QString>& tags);
void createQMLWallpaper(
Q_INVOKABLE QCoro::QmlTask createQMLWallpaper(
const QString& title,
const QString& licenseName,
const QString& licenseFile,
@ -85,7 +94,7 @@ public slots:
const QString& previewThumbnail,
const QVector<QString>& tags);
void createGodotWallpaper(
Q_INVOKABLE QCoro::QmlTask createGodotWallpaper(
const QString& title,
const QString& licenseName,
const QString& licenseFile,
@ -93,7 +102,7 @@ public slots:
const QString& previewThumbnail,
const QVector<QString>& tags);
void createGifWallpaper(
Q_INVOKABLE QCoro::QmlTask createGifWallpaper(
const QString& title,
const QString& licenseName,
const QString& licenseFile,
@ -101,30 +110,26 @@ public slots:
const QString& file,
const QVector<QString>& tags);
void createWebsiteWallpaper(
Q_INVOKABLE QCoro::QmlTask createWebsiteWallpaper(
const QString& title,
const QString& previewThumbnail,
const QUrl& url,
const QVector<QString>& tags);
signals:
void widgetCreationFinished(const Wizards::WizardResult result);
void widgetCreationFinished(const Wizards::WizardResult result, const QVariant value);
private:
void createPreviewImage(const QString& name, const QString& targetPath);
private:
const std::shared_ptr<GlobalVariables> m_globalVariables;
const std::optional<QString> createTemporaryFolder() const;
void createPreviewImage(const QString& name, const QString& targetPath);
private:
QFuture<void> m_wizardFuture;
QVector<QColor> m_gradientColors = {
QColor("#B71C1C"),
QColor("#1B5E20"),
QColor("#0D47A1"),
QColor("#FFD600"),
QColor("#4A148C")
};
Util m_util;
const QVector<QColor> m_gradientColors = {
QColor(183, 28, 28), // #B71C1C
QColor(27, 94, 32), // #1B5E20
QColor(13, 71, 161), // #0D47A1
QColor(255, 214, 0), // #FFD600
QColor(74, 20, 140) // #4A148C
};
};
}

View File

@ -15,7 +15,13 @@ WizardPage {
property bool ready: tfTitle.text.length >= 1 && root.file.length !== ""
function create() {
App.wizards.createGifWallpaper(tfTitle.text, cbLicense.name, cbLicense.licenseFile, tfCreatedBy.text, root.file, tagSelector.getTags());
App.wizards.createGifWallpaper(tfTitle.text, cbLicense.name,
cbLicense.licenseFile,
tfCreatedBy.text, root.file,
tagSelector.getTags()).then(
result => {
wizardFinished(result.success, result.message)
})
}
onReadyChanged: root.ready = ready
@ -54,15 +60,18 @@ WizardPage {
anchors.fill: parent
onDropped: {
root.file = drop.urls[0];
leftWrapper.color = Qt.darker(Qt.darker(Material.backgroundColor));
root.file = drop.urls[0]
leftWrapper.color = Qt.darker(
Qt.darker(Material.backgroundColor))
}
onExited: {
leftWrapper.color = Qt.darker(Material.backgroundColor);
leftWrapper.color = Qt.darker(
Material.backgroundColor)
}
onEntered: {
leftWrapper.color = Qt.darker(Qt.darker(Material.backgroundColor));
drag.accept(Qt.LinkAction);
leftWrapper.color = Qt.darker(
Qt.darker(Material.backgroundColor))
drag.accept(Qt.LinkAction)
}
}
@ -133,34 +142,34 @@ WizardPage {
required: true
}
Util.TextField {
id: tfCreatedBy
Util.TextField {
id: tfCreatedBy
Layout.fillWidth: true
placeholderText: qsTr("Created By")
}
Layout.fillWidth: true
placeholderText: qsTr("Created By")
}
Util.LicenseSelector {
id: cbLicense
Util.LicenseSelector {
id: cbLicense
Layout.fillWidth: true
}
Layout.fillWidth: true
}
Util.HeadlineSection {
text: qsTr("Tags")
}
Util.HeadlineSection {
text: qsTr("Tags")
}
Util.TagSelector {
id: tagSelector
Util.TagSelector {
id: tagSelector
Layout.fillWidth: true
}
Layout.fillWidth: true
}
Item {
Layout.fillHeight: true
Layout.fillWidth: true
Item {
Layout.fillHeight: true
Layout.fillWidth: true
}
}
}
}
}
}

View File

@ -14,7 +14,14 @@ WizardPage {
id: rightWrapper
function create() {
App.wizards.createGodotWallpaper(tfTitle.text, cbLicense.name, cbLicense.licenseFile, tfCreatedBy.text, previewSelector.imageSource, tagSelector.getTags());
App.wizards.createGodotWallpaper(tfTitle.text, cbLicense.name,
cbLicense.licenseFile,
tfCreatedBy.text,
previewSelector.imageSource,
tagSelector.getTags()).then(
result => {
wizardFinished(result.success, result.message)
})
}
spacing: 10

View File

@ -14,7 +14,14 @@ WizardPage {
id: rightWrapper
function create() {
App.wizards.createHTMLWallpaper(tfTitle.text, cbLicense.name, cbLicense.licenseFile, tfCreatedBy.text, previewSelector.imageSource, tagSelector.getTags());
App.wizards.createHTMLWallpaper(tfTitle.text, cbLicense.name,
cbLicense.licenseFile,
tfCreatedBy.text,
previewSelector.imageSource,
tagSelector.getTags()).then(
result => {
wizardFinished(result.success, result.message)
})
}
spacing: 10

View File

@ -11,7 +11,15 @@ WizardPage {
sourceComponent: ColumnLayout {
function create() {
App.wizards.createHTMLWidget(tfTitle.text, cbLicense.name, cbLicense.licenseFile, tfCreatedBy.text, previewSelector.imageSource, tagSelector.getTags());
App.wizards.createHTMLWidget(tfTitle.text, cbLicense.name,
cbLicense.licenseFile,
tfCreatedBy.text,
previewSelector.imageSource,
tagSelector.getTags()).then(result => {
wizardFinished(
result.success,
result.message)
})
}
Util.Headline {

View File

@ -14,7 +14,14 @@ WizardPage {
id: rightWrapper
function create() {
App.wizards.createQMLWallpaper(tfTitle.text, cbLicense.name, cbLicense.licenseFile, tfCreatedBy.text, previewSelector.imageSource, tagSelector.getTags());
App.wizards.createQMLWallpaper(tfTitle.text, cbLicense.name,
cbLicense.licenseFile,
tfCreatedBy.text,
previewSelector.imageSource,
tagSelector.getTags()).then(
result => {
wizardFinished(result.success, result.message)
})
}
spacing: 10

View File

@ -11,7 +11,15 @@ WizardPage {
sourceComponent: ColumnLayout {
function create() {
App.wizards.createQMLWidget(tfTitle.text, cbLicense.name, cbLicense.licenseFile, tfCreatedBy.text, previewSelector.imageSource, tagSelector.getTags());
App.wizards.createQMLWidget(tfTitle.text, cbLicense.name,
cbLicense.licenseFile,
tfCreatedBy.text,
previewSelector.imageSource,
tagSelector.getTags()).then(result => {
wizardFinished(
result.success,
result.message)
})
}
Util.Headline {

View File

@ -10,17 +10,25 @@ import ScreenPlayUtil as Util
WizardPage {
id: root
function isValidURL(string) {
var res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
return (res !== null);
var res = string.match(
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g)
return (res !== null)
}
sourceComponent: ColumnLayout {
id: layout
function validate() {
root.ready = tfTitle.text.length >= 1 && root.isValidURL(tfUrl.text);
root.ready = tfTitle.text.length >= 1 && root.isValidURL(tfUrl.text)
}
function create() {
App.wizards.createWebsiteWallpaper(tfTitle.text, previewSelector.imageSource, tfUrl.text, tagSelector.getTags());
App.wizards.createWebsiteWallpaper(tfTitle.text,
previewSelector.imageSource,
tfUrl.text, tagSelector.getTags(
)).then(result => {
wizardFinished(
result.success,
result.message)
})
}
spacing: 10

View File

@ -14,8 +14,11 @@ FocusScope {
property alias savePopup: savePopup
property bool ready: false
// Signals for the Sidebar
signal wizardStarted
signal wizardExited
// Show error/success popup
signal wizardFinished(bool success, string message)
signal saveClicked
signal saveFinished
signal cancelClicked
@ -71,15 +74,28 @@ FocusScope {
Layout.alignment: Qt.AlignRight
font.family: App.settings.font
onClicked: {
btnSave.enabled = false;
root.saveClicked();
loader.item.create();
savePopup.open();
btnSave.enabled = false
root.saveClicked()
savePopup.open()
loader.item.create()
}
}
}
Popup {
Connections {
target: root
function onWizardFinished(success, message) {
if (success) {
timerSave.start()
return
}
savePopup.title = message
savePopup.standardButtons = Dialog.Ok
}
}
Dialog {
id: savePopup
modal: true
@ -87,32 +103,22 @@ FocusScope {
width: 250
height: 200
anchors.centerIn: Overlay.overlay
onOpened: timerSave.start()
onAccepted: {
savePopup.close()
root.wizardExited()
}
title: qsTr("Saving...")
BusyIndicator {
anchors.centerIn: parent
running: true
}
Text {
text: qsTr("Saving...")
color: Material.primaryHighlightedTextColor
font.family: App.settings.font
anchors {
horizontalCenter: parent.horizontalCenter
bottom: parent.bottom
bottomMargin: 30
}
}
Timer {
id: timerSave
interval: 1000 + Math.random() * 1000
interval: 200 + Math.random() * 500
onTriggered: {
savePopup.close();
root.wizardExited();
savePopup.close()
root.wizardExited()
}
}
}

View File

@ -1,11 +1,5 @@
// SPDX-License-Identifier: LicenseRef-EliasSteurerTachiom OR AGPL-3.0-only
#include "ScreenPlay/wizards.h"
#include "ScreenPlay/CMakeVariables.h"
#include "ScreenPlayUtil/util.h"
#include <QFont>
#include <QLinearGradient>
#include <QPainter>
#include <QTextOption>
namespace ScreenPlay {
/*!
@ -26,24 +20,21 @@ Wizards::Wizards(const std::shared_ptr<GlobalVariables>& globalVariables, QObjec
/*!
\brief Creates a new widget.
*/
void Wizards::createQMLWidget(const QString& title,
QCoro::QmlTask Wizards::createQMLWidget(
const QString& title,
const QString& licenseName,
const QString& licenseFile,
const QString& createdBy,
const QString& previewThumbnail,
const QVector<QString>& tags)
{
if (m_wizardFuture.isRunning()) {
qWarning() << "Another wizard is already running! Abort.";
return;
}
m_wizardFuture = QtConcurrent::run([=]() {
return QCoro::QmlTask([this, title, licenseName, licenseFile, createdBy, previewThumbnail, tags]() -> QCoro::Task<Result> {
std::optional<QString> folderName = createTemporaryFolder();
if (!folderName.has_value()) {
emit widgetCreationFinished(WizardResult::CreateProjectFolderError);
return;
QString errorMessage = tr("Unable to create temporary folder");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CreateProjectFolderError), errorMessage };
}
const QString workingPath = m_util.toLocal(m_globalVariables->localStoragePath().toString() + "/" + folderName.value());
@ -57,21 +48,22 @@ void Wizards::createQMLWidget(const QString& title,
obj.insert("file", "main.qml");
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/assets/wizards/" + licenseFile, workingPath + "/" + licenseFile)) {
qWarning() << "Could not write " << licenseFile;
emit widgetCreationFinished(WizardResult::WriteLicenseFileError);
return;
QString errorMessage = tr("Could not write %1").arg(licenseFile);
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteLicenseFileError), errorMessage };
}
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/qml/Create/WizardsFiles/QMLWidgetMain.qml", workingPath + "/main.qml")) {
qWarning() << "Could not write main.qml";
emit widgetCreationFinished(WizardResult::WriteProjectFileError);
return;
QString errorMessage = tr("Could not write main.qml");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteFileError), errorMessage };
}
if (!previewThumbnail.isEmpty()) {
if (!m_util.copyPreviewThumbnail(obj, previewThumbnail, workingPath)) {
emit widgetCreationFinished(WizardResult::CopyPreviewThumbnailError);
return;
QString errorMessage = tr("Could not copy thumbnail");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CopyPreviewThumbnailError), errorMessage };
}
} else {
obj.insert("preview", "preview.png");
@ -79,35 +71,33 @@ void Wizards::createQMLWidget(const QString& title,
}
if (!m_util.writeSettings(obj, workingPath + "/project.json")) {
emit widgetCreationFinished(WizardResult::WriteProjectFileError);
return;
QString errorMessage = tr("Could not write project file");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteProjectFileError), errorMessage };
}
emit widgetCreationFinished(WizardResult::Ok, workingPath);
});
co_return Result { true };
}());
}
/*!
\brief Creates a new widget.
*/
void Wizards::createHTMLWidget(const QString& title,
QCoro::QmlTask Wizards::createHTMLWidget(
const QString& title,
const QString& licenseName,
const QString& licenseFile,
const QString& createdBy,
const QString& previewThumbnail,
const QVector<QString>& tags)
{
if (m_wizardFuture.isRunning()) {
qWarning() << "Another wizard is already running! Abort.";
return;
}
m_wizardFuture = QtConcurrent::run([=]() {
return QCoro::QmlTask([this, title, licenseName, licenseFile, createdBy, previewThumbnail, tags]() -> QCoro::Task<Result> {
std::optional<QString> folderName = createTemporaryFolder();
if (!folderName.has_value()) {
emit widgetCreationFinished(WizardResult::CreateProjectFolderError);
return;
QString errorMessage = tr("Unable to create temporary folder");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CreateProjectFolderError), errorMessage };
}
const QString workingPath = m_util.toLocal(m_globalVariables->localStoragePath().toString() + "/" + folderName.value());
@ -121,15 +111,15 @@ void Wizards::createHTMLWidget(const QString& title,
obj.insert("file", "index.html");
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/assets/wizards/" + licenseFile, workingPath + "/" + licenseFile)) {
qWarning() << "Could not write " << licenseFile;
emit widgetCreationFinished(WizardResult::WriteLicenseFileError);
return;
QString errorMessage = tr("Could not write %1").arg(licenseFile);
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteLicenseFileError), errorMessage };
}
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/qml/Create/WizardsFiles/HTMLWidgetMain.html", workingPath + "/index.html")) {
qWarning() << "Could not write HTMLWidgetMain.html";
emit widgetCreationFinished(WizardResult::WriteProjectFileError);
return;
QString errorMessage = tr("Could not write index.html");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteFileError), errorMessage };
}
QUrl previewThumbnailUrl { previewThumbnail };
@ -137,8 +127,9 @@ void Wizards::createHTMLWidget(const QString& title,
if (!previewThumbnail.isEmpty()) {
if (!m_util.copyPreviewThumbnail(obj, previewThumbnail, workingPath)) {
emit widgetCreationFinished(WizardResult::CopyPreviewThumbnailError);
return;
QString errorMessage = tr("Could copy preview thumbnail");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CopyPreviewThumbnailError), errorMessage };
}
} else {
obj.insert("preview", "preview.png");
@ -146,18 +137,19 @@ void Wizards::createHTMLWidget(const QString& title,
}
if (!m_util.writeSettings(obj, workingPath + "/project.json")) {
emit widgetCreationFinished(WizardResult::WriteProjectFileError);
return;
QString errorMessage = tr("Could not write project file");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteProjectFileError), errorMessage };
}
emit widgetCreationFinished(WizardResult::Ok, workingPath);
});
co_return Result { true };
}());
}
/*!
\brief Creates a HTML wallpaper.
*/
void Wizards::createHTMLWallpaper(
QCoro::QmlTask Wizards::createHTMLWallpaper(
const QString& title,
const QString& licenseName,
const QString& licenseFile,
@ -165,17 +157,13 @@ void Wizards::createHTMLWallpaper(
const QString& previewThumbnail,
const QVector<QString>& tags)
{
if (m_wizardFuture.isRunning()) {
qWarning() << "Another wizard is already running! Abort.";
return;
}
m_wizardFuture = QtConcurrent::run([=]() {
return QCoro::QmlTask([this, title, licenseName, licenseFile, createdBy, previewThumbnail, tags]() -> QCoro::Task<Result> {
std::optional<QString> folderName = createTemporaryFolder();
if (!folderName.has_value()) {
emit widgetCreationFinished(WizardResult::CreateProjectFolderError);
return;
QString errorMessage = tr("Unable to create temporary folder");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CreateProjectFolderError), errorMessage };
}
const QString workingPath = m_util.toLocal(m_globalVariables->localStoragePath().toString() + "/" + folderName.value());
@ -189,21 +177,22 @@ void Wizards::createHTMLWallpaper(
obj.insert("file", "index.html");
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/assets/wizards/" + licenseFile, workingPath + "/" + licenseFile)) {
qWarning() << "Could not write " << licenseFile;
emit widgetCreationFinished(WizardResult::WriteLicenseFileError);
return;
QString errorMessage = tr("Could not write %1").arg(licenseFile);
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteLicenseFileError), errorMessage };
}
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/qml/Create/WizardsFiles/HTMLWallpaperMain.html", workingPath + "/index.html")) {
qWarning() << "Could not write HTMLWallpaperMain.html";
emit widgetCreationFinished(WizardResult::WriteProjectFileError);
return;
QString errorMessage = tr("Could not write index.html");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteFileError), errorMessage };
}
if (!previewThumbnail.isEmpty()) {
if (!m_util.copyPreviewThumbnail(obj, previewThumbnail, workingPath)) {
emit widgetCreationFinished(WizardResult::CopyPreviewThumbnailError);
return;
QString errorMessage = tr("Could copy preview thumbnail");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CopyPreviewThumbnailError), errorMessage };
}
} else {
obj.insert("preview", "preview.png");
@ -211,18 +200,19 @@ void Wizards::createHTMLWallpaper(
}
if (!m_util.writeSettings(obj, workingPath + "/project.json")) {
emit widgetCreationFinished(WizardResult::WriteProjectFileError);
return;
QString errorMessage = tr("Could not write project file");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteProjectFileError), errorMessage };
}
emit widgetCreationFinished(WizardResult::Ok, workingPath);
});
co_return Result { true };
}());
}
/*!
\brief .
*/
void Wizards::createQMLWallpaper(
QCoro::QmlTask Wizards::createQMLWallpaper(
const QString& title,
const QString& licenseName,
const QString& licenseFile,
@ -230,17 +220,13 @@ void Wizards::createQMLWallpaper(
const QString& previewThumbnail,
const QVector<QString>& tags)
{
if (m_wizardFuture.isRunning()) {
qWarning() << "Another wizard is already running! Abort.";
return;
}
m_wizardFuture = QtConcurrent::run([=]() {
return QCoro::QmlTask([this, title, licenseName, licenseFile, createdBy, previewThumbnail, tags]() -> QCoro::Task<Result> {
std::optional<QString> folderName = createTemporaryFolder();
if (!folderName.has_value()) {
emit widgetCreationFinished(WizardResult::CreateProjectFolderError);
return;
QString errorMessage = tr("Unable to create temporary folder");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CreateProjectFolderError), errorMessage };
}
const QString workingPath = m_util.toLocal(m_globalVariables->localStoragePath().toString() + "/" + folderName.value());
@ -255,8 +241,9 @@ void Wizards::createQMLWallpaper(
if (!previewThumbnail.isEmpty()) {
if (!m_util.copyPreviewThumbnail(obj, previewThumbnail, workingPath)) {
emit widgetCreationFinished(WizardResult::CopyPreviewThumbnailError);
return;
QString errorMessage = tr("Could copy preview thumbnail");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CopyPreviewThumbnailError), errorMessage };
}
} else {
obj.insert("preview", "preview.png");
@ -264,36 +251,37 @@ void Wizards::createQMLWallpaper(
}
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/assets/wizards/" + licenseFile, workingPath + "/" + licenseFile)) {
qWarning() << "Could not write " << licenseFile;
emit widgetCreationFinished(WizardResult::WriteLicenseFileError);
return;
QString errorMessage = tr("Could not write %1").arg(licenseFile);
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteLicenseFileError), errorMessage };
}
const QString qmlproject = workingPath + "/" + title + ".qmlproject";
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/qml/Create/WizardsFiles/" + QString("QmlProject.qmlproject"), qmlproject)) {
qWarning() << "Could not write " << qmlproject;
emit widgetCreationFinished(WizardResult::WriteLicenseFileError);
return;
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/qml/Create/WizardsFiles/QmlProject.qmlproject", qmlproject)) {
QString errorMessage = tr("Could not write QmlProject.qmlproject");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteFileError), errorMessage };
}
if (!m_util.writeSettings(obj, workingPath + "/project.json")) {
emit widgetCreationFinished(WizardResult::WriteProjectFileError);
return;
QString errorMessage = tr("Could not write project file");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteProjectFileError), errorMessage };
}
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/qml/Create/WizardsFiles/QMLWallpaperMain.qml", workingPath + "/main.qml")) {
qWarning() << "Could not write main.qml";
return;
QString errorMessage = tr("Could not write main.qml");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteFileError), errorMessage };
}
emit widgetCreationFinished(WizardResult::Ok, workingPath);
});
co_return Result { true };
}());
}
/*!
\brief .
*/
void Wizards::createGodotWallpaper(
QCoro::QmlTask Wizards::createGodotWallpaper(
const QString& title,
const QString& licenseName,
const QString& licenseFile,
@ -301,17 +289,13 @@ void Wizards::createGodotWallpaper(
const QString& previewThumbnail,
const QVector<QString>& tags)
{
if (m_wizardFuture.isRunning()) {
qWarning() << "Another wizard is already running! Abort.";
return;
}
m_wizardFuture = QtConcurrent::run([=]() {
return QCoro::QmlTask([this, title, licenseName, licenseFile, createdBy, previewThumbnail, tags]() -> QCoro::Task<Result> {
std::optional<QString> folderName = createTemporaryFolder();
if (!folderName.has_value()) {
emit widgetCreationFinished(WizardResult::CreateProjectFolderError);
return;
QString errorMessage = tr("Unable to create temporary folder");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CreateProjectFolderError), errorMessage };
}
const QString workingPath = m_util.toLocal(m_globalVariables->localStoragePath().toString() + "/" + folderName.value());
@ -333,8 +317,9 @@ void Wizards::createGodotWallpaper(
if (!previewThumbnail.isEmpty()) {
if (!m_util.copyPreviewThumbnail(obj, previewThumbnail, workingPath)) {
emit widgetCreationFinished(WizardResult::CopyPreviewThumbnailError);
return;
QString errorMessage = tr("Could copy preview thumbnail");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CopyPreviewThumbnailError), errorMessage };
}
} else {
obj.insert("preview", "preview.png");
@ -342,44 +327,49 @@ void Wizards::createGodotWallpaper(
}
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/assets/wizards/" + licenseFile, workingPath + "/" + licenseFile)) {
qWarning() << "Could not write " << licenseFile;
emit widgetCreationFinished(WizardResult::WriteLicenseFileError);
return;
QString errorMessage = tr("Could not write %1").arg(licenseFile);
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteLicenseFileError), errorMessage };
}
if (!m_util.writeSettings(obj, workingPath + "/project.json")) {
emit widgetCreationFinished(WizardResult::WriteProjectFileError);
return;
QString errorMessage = tr("Could not write project file");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteProjectFileError), errorMessage };
}
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/qml/Create/WizardsFiles/Godot_v5/project.godot", workingPath + "/project.godot")) {
qWarning() << "Could not write project.godot";
return;
QString errorMessage = tr("Could not write project.godot");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteFileError), errorMessage };
}
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/qml/Create/WizardsFiles/Godot_v5/spinner.gd", workingPath + "/spinner.gd")) {
qWarning() << "Could not write spinner.gd";
return;
QString errorMessage = tr("Could not write spinner.gd");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteFileError), errorMessage };
}
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/qml/Create/WizardsFiles/Godot_v5/wallpaper.tscn", workingPath + "/wallpaper.tscn")) {
qWarning() << "Could not write wallpaper.tscn";
return;
QString errorMessage = tr("Could not write wallpaper.tscn");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteFileError), errorMessage };
}
// This presets file is needed for the export. Because we do only export
// package files, it does not matter that we hardcode "Windows Desktop" as
// export preset.
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/qml/Create/WizardsFiles/Godot_v5/export_presets.cfg", workingPath + "/export_presets.cfg")) {
qWarning() << "Could not write export_presets.cfg";
return;
QString errorMessage = tr("Could not write export_presets.cfg");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteFileError), errorMessage };
}
emit widgetCreationFinished(WizardResult::Ok, workingPath);
});
co_return Result { true };
}());
}
void Wizards::createGifWallpaper(
QCoro::QmlTask Wizards::createGifWallpaper(
const QString& title,
const QString& licenseName,
const QString& licenseFile,
@ -387,17 +377,13 @@ void Wizards::createGifWallpaper(
const QString& file,
const QVector<QString>& tags)
{
if (m_wizardFuture.isRunning()) {
qWarning() << "Another wizard is already running! Abort.";
return;
}
m_wizardFuture = QtConcurrent::run([=]() {
return QCoro::QmlTask([this, title, licenseName, licenseFile, creator, file, tags]() -> QCoro::Task<Result> {
std::optional<QString> folderName = createTemporaryFolder();
if (!folderName.has_value()) {
emit widgetCreationFinished(WizardResult::CreateProjectFolderError);
return;
QString errorMessage = tr("Unable to create temporary folder");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CreateProjectFolderError), errorMessage };
}
const QString workingPath = m_util.toLocal(m_globalVariables->localStoragePath().toString() + "/" + folderName.value());
@ -413,46 +399,43 @@ void Wizards::createGifWallpaper(
obj.insert("type", QVariant::fromValue(ContentTypes::InstalledType::GifWallpaper).toString());
if (!m_util.writeFileFromQrc(":/qml/ScreenPlayApp/assets/wizards/" + licenseFile, workingPath + "/" + licenseFile)) {
qWarning() << "Could not write " << licenseFile;
emit widgetCreationFinished(WizardResult::WriteLicenseFileError);
return;
QString errorMessage = tr("Could not write %1").arg(licenseFile);
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteLicenseFileError), errorMessage };
}
if (!m_util.writeSettings(obj, workingPath + "/project.json")) {
emit widgetCreationFinished(WizardResult::WriteProjectFileError);
return;
QString errorMessage = tr("Could not write project file");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteProjectFileError), errorMessage };
}
if (!QFile::copy(m_util.toLocal(file), workingPath + "/" + gifFileName)) {
qWarning() << "Could not copy gif " << file << " to: " << workingPath + "/" + gifFileName;
emit widgetCreationFinished(WizardResult::CopyFileError);
return;
QString errorMessage = tr("Could not copy gif");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CopyFileError), errorMessage };
}
emit widgetCreationFinished(WizardResult::Ok, workingPath);
});
co_return Result { true };
}());
}
/*!
\brief .
*/
void Wizards::createWebsiteWallpaper(
QCoro::QmlTask Wizards::createWebsiteWallpaper(
const QString& title,
const QString& previewThumbnail,
const QUrl& url,
const QVector<QString>& tags)
{
if (m_wizardFuture.isRunning()) {
qWarning() << "Another wizard is already running! Abort.";
return;
}
m_wizardFuture = QtConcurrent::run([=]() {
return QCoro::QmlTask([this, title, previewThumbnail, url, tags]() -> QCoro::Task<Result> {
std::optional<QString> folderName = createTemporaryFolder();
if (!folderName.has_value()) {
emit widgetCreationFinished(WizardResult::CreateProjectFolderError);
return;
QString errorMessage = tr("Unable to create temporary folder");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CreateProjectFolderError), errorMessage };
}
const QString workingPath = m_util.toLocal(m_globalVariables->localStoragePath().toString() + "/" + folderName.value());
@ -465,8 +448,9 @@ void Wizards::createWebsiteWallpaper(
if (!previewThumbnail.isEmpty()) {
if (!m_util.copyPreviewThumbnail(obj, previewThumbnail, workingPath)) {
emit widgetCreationFinished(WizardResult::CopyPreviewThumbnailError);
return;
QString errorMessage = tr("Could copy preview thumbnail");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::CopyPreviewThumbnailError), errorMessage };
}
} else {
obj.insert("preview", "preview.png");
@ -474,12 +458,13 @@ void Wizards::createWebsiteWallpaper(
}
if (!m_util.writeSettings(obj, workingPath + "/project.json")) {
emit widgetCreationFinished(WizardResult::WriteProjectFileError);
return;
QString errorMessage = tr("Could not write project file");
qCritical() << errorMessage;
co_return Result { false, QVariant::fromValue(WizardResult::WriteProjectFileError), errorMessage };
}
emit widgetCreationFinished(WizardResult::Ok, workingPath);
});
co_return Result { true };
}());
}
/*!

View File

@ -66,26 +66,26 @@ class Result {
Q_GADGET
Q_PROPERTY(bool success READ success WRITE setSuccess)
Q_PROPERTY(QVariant status READ status WRITE setStatus)
Q_PROPERTY(QString messag READ messag WRITE setMessag)
Q_PROPERTY(QString message READ message WRITE setMessage)
public:
explicit Result() { }
explicit Result(bool success, const QVariant& status = {}, const QString& messag = "")
explicit Result(bool success, const QVariant& status = {}, const QString& message = "")
{
m_success = success;
m_status = status;
m_messag = messag;
m_message = message;
}
bool success() const { return m_success; }
void setSuccess(bool success) { m_success = success; }
QString messag() const { return m_messag; }
void setMessag(const QString& messag) { m_messag = messag; }
QString message() const { return m_message; }
void setMessage(const QString& message) { m_message = message; }
QVariant status() const { return m_status; }
void setStatus(const QVariant& status) { m_status = status; }
private:
bool m_success;
QString m_messag;
QString m_message;
QVariant m_status;
};