mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
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:
parent
f6caee6a66
commit
9d885d951c
3 changed files with 79 additions and 6 deletions
|
@ -239,6 +239,32 @@ public:
|
||||||
this->vector_->removeAt(signalVectorRow);
|
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
|
bool removeRows(int row, int count, const QModelIndex &parent) override
|
||||||
{
|
{
|
||||||
(void)parent;
|
(void)parent;
|
||||||
|
@ -289,17 +315,18 @@ public:
|
||||||
int from = data->data("chatterino_row_id").toInt();
|
int from = data->data("chatterino_row_id").toInt();
|
||||||
int to = parent.row();
|
int to = parent.row();
|
||||||
|
|
||||||
if (from < 0 || from > this->vector_->raw().size() || to < 0 ||
|
int vectorFrom = this->getVectorIndexFromModelIndex(from);
|
||||||
to > this->vector_->raw().size())
|
int vectorTo = this->getVectorIndexFromModelIndex(to);
|
||||||
|
|
||||||
|
if (vectorFrom < 0 || vectorFrom > this->vector_->raw().size() ||
|
||||||
|
vectorTo < 0 || vectorTo > this->vector_->raw().size())
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (from != to)
|
if (from != to)
|
||||||
{
|
{
|
||||||
auto item = this->vector_->raw()[from];
|
this->moveRow(this->index(from, to), from, parent, to);
|
||||||
this->vector_->removeAt(from);
|
|
||||||
this->vector_->insert(item, to);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We return false since we remove items ourselves.
|
// We return false since we remove items ourselves.
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
#include "EditableModelView.hpp"
|
#include "EditableModelView.hpp"
|
||||||
|
|
||||||
|
#include <QAbstractItemView>
|
||||||
#include <QAbstractTableModel>
|
#include <QAbstractTableModel>
|
||||||
#include <QHBoxLayout>
|
#include <QHBoxLayout>
|
||||||
#include <QHeaderView>
|
#include <QHeaderView>
|
||||||
|
#include <QModelIndex>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QTableView>
|
#include <QTableView>
|
||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
|
@ -14,7 +16,7 @@ EditableModelView::EditableModelView(QAbstractTableModel *model)
|
||||||
, model_(model)
|
, model_(model)
|
||||||
{
|
{
|
||||||
this->model_->setParent(this);
|
this->model_->setParent(this);
|
||||||
this->tableView_->setModel(model);
|
this->tableView_->setModel(model_);
|
||||||
this->tableView_->setSelectionMode(QAbstractItemView::SingleSelection);
|
this->tableView_->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||||
this->tableView_->setSelectionBehavior(QAbstractItemView::SelectRows);
|
this->tableView_->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||||
this->tableView_->setDragDropMode(QTableView::DragDropMode::InternalMove);
|
this->tableView_->setDragDropMode(QTableView::DragDropMode::InternalMove);
|
||||||
|
@ -54,8 +56,25 @@ EditableModelView::EditableModelView(QAbstractTableModel *model)
|
||||||
model_->removeRow(row);
|
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();
|
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
|
// add tableview
|
||||||
vbox->addWidget(this->tableView_);
|
vbox->addWidget(this->tableView_);
|
||||||
|
|
||||||
|
@ -103,4 +122,28 @@ void EditableModelView::addRegexHelpLink()
|
||||||
this->addCustomButton(regexHelpLabel);
|
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
|
} // namespace chatterino
|
||||||
|
|
|
@ -29,6 +29,9 @@ private:
|
||||||
QTableView *tableView_{};
|
QTableView *tableView_{};
|
||||||
QAbstractTableModel *model_{};
|
QAbstractTableModel *model_{};
|
||||||
QHBoxLayout *buttons_{};
|
QHBoxLayout *buttons_{};
|
||||||
|
|
||||||
|
void moveRow(int dir);
|
||||||
|
void selectRow(int row);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace chatterino
|
} // namespace chatterino
|
||||||
|
|
Loading…
Reference in a new issue