Files
BeetRound/libs/BeetRoundCore/model/tablemodel.h

114 lines
3.9 KiB
C++

#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QAbstractTableModel>
#include "metadata.h"
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);
void setOnlineCredentials(const QString& mail, const QString& uuid, const QString& token);
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());
/// property functions
int nExpectedBiddings() const;
int nPlacedBiddings1() const;
int nPlacedBiddings2() const;
int nPlacedBiddings3() const;
int biddingSum1() const;
int biddingSum2() const;
int biddingSum3() const;
qreal biddingAverage1() const;
qreal biddingAverage2() const;
qreal biddingAverage3() const;
signals:
void rowCountChanged();
void nExpectedBiddingsChanged();
void nPlacedBiddingsChanged(const int roundNumber);
void biddingSumChanged(const int roundNumber);
void biddingAverageChanged(const int roundNumber);
private slots:
void onRowCountChanged(const QModelIndex& parent, int first, int last);
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;
QModelIndex searchItemIndex(const ModelItemValues givenItemValues) const;
bool isItemEqualToItemValues(const QModelIndex& itemIndex,
const ModelItemValues givenItemValues) const;
int nPlacedBiddings(const UserRoles biddingRole) const;
int biddingSum(const UserRoles biddingRole) const;
qreal averageBiddingAmount(const UserRoles biddingRole) const;
qreal totalSharesWithBiddings(const UserRoles biddingRole) const;
QModelIndex getIndexByRoleValue(const QString& valueString, const int role) const;
};
#endif // TABLEMODEL_H