From 14bddbe9911d7c6426a065e744501fbc3d095ba6 Mon Sep 17 00:00:00 2001 From: JeJo Date: Mon, 22 Apr 2019 04:57:14 +0200 Subject: [PATCH] class ScreenPlay - minor changes and improvements --- ScreenPlay/src/screenplay.cpp | 140 ++++++++++++++++++---------------- ScreenPlay/src/screenplay.h | 67 ++++++++++------ 2 files changed, 117 insertions(+), 90 deletions(-) diff --git a/ScreenPlay/src/screenplay.cpp b/ScreenPlay/src/screenplay.cpp index 0dd043ce..1290a110 100644 --- a/ScreenPlay/src/screenplay.cpp +++ b/ScreenPlay/src/screenplay.cpp @@ -1,97 +1,93 @@ #include "screenplay.h" ScreenPlay::ScreenPlay(InstalledListModel* ilm, Settings* set, MonitorListModel* mlm, SDKConnector* sdkc, QObject* parent) - : QObject(parent) -{ - m_ilm = ilm; - m_settings = set; - m_mlm = mlm; - m_qGuiApplication = static_cast(QGuiApplication::instance()); - m_sdkc = sdkc; -} + : QObject{parent}, + m_ilm{ilm}, + m_settings{set}, + m_mlm{mlm}, + m_qGuiApplication{static_cast(QGuiApplication::instance())}, + m_sdkc{sdkc} +{} void ScreenPlay::createWallpaper(int monitorIndex, QUrl absoluteStoragePath, QString previewImage, float volume, QString fillMode, QString type) +void ScreenPlay::createWallpaper( + const int monitorIndex, QUrl absoluteStoragePath, + const QString &previewImage, const float volume, + const QString &fillMode, const QString &type) { - ProjectFile project; - + ProjectFile project{}; if (!m_ilm->getProjectByAbsoluteStoragePath(&absoluteStoragePath, &project)) { return; } - - // Remove previous wallpaper - removeWallpaperAt(monitorIndex); - + // Remove previous wallpaper, if any + //this->removeAllWallpaper(); + this->removeWallpaperAt(0); m_settings->increaseActiveWallpaperCounter(); - QVector tmpMonitorIndex; - tmpMonitorIndex.append(monitorIndex); - m_screenPlayWallpaperList.append(QSharedPointer(new ScreenPlayWallpaper(tmpMonitorIndex, absoluteStoragePath.toLocalFile(), previewImage, volume, fillMode, type, this))); - - m_mlm->setWallpaperActiveMonitor(m_qGuiApplication->screens().at(monitorIndex), absoluteStoragePath.toLocalFile() + "/" + previewImage); + m_screenPlayWallpaperList.emplace_back( + RefSPWall::create( + QVector{monitorIndex}, absoluteStoragePath.toLocalFile(), + previewImage, volume, fillMode, type, this) + ); + m_mlm->setWallpaperActiveMonitor(m_qGuiApplication->screens().at(monitorIndex), + QString{absoluteStoragePath.toLocalFile() + "/" + previewImage}); } -void ScreenPlay::createWidget(QUrl absoluteStoragePath, QString previewImage) +void ScreenPlay::createWidget(QUrl absoluteStoragePath, const QString &previewImage) { - ProjectFile project; + ProjectFile project{}; if (!m_ilm->getProjectByAbsoluteStoragePath(&absoluteStoragePath, &project)) { return; } - - QString fullPath = absoluteStoragePath.toLocalFile() + "/" + project.m_file.toString(); - - m_screenPlayWidgetList.append(QSharedPointer(new ScreenPlayWidget(absoluteStoragePath.toLocalFile(), previewImage, fullPath, this))); + m_screenPlayWidgetList.emplace_back( + RefSPWidget::create( + absoluteStoragePath.toLocalFile(), previewImage, + QString{absoluteStoragePath.toLocalFile() + "/" + project.m_file.toString()}, + this) + ); } -void ScreenPlay::removeAllWallpaper() +void ScreenPlay::removeAllWallpaper() noexcept { - m_sdkc->closeAllWallpapers(); - m_settings->setActiveWallpaperCounter(0); - m_screenPlayWallpaperList.clear(); - emit allWallpaperRemoved(); + if(m_sdkc && m_settings && !m_screenPlayWallpaperList.empty()){ + m_sdkc->closeAllWallpapers(); + m_settings->setActiveWallpaperCounter(0); + m_screenPlayWallpaperList.clear(); + emit allWallpaperRemoved(); + } + return; } -void ScreenPlay::requestProjectSettingsListModelAt(int index) +void ScreenPlay::requestProjectSettingsListModelAt(const int index) const noexcept { - - //Q_ASSERT(index > m_mlm->size()); - for (int i = 0; i < m_screenPlayWallpaperList.count(); ++i) { - if (m_screenPlayWallpaperList.at(i).data()->screenNumber().at(0) == index) { - emit projectSettingsListModelFound(m_screenPlayWallpaperList.at(i).data()->projectSettingsListModel().data(), m_screenPlayWallpaperList.at(i).data()->type()); + for (const RefSPWall &refSPWallpaper: m_screenPlayWallpaperList) { + if (!refSPWallpaper.data()->screenNumber().empty() && + refSPWallpaper.data()->screenNumber()[0] == index) { // ??? only at index == 0 + emit projectSettingsListModelFound( + refSPWallpaper.data()->projectSettingsListModel().data(), + refSPWallpaper.data()->type()); return; } } emit projectSettingsListModelNotFound(); } -QString ScreenPlay::generateID() +void ScreenPlay::setWallpaperValue(const int at, const QString &key, const QString &value) noexcept { - const QString possibleCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); - const int randomStringLength = 32; - auto* radomGen = QRandomGenerator::system(); - - QString randomString; - for (int i = 0; i < randomStringLength; ++i) { - int index = radomGen->bounded(possibleCharacters.length()); - QChar nextChar = possibleCharacters.at(index); - randomString.append(nextChar); - } - return randomString; -} - -void ScreenPlay::setWallpaperValue(int at, QString key, QString value) -{ - Q_ASSERT(at < m_screenPlayWallpaperList.size()); - for (int i = 0; i < m_screenPlayWallpaperList.count(); ++i) { - if (m_screenPlayWallpaperList.at(i).data()->screenNumber().at(0) == at) { - m_sdkc->setWallpaperValue(m_screenPlayWallpaperList.at(i).data()->appID(), key, value); + Q_ASSERT(static_cast(at) < m_screenPlayWallpaperList.size() && m_sdkc); + for (const RefSPWall &refSPWallpaper: m_screenPlayWallpaperList) { + if (!refSPWallpaper.data()->screenNumber().empty() && m_sdkc && + refSPWallpaper.data()->screenNumber()[0] == at) { // ??? only at index == 0 + m_sdkc->setWallpaperValue(refSPWallpaper.data()->appID(), key, value); return; } } } -void ScreenPlay::setAllWallpaperValue(QString key, QString value) +void ScreenPlay::setAllWallpaperValue(const QString &key, const QString &value) noexcept { - for (int i = 0; i < m_screenPlayWallpaperList.count(); ++i) { - m_sdkc->setWallpaperValue(m_screenPlayWallpaperList.at(i).data()->appID(), key, value); + Q_ASSERT(m_sdkc); + for (const RefSPWall &refSPWallpaper: m_screenPlayWallpaperList) { + if(m_sdkc) m_sdkc->setWallpaperValue(refSPWallpaper.data()->appID(), key, value); } } @@ -112,20 +108,30 @@ void ScreenPlay::removeWallpaperAt(int at) } } -QVector ScreenPlay::getMonitorByAppID(QString appID) +QVector ScreenPlay::getMonitorByAppID(const QString &appID) const { - for (int i = 0; i < m_screenPlayWallpaperList.length(); ++i) { - if (m_screenPlayWallpaperList.at(i).data()->appID() == appID) { - return m_screenPlayWallpaperList.at(i).data()->screenNumber(); + for (const RefSPWall &refSPWallpaper: m_screenPlayWallpaperList) { + if (refSPWallpaper.data()->appID() == appID) { + return refSPWallpaper.data()->screenNumber(); } } - - return QVector(); + return QVector{}; } -Settings* ScreenPlay::settings() const +QString ScreenPlay::generateID() const { - return m_settings; + const QString possibleCharacters{ + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"}; + const int randomStringLength = 32; + const auto radomGen = QRandomGenerator::system(); + + QString randomString; + for (int i = 0; i < randomStringLength; ++i) { + const int index = radomGen->bounded(possibleCharacters.length()); + const QChar nextChar = possibleCharacters.at(index); + randomString.append(nextChar); + } + return randomString; } QSharedPointer ScreenPlayWallpaper::projectSettingsListModel() const diff --git a/ScreenPlay/src/screenplay.h b/ScreenPlay/src/screenplay.h index e720954e..ed4cb59c 100644 --- a/ScreenPlay/src/screenplay.h +++ b/ScreenPlay/src/screenplay.h @@ -24,40 +24,60 @@ class ScreenPlayWallpaper; class ScreenPlayWidget; +// conveniences types +using RefSPWall = QSharedPointer; +using RefSPWidget = QSharedPointer; + class ScreenPlay : public QObject { Q_OBJECT +private: + InstalledListModel *const m_ilm{nullptr}; + Settings *const m_settings{nullptr}; + MonitorListModel *const m_mlm{nullptr}; + QGuiApplication *const m_qGuiApplication{nullptr}; + SDKConnector *const m_sdkc{nullptr}; + std::vector m_screenPlayWallpaperList; + std::vector m_screenPlayWidgetList; + public: - explicit ScreenPlay(InstalledListModel* ilm, Settings* set, MonitorListModel* mlm, SDKConnector* sdkc, QObject* parent = nullptr); + // constructor(s) + explicit ScreenPlay( + InstalledListModel* ilm, Settings* set, + MonitorListModel* mlm, SDKConnector* sdkc, + QObject* parent = nullptr); + + // copy and move disable(for now) : remember rule of 1/3/5 + Q_DISABLE_COPY_MOVE(ScreenPlay) Settings* settings() const; - InstalledListModel* m_ilm; - Settings* m_settings; - MonitorListModel* m_mlm; - QGuiApplication* m_qGuiApplication; - SDKConnector* m_sdkc; signals: - void allWallpaperRemoved(); - void projectSettingsListModelFound(ProjectSettingsListModel* li, QString type); - void projectSettingsListModelNotFound(); + void allWallpaperRemoved() const; + void projectSettingsListModelFound(ProjectSettingsListModel* li, const QString &type) const; + void projectSettingsListModelNotFound() const; public slots: - void createWallpaper(int monitorIndex, QUrl absoluteStoragePath, QString previewImage, float volume, QString fillMode, QString type); - void createWidget(QUrl absoluteStoragePath, QString previewImage); - void removeAllWallpaper(); - void requestProjectSettingsListModelAt(int index); - void setWallpaperValue(int at, QString key, QString value); - void setAllWallpaperValue(QString key, QString value); void removeWallpaperAt(int at); - QVector getMonitorByAppID(QString appID); - QString generateID(); - -private: - QVector> m_screenPlayWallpaperList; - QVector> m_screenPlayWidgetList; + void createWallpaper( + const int monitorIndex, QUrl absoluteStoragePath, + const QString &previewImage, const float volume, + const QString &fillMode, const QString &type); + void createWidget(QUrl absoluteStoragePath, const QString &previewImage); + void removeAllWallpaper() noexcept; + void requestProjectSettingsListModelAt(const int index) const noexcept; + void setWallpaperValue(const int at, const QString &key, const QString &value) noexcept; + void setAllWallpaperValue(const QString &key, const QString &value) noexcept; + void removeWallpaperAt(const int at); + QVector getMonitorByAppID(const QString &appID) const; + QString generateID() const; }; +/*! + \class ScreenPlayWallpaper + \brief Used for Creation of Wallpaper, Scenes and Widgets +*/ + class ScreenPlayWallpaper : public QObject { Q_OBJECT @@ -72,6 +92,7 @@ public: { m_screenNumber = screenNumber; m_projectPath = projectPath; + m_previewImage = previewImage; m_type = type; @@ -93,7 +114,7 @@ public: proArgs.append(m_projectPath); m_appID = parent->generateID(); proArgs.append("appID=" + m_appID); - proArgs.append(parent->m_settings->decoder()); + proArgs.append(parent->settings()->decoder()); proArgs.append(QString::number(static_cast(volume))); proArgs.append(fillMode); @@ -225,7 +246,7 @@ public: if (fullPath.endsWith(".exe")) { m_process->setProgram(fullPath); } else if (fullPath.endsWith(".qml")) { - m_process->setProgram(parent->m_settings->getScreenPlayWidgetPath().path()); + m_process->setProgram(parent->settings()->getScreenPlayWidgetPath().path()); } qDebug() << m_process->program(); connect(m_process, &QProcess::errorOccurred, this, [](QProcess::ProcessError error) {