diff --git a/lib/macro/macro-action-edit.cpp b/lib/macro/macro-action-edit.cpp index 949f9c10..c5465be0 100644 --- a/lib/macro/macro-action-edit.cpp +++ b/lib/macro/macro-action-edit.cpp @@ -35,12 +35,14 @@ MacroActionEdit::MacroActionEdit(QWidget *parent, _enable(new SwitchButton()), _entryData(entryData) { + auto actionStateTimer = new QTimer(this); + QWidget::connect(_actionSelection, SIGNAL(currentTextChanged(const QString &)), this, SLOT(ActionSelectionChanged(const QString &))); QWidget::connect(_enable, SIGNAL(checked(bool)), this, SLOT(ActionEnableChanged(bool))); - QWidget::connect(&_actionStateTimer, SIGNAL(timeout()), this, + QWidget::connect(actionStateTimer, SIGNAL(timeout()), this, SLOT(UpdateActionState())); populateActionSelection(_actionSelection); @@ -63,7 +65,7 @@ MacroActionEdit::MacroActionEdit(QWidget *parent, _entryData = entryData; SetupWidgets(true); - _actionStateTimer.start(300); + actionStateTimer->start(300); _loading = false; } diff --git a/lib/macro/macro-action-edit.hpp b/lib/macro/macro-action-edit.hpp index 93eeb731..5b462a8b 100644 --- a/lib/macro/macro-action-edit.hpp +++ b/lib/macro/macro-action-edit.hpp @@ -32,7 +32,6 @@ private: SwitchButton *_enable; std::shared_ptr *_entryData; - QTimer _actionStateTimer; bool _loading = true; }; diff --git a/lib/macro/macro-segment-list.cpp b/lib/macro/macro-segment-list.cpp index cdf8de15..99d16ead 100644 --- a/lib/macro/macro-segment-list.cpp +++ b/lib/macro/macro-segment-list.cpp @@ -42,6 +42,13 @@ MacroSegmentList::MacroSegmentList(QWidget *parent) [this]() { SetupVisibleMacroSegmentWidgets(); }); } +static void clearWidgetVector(const std::vector &widgets) +{ + for (auto widget : widgets) { + widget->deleteLater(); + } +}; + MacroSegmentList::~MacroSegmentList() { if (_autoScrollThread.joinable()) { @@ -49,13 +56,6 @@ MacroSegmentList::~MacroSegmentList() _autoScrollThread.join(); } - const auto clearWidgetVector = - [](const std::vector &widgets) { - for (auto widget : widgets) { - widget->deleteLater(); - } - }; - for (const auto &[_, widgets] : _widgetCache) { clearWidgetVector(widgets); } @@ -185,6 +185,16 @@ bool MacroSegmentList::PopulateWidgetsFromCache(const Macro *macro) return true; } +void MacroSegmentList::ClearWidgetsFromCacheFor(const Macro *macro) +{ + auto it = _widgetCache.find(macro); + if (it == _widgetCache.end()) { + return; + } + clearWidgetVector(it->second); + _widgetCache.erase(it); +} + void MacroSegmentList::Highlight(int idx, QColor color) { auto item = _contentLayout->itemAt(idx); diff --git a/lib/macro/macro-segment-list.hpp b/lib/macro/macro-segment-list.hpp index 6d0552cd..f14922fc 100644 --- a/lib/macro/macro-segment-list.hpp +++ b/lib/macro/macro-segment-list.hpp @@ -29,6 +29,7 @@ public: static void SetCachingEnabled(bool enable); void CacheCurrentWidgetsFor(const Macro *); bool PopulateWidgetsFromCache(const Macro *); + void ClearWidgetsFromCacheFor(const Macro *); void Highlight(int idx, QColor color = QColor(Qt::green)); void SetCollapsed(bool) const; void SetSelection(int idx) const; diff --git a/lib/macro/macro-tab.cpp b/lib/macro/macro-tab.cpp index 94b0c6ea..aa6df43b 100644 --- a/lib/macro/macro-tab.cpp +++ b/lib/macro/macro-tab.cpp @@ -140,6 +140,42 @@ void AdvSceneSwitcher::on_macroAdd_clicked() emit MacroAdded(QString::fromStdString(name)); } +static void addGroupSubitems(std::vector> ¯os, + const std::shared_ptr &group) +{ + std::vector> subitems; + subitems.reserve(group->GroupSize()); + + // Find all subitems + auto allMacros = GetMacros(); + for (auto it = allMacros.begin(); it < allMacros.end(); it++) { + if ((*it)->Name() != group->Name()) { + continue; + } + for (uint32_t i = 1; i <= group->GroupSize(); i++) { + subitems.emplace_back(*std::next(it, i)); + } + break; + } + + // Remove subitems which were already selected to avoid duplicates + for (const auto &subitem : subitems) { + auto it = std::find(macros.begin(), macros.end(), subitem); + if (it == macros.end()) { + continue; + } + macros.erase(it); + } + + // Add group subitems + auto it = std::find(macros.begin(), macros.end(), group); + if (it == macros.end()) { + return; + } + it = std::next(it); + macros.insert(it, subitems.begin(), subitems.end()); +} + void AdvSceneSwitcher::RemoveMacro(std::shared_ptr ¯o) { if (!macro) { @@ -155,6 +191,28 @@ void AdvSceneSwitcher::RemoveMacro(std::shared_ptr ¯o) } } + const auto clearWidgetCache = [this](Macro *macro) { + ui->conditionsList->ClearWidgetsFromCacheFor(macro); + ui->actionsList->ClearWidgetsFromCacheFor(macro); + ui->elseActionsList->ClearWidgetsFromCacheFor(macro); + }; + + if (macro->IsGroup()) { + std::vector> macros = {macro}; + addGroupSubitems(macros, macro); + for (auto macro : macros) { + clearWidgetCache(macro.get()); + } + } else { + clearWidgetCache(macro.get()); + } + + // Currently shown macro will always be part of the macros to be + // removed, so, we clear the widgets as they should not be cached + ui->conditionsList->Clear(); + ui->actionsList->Clear(); + ui->elseActionsList->Clear(); + ui->macros->Remove(macro); emit MacroRemoved(name); } @@ -254,41 +312,6 @@ void AdvSceneSwitcher::RenameSelectedMacro() ui->macroName->setText(QString::fromStdString(name)); } -static void addGroupSubitems(std::vector> ¯os, - const std::shared_ptr &group) -{ - std::vector> subitems; - subitems.reserve(group->GroupSize()); - - // Find all subitems - auto allMacros = GetMacros(); - for (auto it = allMacros.begin(); it < allMacros.end(); it++) { - if ((*it)->Name() == group->Name()) { - for (uint32_t i = 1; i <= group->GroupSize(); i++) { - subitems.emplace_back(*std::next(it, i)); - } - break; - } - } - - // Remove subitems which were already selected to avoid duplicates - for (const auto &subitem : subitems) { - auto it = std::find(macros.begin(), macros.end(), subitem); - if (it == macros.end()) { - continue; - } - macros.erase(it); - } - - // Add group subitems - auto it = std::find(macros.begin(), macros.end(), group); - if (it == macros.end()) { - return; - } - it = std::next(it); - macros.insert(it, subitems.begin(), subitems.end()); -} - void AdvSceneSwitcher::ExportMacros() const { auto selectedMacros = GetSelectedMacros();