Compare commits
19 Commits
main
...
169d8f9f1e
| Author | SHA1 | Date | |
|---|---|---|---|
| 169d8f9f1e | |||
| d45b1098f9 | |||
| 4c906099eb | |||
| 144460b5aa | |||
| a07e03d80d | |||
| 40e1dd3002 | |||
| 16c524f7a2 | |||
| 0dba9639e6 | |||
| 6a3725bde7 | |||
| 301d143b37 | |||
| 13b53683a5 | |||
| 7ba0304232 | |||
| d943f2d89a | |||
| bddb6df3ee | |||
| 7ec346b5a5 | |||
| b631f4200e | |||
| 0bbac662f4 | |||
| b3718c211f | |||
| fd32b85554 |
@ -1,6 +1,7 @@
|
|||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
project(GenericCore LANGUAGES CXX)
|
set(TARGET_APP "GenericCore")
|
||||||
|
project(${TARGET_APP} VERSION 0.1.0 LANGUAGES CXX)
|
||||||
|
|
||||||
set(CMAKE_AUTOUIC ON)
|
set(CMAKE_AUTOUIC ON)
|
||||||
set(CMAKE_AUTOMOC ON)
|
set(CMAKE_AUTOMOC ON)
|
||||||
@ -8,20 +9,29 @@ set(CMAKE_AUTORCC ON)
|
|||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core LinguistTools)
|
find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core LinguistTools)
|
||||||
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core LinguistTools)
|
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core LinguistTools)
|
||||||
|
|
||||||
set(TS_FILES GenericCore_en_US.ts)
|
configure_file(CoreConfig.h.in CoreConfig.h)
|
||||||
|
|
||||||
add_library(GenericCore STATIC
|
set(TS_FILES ${TARGET_APP}_en_US.ts)
|
||||||
|
|
||||||
|
add_library(${TARGET_APP} STATIC
|
||||||
genericcore.cpp
|
genericcore.cpp
|
||||||
genericcore.h
|
genericcore.h
|
||||||
${TS_FILES}
|
${TS_FILES}
|
||||||
|
constants.h
|
||||||
|
data/settingshandler.h data/settingshandler.cpp
|
||||||
|
model/tablemodel.h model/tablemodel.cpp
|
||||||
|
model/modelitem.h model/modelitem.cpp
|
||||||
|
formats/jsonparser.h formats/jsonparser.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(GenericCore PRIVATE Qt${QT_VERSION_MAJOR}::Core)
|
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
|
||||||
target_compile_definitions(GenericCore PRIVATE GENERICCORE_LIBRARY)
|
target_link_libraries(${TARGET_APP} PRIVATE Qt${QT_VERSION_MAJOR}::Core)
|
||||||
|
|
||||||
|
target_compile_definitions(${TARGET_APP} PRIVATE ${TARGET_APP}_LIBRARY)
|
||||||
|
|
||||||
if(COMMAND qt_create_translation)
|
if(COMMAND qt_create_translation)
|
||||||
qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
|
qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
|
||||||
|
|||||||
2
CoreConfig.h.in
Normal file
2
CoreConfig.h.in
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#define CORE_NAME "${PROJECT_NAME}"
|
||||||
|
#define CORE_VERSION "${PROJECT_VERSION}"
|
||||||
15
constants.h
Normal file
15
constants.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef CONSTANTS_H
|
||||||
|
#define CONSTANTS_H
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
/// Platform dependent
|
||||||
|
#ifdef Q_OS_LINUX
|
||||||
|
static const QString UPDATER_EXE = "maintenancetool";
|
||||||
|
#elif defined(Q_OS_MAC)
|
||||||
|
static const QString UPDATER_EXE = "maintenancetool.app/Contents/MacOS/maintenancetool";
|
||||||
|
#elif defined(Q_OS_WIN)
|
||||||
|
static const QString UPDATER_EXE = "maintenancetool.exe";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // CONSTANTS_H
|
||||||
43
data/settingshandler.cpp
Normal file
43
data/settingshandler.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#include "settingshandler.h"
|
||||||
|
|
||||||
|
#include <QSettings>
|
||||||
|
|
||||||
|
QVariantMap SettingsHandler::getSettings(QString group) {
|
||||||
|
QSettings settings;
|
||||||
|
QVariantMap result;
|
||||||
|
|
||||||
|
if (!group.isEmpty()) {
|
||||||
|
settings.beginGroup(group);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (QString key, settings.allKeys()) {
|
||||||
|
result.insert(key, settings.value(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!group.isEmpty()) {
|
||||||
|
settings.endGroup();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SettingsHandler::saveSettings(QVariantMap settingMap, QString group) {
|
||||||
|
qInfo() << "saving settings...";
|
||||||
|
|
||||||
|
QSettings settings;
|
||||||
|
if (!group.isEmpty()) {
|
||||||
|
settings.beginGroup(group);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (QString key, settingMap.keys()) {
|
||||||
|
// qDebug() << "saving:" << key << "-" << settingMap.value(key);
|
||||||
|
settings.setValue(key, settingMap.value(key));
|
||||||
|
}
|
||||||
|
if (!group.isEmpty()) {
|
||||||
|
settings.endGroup();
|
||||||
|
}
|
||||||
|
|
||||||
|
settings.sync();
|
||||||
|
}
|
||||||
|
|
||||||
|
SettingsHandler::SettingsHandler() {}
|
||||||
15
data/settingshandler.h
Normal file
15
data/settingshandler.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef SETTINGSHANDLER_H
|
||||||
|
#define SETTINGSHANDLER_H
|
||||||
|
|
||||||
|
#include <QVariantMap>
|
||||||
|
|
||||||
|
class SettingsHandler {
|
||||||
|
public:
|
||||||
|
static QVariantMap getSettings(QString group = "");
|
||||||
|
static void saveSettings(QVariantMap settingMap, QString group = "");
|
||||||
|
|
||||||
|
private:
|
||||||
|
SettingsHandler();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SETTINGSHANDLER_H
|
||||||
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
|
||||||
@ -1,3 +1,93 @@
|
|||||||
#include "genericcore.h"
|
#include "genericcore.h"
|
||||||
|
|
||||||
GenericCore::GenericCore() {}
|
#include <QCoreApplication>
|
||||||
|
#include <QDateTime>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QProcess>
|
||||||
|
#include <QSettings>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
#include "../../ApplicationConfig.h"
|
||||||
|
#include "CoreConfig.h"
|
||||||
|
#include "constants.h"
|
||||||
|
#include "model/tablemodel.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
GenericCore::GenericCore() {
|
||||||
|
qDebug() << "Creating core...";
|
||||||
|
|
||||||
|
setupModels();
|
||||||
|
}
|
||||||
|
|
||||||
|
GenericCore::~GenericCore() { qDebug() << "Destroying core..."; }
|
||||||
|
|
||||||
|
QString GenericCore::toString() const {
|
||||||
|
return QString("%1 (Version %2)").arg(CORE_NAME).arg(CORE_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GenericCore::sayHello() const { qDebug() << "Hello from the core!"; }
|
||||||
|
|
||||||
|
bool GenericCore::isApplicationUpdateAvailable() {
|
||||||
|
QProcess process;
|
||||||
|
const QString programmString = getMaintenanceToolFilePath();
|
||||||
|
const QStringList checkArgs("--checkupdates");
|
||||||
|
process.start(programmString, checkArgs);
|
||||||
|
process.waitForFinished();
|
||||||
|
|
||||||
|
const int exitCode = process.exitCode();
|
||||||
|
if (process.error() != QProcess::UnknownError) {
|
||||||
|
qDebug() << "Error checking for updates";
|
||||||
|
emit displayStatusMessage("Error checking for updates");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray data = process.readAllStandardOutput();
|
||||||
|
|
||||||
|
QSettings settings;
|
||||||
|
settings.beginGroup("Application");
|
||||||
|
settings.setValue("lastCheckForUpdate", QDateTime::currentDateTimeUtc());
|
||||||
|
settings.endGroup();
|
||||||
|
settings.sync();
|
||||||
|
|
||||||
|
if (data.isEmpty() || data.contains("currently no updates")) {
|
||||||
|
qInfo() << "No updates available";
|
||||||
|
emit displayStatusMessage("No updates available.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GenericCore::triggerApplicationUpdate() {
|
||||||
|
// TODO include cleaness of undo stack
|
||||||
|
// if (!m_undoStack->isClean()) {
|
||||||
|
// saveItems();
|
||||||
|
// }
|
||||||
|
// QStringList args("update componentA componentB");
|
||||||
|
QStringList args("--start-updater");
|
||||||
|
QString toolFilePath = getMaintenanceToolFilePath();
|
||||||
|
QProcess::startDetached(toolFilePath, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<TableModel> 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();
|
||||||
|
|
||||||
|
/// setting the applicationDirPath hard coded to test update feature from IDE
|
||||||
|
#ifdef QT_DEBUG
|
||||||
|
applicationDirPath = QString("/opt/%1").arg(APPLICATION_NAME);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef Q_OS_MAC
|
||||||
|
applicationDirPath.append("/../../..");
|
||||||
|
#endif
|
||||||
|
const QString filePath = applicationDirPath + "/" + UPDATER_EXE;
|
||||||
|
return filePath;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,10 +1,37 @@
|
|||||||
#ifndef GENERICCORE_H
|
#ifndef GENERICCORE_H
|
||||||
#define GENERICCORE_H
|
#define GENERICCORE_H
|
||||||
|
|
||||||
class GenericCore
|
#include <QObject>
|
||||||
{
|
|
||||||
public:
|
class QAbstractItemModel;
|
||||||
|
class QString;
|
||||||
|
|
||||||
|
class TableModel;
|
||||||
|
|
||||||
|
class GenericCore : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
GenericCore();
|
GenericCore();
|
||||||
|
~GenericCore();
|
||||||
|
|
||||||
|
QString toString() const;
|
||||||
|
void sayHello() const;
|
||||||
|
|
||||||
|
bool isApplicationUpdateAvailable();
|
||||||
|
void triggerApplicationUpdate();
|
||||||
|
|
||||||
|
std::shared_ptr<TableModel> getModel() const;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void displayStatusMessage(QString message);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<TableModel> m_mainModel;
|
||||||
|
|
||||||
|
void setupModels();
|
||||||
|
|
||||||
|
QString getMaintenanceToolFilePath() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // GENERICCORE_H
|
#endif // GENERICCORE_H
|
||||||
|
|||||||
18
model/modelitem.cpp
Normal file
18
model/modelitem.cpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#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) {
|
||||||
|
bool valueChanged = false;
|
||||||
|
if (m_values.contains(role)) {
|
||||||
|
if (m_values.value(role) != value) {
|
||||||
|
valueChanged = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_values[role] = value;
|
||||||
|
|
||||||
|
return valueChanged;
|
||||||
|
}
|
||||||
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
|
||||||
156
model/tablemodel.cpp
Normal file
156
model/tablemodel.cpp
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
#include "tablemodel.h"
|
||||||
|
|
||||||
|
#include "../formats/jsonparser.h"
|
||||||
|
#include "modelitem.h"
|
||||||
|
|
||||||
|
QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
|
||||||
|
{DescriptionRole, "Description"},
|
||||||
|
{InfoRole, "Info"},
|
||||||
|
{AmountRole, "Amount"},
|
||||||
|
{FactorRole, "Factor"}};
|
||||||
|
|
||||||
|
TableModel::TableModel(QObject* parent)
|
||||||
|
: QAbstractTableModel{parent} {
|
||||||
|
for (int row = 0; row < 5; ++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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
Q_UNUSED(parent);
|
||||||
|
return ROLE_NAMES.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
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:
|
||||||
|
case Qt::EditRole:
|
||||||
|
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) {
|
||||||
|
const int columnRole = getRoleForColumn(section);
|
||||||
|
const QString headerName = ROLE_NAMES.value(columnRole);
|
||||||
|
return QString("%1").arg(headerName);
|
||||||
|
} 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;
|
||||||
|
}
|
||||||
|
int columnRole = getRoleForColumn(index.column());
|
||||||
|
shared_ptr<ModelItem> item = m_items.at(index.row());
|
||||||
|
return item->setData(value, columnRole);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
46
model/tablemodel.h
Normal file
46
model/tablemodel.h
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#ifndef TABLEMODEL_H
|
||||||
|
#define TABLEMODEL_H
|
||||||
|
|
||||||
|
#include <QAbstractTableModel>
|
||||||
|
|
||||||
|
class ModelItem;
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class TableModel : public QAbstractTableModel {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
|
||||||
|
static QHash<int, QByteArray> ROLE_NAMES;
|
||||||
|
|
||||||
|
explicit TableModel(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;
|
||||||
|
|
||||||
|
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:
|
||||||
|
/// members
|
||||||
|
QList<shared_ptr<ModelItem>> m_items;
|
||||||
|
|
||||||
|
/// functions
|
||||||
|
int getRoleForColumn(const int column) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // TABLEMODEL_H
|
||||||
Reference in New Issue
Block a user