#ifndef TABLEMODEL_H #define TABLEMODEL_H #include class QUndoStack; class ModelItem; using namespace std; class TableModel : public QAbstractTableModel { Q_OBJECT friend class InsertRowsCommand; friend class RemoveRowsCommand; friend class EditItemCommand; public: enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole }; static QHash ROLE_NAMES; static QList intColumns; explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr); /// QAbstractItemModel interface Qt::ItemFlags flags(const QModelIndex& index) const override; QHash 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& roles) override; 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); private: /// *** members *** // TODO shared_ptr -> unique_ptr QList> m_items; QUndoStack* m_undoStack; /// *** functions *** /// undo/redo functions void execInsertItems(const int firstRow, const QList> valueList); void execRemoveItems(const int firstRow, const int nRows); void execEditItemData(const int row, const QMap& changedValues); /// misc functions int getRoleForColumn(const int column) const; QMap onlyChangedValues(const QModelIndex& index, const QMap& roleValueMap) const; bool isEmptyValueEqualToZero(const int role) const; }; #endif // TABLEMODEL_H