64 lines
2.2 KiB
C++
64 lines
2.2 KiB
C++
#ifndef TABLEMODEL_H
|
|
#define TABLEMODEL_H
|
|
|
|
#include <QAbstractTableModel>
|
|
|
|
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<int, QByteArray> ROLE_NAMES;
|
|
static QList<QString> intColumns;
|
|
|
|
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;
|
|
|
|
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<shared_ptr<ModelItem>> m_items;
|
|
QUndoStack* m_undoStack;
|
|
|
|
/// *** functions ***
|
|
/// undo/redo functions
|
|
void execInsertItems(const int firstRow, const QList<QHash<int, QVariant>> valueList);
|
|
void execRemoveItems(const int firstRow, const int nRows);
|
|
void execEditItemData(const int row, const QMap<int, QVariant>& changedValues);
|
|
|
|
/// misc functions
|
|
int getRoleForColumn(const int column) const;
|
|
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
|
|
const QMap<int, QVariant>& roleValueMap) const;
|
|
bool isEmptyValueEqualToZero(const int role) const;
|
|
};
|
|
|
|
#endif // TABLEMODEL_H
|