From 9d885d951c2e4b68647aff4a66f3c1e8336dc89e Mon Sep 17 00:00:00 2001 From: tuckerrrrrrrrrr Date: Sat, 26 Sep 2020 06:11:45 -0700 Subject: [PATCH] Allow reordering elements in list settings (#1595) * Add move up, down buttons to list settings Channel notifications, moderation buttons etc. have a move up and move down button now for reordering. * Selection follows moved rows, refactor Also fixed rows past the 2nd one not moving * Update selection property with more than 1 column * Fix crash when moving without a row selected * Move rows with drag and drop Right now it's a little iffy registering the row to be moved, but I wanna go to bed :) * Remove EditableTableView, move to SignalVectorModel Replace my ghetto drag and drop solution in EditableTableView with small patches to the stuff already written in SignalVectorModel::dropMimeData --- src/common/SignalVectorModel.hpp | 37 ++++++++++++++++--- src/widgets/helper/EditableModelView.cpp | 45 +++++++++++++++++++++++- src/widgets/helper/EditableModelView.hpp | 3 ++ 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/common/SignalVectorModel.hpp b/src/common/SignalVectorModel.hpp index 21e84b437..8f670545e 100644 --- a/src/common/SignalVectorModel.hpp +++ b/src/common/SignalVectorModel.hpp @@ -239,6 +239,32 @@ public: this->vector_->removeAt(signalVectorRow); } + bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, + const QModelIndex &destinationParent, int destinationChild) + { + if (count != 1) + { + return false; + } + + assert(sourceRow >= 0 && sourceRow < this->rows_.size()); + + int signalVectorRow = this->getVectorIndexFromModelIndex(sourceRow); + this->beginMoveRows(sourceParent, sourceRow, sourceRow, + destinationParent, destinationChild); + + TVectorItem item = + this->getItemFromRow(this->rows_[sourceRow].items, + this->rows_[sourceRow].original.get()); + this->vector_->removeAt(signalVectorRow); + this->vector_->insert( + item, this->getVectorIndexFromModelIndex(destinationChild)); + + this->endMoveRows(); + + return true; + } + bool removeRows(int row, int count, const QModelIndex &parent) override { (void)parent; @@ -289,17 +315,18 @@ public: int from = data->data("chatterino_row_id").toInt(); int to = parent.row(); - if (from < 0 || from > this->vector_->raw().size() || to < 0 || - to > this->vector_->raw().size()) + int vectorFrom = this->getVectorIndexFromModelIndex(from); + int vectorTo = this->getVectorIndexFromModelIndex(to); + + if (vectorFrom < 0 || vectorFrom > this->vector_->raw().size() || + vectorTo < 0 || vectorTo > this->vector_->raw().size()) { return false; } if (from != to) { - auto item = this->vector_->raw()[from]; - this->vector_->removeAt(from); - this->vector_->insert(item, to); + this->moveRow(this->index(from, to), from, parent, to); } // We return false since we remove items ourselves. diff --git a/src/widgets/helper/EditableModelView.cpp b/src/widgets/helper/EditableModelView.cpp index 8aec92337..fa0efdac7 100644 --- a/src/widgets/helper/EditableModelView.cpp +++ b/src/widgets/helper/EditableModelView.cpp @@ -1,8 +1,10 @@ #include "EditableModelView.hpp" +#include #include #include #include +#include #include #include #include @@ -14,7 +16,7 @@ EditableModelView::EditableModelView(QAbstractTableModel *model) , model_(model) { this->model_->setParent(this); - this->tableView_->setModel(model); + this->tableView_->setModel(model_); this->tableView_->setSelectionMode(QAbstractItemView::SingleSelection); this->tableView_->setSelectionBehavior(QAbstractItemView::SelectRows); this->tableView_->setDragDropMode(QTableView::DragDropMode::InternalMove); @@ -54,8 +56,25 @@ EditableModelView::EditableModelView(QAbstractTableModel *model) model_->removeRow(row); }); + // move up + QPushButton *moveUp = new QPushButton("Move up"); + buttons->addWidget(moveUp); + QObject::connect(moveUp, &QPushButton::clicked, + [this] { this->moveRow(-1); }); + + // move down + QPushButton *moveDown = new QPushButton("Move down"); + buttons->addWidget(moveDown); + QObject::connect(moveDown, &QPushButton::clicked, + [this] { this->moveRow(1); }); + buttons->addStretch(); + QObject::connect(this->model_, &QAbstractTableModel::rowsMoved, + [this](const QModelIndex &parent, int start, int end, + const QModelIndex &destination, + int row) { this->selectRow(row); }); + // add tableview vbox->addWidget(this->tableView_); @@ -103,4 +122,28 @@ void EditableModelView::addRegexHelpLink() this->addCustomButton(regexHelpLabel); } +void EditableModelView::moveRow(int dir) +{ + auto selected = this->getTableView()->selectionModel()->selectedRows(0); + + int row; + if (selected.size() == 0 || + (row = selected.at(0).row()) + dir >= + this->model_->rowCount(QModelIndex()) || + row + dir < 0) + return; + + model_->moveRows(model_->index(row, 0), row, selected.size(), + model_->index(row + dir, 0), row + dir); + this->selectRow(row + dir); +} + +void EditableModelView::selectRow(int row) +{ + this->getTableView()->selectionModel()->clear(); + this->getTableView()->selectionModel()->select( + this->model_->index(row, 0), + QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); +} + } // namespace chatterino diff --git a/src/widgets/helper/EditableModelView.hpp b/src/widgets/helper/EditableModelView.hpp index 5ac5ec9ce..1434ead49 100644 --- a/src/widgets/helper/EditableModelView.hpp +++ b/src/widgets/helper/EditableModelView.hpp @@ -29,6 +29,9 @@ private: QTableView *tableView_{}; QAbstractTableModel *model_{}; QHBoxLayout *buttons_{}; + + void moveRow(int dir); + void selectRow(int row); }; } // namespace chatterino