From ddad68253a995d2dd1955c3ad94c1c48499bf81c Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Wed, 18 Oct 2023 21:22:07 -0500 Subject: [PATCH] Enable layer filtering, and update moving to handle the proxy model --- scwx-qt/source/scwx/qt/ui/layer_dialog.cpp | 161 ++++++++++++++++----- scwx-qt/source/scwx/qt/ui/layer_dialog.ui | 4 +- 2 files changed, 130 insertions(+), 35 deletions(-) diff --git a/scwx-qt/source/scwx/qt/ui/layer_dialog.cpp b/scwx-qt/source/scwx/qt/ui/layer_dialog.cpp index 00f49a80..18a34655 100644 --- a/scwx-qt/source/scwx/qt/ui/layer_dialog.cpp +++ b/scwx-qt/source/scwx/qt/ui/layer_dialog.cpp @@ -4,6 +4,8 @@ #include #include +#include + namespace scwx { namespace qt @@ -18,18 +20,26 @@ class LayerDialogImpl { public: explicit LayerDialogImpl(LayerDialog* self) : - self_ {self}, layerModel_ {new model::LayerModel(self)} + self_ {self}, + layerModel_ {new model::LayerModel(self)}, + layerProxyModel_ {new QSortFilterProxyModel(self_)} { + layerProxyModel_->setSourceModel(layerModel_); + layerProxyModel_->setFilterCaseSensitivity( + Qt::CaseSensitivity::CaseInsensitive); + layerProxyModel_->setFilterKeyColumn(-1); } ~LayerDialogImpl() = default; void ConnectSignals(); void UpdateMoveButtonsEnabled(); - std::vector GetSelectedRows(); + std::vector GetSelectedRows(); + std::vector> GetContiguousRows(); - LayerDialog* self_; - model::LayerModel* layerModel_; + LayerDialog* self_; + model::LayerModel* layerModel_; + QSortFilterProxyModel* layerProxyModel_; }; LayerDialog::LayerDialog(QWidget* parent) : @@ -39,7 +49,7 @@ LayerDialog::LayerDialog(QWidget* parent) : { ui->setupUi(this); - ui->layerTreeView->setModel(p->layerModel_); + ui->layerTreeView->setModel(p->layerProxyModel_); auto layerViewHeader = ui->layerTreeView->header(); @@ -72,6 +82,11 @@ LayerDialog::~LayerDialog() void LayerDialogImpl::ConnectSignals() { + QObject::connect(self_->ui->layerFilter, + &QLineEdit::textChanged, + layerProxyModel_, + &QSortFilterProxyModel::setFilterWildcard); + QObject::connect(self_->ui->layerTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, self_, @@ -94,61 +109,111 @@ void LayerDialogImpl::ConnectSignals() } }); - QObject::connect( + QObject::connect( // self_->ui->moveTopButton, &QAbstractButton::clicked, self_, [this]() { - auto selectedRows = GetSelectedRows(); - int sourceRow = selectedRows.front(); - int count = static_cast(selectedRows.size()); + auto contiguousRows = GetContiguousRows(); int destinationChild = 0; - layerModel_->moveRows( - QModelIndex(), sourceRow, count, QModelIndex(), destinationChild); + for (auto& selectedRows : contiguousRows) + { + int sourceRow = selectedRows.front(); + int count = static_cast(selectedRows.size()); + + layerModel_->moveRows(QModelIndex(), + sourceRow, + count, + QModelIndex(), + destinationChild); + + // Next set of rows should follow rows just added + destinationChild += count; + } }); - QObject::connect( + QObject::connect( // self_->ui->moveUpButton, &QAbstractButton::clicked, self_, [this]() { - auto selectedRows = GetSelectedRows(); - int sourceRow = selectedRows.front(); - int count = static_cast(selectedRows.size()); - int destinationChild = sourceRow - 1; + auto contiguousRows = GetContiguousRows(); + int destinationChild = -1; - layerModel_->moveRows( - QModelIndex(), sourceRow, count, QModelIndex(), destinationChild); + for (auto& selectedRows : contiguousRows) + { + int sourceRow = selectedRows.front(); + int count = static_cast(selectedRows.size()); + if (destinationChild == -1) + { + destinationChild = sourceRow - 1; + } + + layerModel_->moveRows(QModelIndex(), + sourceRow, + count, + QModelIndex(), + destinationChild); + + // Next set of rows should follow rows just added + destinationChild += count; + } }); - QObject::connect( + QObject::connect( // self_->ui->moveDownButton, &QAbstractButton::clicked, self_, [this]() { - auto selectedRows = GetSelectedRows(); - int sourceRow = selectedRows.front(); - int count = static_cast(selectedRows.size()); - int destinationChild = selectedRows.back() + 2; + auto contiguousRows = GetContiguousRows(); + int destinationChild = 0; + int offset = 0; + if (!contiguousRows.empty()) + { + destinationChild = contiguousRows.back().back() + 2; + } - layerModel_->moveRows( - QModelIndex(), sourceRow, count, QModelIndex(), destinationChild); + for (auto& selectedRows : contiguousRows) + { + int sourceRow = selectedRows.front() - offset; + int count = static_cast(selectedRows.size()); + + layerModel_->moveRows(QModelIndex(), + sourceRow, + count, + QModelIndex(), + destinationChild); + + // Next set of rows should be offset + offset += count; + } }); - QObject::connect( + QObject::connect( // self_->ui->moveBottomButton, &QAbstractButton::clicked, self_, [this]() { - auto selectedRows = GetSelectedRows(); - int sourceRow = selectedRows.front(); - int count = static_cast(selectedRows.size()); + auto contiguousRows = GetContiguousRows(); int destinationChild = layerModel_->rowCount(); + int offset = 0; - layerModel_->moveRows( - QModelIndex(), sourceRow, count, QModelIndex(), destinationChild); + for (auto& selectedRows : contiguousRows) + { + int sourceRow = selectedRows.front() - offset; + int count = static_cast(selectedRows.size()); + + layerModel_->moveRows(QModelIndex(), + sourceRow, + count, + QModelIndex(), + destinationChild); + + // Next set of rows should be offset + offset += count; + } }); } @@ -159,12 +224,42 @@ std::vector LayerDialogImpl::GetSelectedRows() std::vector rows {}; for (auto& selectedRow : selectedRows) { - rows.push_back(selectedRow.row()); + rows.push_back(layerProxyModel_->mapToSource(selectedRow).row()); } std::sort(rows.begin(), rows.end()); return rows; } +std::vector> LayerDialogImpl::GetContiguousRows() +{ + std::vector> contiguousRows {}; + std::vector currentContiguousRows {}; + auto rows = GetSelectedRows(); + + for (auto& row : rows) + { + // Next row is not contiguous with current row set + if (!currentContiguousRows.empty() && + currentContiguousRows.back() + 1 < row) + { + // Add current row set to contiguous rows, and reset current set + contiguousRows.emplace_back(std::move(currentContiguousRows)); + currentContiguousRows.clear(); + } + + // Add row to current row set + currentContiguousRows.push_back(row); + } + + if (!currentContiguousRows.empty()) + { + // Add remaining rows to contiguous rows + contiguousRows.emplace_back(currentContiguousRows); + } + + return contiguousRows; +} + void LayerDialogImpl::UpdateMoveButtonsEnabled() { QModelIndexList selectedRows = @@ -177,7 +272,7 @@ void LayerDialogImpl::UpdateMoveButtonsEnabled() for (auto& rowIndex : selectedRows) { - int row = rowIndex.row(); + int row = layerProxyModel_->mapToSource(rowIndex).row(); if (!layerModel_->IsMovable(row)) { // If an item in the selection is not movable, disable all moves diff --git a/scwx-qt/source/scwx/qt/ui/layer_dialog.ui b/scwx-qt/source/scwx/qt/ui/layer_dialog.ui index a47c1583..20979d63 100644 --- a/scwx-qt/source/scwx/qt/ui/layer_dialog.ui +++ b/scwx-qt/source/scwx/qt/ui/layer_dialog.ui @@ -50,7 +50,7 @@ true - QAbstractItemView::ContiguousSelection + QAbstractItemView::ExtendedSelection 0 @@ -176,7 +176,7 @@ 0 - + Filter