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${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core LinguistTools Gui)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Test)
configure_file(CoreConfig.h.in CoreConfig.h)
@ -36,6 +37,8 @@ add_library(${TARGET_APP} STATIC
model/generalsortfiltermodel.h model/generalsortfiltermodel.cpp
)
target_link_libraries(GenericCore PRIVATE Qt${QT_VERSION_MAJOR}::Test)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
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 <QAbstractItemModelTester>
#include <QCoreApplication>
#include <QDateTime>
#include <QDebug>
@ -135,7 +136,19 @@ void GenericCore::setupModels() {
m_mainModel = make_shared<TableModel>(m_modelUndoStack, this);
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();
}

View File

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

View File

@ -7,7 +7,15 @@
#include <QString>
/// 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 QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, AmountRole,
FactorRole};

View File

@ -44,6 +44,20 @@ bool ModelItem::setItemData(const QMap<int, QVariant>& changedValues) {
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 itemObject;
// 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
bool setItemData(const QMap<int, QVariant>& changedValues);
// QString toString() const;
QString toString() const;
QJsonObject toJsonObject() const;
private:

View File

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