1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-09-16 07:22:34 +02:00

Add basic saving and loading of Widgets

This commit is contained in:
Elias Steurer 2020-07-13 17:59:20 +02:00
parent 12ab2a24a6
commit 7c0b5741af
8 changed files with 150 additions and 49 deletions

View File

@ -373,7 +373,7 @@ Item {
if (root.isWidget()) {
ScreenPlay.screenPlayManager.createWidget(
type, absoluteStoragePath, previewImage)
type, Qt.point(0,0), absoluteStoragePath, previewImage, true)
}
root.state = "inactive"

View File

@ -114,9 +114,18 @@ void ScreenPlayManager::createWallpaper(
*/
void ScreenPlayManager::createWidget(
const InstalledType::InstalledType type,
const QPoint& position,
const QString& absoluteStoragePath,
const QString& previewImage)
const QString& previewImage,
const bool saveToProfilesConfigFile)
{
auto saveToProfile = qScopeGuard([=, this] {
// Do not save on app start
if (saveToProfilesConfigFile) {
saveProfiles();
}
});
const QString appID = Util::generateRandomString();
const QString path = QUrl::fromUserInput(absoluteStoragePath).toLocalFile();
@ -126,7 +135,7 @@ void ScreenPlayManager::createWidget(
}
increaseActiveWidgetsCounter();
m_screenPlayWidgets.append(std::make_unique<ScreenPlayWidget>(appID, m_globalVariables, path, previewImage, type));
m_screenPlayWidgets.append(std::make_unique<ScreenPlayWidget>(appID, m_globalVariables, position, path, previewImage, type));
}
/*!
@ -170,6 +179,7 @@ void ScreenPlayManager::removeAllWidgets()
saveProfiles();
setActiveWidgetsCounter(0);
}
}
/*!
@ -271,14 +281,18 @@ bool ScreenPlayManager::saveProfiles()
{
QJsonArray wallpaper {};
for (const auto activeWallpaper : m_screenPlayWallpapers) {
for (const auto& activeWallpaper : m_screenPlayWallpapers) {
wallpaper.append(activeWallpaper->getActiveSettingsJson());
}
QJsonArray widgets {};
for (const auto& activeWidget : m_screenPlayWidgets) {
widgets.append(activeWidget->getActiveSettingsJson());
}
QJsonObject profileDefault;
profileDefault.insert("widgets", QJsonArray {});
profileDefault.insert("appdrawer", QJsonArray {});
profileDefault.insert("widgets", widgets);
profileDefault.insert("wallpaper", wallpaper);
profileDefault.insert("name", "default");
@ -371,18 +385,35 @@ void ScreenPlayManager::loadProfiles()
if (volume == -1.0F)
volume = 1.0f;
QString absolutePath = wallpaperObj.value("absolutePath").toString();
QString fillModeString = wallpaperObj.value("fillMode").toString();
QString previewImage = wallpaperObj.value("previewImage").toString();
QString file = wallpaperObj.value("file").toString();
QString typeString = wallpaperObj.value("type").toString();
const QString absolutePath = wallpaperObj.value("absolutePath").toString();
const QString fillModeString = wallpaperObj.value("fillMode").toString();
const QString previewImage = wallpaperObj.value("previewImage").toString();
const QString file = wallpaperObj.value("file").toString();
const QString typeString = wallpaperObj.value("type").toString();
auto type = QStringToEnum<InstalledType::InstalledType>(typeString, InstalledType::InstalledType::VideoWallpaper);
auto fillMode = QStringToEnum<FillMode::FillMode>(fillModeString, FillMode::FillMode::Cover);
const auto type = QStringToEnum<InstalledType::InstalledType>(typeString, InstalledType::InstalledType::VideoWallpaper);
const auto fillMode = QStringToEnum<FillMode::FillMode>(fillModeString, FillMode::FillMode::Cover);
createWallpaper(type, fillMode, absolutePath, previewImage, file, monitors, volume, false);
monitors.clear();
}
for (const QJsonValueRef widget : wallpaper.toObject().value("widgets").toArray()) {
QJsonObject widgetObj = widget.toObject();
if (widgetObj.empty())
continue;
const QString absolutePath = widgetObj.value("absolutePath").toString();
const QString previewImage = widgetObj.value("previewImage").toString();
const QString typeString = widgetObj.value("type").toString();
const int positionX = widgetObj.value("positionX").toInt(0);
const int positionY = widgetObj.value("positionY").toInt(0);
const QPoint position { positionX, positionY };
const auto type = QStringToEnum<InstalledType::InstalledType>(typeString, InstalledType::InstalledType::QMLWidget);
createWidget(type, position, absolutePath, previewImage, false);
}
}
}
}

View File

@ -102,10 +102,10 @@ public slots:
const float volume,
const bool saveToProfilesConfigFile);
void createWidget(
const ScreenPlay::InstalledType::InstalledType type,
void createWidget(const ScreenPlay::InstalledType::InstalledType type, const QPoint &position,
const QString& absoluteStoragePath,
const QString& previewImage);
const QString& previewImage,
const bool saveToProfilesConfigFile);
void removeAllWallpapers();
void removeAllWidgets();

View File

@ -16,21 +16,24 @@ namespace ScreenPlay {
ScreenPlayWidget::ScreenPlayWidget(
const QString& appID,
const std::shared_ptr<GlobalVariables>& globalVariables,
const QString& projectPath,
const QPoint& position,
const QString& absolutePath,
const QString& previewImage,
const InstalledType::InstalledType type)
: QObject { nullptr }
, m_globalVariables { globalVariables }
, m_projectPath { projectPath }
, m_previewImage { previewImage }
, m_appID { appID }
, m_position { 0, 0 }
, m_position { position }
, m_type { type }
, m_absolutePath { absolutePath }
{
const QStringList proArgs {
m_projectPath,
m_absolutePath,
QString { "appID=" + m_appID },
QVariant::fromValue(m_type).toString()
QVariant::fromValue(m_type).toString(),
QString::number(m_position.x()),
QString::number(m_position.y()),
};
m_process.setArguments(proArgs);
@ -43,4 +46,15 @@ ScreenPlayWidget::ScreenPlayWidget(
});
m_process.startDetached();
}
QJsonObject ScreenPlayWidget::getActiveSettingsJson()
{
QJsonObject obj;
obj.insert("previewImage", m_previewImage);
obj.insert("absolutePath", m_absolutePath);
obj.insert("positionX", m_position.x());
obj.insert("positionY", m_position.y());
obj.insert("type", QVariant::fromValue(m_type).toString());
return obj;
}
}

View File

@ -36,6 +36,7 @@
#include <QCoreApplication>
#include <QDebug>
#include <QJsonObject>
#include <QObject>
#include <QPoint>
#include <QProcess>
@ -49,7 +50,7 @@ namespace ScreenPlay {
class ScreenPlayWidget : public QObject {
Q_OBJECT
Q_PROPERTY(QString projectPath READ projectPath WRITE setProjectPath NOTIFY projectPathChanged)
Q_PROPERTY(QString absolutePath READ absolutePath WRITE setAbsolutePath NOTIFY absolutePathChanged)
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)
@ -58,17 +59,13 @@ class ScreenPlayWidget : public QObject {
public:
explicit ScreenPlayWidget(const QString& appID,
const std::shared_ptr<GlobalVariables>& globalVariables,
const QString& projectPath,
const QPoint& position,
const QString& absolutePath,
const QString& previewImage,
const InstalledType::InstalledType type);
~ScreenPlayWidget() { }
QString projectPath() const
{
return m_projectPath;
}
QString previewImage() const
{
return m_previewImage;
@ -79,6 +76,11 @@ public:
return m_position;
}
QString absolutePath() const
{
return m_absolutePath;
}
QString appID() const
{
return m_appID;
@ -90,14 +92,7 @@ public:
}
public slots:
void setProjectPath(QString projectPath)
{
if (m_projectPath == projectPath)
return;
m_projectPath = projectPath;
emit projectPathChanged(m_projectPath);
}
QJsonObject getActiveSettingsJson();
void setPreviewImage(QString previewImage)
{
@ -135,21 +130,30 @@ public slots:
emit typeChanged(m_type);
}
void setAbsolutePath(QString absolutePath)
{
if (m_absolutePath == absolutePath)
return;
m_absolutePath = absolutePath;
emit absolutePathChanged(m_absolutePath);
}
signals:
void projectPathChanged(QString projectPath);
void previewImageChanged(QString previewImage);
void positionChanged(QPoint position);
void appIDChanged(QString appID);
void typeChanged(InstalledType::InstalledType type);
void absolutePathChanged(QString absolutePath);
private:
QProcess m_process;
const std::shared_ptr<GlobalVariables>& m_globalVariables;
QString m_projectPath;
QString m_previewImage;
QString m_appID;
QPoint m_position;
InstalledType::InstalledType m_type;
QString m_absolutePath;
};
}

View File

@ -16,20 +16,29 @@ int main(int argc, char* argv[])
// If we start with only one argument (path, appID, type),
// it means we want to test a single widget
if (argumentList.length() == 1) {
WidgetWindow spwmw("test", "appid", "qmlWidget");
WidgetWindow spwmw("test", { 0, 0 }, "appid", "qmlWidget");
return app.exec();
}
if (argumentList.length() != 4) {
if (argumentList.length() != 6) {
return -3;
}
ScreenPlaySDK sdk(argumentList.at(2), argumentList.at(3));
// 1. Project path, 2. AppID, 3. Type
WidgetWindow spwmw(argumentList.at(1), argumentList.at(2), argumentList.at(3));
bool okPosX = false;
int positionX = QVariant(argumentList.at(4)).toInt(&okPosX);
if (!okPosX) {
qWarning() << "Could not parse PositionX value to int: " << argumentList.at(4);
positionX = 0;
}
bool okPosY = false;
int positionY = QVariant(argumentList.at(5)).toInt(&okPosY);
if (!okPosY) {
qWarning() << "Could not parse PositionY value to int: " << argumentList.at(5);
positionY = 0;
}
QObject::connect(&sdk, &ScreenPlaySDK::sdkDisconnected, &spwmw, &WidgetWindow::qmlExit);
QObject::connect(&sdk, &ScreenPlaySDK::incommingMessage, &spwmw, &WidgetWindow::messageReceived);
// 0. App Path, 1. Project path, 2. AppID, 3. Type, 4. Posx, 5. PosY
WidgetWindow spwmw(argumentList.at(1), QPoint { positionX, positionY }, argumentList.at(2), argumentList.at(3));
return app.exec();
}

View File

@ -2,11 +2,21 @@
#include <QCoreApplication>
WidgetWindow::WidgetWindow(const QString projectPath, const QString appid, const QString type)
WidgetWindow::WidgetWindow(const QString& projectPath,
const QPoint& position,
const QString& appid,
const QString& type)
: QObject(nullptr)
, m_appID { appid }
, m_type { type }
, m_position { position }
{
m_sdk = std::make_unique<ScreenPlaySDK>(appid, type);
QObject::connect(m_sdk.get(), &ScreenPlaySDK::sdkDisconnected, this, &WidgetWindow::qmlExit);
QObject::connect(m_sdk.get(), &ScreenPlaySDK::incommingMessage, this, &WidgetWindow::messageReceived);
QStringList availableTypes {
"qmlWidget",
"htmlWidget"
@ -15,6 +25,11 @@ WidgetWindow::WidgetWindow(const QString projectPath, const QString appid, const
if (!availableTypes.contains(m_type)) {
QApplication::exit(-4);
}
auto sendPositionUpdate = [this](int arg) {
m_sdk->sendMessage({});
};
QObject::connect(&m_window, &QWindow::xChanged, this, sendPositionUpdate);
QObject::connect(&m_window, &QWindow::yChanged, this, sendPositionUpdate);
Qt::WindowFlags flags = m_window.flags();
m_window.setWidth(300);
@ -40,7 +55,7 @@ WidgetWindow::WidgetWindow(const QString projectPath, const QString appid, const
configJsonDocument = QJsonDocument::fromJson(m_projectConfig.toUtf8(), &parseError);
if (!(parseError.error == QJsonParseError::NoError)) {
qWarning() << "Settings Json Parse Error " << parseError.errorString() << configTmp.fileName();
qWarning() << "Settings Json Parse Error " << parseError.errorString() << configTmp.fileName();
}
m_project = configJsonDocument.object();
@ -55,6 +70,7 @@ WidgetWindow::WidgetWindow(const QString projectPath, const QString appid, const
m_window.setTextRenderType(QQuickWindow::TextRenderType::NativeTextRendering);
// m_window.setResizeMode(QQuickView::ResizeMode::SizeViewToRootObject);
m_window.setSource(QUrl("qrc:/Widget.qml"));
m_window.setPosition(m_position);
m_window.show();
}

View File

@ -34,9 +34,9 @@
#pragma once
#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
@ -55,16 +55,25 @@
#include <qt_windows.h>
#endif
#include <memory>
#include "screenplaysdk.h"
class WidgetWindow : public QObject {
Q_OBJECT
public:
explicit WidgetWindow(const QString projectPath, const QString appid, const QString type);
explicit WidgetWindow(
const QString& projectPath,
const QPoint& position,
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)
Q_PROPERTY(QString projectConfig READ projectConfig WRITE setProjectConfig NOTIFY projectConfigChanged)
Q_PROPERTY(QString sourcePath READ sourcePath WRITE setSourcePath NOTIFY sourcePathChanged)
Q_PROPERTY(QPoint position READ position WRITE setPosition NOTIFY positionChanged)
QString appID() const
{
@ -86,6 +95,11 @@ public:
return m_sourcePath;
}
QPoint position() const
{
return m_position;
}
signals:
void qmlExit();
@ -95,6 +109,8 @@ signals:
void sourcePathChanged(QString sourcePath);
void qmlSceneValueReceived(QString key, QString value);
void positionChanged(QPoint position);
public slots:
void setSize(QSize size);
void destroyThis();
@ -142,6 +158,15 @@ public slots:
void setWindowBlur(unsigned int style = 3);
#endif
void setPosition(QPoint position)
{
if (m_position == position)
return;
m_position = position;
emit positionChanged(m_position);
}
private:
QString m_appID { "" };
QString m_type { "qmlWidget" };
@ -156,4 +181,6 @@ private:
#ifdef Q_OS_WIN
HWND m_hwnd;
#endif
QPoint m_position;
std::unique_ptr<ScreenPlaySDK> m_sdk;
};