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
This commit is contained in:
tuckerrrrrrrrrr
2020-09-26 06:11:45 -07:00
committed by GitHub
parent f6caee6a66
commit 9d885d951c
3 changed files with 79 additions and 6 deletions
+44 -1
View File
@@ -1,8 +1,10 @@
#include "EditableModelView.hpp"
#include <QAbstractItemView>
#include <QAbstractTableModel>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QModelIndex>
#include <QPushButton>
#include <QTableView>
#include <QVBoxLayout>
@@ -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