changed CommandManager to CommandController
This commit is contained in:
@@ -57,7 +57,8 @@ template <typename TVectorItem>
|
||||
class BaseSignalVector : public ReadOnlySignalVector<TVectorItem>
|
||||
{
|
||||
public:
|
||||
virtual void appendItem(const TVectorItem &item, void *caller = 0) = 0;
|
||||
// returns the actual index of the inserted item
|
||||
virtual int insertItem(const TVectorItem &item, int proposedIndex = -1, void *caller = 0) = 0;
|
||||
|
||||
void removeItem(int index, void *caller = 0)
|
||||
{
|
||||
@@ -69,26 +70,31 @@ public:
|
||||
typename ReadOnlySignalVector<TVectorItem>::ItemArgs args{item, index, caller};
|
||||
this->itemRemoved.invoke(args);
|
||||
}
|
||||
|
||||
int appendItem(const TVectorItem &item, void *caller = 0)
|
||||
{
|
||||
return this->insertItem(item, -1, caller);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TVectorItem>
|
||||
class UnsortedSignalVector : public BaseSignalVector<TVectorItem>
|
||||
{
|
||||
public:
|
||||
void insertItem(const TVectorItem &item, int index, void *caller = 0)
|
||||
virtual int insertItem(const TVectorItem &item, int index = -1, void *caller = 0) override
|
||||
{
|
||||
util::assertInGuiThread();
|
||||
assert(index >= 0 && index <= this->vector.size());
|
||||
if (index == -1) {
|
||||
index = this->vector.size();
|
||||
} else {
|
||||
assert(index >= 0 && index <= this->vector.size());
|
||||
}
|
||||
|
||||
this->vector.insert(this->vector.begin() + index, item);
|
||||
|
||||
typename ReadOnlySignalVector<TVectorItem>::ItemArgs args{item, index, caller};
|
||||
this->itemInserted.invoke(args);
|
||||
}
|
||||
|
||||
virtual void appendItem(const TVectorItem &item, void *caller = 0) override
|
||||
{
|
||||
this->insertItem(item, this->vector.size(), caller);
|
||||
return index;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -96,7 +102,7 @@ template <typename TVectorItem>
|
||||
class SortedSignalVector : public BaseSignalVector<TVectorItem>
|
||||
{
|
||||
public:
|
||||
virtual void appendItem(const TVectorItem &item, void *caller = 0) override
|
||||
virtual int insertItem(const TVectorItem &item, int index = -1, void *caller = 0) override
|
||||
{
|
||||
util::assertInGuiThread();
|
||||
|
||||
@@ -105,6 +111,7 @@ public:
|
||||
this->vector.begin();
|
||||
typename ReadOnlySignalVector<TVectorItem>::ItemArgs args{item, index, caller};
|
||||
this->itemInserted.invoke(args);
|
||||
return index;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user