Compare commits
7 Commits
0dba9639e6
...
169d8f9f1e
| Author | SHA1 | Date | |
|---|---|---|---|
| 169d8f9f1e | |||
| d45b1098f9 | |||
| 4c906099eb | |||
| 144460b5aa | |||
| a07e03d80d | |||
| 40e1dd3002 | |||
| 16c524f7a2 |
@ -24,6 +24,7 @@ add_library(${TARGET_APP} STATIC
|
|||||||
data/settingshandler.h data/settingshandler.cpp
|
data/settingshandler.h data/settingshandler.cpp
|
||||||
model/tablemodel.h model/tablemodel.cpp
|
model/tablemodel.h model/tablemodel.cpp
|
||||||
model/modelitem.h model/modelitem.cpp
|
model/modelitem.h model/modelitem.cpp
|
||||||
|
formats/jsonparser.h formats/jsonparser.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
|||||||
58
formats/jsonparser.cpp
Normal file
58
formats/jsonparser.cpp
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#include "jsonparser.h"
|
||||||
|
|
||||||
|
#include <QJsonArray>
|
||||||
|
#include <QJsonObject>
|
||||||
|
|
||||||
|
#include "../model/tablemodel.h"
|
||||||
|
|
||||||
|
QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonData,
|
||||||
|
const QString& objectName) {
|
||||||
|
QList<QHash<int, QVariant>> result;
|
||||||
|
|
||||||
|
if (jsonData.isEmpty()) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonArray itemArray = extractItemArray(jsonData, objectName);
|
||||||
|
|
||||||
|
foreach (QJsonValue value, itemArray) {
|
||||||
|
QJsonObject itemJsonObject = value.toObject();
|
||||||
|
QHash<int, QVariant> values = jsonObjectToItemValues(itemJsonObject);
|
||||||
|
result.append(values);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
QHash<int, QVariant> JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
||||||
|
QHash<int, QVariant> values;
|
||||||
|
|
||||||
|
// TODO make this more generic (by reading from model meta data)
|
||||||
|
values[TableModel::NameRole] =
|
||||||
|
itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::NameRole)].toString();
|
||||||
|
values[TableModel::DescriptionRole] =
|
||||||
|
itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::DescriptionRole)].toString();
|
||||||
|
values[TableModel::InfoRole] =
|
||||||
|
itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::InfoRole)].toString();
|
||||||
|
values[TableModel::AmountRole] =
|
||||||
|
itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::AmountRole)].toInt();
|
||||||
|
values[TableModel::FactorRole] =
|
||||||
|
itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::FactorRole)].toDouble();
|
||||||
|
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonParser::JsonParser() {}
|
||||||
|
|
||||||
|
QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QString& objectName) {
|
||||||
|
QJsonDocument doc = QJsonDocument::fromJson(jsonData);
|
||||||
|
QJsonArray itemArray;
|
||||||
|
if (objectName.isEmpty()) {
|
||||||
|
itemArray = doc.array();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
QJsonObject rootObject = doc.object();
|
||||||
|
itemArray = rootObject.value(QString("items")).toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
return itemArray;
|
||||||
|
}
|
||||||
23
formats/jsonparser.h
Normal file
23
formats/jsonparser.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef JSONPARSER_H
|
||||||
|
#define JSONPARSER_H
|
||||||
|
|
||||||
|
#include <QVariant>
|
||||||
|
|
||||||
|
class QJsonObject;
|
||||||
|
class QString;
|
||||||
|
class QByteArray;
|
||||||
|
class QJsonArray;
|
||||||
|
|
||||||
|
class JsonParser {
|
||||||
|
public:
|
||||||
|
static QList<QHash<int, QVariant>> toItemValuesList(const QByteArray& jsonData,
|
||||||
|
const QString& objectName = "");
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit JsonParser();
|
||||||
|
|
||||||
|
static QJsonArray extractItemArray(const QByteArray& jsonData, const QString& objectName);
|
||||||
|
static QHash<int, QVariant> jsonObjectToItemValues(const QJsonObject& itemJsonObject);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // JSONPARSER_H
|
||||||
@ -70,7 +70,7 @@ void GenericCore::triggerApplicationUpdate() {
|
|||||||
QProcess::startDetached(toolFilePath, args);
|
QProcess::startDetached(toolFilePath, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<QAbstractItemModel> GenericCore::getModel() const { return m_mainModel; }
|
std::shared_ptr<TableModel> GenericCore::getModel() const { return m_mainModel; }
|
||||||
|
|
||||||
void GenericCore::setupModels() {
|
void GenericCore::setupModels() {
|
||||||
m_mainModel = make_shared<TableModel>(this);
|
m_mainModel = make_shared<TableModel>(this);
|
||||||
@ -82,7 +82,6 @@ QString GenericCore::getMaintenanceToolFilePath() const {
|
|||||||
|
|
||||||
/// setting the applicationDirPath hard coded to test update feature from IDE
|
/// setting the applicationDirPath hard coded to test update feature from IDE
|
||||||
#ifdef QT_DEBUG
|
#ifdef QT_DEBUG
|
||||||
// REFACTOR retrieve application name automatically instead of using hard coded name
|
|
||||||
applicationDirPath = QString("/opt/%1").arg(APPLICATION_NAME);
|
applicationDirPath = QString("/opt/%1").arg(APPLICATION_NAME);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@ -21,7 +21,7 @@ class GenericCore : public QObject {
|
|||||||
bool isApplicationUpdateAvailable();
|
bool isApplicationUpdateAvailable();
|
||||||
void triggerApplicationUpdate();
|
void triggerApplicationUpdate();
|
||||||
|
|
||||||
std::shared_ptr<QAbstractItemModel> getModel() const;
|
std::shared_ptr<TableModel> getModel() const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void displayStatusMessage(QString message);
|
void displayStatusMessage(QString message);
|
||||||
|
|||||||
@ -5,4 +5,14 @@ ModelItem::ModelItem(const QHash<int, QVariant> values)
|
|||||||
|
|
||||||
QVariant ModelItem::data(int role) const { return m_values.value(role); }
|
QVariant ModelItem::data(int role) const { return m_values.value(role); }
|
||||||
|
|
||||||
bool ModelItem::setData(const QVariant& value, int role) {}
|
bool ModelItem::setData(const QVariant& value, int role) {
|
||||||
|
bool valueChanged = false;
|
||||||
|
if (m_values.contains(role)) {
|
||||||
|
if (m_values.value(role) != value) {
|
||||||
|
valueChanged = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_values[role] = value;
|
||||||
|
|
||||||
|
return valueChanged;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,19 +1,17 @@
|
|||||||
#include "tablemodel.h"
|
#include "tablemodel.h"
|
||||||
|
|
||||||
|
#include "../formats/jsonparser.h"
|
||||||
#include "modelitem.h"
|
#include "modelitem.h"
|
||||||
|
|
||||||
enum UserRoles {
|
QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
|
||||||
NameRole = Qt::UserRole + 1,
|
{DescriptionRole, "Description"},
|
||||||
DescriptionRole,
|
{InfoRole, "Info"},
|
||||||
InfoRole,
|
{AmountRole, "Amount"},
|
||||||
AmountRole,
|
{FactorRole, "Factor"}};
|
||||||
FactorRole,
|
|
||||||
FactoredAmountRole
|
|
||||||
};
|
|
||||||
|
|
||||||
TableModel::TableModel(QObject* parent)
|
TableModel::TableModel(QObject* parent)
|
||||||
: QAbstractTableModel{parent} {
|
: QAbstractTableModel{parent} {
|
||||||
for (int row = 0; row < 23; ++row) {
|
for (int row = 0; row < 5; ++row) {
|
||||||
QHash<int, QVariant> values;
|
QHash<int, QVariant> values;
|
||||||
values[NameRole] = QString("Item %1").arg(row);
|
values[NameRole] = QString("Item %1").arg(row);
|
||||||
values[DescriptionRole] = QString("This is item %1").arg(row);
|
values[DescriptionRole] = QString("This is item %1").arg(row);
|
||||||
@ -27,15 +25,19 @@ TableModel::TableModel(QObject* parent)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
|
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
|
||||||
// return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
|
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
|
||||||
return QAbstractTableModel::flags(index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int TableModel::rowCount(const QModelIndex& parent) const { return m_items.size(); }
|
QHash<int, QByteArray> TableModel::roleNames() const { return ROLE_NAMES; }
|
||||||
|
|
||||||
|
int TableModel::rowCount(const QModelIndex& parent) const {
|
||||||
|
Q_UNUSED(parent);
|
||||||
|
return m_items.size();
|
||||||
|
}
|
||||||
|
|
||||||
int TableModel::columnCount(const QModelIndex& parent) const {
|
int TableModel::columnCount(const QModelIndex& parent) const {
|
||||||
// TODO read from amount of header names (when available)
|
Q_UNUSED(parent);
|
||||||
return 5;
|
return ROLE_NAMES.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant TableModel::data(const QModelIndex& index, int role) const {
|
QVariant TableModel::data(const QModelIndex& index, int role) const {
|
||||||
@ -52,6 +54,7 @@ QVariant TableModel::data(const QModelIndex& index, int role) const {
|
|||||||
int roleForColumn = getRoleForColumn(column);
|
int roleForColumn = getRoleForColumn(column);
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
|
case Qt::EditRole:
|
||||||
return m_items.at(row)->data(roleForColumn);
|
return m_items.at(row)->data(roleForColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +64,9 @@ QVariant TableModel::data(const QModelIndex& index, int role) const {
|
|||||||
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
||||||
if (role == Qt::DisplayRole) {
|
if (role == Qt::DisplayRole) {
|
||||||
if (orientation == Qt::Horizontal) {
|
if (orientation == Qt::Horizontal) {
|
||||||
return QString("Section %1").arg(section);
|
const int columnRole = getRoleForColumn(section);
|
||||||
|
const QString headerName = ROLE_NAMES.value(columnRole);
|
||||||
|
return QString("%1").arg(headerName);
|
||||||
} else {
|
} else {
|
||||||
return QString("%1").arg(section);
|
return QString("%1").arg(section);
|
||||||
}
|
}
|
||||||
@ -74,23 +79,59 @@ bool TableModel::setData(const QModelIndex& index, const QVariant& value, int ro
|
|||||||
if (!checkIndex(index)) {
|
if (!checkIndex(index)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// save value from editor to member m_gridData
|
int columnRole = getRoleForColumn(index.column());
|
||||||
// m_gridData[index.row()][index.column()] = value.toString();
|
shared_ptr<ModelItem> item = m_items.at(index.row());
|
||||||
// // for presentation purposes only: build and emit a joined string
|
return item->setData(value, columnRole);
|
||||||
// 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// bool TableModel::setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) {}
|
// bool TableModel::setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) {}
|
||||||
|
|
||||||
|
bool TableModel::removeRows(int position, int rows, const QModelIndex& parentIndex) {
|
||||||
|
if (parentIndex != QModelIndex()) {
|
||||||
|
qWarning() << "Removing of child rows is not supported yet!";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int endPosition = position + rows;
|
||||||
|
if (position < 0 || endPosition >= m_items.size()) {
|
||||||
|
qWarning() << "Trying to remove rows is out of bounds!";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
beginRemoveRows(QModelIndex(), position, position + rows - 1);
|
||||||
|
m_items.remove(position, rows);
|
||||||
|
endRemoveRows();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TableModel::appendItems(const QByteArray& jsonDoc) { insertItems(-1, jsonDoc, QModelIndex()); }
|
||||||
|
|
||||||
|
void TableModel::insertItems(int startPosition,
|
||||||
|
const QByteArray& jsonDoc,
|
||||||
|
const QModelIndex& parentIndex) {
|
||||||
|
qInfo() << "Inserting item(s) into model...";
|
||||||
|
if (parentIndex != QModelIndex()) {
|
||||||
|
qWarning() << "Using invalid parent index (no child support for now)";
|
||||||
|
}
|
||||||
|
if (startPosition == -1 || startPosition > m_items.size()) {
|
||||||
|
/// Appending item(s)
|
||||||
|
startPosition = m_items.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<QHash<int, QVariant>> valueList = JsonParser::toItemValuesList(jsonDoc);
|
||||||
|
const int nRows = valueList.size();
|
||||||
|
beginInsertRows(QModelIndex(), startPosition, startPosition + nRows - 1);
|
||||||
|
for (int row = 0; row < nRows; ++row) {
|
||||||
|
const int rowPosition = startPosition + row;
|
||||||
|
shared_ptr<ModelItem> item = make_unique<ModelItem>(valueList.at(row));
|
||||||
|
m_items.insert(rowPosition, std::move(item));
|
||||||
|
}
|
||||||
|
endInsertRows();
|
||||||
|
}
|
||||||
|
|
||||||
int TableModel::getRoleForColumn(const int column) const {
|
int TableModel::getRoleForColumn(const int column) const {
|
||||||
switch (column) {
|
switch (column) {
|
||||||
case 0:
|
case 0:
|
||||||
|
|||||||
@ -8,20 +8,33 @@ class ModelItem;
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class TableModel : public QAbstractTableModel {
|
class TableModel : public QAbstractTableModel {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
|
||||||
|
static QHash<int, QByteArray> ROLE_NAMES;
|
||||||
|
|
||||||
explicit TableModel(QObject* parent = nullptr);
|
explicit TableModel(QObject* parent = nullptr);
|
||||||
|
|
||||||
/// QAbstractItemModel interface
|
/// QAbstractItemModel interface
|
||||||
Qt::ItemFlags flags(const QModelIndex& index) const override;
|
Qt::ItemFlags flags(const QModelIndex& index) const override;
|
||||||
|
QHash<int, QByteArray> roleNames() const override;
|
||||||
|
|
||||||
int rowCount(const QModelIndex& parent) const override;
|
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||||
int columnCount(const QModelIndex& parent) const override;
|
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||||
QVariant data(const QModelIndex& index, int role) const override;
|
QVariant data(const QModelIndex& index, int role) const override;
|
||||||
QVariant headerData(int section, Qt::Orientation orientation, 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 setData(const QModelIndex& index, const QVariant& value, int role) override;
|
||||||
// bool setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) 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 position, int rows, const QModelIndex& parentIndex = QModelIndex()) override;
|
||||||
|
void appendItems(const QByteArray& jsonDoc);
|
||||||
|
void insertItems(int startPosition, const QByteArray& jsonDoc, const QModelIndex& parentIndex);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// members
|
/// members
|
||||||
QList<shared_ptr<ModelItem>> m_items;
|
QList<shared_ptr<ModelItem>> m_items;
|
||||||
|
|||||||
Reference in New Issue
Block a user