changed CommandManager to CommandController

This commit is contained in:
fourtf
2018-04-30 23:30:05 +02:00
parent b907bf5639
commit 4c3f0921e2
17 changed files with 524 additions and 398 deletions
+42 -18
View File
@@ -27,13 +27,21 @@ public:
this->vector = vec;
auto insert = [this](const typename BaseSignalVector<TVectorItem>::ItemArgs &args) {
if (args.caller == this) {
return;
}
// get row index
int row = this->getModelIndexFromVectorIndex(args.index);
assert(row >= 0 && row <= this->rows.size());
// get row items
std::vector<QStandardItem *> items;
for (int i = 0; i < this->_columnCount; i++) {
items.push_back(new QStandardItem());
}
int row = this->prepareVectorInserted(args.item, args.index, items);
assert(row >= 0 && row <= this->rows.size());
this->getRowFromItem(args.item, items);
// insert row
this->beginInsertRows(QModelIndex(), row, row);
@@ -51,7 +59,11 @@ public:
this->managedConnect(vec->itemInserted, insert);
this->managedConnect(vec->itemRemoved, [this](auto args) {
int row = this->prepareVectorRemoved(args.item, args.index);
if (args.caller == this) {
return;
}
int row = this->getModelIndexFromVectorIndex(args.index);
assert(row >= 0 && row <= this->rows.size());
// remove row
@@ -93,7 +105,15 @@ public:
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
{
this->rows[index.row()].items[index.column()]->setData(value, role);
int row = index.row(), column = index.column();
assert(row >= 0 && row < this->rows.size() && column >= 0 && column < this->_columnCount);
this->rows[row].items[column]->setData(value, role);
int vecRow = this->getVectorIndexFromModelIndex(row);
this->vector->removeItem(vecRow, this);
TVectorItem item = this->getItemFromRow(this->rows[row].items);
this->vector->insertItem(item, vecRow, this);
return true;
}
@@ -123,6 +143,14 @@ public:
return true;
}
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();
}
virtual QStandardItem *getItem(int row, int column)
{
assert(row >= 0 && row < this->rows.size() && column >= 0 && column < this->_columnCount);
@@ -134,26 +162,22 @@ public:
{
assert(row >= 0 && row <= this->rows.size());
int signalVectorRow = this->prepareModelItemRemoved(row);
int signalVectorRow = this->getVectorIndexFromModelIndex(row);
this->vector->removeItem(signalVectorRow);
}
protected:
// 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;
// turn a vector item into a model row
virtual TVectorItem getItemFromRow(std::vector<QStandardItem *> &row) = 0;
// turns a row in the model into a vector item
virtual void getRowFromItem(const TVectorItem &item, std::vector<QStandardItem *> &row) = 0;
// gets called when an item gets removed from the model
//
// returns the related index of the SignalVector
virtual int prepareModelItemRemoved(int index) = 0;
virtual int getVectorIndexFromModelIndex(int index) = 0;
// returns the related index of the model
virtual int getModelIndexFromVectorIndex(int index) = 0;
private:
struct Row {