From b8e3609f42d6d5bb53db2d3d079c243f076c8aab Mon Sep 17 00:00:00 2001 From: Alex Robenko Date: Mon, 18 Mar 2024 12:46:25 +1000 Subject: [PATCH] Fixing plugin configuration widgets re-ordering. --- .../widget/PluginConfigWrapsListWidget.cpp | 53 ++++++++++++++----- .../src/widget/PluginConfigWrapsListWidget.h | 2 + 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/app/cc_view/src/widget/PluginConfigWrapsListWidget.cpp b/app/cc_view/src/widget/PluginConfigWrapsListWidget.cpp index 31af5a1..381225c 100644 --- a/app/cc_view/src/widget/PluginConfigWrapsListWidget.cpp +++ b/app/cc_view/src/widget/PluginConfigWrapsListWidget.cpp @@ -108,11 +108,13 @@ void PluginConfigWrapsListWidget::moveTop(PluginInfoPtr pluginInfo) } auto idx = findWidgetIdx(pluginInfo); - if (idx <= 0) { + auto topIdx = getTopFilterIdx(); + + if ((idx < 0) || (idx <= topIdx)) { return; } - relocateWidget(idx, 0); + relocateWidget(idx, topIdx); } void PluginConfigWrapsListWidget::moveBottom(PluginInfoPtr pluginInfo) @@ -122,11 +124,12 @@ void PluginConfigWrapsListWidget::moveBottom(PluginInfoPtr pluginInfo) } auto idx = findWidgetIdx(pluginInfo); - if ((idx < 0) || (static_cast(m_widgets.size()) <= (idx + 1))) { + auto bottomIdx = getBottomFilterIdx(); + if ((idx < 0) || (bottomIdx <= idx)) { return; } - relocateWidget(idx, static_cast(m_widgets.size()) - 1); + relocateWidget(idx, bottomIdx); } void PluginConfigWrapsListWidget::moveUp(PluginInfoPtr pluginInfo) @@ -136,7 +139,8 @@ void PluginConfigWrapsListWidget::moveUp(PluginInfoPtr pluginInfo) } auto idx = findWidgetIdx(pluginInfo); - if (idx <= 0) { + auto topIdx = getTopFilterIdx(); + if ((idx <= 0) || (idx <= topIdx)) { return; } @@ -150,7 +154,8 @@ void PluginConfigWrapsListWidget::moveDown(PluginInfoPtr pluginInfo) } auto idx = findWidgetIdx(pluginInfo); - if ((idx < 0) || (static_cast(m_widgets.size()) <= (idx + 1))) { + auto bottomIdx = getBottomFilterIdx(); + if ((idx < 0) || (bottomIdx <= idx)) { return; } @@ -172,7 +177,31 @@ int PluginConfigWrapsListWidget::findWidgetIdx(PluginInfoPtr pluginInfo) const return -1; } - return static_cast(std::distance(m_widgets.begin(), m_widgets.end())); + return static_cast(std::distance(m_widgets.begin(), iter)); +} + +int PluginConfigWrapsListWidget::getTopFilterIdx() const +{ + auto iter = std::lower_bound( + m_widgets.begin(), m_widgets.end(), PluginMgr::PluginInfo::Type::Filter, + [](auto& w, auto t) + { + return w->getType() < t; + }); + + return static_cast(std::distance(m_widgets.begin(), iter)); +} + +int PluginConfigWrapsListWidget::getBottomFilterIdx() const +{ + auto iter = std::upper_bound( + m_widgets.begin(), m_widgets.end(), PluginMgr::PluginInfo::Type::Filter, + [](auto t, auto& w) + { + return t < w->getType(); + }); + + return static_cast(std::distance(m_widgets.begin(), iter)); } void PluginConfigWrapsListWidget::relocateWidget(int from, int to) @@ -180,19 +209,17 @@ void PluginConfigWrapsListWidget::relocateWidget(int from, int to) assert(from != to); auto* l = qobject_cast(layout()); - auto* item = l->takeAt(from); - delete item; // expected to keep the widget + auto* item = l->takeAt(from); auto iter = m_widgets.begin() + from; auto wrap = std::move(*iter); m_widgets.erase(iter); - assert(l->count() == static_cast(m_widgets.size())); - to = std::min(to, l->count()); + to = std::min(to, static_cast(m_widgets.size())); - l->insertWidget(to, wrap.get()); + l->insertItem(to, item); m_widgets.insert(m_widgets.begin() + to, std::move(wrap)); } -} // namespace cc_tools_qt \ No newline at end of file +} // namespace cc_tools_qt diff --git a/app/cc_view/src/widget/PluginConfigWrapsListWidget.h b/app/cc_view/src/widget/PluginConfigWrapsListWidget.h index 70218fb..0f125bc 100644 --- a/app/cc_view/src/widget/PluginConfigWrapsListWidget.h +++ b/app/cc_view/src/widget/PluginConfigWrapsListWidget.h @@ -48,6 +48,8 @@ class PluginConfigWrapsListWidget: public QWidget private: int findWidgetIdx(PluginInfoPtr pluginInfo) const; + int getTopFilterIdx() const; + int getBottomFilterIdx() const; void relocateWidget(int from, int to); using PluginConfigWrapWidgetPtr = std::unique_ptr; using WrapWidgetsList = std::vector;