Compare commits

...

12 Commits

11 changed files with 390 additions and 14 deletions

View File

@ -1,6 +1,7 @@
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_AUTOMOC ON)
@ -8,20 +9,28 @@ set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
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)
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.h
${TS_FILES}
constants.h
data/settingshandler.h data/settingshandler.cpp
model/tablemodel.h model/tablemodel.cpp
model/modelitem.h model/modelitem.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)
qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})

2
CoreConfig.h.in Normal file
View File

@ -0,0 +1,2 @@
#define CORE_NAME "${PROJECT_NAME}"
#define CORE_VERSION "${PROJECT_VERSION}"

15
constants.h Normal file
View 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
View 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
View 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

View File

@ -1,3 +1,94 @@
#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<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();
/// setting the applicationDirPath hard coded to test update feature from IDE
#ifdef QT_DEBUG
// REFACTOR retrieve application name automatically instead of using hard coded name
applicationDirPath = QString("/opt/%1").arg(APPLICATION_NAME);
#endif
#ifdef Q_OS_MAC
applicationDirPath.append("/../../..");
#endif
const QString filePath = applicationDirPath + "/" + UPDATER_EXE;
return filePath;
}

View File

@ -1,10 +1,37 @@
#ifndef GENERICCORE_H
#define GENERICCORE_H
class GenericCore
{
#include <QObject>
class QAbstractItemModel;
class QString;
class TableModel;
class GenericCore : public QObject {
Q_OBJECT
public:
GenericCore();
~GenericCore();
QString toString() const;
void sayHello() const;
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;
};
#endif // GENERICCORE_H

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