2018-04-29 23:25:49 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QAbstractTableModel>
|
|
|
|
#include <QStandardItem>
|
2018-05-26 20:25:00 +02:00
|
|
|
#include <boost/optional.hpp>
|
2018-04-29 23:25:49 +02:00
|
|
|
#include <util/signalvector2.hpp>
|
|
|
|
|
|
|
|
#include <pajlada/signals/signalholder.hpp>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace util {
|
|
|
|
|
|
|
|
template <typename TVectorItem>
|
|
|
|
class SignalVectorModel : public QAbstractTableModel, pajlada::Signals::SignalHolder
|
|
|
|
{
|
|
|
|
public:
|
2018-04-30 00:41:58 +02:00
|
|
|
SignalVectorModel(int columnCount, QObject *parent = nullptr)
|
2018-04-29 23:25:49 +02:00
|
|
|
: QAbstractTableModel(parent)
|
|
|
|
, _columnCount(columnCount)
|
|
|
|
{
|
2018-04-30 00:41:58 +02:00
|
|
|
for (int i = 0; i < columnCount; i++) {
|
|
|
|
this->_headerData.emplace_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(util::BaseSignalVector<TVectorItem> *vec)
|
|
|
|
{
|
|
|
|
this->vector = vec;
|
|
|
|
|
2018-05-27 03:17:33 +02:00
|
|
|
auto insert = [this](const SignalVectorItemArgs<TVectorItem> &args) {
|
2018-04-30 23:30:05 +02:00
|
|
|
if (args.caller == this) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get row index
|
2018-05-06 00:32:45 +02:00
|
|
|
int index = this->getModelIndexFromVectorIndex(args.index);
|
|
|
|
assert(index >= 0 && index <= this->rows.size());
|
2018-04-30 23:30:05 +02:00
|
|
|
|
|
|
|
// get row items
|
2018-05-06 00:32:45 +02:00
|
|
|
std::vector<QStandardItem *> row = this->createRow();
|
|
|
|
this->getRowFromItem(args.item, row);
|
2018-04-29 23:25:49 +02:00
|
|
|
|
|
|
|
// insert row
|
2018-05-06 00:32:45 +02:00
|
|
|
index = this->beforeInsert(args.item, row, index);
|
|
|
|
|
|
|
|
this->beginInsertRows(QModelIndex(), index, index);
|
2018-05-26 20:25:00 +02:00
|
|
|
this->rows.insert(this->rows.begin() + index, Row(row, args.item));
|
2018-04-29 23:25:49 +02:00
|
|
|
this->endInsertRows();
|
2018-04-30 00:41:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (const TVectorItem &item : vec->getVector()) {
|
2018-05-27 03:17:33 +02:00
|
|
|
SignalVectorItemArgs<TVectorItem> args{item, i++, 0};
|
2018-04-30 00:41:58 +02:00
|
|
|
|
|
|
|
insert(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->managedConnect(vec->itemInserted, insert);
|
|
|
|
|
2018-04-29 23:25:49 +02:00
|
|
|
this->managedConnect(vec->itemRemoved, [this](auto args) {
|
2018-04-30 23:30:05 +02:00
|
|
|
if (args.caller == this) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int row = this->getModelIndexFromVectorIndex(args.index);
|
2018-04-29 23:25:49 +02:00
|
|
|
assert(row >= 0 && row <= this->rows.size());
|
|
|
|
|
|
|
|
// remove row
|
|
|
|
this->beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
for (QStandardItem *item : this->rows[row].items) {
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
this->rows.erase(this->rows.begin() + row);
|
|
|
|
this->endRemoveRows();
|
|
|
|
});
|
2018-05-06 00:32:45 +02:00
|
|
|
|
|
|
|
this->afterInit();
|
2018-04-29 23:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~SignalVectorModel()
|
|
|
|
{
|
|
|
|
for (Row &row : this->rows) {
|
|
|
|
for (QStandardItem *item : row.items) {
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 00:41:58 +02:00
|
|
|
virtual int rowCount(const QModelIndex &parent) const
|
2018-04-29 23:25:49 +02:00
|
|
|
{
|
|
|
|
return this->rows.size();
|
|
|
|
}
|
|
|
|
|
2018-04-30 00:41:58 +02:00
|
|
|
virtual int columnCount(const QModelIndex &parent) const
|
2018-04-29 23:25:49 +02:00
|
|
|
{
|
|
|
|
return this->_columnCount;
|
|
|
|
}
|
|
|
|
|
2018-04-30 00:41:58 +02:00
|
|
|
virtual QVariant data(const QModelIndex &index, int role) const
|
2018-04-29 23:25:49 +02:00
|
|
|
{
|
|
|
|
int row = index.row(), column = index.column();
|
|
|
|
assert(row >= 0 && row < this->rows.size() && column >= 0 && column < this->_columnCount);
|
|
|
|
|
|
|
|
return rows[row].items[column]->data(role);
|
|
|
|
}
|
|
|
|
|
2018-04-30 00:41:58 +02:00
|
|
|
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
|
2018-04-29 23:25:49 +02:00
|
|
|
{
|
2018-04-30 23:30:05 +02:00
|
|
|
int row = index.row(), column = index.column();
|
|
|
|
assert(row >= 0 && row < this->rows.size() && column >= 0 && column < this->_columnCount);
|
|
|
|
|
2018-05-06 00:32:45 +02:00
|
|
|
Row &rowItem = this->rows[row];
|
|
|
|
|
|
|
|
rowItem.items[column]->setData(value, role);
|
2018-04-30 23:30:05 +02:00
|
|
|
|
2018-05-06 00:32:45 +02:00
|
|
|
if (rowItem.isCustomRow) {
|
|
|
|
this->customRowSetData(rowItem.items, column, value, role);
|
|
|
|
} else {
|
|
|
|
int vecRow = this->getVectorIndexFromModelIndex(row);
|
|
|
|
this->vector->removeItem(vecRow, this);
|
2018-05-26 20:25:00 +02:00
|
|
|
|
|
|
|
assert(this->rows[row].original);
|
|
|
|
TVectorItem item =
|
|
|
|
this->getItemFromRow(this->rows[row].items, this->rows[row].original.get());
|
2018-05-06 00:32:45 +02:00
|
|
|
this->vector->insertItem(item, vecRow, this);
|
|
|
|
}
|
2018-04-29 23:25:49 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-30 00:41:58 +02:00
|
|
|
QVariant headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if (orientation != Qt::Horizontal) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = this->_headerData[section].find(role);
|
|
|
|
if (it == this->_headerData[section].end()) {
|
|
|
|
return QVariant();
|
|
|
|
} else {
|
|
|
|
return it.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
|
|
|
|
int role = Qt::DisplayRole)
|
|
|
|
{
|
|
|
|
if (orientation != Qt::Horizontal) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->_headerData[section][role] = value;
|
2018-05-06 00:32:45 +02:00
|
|
|
|
|
|
|
emit this->headerDataChanged(Qt::Horizontal, section, section);
|
2018-04-30 00:41:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-30 23:30:05 +02:00
|
|
|
Qt::ItemFlags flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
int row = index.row(), column = index.column();
|
|
|
|
assert(row >= 0 && row < this->rows.size() && column >= 0 && column < this->_columnCount);
|
|
|
|
|
|
|
|
return this->rows[index.row()].items[index.column()]->flags();
|
|
|
|
}
|
|
|
|
|
2018-05-06 00:32:45 +02:00
|
|
|
QStandardItem *getItem(int row, int column)
|
2018-04-29 23:25:49 +02:00
|
|
|
{
|
|
|
|
assert(row >= 0 && row < this->rows.size() && column >= 0 && column < this->_columnCount);
|
|
|
|
|
2018-04-30 00:41:58 +02:00
|
|
|
return rows[row].items[column];
|
|
|
|
}
|
|
|
|
|
2018-05-06 00:32:45 +02:00
|
|
|
void deleteRow(int row)
|
|
|
|
{
|
|
|
|
int signalVectorRow = this->getVectorIndexFromModelIndex(row);
|
|
|
|
this->vector->removeItem(signalVectorRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool removeRows(int row, int count, const QModelIndex &parent) override
|
2018-04-30 00:41:58 +02:00
|
|
|
{
|
2018-05-06 00:32:45 +02:00
|
|
|
if (count != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(row >= 0 && row < this->rows.size());
|
2018-04-30 00:41:58 +02:00
|
|
|
|
2018-04-30 23:30:05 +02:00
|
|
|
int signalVectorRow = this->getVectorIndexFromModelIndex(row);
|
2018-04-30 00:41:58 +02:00
|
|
|
this->vector->removeItem(signalVectorRow);
|
2018-05-06 00:32:45 +02:00
|
|
|
|
|
|
|
return true;
|
2018-04-29 23:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-05-06 00:32:45 +02:00
|
|
|
virtual void afterInit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-04-30 23:30:05 +02:00
|
|
|
// turn a vector item into a model row
|
2018-05-26 20:25:00 +02:00
|
|
|
virtual TVectorItem getItemFromRow(std::vector<QStandardItem *> &row,
|
|
|
|
const TVectorItem &original) = 0;
|
2018-04-30 23:30:05 +02:00
|
|
|
|
|
|
|
// turns a row in the model into a vector item
|
|
|
|
virtual void getRowFromItem(const TVectorItem &item, std::vector<QStandardItem *> &row) = 0;
|
|
|
|
|
2018-05-06 00:32:45 +02:00
|
|
|
virtual int beforeInsert(const TVectorItem &item, std::vector<QStandardItem *> &row,
|
|
|
|
int proposedIndex)
|
|
|
|
{
|
|
|
|
return proposedIndex;
|
|
|
|
}
|
2018-04-30 23:30:05 +02:00
|
|
|
|
2018-05-06 00:32:45 +02:00
|
|
|
virtual void afterRemoved(const TVectorItem &item, std::vector<QStandardItem *> &row, int index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void customRowSetData(const std::vector<QStandardItem *> &row, int column,
|
|
|
|
const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void insertCustomRow(std::vector<QStandardItem *> row, int index)
|
|
|
|
{
|
|
|
|
assert(index >= 0 && index <= this->rows.size());
|
|
|
|
|
|
|
|
this->beginInsertRows(QModelIndex(), index, index);
|
|
|
|
this->rows.insert(this->rows.begin() + index, Row(std::move(row), true));
|
|
|
|
this->endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<QStandardItem *> createRow()
|
|
|
|
{
|
|
|
|
std::vector<QStandardItem *> row;
|
|
|
|
for (int i = 0; i < this->_columnCount; i++) {
|
|
|
|
row.push_back(new QStandardItem());
|
|
|
|
}
|
|
|
|
return row;
|
|
|
|
}
|
2018-04-29 23:25:49 +02:00
|
|
|
|
|
|
|
struct Row {
|
|
|
|
std::vector<QStandardItem *> items;
|
2018-05-26 20:25:00 +02:00
|
|
|
boost::optional<TVectorItem> original;
|
2018-04-29 23:25:49 +02:00
|
|
|
bool isCustomRow;
|
|
|
|
|
2018-05-06 00:32:45 +02:00
|
|
|
Row(std::vector<QStandardItem *> _items, bool _isCustomRow = false)
|
|
|
|
: items(std::move(_items))
|
2018-04-29 23:25:49 +02:00
|
|
|
, isCustomRow(_isCustomRow)
|
|
|
|
{
|
2018-05-26 20:25:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Row(std::vector<QStandardItem *> _items, const TVectorItem &_original,
|
|
|
|
bool _isCustomRow = false)
|
|
|
|
: items(std::move(_items))
|
|
|
|
, original(_original)
|
|
|
|
, isCustomRow(_isCustomRow)
|
|
|
|
{
|
2018-04-29 23:25:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
std::vector<Row> rows;
|
2018-05-06 00:32:45 +02:00
|
|
|
|
|
|
|
private:
|
2018-04-30 00:41:58 +02:00
|
|
|
std::vector<QMap<int, QVariant>> _headerData;
|
|
|
|
BaseSignalVector<TVectorItem> *vector;
|
|
|
|
|
2018-04-29 23:25:49 +02:00
|
|
|
int _columnCount;
|
2018-05-06 00:32:45 +02:00
|
|
|
|
|
|
|
// returns the related index of the SignalVector
|
|
|
|
int getVectorIndexFromModelIndex(int index)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (auto &row : this->rows) {
|
|
|
|
if (row.isCustomRow) {
|
|
|
|
index--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == index) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the related index of the model
|
|
|
|
int getModelIndexFromVectorIndex(int index)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (auto &row : this->rows) {
|
|
|
|
if (row.isCustomRow) {
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == index) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
2018-04-29 23:25:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
} // namespace chatterino
|