created base for all the list based settings
This commit is contained in:
@@ -19,6 +19,8 @@ public:
|
||||
{
|
||||
QObject::connect(&this->itemsChangedTimer, &QTimer::timeout,
|
||||
[this] { this->delayedItemsChanged.invoke(); });
|
||||
this->itemsChangedTimer.setInterval(100);
|
||||
this->itemsChangedTimer.setSingleShot(true);
|
||||
}
|
||||
virtual ~ReadOnlySignalVector() = default;
|
||||
|
||||
@@ -69,6 +71,8 @@ public:
|
||||
this->vector.erase(this->vector.begin() + index);
|
||||
typename ReadOnlySignalVector<TVectorItem>::ItemArgs args{item, index, caller};
|
||||
this->itemRemoved.invoke(args);
|
||||
|
||||
this->invokeDelayedItemsChanged();
|
||||
}
|
||||
|
||||
int appendItem(const TVectorItem &item, void *caller = 0)
|
||||
@@ -94,6 +98,7 @@ public:
|
||||
|
||||
typename ReadOnlySignalVector<TVectorItem>::ItemArgs args{item, index, caller};
|
||||
this->itemInserted.invoke(args);
|
||||
this->invokeDelayedItemsChanged();
|
||||
return index;
|
||||
}
|
||||
};
|
||||
@@ -111,6 +116,7 @@ public:
|
||||
this->vector.begin();
|
||||
typename ReadOnlySignalVector<TVectorItem>::ItemArgs args{item, index, caller};
|
||||
this->itemInserted.invoke(args);
|
||||
this->invokeDelayedItemsChanged();
|
||||
return index;
|
||||
}
|
||||
};
|
||||
|
||||
+117
-27
@@ -32,20 +32,18 @@ public:
|
||||
}
|
||||
|
||||
// get row index
|
||||
int row = this->getModelIndexFromVectorIndex(args.index);
|
||||
assert(row >= 0 && row <= this->rows.size());
|
||||
int index = this->getModelIndexFromVectorIndex(args.index);
|
||||
assert(index >= 0 && index <= this->rows.size());
|
||||
|
||||
// get row items
|
||||
std::vector<QStandardItem *> items;
|
||||
for (int i = 0; i < this->_columnCount; i++) {
|
||||
items.push_back(new QStandardItem());
|
||||
}
|
||||
|
||||
this->getRowFromItem(args.item, items);
|
||||
std::vector<QStandardItem *> row = this->createRow();
|
||||
this->getRowFromItem(args.item, row);
|
||||
|
||||
// insert row
|
||||
this->beginInsertRows(QModelIndex(), row, row);
|
||||
this->rows.insert(this->rows.begin() + row, Row(items));
|
||||
index = this->beforeInsert(args.item, row, index);
|
||||
|
||||
this->beginInsertRows(QModelIndex(), index, index);
|
||||
this->rows.insert(this->rows.begin() + index, Row(row));
|
||||
this->endInsertRows();
|
||||
};
|
||||
|
||||
@@ -74,6 +72,8 @@ public:
|
||||
this->rows.erase(this->rows.begin() + row);
|
||||
this->endRemoveRows();
|
||||
});
|
||||
|
||||
this->afterInit();
|
||||
}
|
||||
|
||||
virtual ~SignalVectorModel()
|
||||
@@ -108,12 +108,18 @@ public:
|
||||
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);
|
||||
Row &rowItem = this->rows[row];
|
||||
|
||||
int vecRow = this->getVectorIndexFromModelIndex(row);
|
||||
this->vector->removeItem(vecRow, this);
|
||||
TVectorItem item = this->getItemFromRow(this->rows[row].items);
|
||||
this->vector->insertItem(item, vecRow, this);
|
||||
rowItem.items[column]->setData(value, role);
|
||||
|
||||
if (rowItem.isCustomRow) {
|
||||
this->customRowSetData(rowItem.items, column, value, role);
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
@@ -140,6 +146,8 @@ public:
|
||||
}
|
||||
|
||||
this->_headerData[section][role] = value;
|
||||
|
||||
emit this->headerDataChanged(Qt::Horizontal, section, section);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -151,51 +159,133 @@ public:
|
||||
return this->rows[index.row()].items[index.column()]->flags();
|
||||
}
|
||||
|
||||
virtual QStandardItem *getItem(int row, int column)
|
||||
QStandardItem *getItem(int row, int column)
|
||||
{
|
||||
assert(row >= 0 && row < this->rows.size() && column >= 0 && column < this->_columnCount);
|
||||
|
||||
return rows[row].items[column];
|
||||
}
|
||||
|
||||
void removeRow(int row)
|
||||
void deleteRow(int row)
|
||||
{
|
||||
assert(row >= 0 && row <= this->rows.size());
|
||||
|
||||
int signalVectorRow = this->getVectorIndexFromModelIndex(row);
|
||||
this->vector->removeItem(signalVectorRow);
|
||||
}
|
||||
|
||||
virtual bool removeRows(int row, int count, const QModelIndex &parent) override
|
||||
{
|
||||
if (count != 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
assert(row >= 0 && row < this->rows.size());
|
||||
|
||||
int signalVectorRow = this->getVectorIndexFromModelIndex(row);
|
||||
this->vector->removeItem(signalVectorRow);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void afterInit()
|
||||
{
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
// returns the related index of the SignalVector
|
||||
virtual int getVectorIndexFromModelIndex(int index) = 0;
|
||||
virtual int beforeInsert(const TVectorItem &item, std::vector<QStandardItem *> &row,
|
||||
int proposedIndex)
|
||||
{
|
||||
return proposedIndex;
|
||||
}
|
||||
|
||||
// returns the related index of the model
|
||||
virtual int getModelIndexFromVectorIndex(int index) = 0;
|
||||
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;
|
||||
}
|
||||
|
||||
private:
|
||||
struct Row {
|
||||
std::vector<QStandardItem *> items;
|
||||
bool isCustomRow;
|
||||
|
||||
Row(const std::vector<QStandardItem *> _items, bool _isCustomRow = false)
|
||||
: items(_items)
|
||||
Row(std::vector<QStandardItem *> _items, bool _isCustomRow = false)
|
||||
: items(std::move(_items))
|
||||
, isCustomRow(_isCustomRow)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<Row> rows;
|
||||
|
||||
private:
|
||||
std::vector<QMap<int, QVariant>> _headerData;
|
||||
BaseSignalVector<TVectorItem> *vector;
|
||||
|
||||
int _columnCount;
|
||||
|
||||
// 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;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace util
|
||||
|
||||
@@ -5,21 +5,20 @@
|
||||
namespace chatterino {
|
||||
namespace util {
|
||||
|
||||
static QStandardItem *boolItem(bool value, bool userCheckable = true, bool selectable = true)
|
||||
static void setBoolItem(QStandardItem *item, bool value, bool userCheckable = true,
|
||||
bool selectable = true)
|
||||
{
|
||||
auto *item = new QStandardItem();
|
||||
item->setFlags((Qt::ItemFlags)(Qt::ItemIsEnabled | (selectable ? Qt::ItemIsSelectable : 0) |
|
||||
(userCheckable ? Qt::ItemIsUserCheckable : 0)));
|
||||
item->setCheckState(value ? Qt::Checked : Qt::Unchecked);
|
||||
return item;
|
||||
}
|
||||
|
||||
static QStandardItem *stringItem(const QString &value, bool editable = true, bool selectable = true)
|
||||
static void setStringItem(QStandardItem *item, const QString &value, bool editable = true,
|
||||
bool selectable = true)
|
||||
{
|
||||
auto *item = new QStandardItem(value);
|
||||
item->setData(value, Qt::EditRole);
|
||||
item->setFlags((Qt::ItemFlags)(Qt::ItemIsEnabled | (selectable ? Qt::ItemIsSelectable : 0) |
|
||||
(editable ? (Qt::ItemIsEditable) : 0)));
|
||||
return item;
|
||||
}
|
||||
|
||||
static QStandardItem *emptyItem()
|
||||
|
||||
Reference in New Issue
Block a user