81 lines
2.9 KiB
C++
81 lines
2.9 KiB
C++
#ifndef TABLEMODEL_H
|
|
#define TABLEMODEL_H
|
|
|
|
#include <QAbstractTableModel>
|
|
|
|
class QUndoStack;
|
|
class ModelItem;
|
|
|
|
using namespace std;
|
|
|
|
typedef QMap<int, QVariant> ModelItemValues;
|
|
|
|
class TableModel : public QAbstractTableModel {
|
|
Q_OBJECT
|
|
|
|
friend class InsertRowsCommand;
|
|
friend class RemoveRowsCommand;
|
|
friend class EditItemCommand;
|
|
|
|
public:
|
|
static QByteArray generateExampleItems();
|
|
|
|
explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr);
|
|
|
|
/// QAbstractItemModel interface
|
|
Qt::ItemFlags flags(const QModelIndex& index) const override;
|
|
QHash<int, QByteArray> roleNames() const override;
|
|
|
|
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
|
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
|
QVariant data(const QModelIndex& index, int role) const override;
|
|
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
|
|
|
bool setData(const QModelIndex& index, const QVariant& value, int role) override;
|
|
bool setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) override;
|
|
|
|
ModelItemValues getItemValues(const QModelIndex& index) const;
|
|
QJsonDocument getAllItemsAsJsonDoc() const;
|
|
QList<QStringList> getItemsAsStringLists() const;
|
|
|
|
QByteArray jsonDataForServer(const QModelIndex& currentIndex) const;
|
|
|
|
QString updateItemsFromJson(const QByteArray& jsonData);
|
|
bool updateItem(const ModelItemValues& itemValues);
|
|
|
|
public slots:
|
|
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
|
// override;
|
|
bool removeRows(int firstRow, int nRows, const QModelIndex& parentIndex = QModelIndex()) override;
|
|
void appendItems(const QByteArray& jsonDoc);
|
|
void insertItems(int startPosition,
|
|
const QByteArray& jsonDoc,
|
|
const QModelIndex& parentIndex = QModelIndex());
|
|
void insertItems(int startPosition,
|
|
const QList<ModelItemValues>& itemValuesList,
|
|
const QModelIndex& parentIndex = QModelIndex());
|
|
|
|
private:
|
|
/// *** members ***
|
|
// TODO shared_ptr -> unique_ptr
|
|
QList<shared_ptr<ModelItem>> m_items;
|
|
QUndoStack* m_undoStack;
|
|
|
|
/// *** functions ***
|
|
/// undo/redo functions
|
|
void execInsertItems(const int firstRow, const QList<ModelItemValues> valueList);
|
|
void execRemoveItems(const int firstRow, const int nRows);
|
|
void execEditItemData(const int row, const QMap<int, QVariant>& changedValues);
|
|
|
|
/// misc functions
|
|
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
|
|
const QMap<int, QVariant>& roleValueMap) const;
|
|
bool isEmptyValueEqualToZero(const int role) const;
|
|
int getAppropriateRoleForIndex(const QModelIndex& index, const int role) const;
|
|
QModelIndex searchItemIndex(const ModelItemValues givenItemValues) const;
|
|
bool isItemEqualToItemValues(const QModelIndex& itemIndex,
|
|
const ModelItemValues givenItemValues) const;
|
|
};
|
|
|
|
#endif // TABLEMODEL_H
|