Added a ModelItem class to hold the data for each row.
This commit is contained in:
@ -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})
|
||||
|
||||
@ -72,7 +72,10 @@ void GenericCore::triggerApplicationUpdate() {
|
||||
|
||||
std::shared_ptr<QAbstractItemModel> GenericCore::getModel() const { return m_mainModel; }
|
||||
|
||||
void GenericCore::setupModels() { m_mainModel = make_shared<TableModel>(this); }
|
||||
void GenericCore::setupModels() {
|
||||
m_mainModel = make_shared<TableModel>(this);
|
||||
// TODO add QAbstractItemModelTester
|
||||
}
|
||||
|
||||
QString GenericCore::getMaintenanceToolFilePath() const {
|
||||
QString applicationDirPath = QCoreApplication::applicationDirPath();
|
||||
|
||||
8
model/modelitem.cpp
Normal file
8
model/modelitem.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include "modelitem.h"
|
||||
|
||||
ModelItem::ModelItem(const QHash<int, QVariant> values)
|
||||
: m_values(values) {}
|
||||
|
||||
QVariant ModelItem::data(int role) const { return m_values.value(role); }
|
||||
|
||||
bool ModelItem::setData(const QVariant& value, int role) {}
|
||||
18
model/modelitem.h
Normal file
18
model/modelitem.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef MODELITEM_H
|
||||
#define MODELITEM_H
|
||||
|
||||
#include <QVariant>
|
||||
|
||||
class ModelItem {
|
||||
public:
|
||||
ModelItem(const QHash<int, QVariant> values);
|
||||
|
||||
QVariant data(int role) const;
|
||||
|
||||
bool setData(const QVariant& value, int role);
|
||||
|
||||
private:
|
||||
QHash<int, QVariant> m_values;
|
||||
};
|
||||
|
||||
#endif // MODELITEM_H
|
||||
@ -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<int, QVariant> 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<ModelItem> item = make_unique<ModelItem>(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<int, QVariant>& 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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,16 +3,31 @@
|
||||
|
||||
#include <QAbstractTableModel>
|
||||
|
||||
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<int, QVariant>& roles) override;
|
||||
|
||||
private:
|
||||
/// members
|
||||
QList<shared_ptr<ModelItem>> m_items;
|
||||
|
||||
/// functions
|
||||
int getRoleForColumn(const int column) const;
|
||||
};
|
||||
|
||||
#endif // TABLEMODEL_H
|
||||
|
||||
Reference in New Issue
Block a user