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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 6 deletions

View file

@ -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.

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

View file

@ -29,6 +29,9 @@ private:
QTableView *tableView_{};
QAbstractTableModel *model_{};
QHBoxLayout *buttons_{};
void moveRow(int dir);
void selectRow(int row);
};
} // namespace chatterino