Compare commits

...

2 Commits

7 changed files with 55 additions and 5 deletions

View File

@ -11,6 +11,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core LinguistTools) find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core LinguistTools)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core LinguistTools Gui) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core LinguistTools Gui)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Test)
configure_file(CoreConfig.h.in CoreConfig.h) configure_file(CoreConfig.h.in CoreConfig.h)
@ -36,6 +37,8 @@ add_library(${TARGET_APP} STATIC
model/generalsortfiltermodel.h model/generalsortfiltermodel.cpp model/generalsortfiltermodel.h model/generalsortfiltermodel.cpp
) )
target_link_libraries(GenericCore PRIVATE Qt${QT_VERSION_MAJOR}::Test)
include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(${TARGET_APP} PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Gui) target_link_libraries(${TARGET_APP} PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Gui)

View File

@ -1,5 +1,6 @@
#include "genericcore.h" #include "genericcore.h"
#include <QAbstractItemModelTester>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDateTime> #include <QDateTime>
#include <QDebug> #include <QDebug>
@ -135,7 +136,19 @@ void GenericCore::setupModels() {
m_mainModel = make_shared<TableModel>(m_modelUndoStack, this); m_mainModel = make_shared<TableModel>(m_modelUndoStack, this);
m_sortFilterModel = make_shared<GeneralSortFilterModel>(m_mainModel); m_sortFilterModel = make_shared<GeneralSortFilterModel>(m_mainModel);
// TODO add QAbstractItemModelTester /// QAbstractItemModelTester
#ifdef QT_DEBUG
m_mainModelTester = make_unique<QAbstractItemModelTester>(
m_mainModel.get(), QAbstractItemModelTester::FailureReportingMode::Fatal);
m_proxyModelTester = make_unique<QAbstractItemModelTester>(
m_sortFilterModel.get(), QAbstractItemModelTester::FailureReportingMode::Fatal);
#else
m_mainModelTester = make_unique<QAbstractItemModelTester>(
m_mainModel.get(), QAbstractItemModelTester::FailureReportingMode::Warning);
m_modelTester = make_unique<QAbstractItemModelTester>(
m_sortFilterModel.get(), QAbstractItemModelTester::FailureReportingMode::Warning);
#endif
initModelData(); initModelData();
} }

View File

@ -5,6 +5,7 @@
class QUndoStack; class QUndoStack;
class QAbstractItemModel; class QAbstractItemModel;
class QAbstractItemModelTester;
class QString; class QString;
class TableModel; class TableModel;
@ -38,6 +39,8 @@ class GenericCore : public QObject {
QUndoStack* m_modelUndoStack; QUndoStack* m_modelUndoStack;
std::shared_ptr<TableModel> m_mainModel; std::shared_ptr<TableModel> m_mainModel;
std::shared_ptr<GeneralSortFilterModel> m_sortFilterModel; std::shared_ptr<GeneralSortFilterModel> m_sortFilterModel;
std::unique_ptr<QAbstractItemModelTester> m_mainModelTester;
std::unique_ptr<QAbstractItemModelTester> m_proxyModelTester;
void setupModels(); void setupModels();
void initModelData(); void initModelData();

View File

@ -7,7 +7,15 @@
#include <QString> #include <QString>
/// model data /// model data
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole }; enum UserRoles {
NameRole = Qt::UserRole + 1,
DescriptionRole,
InfoRole,
AmountRole,
FactorRole,
/// helper roles
ToStringRole
};
static UserRoles DEFAULT_ROLE = NameRole; static UserRoles DEFAULT_ROLE = NameRole;
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, AmountRole, static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, AmountRole,
FactorRole}; FactorRole};

View File

@ -44,6 +44,20 @@ bool ModelItem::setItemData(const QMap<int, QVariant>& changedValues) {
return valueChanged; return valueChanged;
} }
QString ModelItem::toString() const {
QString result;
QListIterator<UserRoles> i(USER_FACING_ROLES);
while (i.hasNext()) {
const UserRoles role = i.next();
const QString roleName = ROLE_NAMES.value(role);
const QVariant value = data(role);
// result.append(value.toString());
result.append(QString("%1: %2\n").arg(roleName, data(role).toString()));
}
return result;
}
QJsonObject ModelItem::toJsonObject() const { QJsonObject ModelItem::toJsonObject() const {
QJsonObject itemObject; QJsonObject itemObject;
// TODO add UUID and dates (entry, modification, end) // TODO add UUID and dates (entry, modification, end)

View File

@ -14,7 +14,7 @@ class ModelItem {
// TODO change return value to list of changed roles // TODO change return value to list of changed roles
bool setItemData(const QMap<int, QVariant>& changedValues); bool setItemData(const QMap<int, QVariant>& changedValues);
// QString toString() const; QString toString() const;
QJsonObject toJsonObject() const; QJsonObject toJsonObject() const;
private: private:

View File

@ -39,18 +39,25 @@ TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
, m_undoStack(undoStack) {} , m_undoStack(undoStack) {}
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const { Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
if (!index.isValid()) {
return QAbstractTableModel::flags(index);
}
return Qt::ItemIsEditable | QAbstractTableModel::flags(index); return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
} }
QHash<int, QByteArray> TableModel::roleNames() const { return ROLE_NAMES; } QHash<int, QByteArray> TableModel::roleNames() const { return ROLE_NAMES; }
int TableModel::rowCount(const QModelIndex& parent) const { int TableModel::rowCount(const QModelIndex& parent) const {
Q_UNUSED(parent); if (parent.isValid()) {
return 0; // no children
}
return m_items.size(); return m_items.size();
} }
int TableModel::columnCount(const QModelIndex& parent) const { int TableModel::columnCount(const QModelIndex& parent) const {
Q_UNUSED(parent); if (parent.isValid()) {
return 0; // no children
}
return ROLE_NAMES.size(); return ROLE_NAMES.size();
} }
@ -76,6 +83,8 @@ QVariant TableModel::data(const QModelIndex& index, int role) const {
case AmountRole: case AmountRole:
case FactorRole: case FactorRole:
return m_items.at(row)->data(role); return m_items.at(row)->data(role);
case ToStringRole:
return m_items.at(row)->toString();
} }
return QVariant(); return QVariant();