Skip to content

Commit

Permalink
Performing plugin config widget relocation when the selected filters …
Browse files Browse the repository at this point in the history
…get reordered.
  • Loading branch information
arobenko committed Mar 16, 2024
1 parent 792c4ac commit 19e87d1
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 4 deletions.
28 changes: 28 additions & 0 deletions app/cc_view/src/widget/PluginConfigDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ void PluginConfigDialog::clearClicked()
void PluginConfigDialog::topClicked()
{
assert(m_currentSelectedList != nullptr);
auto* item = m_currentSelectedList->currentItem();
assert(item != nullptr);

auto pluginInfoPtr = getPluginInfo(item);
assert(pluginInfoPtr);
m_allConfigWidget->moveTop(pluginInfoPtr);

auto curRow = m_currentSelectedList->currentRow();
if (curRow <= 0) {
[[maybe_unused]] static constexpr bool No_item_is_selected_or_moving_up_top_item = false;
Expand All @@ -331,6 +338,13 @@ void PluginConfigDialog::topClicked()
void PluginConfigDialog::upClicked()
{
assert(m_currentSelectedList != nullptr);
auto* item = m_currentSelectedList->currentItem();
assert(item != nullptr);

auto pluginInfoPtr = getPluginInfo(item);
assert(pluginInfoPtr);
m_allConfigWidget->moveUp(pluginInfoPtr);

auto curRow = m_currentSelectedList->currentRow();
if (curRow <= 0) {
[[maybe_unused]] static constexpr bool No_item_is_selected_or_moving_up_top_item = false;
Expand All @@ -344,6 +358,13 @@ void PluginConfigDialog::upClicked()
void PluginConfigDialog::downClicked()
{
assert(m_currentSelectedList != nullptr);
auto* item = m_currentSelectedList->currentItem();
assert(item != nullptr);

auto pluginInfoPtr = getPluginInfo(item);
assert(pluginInfoPtr);
m_allConfigWidget->moveDown(pluginInfoPtr);

auto curRow = m_currentSelectedList->currentRow();
if ((m_currentSelectedList->count() - 1) <= curRow) {
[[maybe_unused]] static constexpr bool No_item_is_selected_or_moving_down_bottom_item = false;
Expand All @@ -357,6 +378,13 @@ void PluginConfigDialog::downClicked()
void PluginConfigDialog::bottomClicked()
{
assert(m_currentSelectedList != nullptr);
auto* item = m_currentSelectedList->currentItem();
assert(item != nullptr);

auto pluginInfoPtr = getPluginInfo(item);
assert(pluginInfoPtr);
m_allConfigWidget->moveBottom(pluginInfoPtr);

auto curRow = m_currentSelectedList->currentRow();
if ((m_currentSelectedList->count() - 1) <= curRow) {
[[maybe_unused]] static constexpr bool No_item_is_selected_or_moving_down_bottom_item = false;
Expand Down
94 changes: 90 additions & 4 deletions app/cc_view/src/widget/PluginConfigWrapsListWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,77 @@ void PluginConfigWrapsListWidget::removePluginConfig(PluginInfoPtr pluginInfo)
m_loadedPlugins.erase(pluginIter);
}

auto idx = findWidgetIdx(pluginInfo);
if (idx < 0) {
return;
}

m_widgets.erase(m_widgets.begin() + idx); // Will remove widget as well
}

void PluginConfigWrapsListWidget::removeAll()
{
m_widgets.clear();
}

void PluginConfigWrapsListWidget::moveTop(PluginInfoPtr pluginInfo)
{
if (pluginInfo->getType() != PluginMgr::PluginInfo::Type::Filter) {
return;
}

auto idx = findWidgetIdx(pluginInfo);
if (idx <= 0) {
return;
}

relocateWidget(idx, 0);
}

void PluginConfigWrapsListWidget::moveBottom(PluginInfoPtr pluginInfo)
{
if (pluginInfo->getType() != PluginMgr::PluginInfo::Type::Filter) {
return;
}

auto idx = findWidgetIdx(pluginInfo);
if ((idx < 0) || (static_cast<int>(m_widgets.size()) <= (idx + 1))) {
return;
}

relocateWidget(idx, static_cast<int>(m_widgets.size()) - 1);
}

void PluginConfigWrapsListWidget::moveUp(PluginInfoPtr pluginInfo)
{
if (pluginInfo->getType() != PluginMgr::PluginInfo::Type::Filter) {
return;
}

auto idx = findWidgetIdx(pluginInfo);
if (idx <= 0) {
return;
}

relocateWidget(idx, idx - 1);
}

void PluginConfigWrapsListWidget::moveDown(PluginInfoPtr pluginInfo)
{
if (pluginInfo->getType() != PluginMgr::PluginInfo::Type::Filter) {
return;
}

auto idx = findWidgetIdx(pluginInfo);
if ((idx < 0) || (static_cast<int>(m_widgets.size()) <= (idx + 1))) {
return;
}

relocateWidget(idx, idx + 1);
}

int PluginConfigWrapsListWidget::findWidgetIdx(PluginInfoPtr pluginInfo) const
{
auto iid = pluginInfo->getIid();
auto iter =
std::find_if(
Expand All @@ -98,15 +169,30 @@ void PluginConfigWrapsListWidget::removePluginConfig(PluginInfoPtr pluginInfo)
});

if (iter == m_widgets.end()) {
return;
return -1;
}

m_widgets.erase(iter); // Will remove widget as well
return static_cast<int>(std::distance(m_widgets.begin(), m_widgets.end()));
}

void PluginConfigWrapsListWidget::removeAll()
void PluginConfigWrapsListWidget::relocateWidget(int from, int to)
{
m_widgets.clear();
assert(from != to);

auto* l = qobject_cast<QVBoxLayout*>(layout());
auto* item = l->takeAt(from);
delete item; // expected to keep the widget

auto iter = m_widgets.begin() + from;

auto wrap = std::move(*iter);
m_widgets.erase(iter);
assert(l->count() == static_cast<int>(m_widgets.size()));

to = std::min(to, l->count());

l->insertWidget(to, wrap.get());
m_widgets.insert(m_widgets.begin() + to, std::move(wrap));
}

} // namespace cc_tools_qt
6 changes: 6 additions & 0 deletions app/cc_view/src/widget/PluginConfigWrapsListWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ class PluginConfigWrapsListWidget: public QWidget
void addPluginConfig(PluginInfoPtr pluginInfo);
void removePluginConfig(PluginInfoPtr pluginInfo);
void removeAll();
void moveTop(PluginInfoPtr pluginInfo);
void moveBottom(PluginInfoPtr pluginInfo);
void moveUp(PluginInfoPtr pluginInfo);
void moveDown(PluginInfoPtr pluginInfo);

private:
int findWidgetIdx(PluginInfoPtr pluginInfo) const;
void relocateWidget(int from, int to);
using PluginConfigWrapWidgetPtr = std::unique_ptr<PluginConfigWrapWidget>;
using WrapWidgetsList = std::vector<PluginConfigWrapWidgetPtr>;
WrapWidgetsList m_widgets;
Expand Down

0 comments on commit 19e87d1

Please sign in to comment.