diff --git a/CMakeLists.txt b/CMakeLists.txt index 008325c..40ab47c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ add_library(${TARGET_APP} STATIC constants.h data/settingshandler.h data/settingshandler.cpp model/tablemodel.h model/tablemodel.cpp + model/modelitem.h model/modelitem.cpp ) include_directories(${CMAKE_CURRENT_BINARY_DIR}) diff --git a/genericcore.cpp b/genericcore.cpp index 57fb303..b589405 100644 --- a/genericcore.cpp +++ b/genericcore.cpp @@ -72,7 +72,10 @@ void GenericCore::triggerApplicationUpdate() { std::shared_ptr GenericCore::getModel() const { return m_mainModel; } -void GenericCore::setupModels() { m_mainModel = make_shared(this); } +void GenericCore::setupModels() { + m_mainModel = make_shared(this); + // TODO add QAbstractItemModelTester +} QString GenericCore::getMaintenanceToolFilePath() const { QString applicationDirPath = QCoreApplication::applicationDirPath(); diff --git a/model/modelitem.cpp b/model/modelitem.cpp new file mode 100644 index 0000000..258c713 --- /dev/null +++ b/model/modelitem.cpp @@ -0,0 +1,8 @@ +#include "modelitem.h" + +ModelItem::ModelItem(const QHash values) + : m_values(values) {} + +QVariant ModelItem::data(int role) const { return m_values.value(role); } + +bool ModelItem::setData(const QVariant& value, int role) {} diff --git a/model/modelitem.h b/model/modelitem.h new file mode 100644 index 0000000..002eafd --- /dev/null +++ b/model/modelitem.h @@ -0,0 +1,18 @@ +#ifndef MODELITEM_H +#define MODELITEM_H + +#include + +class ModelItem { + public: + ModelItem(const QHash values); + + QVariant data(int role) const; + + bool setData(const QVariant& value, int role); + + private: + QHash m_values; +}; + +#endif // MODELITEM_H diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index 0681c5f..cb415cf 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -1,11 +1,42 @@ #include "tablemodel.h" +#include "modelitem.h" + +enum UserRoles { + NameRole = Qt::UserRole + 1, + DescriptionRole, + InfoRole, + AmountRole, + FactorRole, + FactoredAmountRole +}; + TableModel::TableModel(QObject* parent) - : QAbstractTableModel{parent} {} + : QAbstractTableModel{parent} { + for (int row = 0; row < 23; ++row) { + QHash values; + values[NameRole] = QString("Item %1").arg(row); + values[DescriptionRole] = QString("This is item %1").arg(row); + values[InfoRole] = QString("Info of item %1").arg(row); + values[AmountRole] = row; + values[FactorRole] = row * 1.1; -int TableModel::rowCount(const QModelIndex& parent) const { return 5; } + shared_ptr item = make_unique(values); + m_items.append(std::move(item)); + } +} -int TableModel::columnCount(const QModelIndex& parent) const { return 5; } +Qt::ItemFlags TableModel::flags(const QModelIndex& index) const { + // return Qt::ItemIsEditable | QAbstractTableModel::flags(index); + return QAbstractTableModel::flags(index); +} + +int TableModel::rowCount(const QModelIndex& parent) const { return m_items.size(); } + +int TableModel::columnCount(const QModelIndex& parent) const { + // TODO read from amount of header names (when available) + return 5; +} QVariant TableModel::data(const QModelIndex& index, int role) const { const int row = index.row(); @@ -14,13 +45,14 @@ QVariant TableModel::data(const QModelIndex& index, int role) const { if (!index.isValid()) { return QVariant(); } - if (row >= 5 || column >= 5) { + if (row >= rowCount(QModelIndex()) || column >= columnCount(QModelIndex())) { return QVariant(); } + int roleForColumn = getRoleForColumn(column); switch (role) { case Qt::DisplayRole: - return QString("Data %1/%2").arg(row).arg(column); + return m_items.at(row)->data(roleForColumn); } return QVariant(); @@ -36,3 +68,48 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation, int ro } return QVariant(); } + +bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) { + if (role == Qt::EditRole) { + if (!checkIndex(index)) { + return false; + } + // save value from editor to member m_gridData + // m_gridData[index.row()][index.column()] = value.toString(); + // // for presentation purposes only: build and emit a joined string + // QString result; + // for (int row = 0; row < ROWS; row++) { + // for (int col = 0; col < COLS; col++) { + // result += m_gridData[row][col] + ' '; + // } + // } + // emit editCompleted(result); + // return true; + } + return false; +} + +// bool TableModel::setItemData(const QModelIndex& index, const QMap& roles) {} + +int TableModel::getRoleForColumn(const int column) const { + switch (column) { + case 0: + return NameRole; + break; + case 1: + return DescriptionRole; + break; + case 2: + return InfoRole; + break; + case 3: + return AmountRole; + break; + case 4: + return FactorRole; + break; + default: + return NameRole; + break; + } +} diff --git a/model/tablemodel.h b/model/tablemodel.h index 64523f0..f6b4180 100644 --- a/model/tablemodel.h +++ b/model/tablemodel.h @@ -3,16 +3,31 @@ #include +class ModelItem; + +using namespace std; + class TableModel : public QAbstractTableModel { public: explicit TableModel(QObject* parent = nullptr); /// QAbstractItemModel interface - public: + Qt::ItemFlags flags(const QModelIndex& index) const override; + int rowCount(const QModelIndex& parent) const override; int columnCount(const QModelIndex& parent) 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; + + private: + /// members + QList> m_items; + + /// functions + int getRoleForColumn(const int column) const; }; #endif // TABLEMODEL_H