made commands view more abstract

This commit is contained in:
fourtf
2018-04-30 00:41:58 +02:00
parent 6bd787423d
commit 13f1caa294
6 changed files with 146 additions and 33 deletions
+1 -1
View File
@@ -66,7 +66,7 @@ public:
TVectorItem item = this->vector[index];
this->vector.erase(this->vector.begin() + index);
ItemArgs args{item, args, caller};
ItemArgs args{item, index, caller};
this->itemRemoved.invoke(args);
}
};
+81 -15
View File
@@ -13,27 +13,45 @@ template <typename TVectorItem>
class SignalVectorModel : public QAbstractTableModel, pajlada::Signals::SignalHolder
{
public:
SignalVectorModel(util::BaseSignalVector<TVectorItem> *vec, int columnCount,
QObject *parent = nullptr)
SignalVectorModel(int columnCount, QObject *parent = nullptr)
: QAbstractTableModel(parent)
, _columnCount(columnCount)
{
this->managedConnect(vec->itemInserted, [this](auto args) {
for (int i = 0; i < columnCount; i++) {
this->_headerData.emplace_back();
}
}
void init(util::BaseSignalVector<TVectorItem> *vec)
{
this->vector = vec;
auto insert = [this](const BaseSignalVector<TVectorItem>::ItemArgs &args) {
std::vector<QStandardItem *> items;
for (int i = 0; i < this->_columnCount; i++) {
items.push_back(new QStandardItem());
}
int row = this->prepareInsert(args.item, args.index, items);
int row = this->prepareVectorInserted(args.item, args.index, items);
assert(row >= 0 && row <= this->rows.size());
// insert row
this->beginInsertRows(QModelIndex(), row, row);
this->rows.insert(this->rows.begin() + row, Row(items));
this->endInsertRows();
});
};
int i = 0;
for (const TVectorItem &item : vec->getVector()) {
BaseSignalVector<TVectorItem>::ItemArgs args{item, i++, 0};
insert(args);
}
this->managedConnect(vec->itemInserted, insert);
this->managedConnect(vec->itemRemoved, [this](auto args) {
int row = this->prepareRemove(args.item, args.index);
int row = this->prepareVectorRemoved(args.item, args.index);
assert(row >= 0 && row <= this->rows.size());
// remove row
@@ -55,17 +73,17 @@ public:
}
}
int rowCount(const QModelIndex &parent) const
virtual int rowCount(const QModelIndex &parent) const
{
return this->rows.size();
}
int columnCount(const QModelIndex &parent) const
virtual int columnCount(const QModelIndex &parent) const
{
return this->_columnCount;
}
QVariant data(const QModelIndex &index, int role) const
virtual QVariant data(const QModelIndex &index, int role) const
{
int row = index.row(), column = index.column();
assert(row >= 0 && row < this->rows.size() && column >= 0 && column < this->_columnCount);
@@ -73,24 +91,69 @@ public:
return rows[row].items[column]->data(role);
}
bool setData(const QModelIndex &index, const QVariant &value, int role)
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
{
this->rows[index.row()].items[index.column()]->setData(value, role);
return true;
}
QStandardItem *getItem(int row, int column)
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;
return true;
}
virtual QStandardItem *getItem(int row, int column)
{
assert(row >= 0 && row < this->rows.size() && column >= 0 && column < this->_columnCount);
return rows[row][column];
return rows[row].items[column];
}
void removeRow(int row)
{
assert(row >= 0 && row <= this->rows.size());
int signalVectorRow = this->prepareModelItemRemoved(row);
this->vector->removeItem(signalVectorRow);
}
protected:
virtual int prepareInsert(const TVectorItem &item, int index,
std::vector<QStandardItem *> &rowToAdd) = 0;
virtual int prepareRemove(const TVectorItem &item, int index) = 0;
// gets called when an item gets inserted into the SignalVector
//
// returns the index of that the row should be inserted into and edits the rowToAdd elements
// based on the item
virtual int prepareVectorInserted(const TVectorItem &item, int index,
std::vector<QStandardItem *> &rowToAdd) = 0;
// gets called when an item gets removed from a SignalVector
//
// returns the index of the row in the model that should be removed
virtual int prepareVectorRemoved(const TVectorItem &item, int index) = 0;
// gets called when an item gets removed from the model
//
// returns the related index of the SignalVector
virtual int prepareModelItemRemoved(int index) = 0;
private:
struct Row {
@@ -105,6 +168,9 @@ private:
};
std::vector<Row> rows;
std::vector<QMap<int, QVariant>> _headerData;
BaseSignalVector<TVectorItem> *vector;
int _columnCount;
};