Compare commits

...

2 Commits

7 changed files with 198 additions and 1 deletions

View File

@ -22,6 +22,8 @@ add_library(${TARGET_APP} STATIC
${TS_FILES}
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})

View File

@ -10,10 +10,15 @@
#include "../../ApplicationConfig.h"
#include "CoreConfig.h"
#include "constants.h"
#include "model/tablemodel.h"
using namespace std;
GenericCore::GenericCore() { qDebug() << "Creating core..."; }
GenericCore::GenericCore() {
qDebug() << "Creating core...";
setupModels();
}
GenericCore::~GenericCore() { qDebug() << "Destroying core..."; }
@ -65,6 +70,13 @@ void GenericCore::triggerApplicationUpdate() {
QProcess::startDetached(toolFilePath, args);
}
std::shared_ptr<QAbstractItemModel> GenericCore::getModel() const { return m_mainModel; }
void GenericCore::setupModels() {
m_mainModel = make_shared<TableModel>(this);
// TODO add QAbstractItemModelTester
}
QString GenericCore::getMaintenanceToolFilePath() const {
QString applicationDirPath = QCoreApplication::applicationDirPath();

View File

@ -3,8 +3,11 @@
#include <QObject>
class QAbstractItemModel;
class QString;
class TableModel;
class GenericCore : public QObject {
Q_OBJECT
@ -18,10 +21,16 @@ class GenericCore : public QObject {
bool isApplicationUpdateAvailable();
void triggerApplicationUpdate();
std::shared_ptr<QAbstractItemModel> getModel() const;
signals:
void displayStatusMessage(QString message);
private:
std::shared_ptr<TableModel> m_mainModel;
void setupModels();
QString getMaintenanceToolFilePath() const;
};

8
model/modelitem.cpp Normal file
View 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
View 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

115
model/tablemodel.cpp Normal file
View File

@ -0,0 +1,115 @@
#include "tablemodel.h"
#include "modelitem.h"
enum UserRoles {
NameRole = Qt::UserRole + 1,
DescriptionRole,
InfoRole,
AmountRole,
FactorRole,
FactoredAmountRole
};
TableModel::TableModel(QObject* 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;
shared_ptr<ModelItem> item = make_unique<ModelItem>(values);
m_items.append(std::move(item));
}
}
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();
const int column = index.column();
if (!index.isValid()) {
return QVariant();
}
if (row >= rowCount(QModelIndex()) || column >= columnCount(QModelIndex())) {
return QVariant();
}
int roleForColumn = getRoleForColumn(column);
switch (role) {
case Qt::DisplayRole:
return m_items.at(row)->data(roleForColumn);
}
return QVariant();
}
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (role == Qt::DisplayRole) {
if (orientation == Qt::Horizontal) {
return QString("Section %1").arg(section);
} else {
return QString("%1").arg(section);
}
}
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;
}
}

33
model/tablemodel.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QAbstractTableModel>
class ModelItem;
using namespace std;
class TableModel : public QAbstractTableModel {
public:
explicit TableModel(QObject* parent = nullptr);
/// QAbstractItemModel interface
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