From 04705a5ec3517cc86986e88fcb07849fd03959f4 Mon Sep 17 00:00:00 2001 From: Elias Steurer Date: Thu, 9 Jan 2020 16:08:32 +0100 Subject: [PATCH] Add widget type to the ScreenPlayWidget executable as an argument. This can be ether qmlWidget or htmlWidget for now. --- ScreenPlay/qml/Installed/Sidebar.qml | 8 +- ScreenPlay/src/create.cpp | 134 ++++++++++++++------------ ScreenPlay/src/screenplaymanager.cpp | 12 +-- ScreenPlay/src/screenplaymanager.h | 2 +- ScreenPlay/src/screenplaywidget.cpp | 9 +- ScreenPlay/src/screenplaywidget.h | 20 +++- ScreenPlayWidget/main.cpp | 8 +- ScreenPlayWidget/src/widgetwindow.cpp | 15 ++- ScreenPlayWidget/src/widgetwindow.h | 3 +- 9 files changed, 128 insertions(+), 83 deletions(-) diff --git a/ScreenPlay/qml/Installed/Sidebar.qml b/ScreenPlay/qml/Installed/Sidebar.qml index 3d428fc7..c32b7f1b 100644 --- a/ScreenPlay/qml/Installed/Sidebar.qml +++ b/ScreenPlay/qml/Installed/Sidebar.qml @@ -337,14 +337,14 @@ Item { + "/" + activeScreen, ScreenPlay.installedListModel.get(activeScreen).screenPreview, (Math.round(sliderVolume.value * 100) / 100), - settingsComboBox.model.get(settingsComboBox.currentIndex).text.toString( - ), type) + settingsComboBox.model.get(settingsComboBox.currentIndex).text.toString(), + type) } else { ScreenPlay.screenPlayManager.createWidget( ScreenPlay.globalVariables.localStoragePath + "/" + activeScreen, - ScreenPlay.installedListModel.get( - activeScreen).screenPreview) + ScreenPlay.installedListModel.get(activeScreen).screenPreview, + type) } sidebar.state = "inactive" monitorSelection.deselectAll() diff --git a/ScreenPlay/src/create.cpp b/ScreenPlay/src/create.cpp index 49bfc6be..a910adf8 100644 --- a/ScreenPlay/src/create.cpp +++ b/ScreenPlay/src/create.cpp @@ -36,73 +36,87 @@ Create::Create() void Create::createWidget(const QString& localStoragePath, const QString& title, const QString& previewThumbnail, const QString& createdBy, const QString& license, const QString& type, const QVector& tags) { - QUrl localStoragePathUrl { localStoragePath }; - QDir dir; - dir.cd(localStoragePathUrl.toLocalFile()); - // Create a temp dir so we can later alter it to the workshop id - auto folderName = QString("_tmp_" + QTime::currentTime().toString()).replace(":", ""); + QtConcurrent::run([=]() { + QUrl localStoragePathUrl { localStoragePath }; + QDir dir; + dir.cd(localStoragePathUrl.toLocalFile()); + // Create a temp dir so we can later alter it to the workshop id + auto folderName = QString("_tmp_" + QTime::currentTime().toString()).replace(":", ""); + QString workingPath = dir.path() + "/" + folderName; - if (!dir.mkdir(folderName)) { - qDebug() << "Could create folder: " << folderName; - return; - } - - QJsonObject obj; - obj.insert("license", license); - obj.insert("title", title); - obj.insert("createdBy", createdBy); - - if (type == "QML") { - obj.insert("type", "qmlWidget"); - obj.insert("file", "main.qml"); - } else { - obj.insert("type", "htmlWidget"); - obj.insert("file", "index.html"); - } - - QJsonArray tagsJsonArray; - for (QString tmp : tags) { - tagsJsonArray.append(tmp); - } - obj.insert("tags", tagsJsonArray); - QString workingPath = dir.path() + "/" + folderName; - - QFile file(workingPath + "/project.json"); - if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { - qDebug() << "Could not open /project.json"; - return; - } - - QUrl previewThumbnailUrl { previewThumbnail }; - QFileInfo previewImageFile(previewThumbnailUrl.toLocalFile()); - - if (!previewThumbnail.isEmpty()) { - obj.insert("previewThumbnail", previewImageFile.fileName()); - obj.insert("preview", previewImageFile.fileName()); - if (!QFile::copy(previewThumbnailUrl.toLocalFile(), workingPath + "/" + previewImageFile.fileName())) { - qDebug() << "Could not copy" << previewThumbnailUrl.toLocalFile() << " to " << workingPath + "/" + previewImageFile.fileName(); + if (!dir.mkdir(folderName)) { + qDebug() << "Could create folder: " << folderName; return; } - } - QTextStream out(&file); - out.setCodec("UTF-8"); - QJsonDocument doc(obj); - out << doc.toJson(); - file.close(); + QJsonObject obj; + obj.insert("license", license); + obj.insert("title", title); + obj.insert("createdBy", createdBy); - QFile fileMainQML(workingPath + "/main.qml"); - if (!fileMainQML.open(QIODevice::WriteOnly | QIODevice::Text)) { - qDebug() << "Could not open /main.qml"; - return; - } + if (type == "QML") { + obj.insert("type", "qmlWidget"); + obj.insert("file", "main.qml"); - QTextStream outMainQML(&fileMainQML); - outMainQML.setCodec("UTF-8"); - outMainQML << "import QtQuick 2.14 \n\n Item {\n id:root \n}"; - fileMainQML.close(); + QFile fileMainQML(workingPath + "/main.qml"); + if (!fileMainQML.open(QIODevice::WriteOnly | QIODevice::Text)) { + qDebug() << "Could not open /main.qml"; + return; + } - emit widgetCreatedSuccessful(workingPath); + QTextStream outMainQML(&fileMainQML); + outMainQML.setCodec("UTF-8"); + outMainQML << "import QtQuick 2.14 \n\n Item {\n id:root \n}"; + fileMainQML.close(); + + } else { + obj.insert("type", "htmlWidget"); + obj.insert("file", "index.html"); + + QFile fileMainHTML(workingPath + "/index.html"); + if (!fileMainHTML.open(QIODevice::WriteOnly | QIODevice::Text)) { + qDebug() << "Could not open /index.html"; + return; + } + + QTextStream outMainHTML(&fileMainHTML); + outMainHTML.setCodec("UTF-8"); + outMainHTML << "\n\n\n"; + fileMainHTML.close(); + } + + QJsonArray tagsJsonArray; + for (QString tmp : tags) { + tagsJsonArray.append(tmp); + } + obj.insert("tags", tagsJsonArray); + + QFile file(workingPath + "/project.json"); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { + qDebug() << "Could not open /project.json"; + return; + } + + QUrl previewThumbnailUrl { previewThumbnail }; + QFileInfo previewImageFile(previewThumbnailUrl.toLocalFile()); + + if (!previewThumbnail.isEmpty()) { + obj.insert("previewThumbnail", previewImageFile.fileName()); + obj.insert("preview", previewImageFile.fileName()); + if (!QFile::copy(previewThumbnailUrl.toLocalFile(), workingPath + "/" + previewImageFile.fileName())) { + qDebug() << "Could not copy" << previewThumbnailUrl.toLocalFile() << " to " << workingPath + "/" + previewImageFile.fileName(); + return; + } + } + + QTextStream out(&file); + out.setCodec("UTF-8"); + QJsonDocument doc(obj); + out << doc.toJson(); + file.close(); + + emit widgetCreatedSuccessful(workingPath); + }); } /*! diff --git a/ScreenPlay/src/screenplaymanager.cpp b/ScreenPlay/src/screenplaymanager.cpp index b4ee9514..d9c62714 100644 --- a/ScreenPlay/src/screenplaymanager.cpp +++ b/ScreenPlay/src/screenplaymanager.cpp @@ -41,7 +41,7 @@ void ScreenPlayManager::createWallpaper( const bool saveToProfilesConfigFile) { - m_tracker->sendEvent("wallpaper","start"); + m_tracker->sendEvent("wallpaper", "start"); QString path = absoluteStoragePath; if (absoluteStoragePath.contains("file:///")) @@ -105,9 +105,9 @@ void ScreenPlayManager::createWallpaper( /*! Creates a ScreenPlayWidget object via a \a absoluteStoragePath and a \a preview image (relative path). */ -void ScreenPlayManager::createWidget(const QUrl& absoluteStoragePath, const QString& previewImage) +void ScreenPlayManager::createWidget(const QUrl& absoluteStoragePath, const QString& previewImage, const QString& type) { - m_tracker->sendEvent("widget","start"); + m_tracker->sendEvent("widget", "start"); increaseActiveWidgetsCounter(); m_screenPlayWidgets.append( @@ -117,7 +117,7 @@ void ScreenPlayManager::createWidget(const QUrl& absoluteStoragePath, const QStr absoluteStoragePath.toLocalFile(), previewImage, absoluteStoragePath.toString(), - this)); + type)); } /*! @@ -127,7 +127,7 @@ void ScreenPlayManager::createWidget(const QUrl& absoluteStoragePath, const QStr void ScreenPlayManager::removeAllWallpapers() { if (!m_screenPlayWallpapers.empty()) { - m_tracker->sendEvent("wallpaper","stopAll"); + m_tracker->sendEvent("wallpaper", "stopAll"); m_sdkconnector->closeAllWallpapers(); m_screenPlayWallpapers.clear(); m_monitorListModel->clearActiveWallpaper(); @@ -168,7 +168,7 @@ void ScreenPlayManager::removeAllWallpapers() */ bool ScreenPlayManager::removeWallpaperAt(int at) { - m_tracker->sendEvent("wallpaper","removeSingleWallpaper"); + m_tracker->sendEvent("wallpaper", "removeSingleWallpaper"); if (auto appID = m_monitorListModel->getAppIDByMonitorIndex(at)) { m_sdkconnector->closeWallpaper(appID.value()); diff --git a/ScreenPlay/src/screenplaymanager.h b/ScreenPlay/src/screenplaymanager.h index 616ca1ff..6cbe1907 100644 --- a/ScreenPlay/src/screenplaymanager.h +++ b/ScreenPlay/src/screenplaymanager.h @@ -95,7 +95,7 @@ public slots: const QString& fillMode, const QString& type, const bool saveToProfilesConfigFile = true); - void createWidget(const QUrl& absoluteStoragePath, const QString& previewImage); + void createWidget(const QUrl& absoluteStoragePath, const QString& previewImage, const QString &type); void removeAllWallpapers(); bool removeWallpaperAt(const int at = 0); diff --git a/ScreenPlay/src/screenplaywidget.cpp b/ScreenPlay/src/screenplaywidget.cpp index fd07b6c8..e6b27052 100644 --- a/ScreenPlay/src/screenplaywidget.cpp +++ b/ScreenPlay/src/screenplaywidget.cpp @@ -16,20 +16,21 @@ ScreenPlayWidget::ScreenPlayWidget( const QString& projectPath, const QString& previewImage, const QString& fullPath, - QObject* parent) - : QObject { parent } + const QString& type) + : QObject { nullptr } , m_globalVariables { globalVariables } , m_projectPath { projectPath } , m_previewImage { previewImage } , m_appID { appID } , m_position { 0, 0 } + , m_type { type } { - const QStringList proArgs { m_projectPath, m_appID }; + const QStringList proArgs { m_projectPath, m_appID, m_type }; m_process.setArguments(proArgs); if (fullPath.endsWith(".exe")) { m_process.setProgram(fullPath); - } else if (fullPath.endsWith(".qml")) { + } else { m_process.setProgram(m_globalVariables->widgetExecutablePath().path()); } diff --git a/ScreenPlay/src/screenplaywidget.h b/ScreenPlay/src/screenplaywidget.h index 98e15c38..b95312de 100644 --- a/ScreenPlay/src/screenplaywidget.h +++ b/ScreenPlay/src/screenplaywidget.h @@ -22,6 +22,7 @@ class ScreenPlayWidget : public QObject { Q_PROPERTY(QString previewImage READ previewImage WRITE setPreviewImage NOTIFY previewImageChanged) Q_PROPERTY(QPoint position READ position WRITE setPosition NOTIFY positionChanged) Q_PROPERTY(QString appID READ appID WRITE setAppID NOTIFY appIDChanged) + Q_PROPERTY(QString type READ type WRITE setType NOTIFY typeChanged) public: explicit ScreenPlayWidget( @@ -30,7 +31,7 @@ public: const QString& projectPath, const QString& previewImage, const QString& fullPath, - QObject* parent = nullptr); + const QString& type); ~ScreenPlayWidget() {} @@ -54,6 +55,11 @@ public: return m_appID; } + QString type() const + { + return m_type; + } + public slots: void setProjectPath(QString projectPath) { @@ -91,12 +97,23 @@ public slots: emit appIDChanged(m_appID); } + void setType(QString type) + { + if (m_type == type) + return; + + m_type = type; + emit typeChanged(m_type); + } + signals: void projectPathChanged(QString projectPath); void previewImageChanged(QString previewImage); void positionChanged(QPoint position); void appIDChanged(QString appID); + void typeChanged(QString type); + private: QProcess m_process; const shared_ptr& m_globalVariables; @@ -105,5 +122,6 @@ private: QString m_previewImage; QString m_appID; QPoint m_position; + QString m_type; }; } diff --git a/ScreenPlayWidget/main.cpp b/ScreenPlayWidget/main.cpp index 669bb735..12a22678 100644 --- a/ScreenPlayWidget/main.cpp +++ b/ScreenPlayWidget/main.cpp @@ -14,18 +14,18 @@ int main(int argc, char* argv[]) QStringList argumentList = app.arguments(); - // If we start with only one argument (app path), + // If we start with only one argument (app, path, type), // it means we want to test a single widget if (argumentList.length() == 1) { - WidgetWindow spwmw("test","appid" ); + WidgetWindow spwmw("test","appid", "qmlWidget"); return app.exec(); } - if (argumentList.length() != 3) { + if (argumentList.length() != 4) { return -3; } - WidgetWindow spwmw(argumentList.at(1), argumentList.at(2)); + WidgetWindow spwmw(argumentList.at(1), argumentList.at(2), argumentList.at(3)); QObject::connect(&sdk, &ScreenPlaySDK::sdkDisconnected, &spwmw, &WidgetWindow::destroyThis); QObject::connect(&sdk, &ScreenPlaySDK::incommingMessage, &spwmw, &WidgetWindow::messageReceived); diff --git a/ScreenPlayWidget/src/widgetwindow.cpp b/ScreenPlayWidget/src/widgetwindow.cpp index a5f5234c..d6c16942 100644 --- a/ScreenPlayWidget/src/widgetwindow.cpp +++ b/ScreenPlayWidget/src/widgetwindow.cpp @@ -2,10 +2,20 @@ #include -WidgetWindow::WidgetWindow(QString projectPath, QString appid, QObject* parent) - : QObject(parent) +WidgetWindow::WidgetWindow(const QString projectPath, const QString appid, const QString type) + : QObject(nullptr) , m_appID { appid } + , m_type { type } { + QStringList availableTypes { + "qmlWidget", + "htmlWidget", + "standaloneWidget" + }; + + if (!availableTypes.contains(m_type)) { + QGuiApplication::exit(-4); + } Qt::WindowFlags flags = m_window.flags(); m_window.setWidth(300); @@ -41,6 +51,7 @@ WidgetWindow::WidgetWindow(QString projectPath, QString appid, QObject* parent) } // Instead of setting "renderType: Text.NativeRendering" every time // we can set it here once :) + m_window.setTextRenderType(QQuickWindow::TextRenderType::NativeTextRendering); // m_window.setResizeMode(QQuickView::ResizeMode::SizeViewToRootObject); m_window.setSource(QUrl("qrc:/mainWidget.qml")); diff --git a/ScreenPlayWidget/src/widgetwindow.h b/ScreenPlayWidget/src/widgetwindow.h index 0a37f8f4..4a7da77d 100644 --- a/ScreenPlayWidget/src/widgetwindow.h +++ b/ScreenPlayWidget/src/widgetwindow.h @@ -15,6 +15,7 @@ #include #include #include +#include #ifdef Q_OS_WIN #include @@ -24,7 +25,7 @@ class WidgetWindow : public QObject { Q_OBJECT public: - explicit WidgetWindow(QString projectPath, QString appid, QObject* parent = nullptr); + explicit WidgetWindow(const QString projectPath, const QString appid, const QString type); Q_PROPERTY(QString appID READ appID WRITE setAppID NOTIFY appIDChanged) Q_PROPERTY(QString type READ type WRITE setType NOTIFY typeChanged)