From 358800a94c03433d81f3656bce81a7513ccb4173 Mon Sep 17 00:00:00 2001 From: Elias Steurer Date: Wed, 22 May 2024 11:00:52 +0200 Subject: [PATCH] Add timeline reset --- .../inc/public/ScreenPlay/screenplaymanager.h | 10 +- .../public/ScreenPlay/screenplaywallpaper.h | 3 +- ScreenPlay/profiles.json | 1 - ScreenPlay/qml/Components/Timeline.qml | 16 ++- ScreenPlay/src/screenplaymanager.cpp | 103 ++++++++++++------ .../inc/public/ScreenPlayUtil/util.h | 2 +- 6 files changed, 89 insertions(+), 46 deletions(-) diff --git a/ScreenPlay/inc/public/ScreenPlay/screenplaymanager.h b/ScreenPlay/inc/public/ScreenPlay/screenplaymanager.h index 604a112d..9d4a71f3 100644 --- a/ScreenPlay/inc/public/ScreenPlay/screenplaymanager.h +++ b/ScreenPlay/inc/public/ScreenPlay/screenplaymanager.h @@ -4,9 +4,8 @@ #include #include -#include #include -#include +#include #include "ScreenPlayUtil/projectfile.h" #include "ScreenPlayUtil/util.h" @@ -51,7 +50,6 @@ public: Q_INVOKABLE bool removeAllWidgets(bool saveToProfile = false); Q_INVOKABLE bool removeWallpaperAt(const int index); - Q_INVOKABLE ScreenPlayWallpaper* getWallpaperByAppID(const QString& appID); Q_INVOKABLE bool moveTimelineAt( @@ -62,12 +60,11 @@ public: Q_INVOKABLE QString getTimeString(double relativeLinePosition); - Q_INVOKABLE bool addTimelineAt( const int index, const float reltiaveLinePosition, QString identifier); - + Q_INVOKABLE void removeAllTimlineSections(); Q_INVOKABLE bool removeTimelineAt(const int index); Q_INVOKABLE QVariantMap initialStopPositions(); Q_INVOKABLE bool setWallpaperAtTimelineIndex( @@ -184,8 +181,6 @@ private: std::shared_ptr m_monitorListModel; std::shared_ptr m_settings; std::unique_ptr m_server; - std::unique_ptr m_websocketServer; - QVector m_connections; std::shared_ptr m_activeWallpaperTimeline; QVector> m_wallpaperTimelineSectionsList; @@ -201,6 +196,5 @@ private: // We use a 24 hour system const QString m_timelineTimeFormat = "hh:mm:ss"; const quint16 m_webSocketPort = 16395; - }; } diff --git a/ScreenPlay/inc/public/ScreenPlay/screenplaywallpaper.h b/ScreenPlay/inc/public/ScreenPlay/screenplaywallpaper.h index de0cc42d..797084c0 100644 --- a/ScreenPlay/inc/public/ScreenPlay/screenplaywallpaper.h +++ b/ScreenPlay/inc/public/ScreenPlay/screenplaywallpaper.h @@ -46,7 +46,8 @@ struct WallpaperTimelineSection { QTime startTime; QTime endTime; // Data from the profiles.json that we need when we - // enable this section of the pipeline + // enable this section of the pipeline. We keep a copy + // here when this timeline needs to become active std::vector wallpaperData; // All active wallpaper. std::vector> activeWallpaperList; diff --git a/ScreenPlay/profiles.json b/ScreenPlay/profiles.json index 72c748b4..c2f8336c 100644 --- a/ScreenPlay/profiles.json +++ b/ScreenPlay/profiles.json @@ -1,7 +1,6 @@ { "profiles": [ { - "appdrawer": [], "name": "default", "timelineWallpaper": [ { diff --git a/ScreenPlay/qml/Components/Timeline.qml b/ScreenPlay/qml/Components/Timeline.qml index 765d42ad..497b2583 100644 --- a/ScreenPlay/qml/Components/Timeline.qml +++ b/ScreenPlay/qml/Components/Timeline.qml @@ -63,7 +63,15 @@ Control { } timeLine.sectionsList = [] - init() + App.screenPlayManager.removeAllTimlineSections() + + const position = 1.0 + const identifier = App.util.generateRandomString(4); + const sectionObject = timeLine.addSection(identifier,position) + App.screenPlayManager.addTimelineAt( + sectionObject.index, + sectionObject.relativeLinePosition, + sectionObject.identifier) } function createAllSections(initialStopPositions) { @@ -242,11 +250,13 @@ Control { //timeLine.sectionsList[i].relativeLinePosition =prevPos / timeLine.width - //print("sections: ", i, "prev minimum ",prevPos,"next maximum", nextPos, timeLine.sectionsList[i].relativeLinePosition) + print("sections: ", i, "prev minimum ",prevPos,"next maximum", nextPos, timeLine.sectionsList[i].relativeLinePosition) } + print("++++++++++++++++++") for (let i = 0; i < timeLine.sectionsList.length; i++) { let section = timeLine.sectionsList[i] section.relativeLinePosition = section.lineHandle.linePosition + // print(section.relativeLinePosition, section.lineHandle.lineMinimum, section.lineHandle.lineMaximum) } updateIndicatorPositions() updateLastHandle() @@ -342,7 +352,7 @@ Control { text: "➕" onClicked: { const p = this.x / timeLine.width - const position = p.toFixed(2) + const position = p.toFixed(4) const identifier = App.util.generateRandomString(4); const sectionObject = timeLine.addSection(identifier,position) App.screenPlayManager.addTimelineAt( diff --git a/ScreenPlay/src/screenplaymanager.cpp b/ScreenPlay/src/screenplaymanager.cpp index f180d3ef..28b242a1 100644 --- a/ScreenPlay/src/screenplaymanager.cpp +++ b/ScreenPlay/src/screenplaymanager.cpp @@ -93,7 +93,6 @@ void ScreenPlayManager::checkActiveWallpaperTimeline() // TODO: Check if we can reuse some active wallpaper } - /*! \brief Qml function, because we cannot create the WallpaperData in qml. */ @@ -125,12 +124,12 @@ bool ScreenPlayManager::setWallpaperAtTimelineIndex( for (auto& timelineSection : m_wallpaperTimelineSectionsList) { const bool sameIndex = timelineSection->index == timelineIndex; const bool sameIdentifier = timelineSection->identifier == identifier; - if(sameIndex && sameIdentifier){ + if (sameIndex && sameIdentifier) { // TODO vec - timelineSection->wallpaperData = {wallpaperData}; + timelineSection->wallpaperData = { wallpaperData }; break; - } else if(sameIdentifier || sameIdentifier){ - qCritical()<< "Invalid"; + } else if (sameIdentifier || sameIdentifier) { + qCritical() << "Invalid"; } } @@ -281,13 +280,13 @@ bool ScreenPlayManager::removeAllWallpapers(bool saveToProfile) { if (m_wallpaperTimelineSectionsList.empty()) { - qWarning() << "Trying to remove all Wallpapers while m_screenPlayWallpapers is not empty."; return false; } QStringList appIDs; auto activeTimelineSection = findActiveSection(); if (!activeTimelineSection) { + qWarning() << "Trying to remove all Wallpapers while findActiveSection is empty."; return false; } // Do not remove items from the vector you iterate on. @@ -313,7 +312,6 @@ bool ScreenPlayManager::removeAllWallpapers(bool saveToProfile) bool ScreenPlayManager::removeAllWidgets(bool saveToProfile) { if (m_screenPlayWidgets.empty()) { - qWarning() << "Trying to remove all Widgets while m_screenPlayWidgets is empty."; return false; } @@ -438,8 +436,8 @@ ScreenPlayWallpaper* ScreenPlayManager::getWallpaperByAppID(const QString& appID return nullptr; } - // We always handle the endTimeString, because it is the handle for the - // timeline. The last, default, timeline does not have a handle. +// We always handle the endTimeString, because it is the handle for the +// timeline. The last, default, timeline does not have a handle. bool ScreenPlayManager::moveTimelineAt(const int index, const QString identifier, const float relativePosition, QString positionTimeString) { m_contentTimer.stop(); @@ -466,11 +464,16 @@ bool ScreenPlayManager::moveTimelineAt(const int index, const QString identifier wallpapterTimelineSectionNext->startTime = newPositionTime; } printTimelines(); + emit requestSaveProfiles(); return true; } QString ScreenPlayManager::getTimeString(double relativeLinePosition) { + if (relativeLinePosition == 1.0) { + // We overwrite the endTime here + return "23:59:59"; + } const double totalHours = relativeLinePosition * 24; int hours = static_cast(std::floor(totalHours)); // Gets the whole hour part double fractionalHours = totalHours - hours; @@ -545,23 +548,51 @@ bool ScreenPlayManager::addTimelineAt(const int index, const float reltiaveLineP newTimelineSection->index = index; newTimelineSection->identifier = identifier; newTimelineSection->endTime = newStopPositionTime; - // We can use the given index here, because it points - // the the current item at that index, and we have not yet - // added our new timelineSection to our list. - newTimelineSection->startTime = m_wallpaperTimelineSectionsList.at(index)->startTime; + // In case we do a full reset, we must set the start time manually + if (m_wallpaperTimelineSectionsList.empty()) { + newTimelineSection->startTime = QTime::fromString("00:00:00", m_timelineTimeFormat); + } else { + + // We can use the given index here, because it points + // the the current item at that index, and we have not yet + // added our new timelineSection to our list. + newTimelineSection->startTime = m_wallpaperTimelineSectionsList.at(index)->startTime; + } const bool isLast = (m_wallpaperTimelineSectionsList.length() - 1) == index; - if(isLast){ - m_wallpaperTimelineSectionsList.last()->startTime = newTimelineSection->endTime; + if (isLast) { + m_wallpaperTimelineSectionsList.last()->startTime = newTimelineSection->endTime; } m_wallpaperTimelineSectionsList.append(newTimelineSection); updateIndices(); printTimelines(); + emit requestSaveProfiles(); return true; } +void ScreenPlayManager::removeAllTimlineSections() +{ + m_contentTimer.stop(); + auto updateTimer = qScopeGuard([this] { m_contentTimer.start(); }); + + // First check if there is any active wallpaper that we save in + // this shared ptr. We can have many timlines, but the current timeline + // can have no active wallpaper + if (m_activeWallpaperTimeline) { + // Close the localsocket + for (auto& activeWallpaper : m_activeWallpaperTimeline->activeWallpaperList) { + activeWallpaper->close(); + } + // Reset all active wallpaper + m_activeWallpaperTimeline->activeWallpaperList.clear(); + m_activeWallpaperTimeline.reset(); + } + + m_wallpaperTimelineSectionsList.clear(); +} + bool ScreenPlayManager::removeTimelineAt(const int index) { printTimelines(); @@ -603,9 +634,9 @@ bool ScreenPlayManager::removeTimelineAt(const int index) // check for the timelineAfter, because the last timeline // cannot be deleted QTime endTime; - if(index == 0){ + if (index == 0) { endTime = QTime::fromString("00:00:00", m_timelineTimeFormat); - }else { + } else { endTime = m_wallpaperTimelineSectionsList.at(index - 1)->endTime; } auto timelineAfter = m_wallpaperTimelineSectionsList.at(index + 1); @@ -618,9 +649,8 @@ bool ScreenPlayManager::removeTimelineAt(const int index) m_wallpaperTimelineSectionsList.removeAt(index); updateIndices(); printTimelines(); - return true; - printTimelines(); + emit requestSaveProfiles(); return true; } @@ -628,7 +658,7 @@ void ScreenPlayManager::printTimelines() { std::cout << "#############################\n"; for (auto& timeline : m_wallpaperTimelineSectionsList) { - std::cout <index << ": " << timeline->identifier.toStdString() << "\t" << timeline->relativePosition << " start: " << timeline->startTime.toString().toStdString() << " end: " << timeline->endTime.toString().toStdString() << std::endl; + std::cout << timeline->index << ": " << timeline->identifier.toStdString() << "\t" << timeline->relativePosition << " start: " << timeline->startTime.toString().toStdString() << " end: " << timeline->endTime.toString().toStdString() << std::endl; } } @@ -636,7 +666,7 @@ QVariantMap ScreenPlayManager::initialStopPositions() { QVariantMap sectionPositions; for (const auto& timelineSection : m_wallpaperTimelineSectionsList) { - sectionPositions.insert({timelineSection->identifier},{timelineSection->relativePosition}); + sectionPositions.insert({ timelineSection->identifier }, { timelineSection->relativePosition }); } return sectionPositions; } @@ -796,30 +826,39 @@ bool ScreenPlayManager::saveProfiles() { m_saveLimiter.stop(); - // QJsonArray wallpaper {}; - // for (const auto& activeWallpaper : std::as_const(m_screenPlayWallpapers)) { - // wallpaper.append(activeWallpaper->getActiveSettingsJson()); - // } + QJsonArray timelineWallpaperList {}; + for (const auto& activeTimelineWallpaper : std::as_const(m_wallpaperTimelineSectionsList)) { + QJsonObject timelineWallpaper; + timelineWallpaper.insert("startTime", activeTimelineWallpaper->startTime.toString()); + timelineWallpaper.insert("endTime", activeTimelineWallpaper->endTime.toString()); + QJsonArray wallpaper; + for (const auto& activeWallpaper : activeTimelineWallpaper->activeWallpaperList) { + wallpaper.append(activeWallpaper->getActiveSettingsJson()); + } + timelineWallpaper.insert("wallpaper", wallpaper); - QJsonArray widgets {}; + timelineWallpaperList.append(timelineWallpaper); + } + + QJsonArray widgetList {}; for (const auto& activeWidget : std::as_const(m_screenPlayWidgets)) { - widgets.append(activeWidget->getActiveSettingsJson()); + widgetList.append(activeWidget->getActiveSettingsJson()); } QJsonObject profileDefault; - profileDefault.insert("appdrawer", QJsonArray {}); - profileDefault.insert("widgets", widgets); - // profileDefault.insert("wallpaper", wallpaper); + profileDefault.insert("widgets", widgetList); + profileDefault.insert("timelineWallpaper", timelineWallpaperList); profileDefault.insert("name", "default"); QJsonArray activeProfileList; activeProfileList.append(profileDefault); QJsonObject profile; - profile.insert("version", "1.0.0"); + profile.insert("version", m_settings->getProfilesVersion().toString()); profile.insert("profiles", activeProfileList); - if (Util().writeJsonObjectToFile({ m_globalVariables->localSettingsPath().toString() + "/profiles.json" }, profile)) { + Util util; + if (util.writeJsonObjectToFile({ m_globalVariables->localSettingsPath().toString() + "/profiles.json" }, profile)) { emit profilesSaved(); return true; } diff --git a/ScreenPlayUtil/inc/public/ScreenPlayUtil/util.h b/ScreenPlayUtil/inc/public/ScreenPlayUtil/util.h index a4a3c04a..6b33b52f 100644 --- a/ScreenPlayUtil/inc/public/ScreenPlayUtil/util.h +++ b/ScreenPlayUtil/inc/public/ScreenPlayUtil/util.h @@ -112,7 +112,7 @@ public: QString toString(const QStringList& list) const; std::optional> parseStringToIntegerList(const QString& string) const; float roundDecimalPlaces(const float number) const; - Q_INVOKABLE QString generateRandomString(quint32 length = 32); + Q_INVOKABLE QString generateRandomString(quint32 length = 32); QString executableAppEnding(); QString executableBinEnding(); QStringList getAvailableWallpaper() const;