From 5c04b3074fc4e9937a28403dd214c783bfa3afdc Mon Sep 17 00:00:00 2001 From: Elias Date: Sat, 6 Apr 2019 21:25:00 +0200 Subject: [PATCH] Add ImageSelector Add project config creation Add more enum states Remove no longer used functions --- ScreenPlay/qml/Common/TagSelector.qml | 2 +- .../CreateWallpaperVideoImportConvert.qml | 53 +++----------- .../CreateWallpaper/CreateWallpaperWizard.qml | 25 ++++--- ScreenPlay/qml/Screen/Screen.qml | 3 + ScreenPlay/src/create.cpp | 72 ++++++++----------- ScreenPlay/src/create.h | 4 +- ScreenPlay/src/createimportvideo.cpp | 54 ++++++-------- ScreenPlay/src/createimportvideo.h | 14 ++-- 8 files changed, 87 insertions(+), 140 deletions(-) diff --git a/ScreenPlay/qml/Common/TagSelector.qml b/ScreenPlay/qml/Common/TagSelector.qml index 8ba27aa0..7769fd2f 100644 --- a/ScreenPlay/qml/Common/TagSelector.qml +++ b/ScreenPlay/qml/Common/TagSelector.qml @@ -127,7 +127,7 @@ Item { Material.foreground: "white" anchors { right: parent.right - rightMargin: 20 + rightMargin: 10 verticalCenter: parent.verticalCenter } diff --git a/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImportConvert.qml b/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImportConvert.qml index 7960d88d..dff03619 100644 --- a/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImportConvert.qml +++ b/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImportConvert.qml @@ -14,6 +14,7 @@ Item { property bool conversionFinishedSuccessful: false property bool canSave: false + property string filePath onCanSaveChanged: wrapperContent.checkCanSave() signal save @@ -145,9 +146,9 @@ Item { } } } - RowLayout { - id: row - height: 50 + ImageSelector { + id: previewSelector + anchors { top: imgWrapper.bottom topMargin: 20 @@ -155,46 +156,6 @@ Item { rightMargin: 30 left: parent.left } - - Rectangle { - height: 50 - color: "#eeeeee" - Layout.preferredWidth: imgWrapper.width - - Text { - id: txtCustomPreviewPath - color: "#333333" - - text: qsTr("Add custom preview image") - anchors { - verticalCenter: parent.verticalCenter - left: parent.left - leftMargin: 20 - } - } - - Button { - id: button - Material.background: Material.Orange - Material.foreground: "white" - text: qsTr("Choose Image") - anchors { - right: parent.right - rightMargin: 10 - } - onClicked: fileDialogOpenFile.open() - } - - FileDialog { - id: fileDialogOpenFile - nameFilters: [] - onAccepted: { - var file = fileDialogOpenFile.file.toString() - - txtCustomPreviewPath.text = fileDialogOpenFile.file - } - } - } } } Item { @@ -290,6 +251,8 @@ Item { screenPlayCreate.saveWallpaper( textFieldName.text, textFieldDescription.text, + wrapperContent.filePath, + previewSelector.imageSource, textFieldYoutubeURL.text, textFieldTags.getTags()) savePopup.open() @@ -329,8 +292,10 @@ Item { } } + + + /*##^## Designer { D{i:0;autoSize:true;height:480;width:950} } ##^##*/ - diff --git a/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperWizard.qml b/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperWizard.qml index dba3fe16..d7efd795 100644 --- a/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperWizard.qml +++ b/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperWizard.qml @@ -18,8 +18,12 @@ Item { Component.onCompleted: { state = "in" utility.setNavigationActive(false) - loader_wrapperContent.source - = "qrc:/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImportConvert.qml" + + loader_wrapperContent.setSource( + "qrc:/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImportConvert.qml", + { + "filePath": createNew.filePath + }) } //Blocks some MouseArea from create page @@ -30,13 +34,14 @@ Item { Connections { target: screenPlayCreate onCreateWallpaperStateChanged: { - if (state === CreateImportVideo.State.AnalyseVideoError - || state === CreateImportVideo.State.ConvertingPreviewVideoError - || state === CreateImportVideo.State.ConvertingPreviewGifError - || state === CreateImportVideo.State.ConvertingPreviewImageError - || state === CreateImportVideo.State.ConvertingAudioError - || state === CreateImportVideo.State.AbortCleanupError - || state === CreateImportVideo.State.ErrorUnknown) { + if (state === CreateImportVideo.State.AnalyseVideoError || state + === CreateImportVideo.State.ConvertingPreviewGifError || state + === CreateImportVideo.State.ConvertingPreviewImageError || state + === CreateImportVideo.State.ConvertingAudioError || state + === CreateImportVideo.State.AbortCleanupError || state + === CreateImportVideo.State.CopyFilesError || state + === CreateImportVideo.State.CreateProjectFileError || state + === CreateImportVideo.State.CreateTmpFolderError) { createNew.state = "result" } } @@ -88,7 +93,7 @@ Item { z: 10 Connections { target: loader_wrapperContent.sourceComponent - onSave:{ + onSave: { createNew.state = "result" } } diff --git a/ScreenPlay/qml/Screen/Screen.qml b/ScreenPlay/qml/Screen/Screen.qml index 7481e92e..dc2e9fd7 100644 --- a/ScreenPlay/qml/Screen/Screen.qml +++ b/ScreenPlay/qml/Screen/Screen.qml @@ -2,7 +2,10 @@ import QtQuick 2.12 import QtQuick.Controls 2.2 import QtQuick.Controls.Material 2.2 +import "../Common" + Item { id: screen anchors.fill: parent + } diff --git a/ScreenPlay/src/create.cpp b/ScreenPlay/src/create.cpp index f551db22..532053c0 100644 --- a/ScreenPlay/src/create.cpp +++ b/ScreenPlay/src/create.cpp @@ -12,44 +12,6 @@ Create::Create(Settings* st, QMLUtilities* util, QObject* parent) qmlRegisterType("net.aimber.create", 1, 0, "CreateImportVideo"); } -void Create::copyProject(QString relativeProjectPath, QString toPath) -{ - if (toPath.contains("file:///")) { - toPath.remove("file:///"); - } - QString srcFilePath = m_settings->getScreenPlayBasePath().toString() + relativeProjectPath; - - // Todo: UI Error handling - if (copyRecursively(srcFilePath, toPath)) { - emit m_utils->openFolderInExplorer(toPath); - } -} - -bool Create::copyRecursively(const QString& srcFilePath, const QString& tgtFilePath) -{ - QFileInfo srcFileInfo(srcFilePath); - if (srcFileInfo.isDir()) { - QDir targetDir(tgtFilePath); - targetDir.cdUp(); - QDir sourceDir(srcFilePath); - QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System); - Q_FOREACH (const QString& fileName, fileNames) { - const QString newSrcFilePath - = srcFilePath + QLatin1Char('/') + fileName; - const QString newTgtFilePath - = tgtFilePath + QLatin1Char('/') + fileName; - if (!copyRecursively(newSrcFilePath, newTgtFilePath)) { - return false; - } - } - } else { - if (!QFile::copy(srcFilePath, tgtFilePath)) { - return false; - } - } - return true; -} - void Create::createWallpaperStart(QString videoPath) { videoPath.remove("file:///"); @@ -67,11 +29,12 @@ void Create::createWallpaperStart(QString videoPath) } setWorkingDir(dir.path() + "/" + folderName); - m_createImportVideoThread = new QThread; + m_createImportVideoThread = new QThread(); m_createImportVideo = new CreateImportVideo(videoPath, workingDir()); connect(m_createImportVideo, &CreateImportVideo::createWallpaperStateChanged, this, &Create::createWallpaperStateChanged); connect(m_createImportVideoThread, &QThread::started, m_createImportVideo, &CreateImportVideo::process); + connect(m_createImportVideo, &CreateImportVideo::canceled, this, &Create::abortAndCleanup); connect(m_createImportVideo, &CreateImportVideo::finished, m_createImportVideoThread, &QThread::quit); connect(m_createImportVideo, &CreateImportVideo::finished, m_createImportVideo, &QObject::deleteLater); connect(m_createImportVideoThread, &QThread::finished, m_createImportVideoThread, &QObject::deleteLater); @@ -80,18 +43,38 @@ void Create::createWallpaperStart(QString videoPath) m_createImportVideoThread->start(); } -void Create::saveWallpaper(QString title, QString description, QString youtube, QVector tags) +void Create::saveWallpaper(QString title, QString description, QString filePath, QString previewImagePath, QString youtube, QVector tags) { - qDebug() << tags; + filePath.remove("file:///"); + previewImagePath.remove("file:///"); + emit createWallpaperStateChanged(CreateImportVideo::State::CopyFiles); + + QFileInfo previewImageFile(previewImagePath); + if (!QFile::copy(previewImagePath, m_workingDir + "/" + previewImageFile.fileName())) { + qDebug() << "Could not copy" << previewImagePath << " to " << m_workingDir + "/" + previewImageFile.fileName(); + emit createWallpaperStateChanged(CreateImportVideo::State::CopyFilesError); + return; + } + + QFileInfo filePathFile(filePath); + if (!QFile::copy(filePath, m_workingDir + "/" + filePathFile.fileName())) { + qDebug() << "Could not copy" << filePath << " to " << m_workingDir + "/" + filePathFile.fileName(); + emit createWallpaperStateChanged(CreateImportVideo::State::CopyFilesError); + return; + } + emit createWallpaperStateChanged(CreateImportVideo::State::CopyFilesFinished); + emit createWallpaperStateChanged(CreateImportVideo::State::CreateProjectFile); QFile file(m_workingDir + "/project.json"); QJsonObject obj; obj.insert("description", description); obj.insert("title", title); + obj.insert("youtube", youtube); obj.insert("file", "video.webm"); obj.insert("previewGIF", "preview.gif"); - obj.insert("previewWEBM", "preview.webm"); + obj.insert("previewWEBM", filePathFile.fileName()); + obj.insert("preview", previewImageFile.fileName()); obj.insert("type", "video"); QJsonArray arr; @@ -102,6 +85,7 @@ void Create::saveWallpaper(QString title, QString description, QString youtube, if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "Could not open /project.json"; + emit createWallpaperStateChanged(CreateImportVideo::State::CreateProjectFileError); return; } @@ -111,6 +95,7 @@ void Create::saveWallpaper(QString title, QString description, QString youtube, out << doc.toJson(); file.close(); + emit createWallpaperStateChanged(CreateImportVideo::State::CreateProjectFileFinished); } void Create::abortAndCleanup() @@ -120,6 +105,9 @@ void Create::abortAndCleanup() return; } + disconnect(m_createImportVideo); + disconnect(m_createImportVideoThread); + // Save to export path before aborting to be able to cleanup the tmp folder QString tmpExportPath = m_createImportVideo->m_exportPath; diff --git a/ScreenPlay/src/create.h b/ScreenPlay/src/create.h index 1ff95bfa..d08b53d1 100644 --- a/ScreenPlay/src/create.h +++ b/ScreenPlay/src/create.h @@ -55,10 +55,8 @@ signals: void workingDirChanged(QString workingDir); public slots: - void copyProject(QString relativeProjectPath, QString toPath); - bool copyRecursively(const QString& srcFilePath, const QString& tgtFilePath); void createWallpaperStart(QString videoPath); - void saveWallpaper(QString title, QString description, QString youtube, QVector tags); + void saveWallpaper(QString title, QString description, QString filePath, QString previewImagePath, QString youtube, QVector tags); void abortAndCleanup(); void setProgress(float progress) diff --git a/ScreenPlay/src/createimportvideo.cpp b/ScreenPlay/src/createimportvideo.cpp index e3c56cd4..a58f917f 100644 --- a/ScreenPlay/src/createimportvideo.cpp +++ b/ScreenPlay/src/createimportvideo.cpp @@ -1,4 +1,5 @@ #include "createimportvideo.h" +#include CreateImportVideo::CreateImportVideo(QObject* parent) : QObject(parent) @@ -15,13 +16,26 @@ CreateImportVideo::CreateImportVideo(QString videoPath, QString exportPath, QObj void CreateImportVideo::process() { qDebug() << "start converting video in thread: " << QThread::currentThreadId(); - createWallpaperInfo(); - createWallpaperVideoPreview(); - createWallpaperGifPreview(); - extractWallpaperAudio(); - emit createWallpaperStateChanged(CreateImportVideo::State::Finished); - emit finished(); + auto cleanup = qScopeGuard([this] { + qDebug() << "ABORT"; + this->requestInterruption(); + emit canceled(); + }); + + if (!createWallpaperInfo()) + return; + + if (!createWallpaperVideoPreview()) + return; + + if (!createWallpaperGifPreview()) + return; + + if (!extractWallpaperAudio()) + return; + + emit createWallpaperStateChanged(CreateImportVideo::State::Finished); } bool CreateImportVideo::createWallpaperInfo() @@ -345,32 +359,4 @@ bool CreateImportVideo::extractWallpaperAudio() return true; } -bool CreateImportVideo::createWallpaperProjectFile(const QString title, const QString description) -{ - //Copy Project File - QFile configFile(m_exportPath + "/project.json"); - if (!configFile.open(QIODevice::ReadWrite | QIODevice::Text)) { - return false; - } - - QTextStream out(&configFile); - QJsonObject configObj; - - configObj.insert("description", description); - configObj.insert("title", title); - - configObj.insert("file", "video.webm"); - configObj.insert("preview", "preview.png"); - configObj.insert("previewGIF", "preview.gif"); - configObj.insert("previewWebM", "preview.webm"); - configObj.insert("type", "video"); - - QJsonDocument configJsonDocument(configObj); - out << configJsonDocument.toJson(); - configFile.close(); - - emit createWallpaperStateChanged(CreateImportVideo::State::Finished); - - return true; -} diff --git a/ScreenPlay/src/createimportvideo.h b/ScreenPlay/src/createimportvideo.h index 55a9633f..535254d6 100644 --- a/ScreenPlay/src/createimportvideo.h +++ b/ScreenPlay/src/createimportvideo.h @@ -44,14 +44,18 @@ public: // ConvertingVideo, // ConvertingVideoFinished, // ConvertingVideoError, + CopyFiles, + CopyFilesFinished, + CopyFilesError, + CreateProjectFile, + CreateProjectFileFinished, + CreateProjectFileError, AbortCleanupError, CreateTmpFolderError, Finished, - ErrorUnknown, }; Q_ENUM(State) - QProcess m_process; QString m_videoPath; QString m_exportPath; int m_length = 0; @@ -61,17 +65,15 @@ signals: void createWallpaperStateChanged(CreateImportVideo::State state); void processOutput(QString text); void finished(); + void canceled(); public slots: void process(); - void requestInterruption() - { - } + void requestInterruption() {} bool createWallpaperInfo(); bool createWallpaperVideoPreview(); bool createWallpaperGifPreview(); bool createWallpaperImagePreview(); bool extractWallpaperAudio(); - bool createWallpaperProjectFile(const QString title, const QString description); };