From 6eb585b08cad1b3a5b4781e4dfd0fdeb033bb117 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sat, 15 Dec 2018 16:12:20 +0100 Subject: [PATCH] Use central table format for printing --- source/CMakeLists.txt | 1 + .../CheckableTestModel/CheckableTestModel.cpp | 8 ++----- .../CheckableTestModel/CheckableTestModel.h | 6 +++-- source/DataModel.h | 24 +------------------ source/MetaData/MetaDataModel.cpp | 4 +--- source/MetaData/MetaDataModel.h | 13 +++++----- source/PrintableModel.cpp | 11 +++++++++ source/PrintableModel.h | 12 ++++++++++ source/ResultWidget/ResultModel.cpp | 4 +--- source/ResultWidget/ResultModel.h | 5 ++-- .../SubTests/LateSkills/LateSkillsModel.cpp | 4 +--- source/SubTests/Plural/PluralModel.cpp | 4 +--- source/SubTests/V2Svk/V2SvkModel.cpp | 12 +++------- source/SubTests/VerbEnd/VerbEndModel.cpp | 4 +--- 14 files changed, 48 insertions(+), 64 deletions(-) create mode 100644 source/PrintableModel.cpp create mode 100644 source/PrintableModel.h diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 0a0f40f..147bf10 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -36,6 +36,7 @@ protobuf_generate_cpp(DataModel_PROTO_SRCS DataModel_PROTO_HDRS add_executable(${PROJECT_NAME} WIN32 ESGRAF48.cpp DataModel.cpp + PrintableModel.cpp mainwindow.cpp ${LOGO_TEST_UI} ${LOGO_TEST_QRC} diff --git a/source/CheckableTestModel/CheckableTestModel.cpp b/source/CheckableTestModel/CheckableTestModel.cpp index 00da00c..953089a 100644 --- a/source/CheckableTestModel/CheckableTestModel.cpp +++ b/source/CheckableTestModel/CheckableTestModel.cpp @@ -134,9 +134,7 @@ void CheckableTestModel::printTo(QTextCursor &cursor) const void CheckableTestModel::printTableTo(QTextCursor &cursor) const { - QTextTableFormat tableFormat; - tableFormat.setCellPadding(2); - tableFormat.setCellSpacing(0); + QTextTableFormat tableFormat = defaultTableFormat(); tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::PercentageLength, 20), QTextLength(QTextLength::PercentageLength, 9), @@ -180,9 +178,7 @@ void CheckableTestModel::printTableTo(QTextCursor &cursor) const void CheckableTestModel::printSummaryTo(QTextCursor &cursor) const { - QTextTableFormat tableFormat; - tableFormat.setCellPadding(2); - tableFormat.setCellSpacing(0); + QTextTableFormat tableFormat = defaultTableFormat(); tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::PercentageLength, 76), QTextLength(QTextLength::PercentageLength, 20), diff --git a/source/CheckableTestModel/CheckableTestModel.h b/source/CheckableTestModel/CheckableTestModel.h index 4389e04..bf58c07 100644 --- a/source/CheckableTestModel/CheckableTestModel.h +++ b/source/CheckableTestModel/CheckableTestModel.h @@ -1,10 +1,12 @@ #pragma once +#include "../PrintableModel.h" #include "CheckableTest.h" + #include #include -class CheckableTestModel : public QAbstractTableModel +class CheckableTestModel : public QAbstractTableModel, protected PrintableModel { Q_OBJECT @@ -28,7 +30,7 @@ public: unsigned int getPoints() const; - virtual void printTo(QTextCursor &cursor) const; + void printTo(QTextCursor &cursor) const override; protected: virtual bool isValidIndex(const QModelIndex &index) const; diff --git a/source/DataModel.h b/source/DataModel.h index a718ef7..ce3a92a 100644 --- a/source/DataModel.h +++ b/source/DataModel.h @@ -36,34 +36,12 @@ public: DataModel(QObject *parent); void printTo(QTextCursor &cursor) const; - - void write(std::ostream &outStream) const; +void write(std::ostream &outStream) const; void read(std::istream &inStream); signals: void modelChanged(); -private: - template - void write( - const ModelType &model, QJsonObject &target, const char *name) const - { - QJsonObject jsonObject; - model.write(jsonObject); - target[name] = jsonObject; - } - - template - void read( - ModelType &model, const QJsonObject &source, const char *name) const - { - const auto &jsonObject = source[name]; - if (jsonObject.isObject()) - { - model.read(jsonObject.toObject()); - } - } - private slots: void pluralModelChanged(); void metaDataChanged(); diff --git a/source/MetaData/MetaDataModel.cpp b/source/MetaData/MetaDataModel.cpp index 7f3769a..4a8403e 100644 --- a/source/MetaData/MetaDataModel.cpp +++ b/source/MetaData/MetaDataModel.cpp @@ -135,10 +135,8 @@ void MetaDataModel::printTo(QTextCursor &cursor) const { cursor.insertBlock(); - QTextTableFormat tableFormat; + QTextTableFormat tableFormat = defaultTableFormat(); tableFormat.setBorderStyle(QTextTableFormat::BorderStyle_None); - tableFormat.setCellPadding(2); - tableFormat.setCellSpacing(0); tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::PercentageLength, 25), QTextLength(QTextLength::PercentageLength, 25), diff --git a/source/MetaData/MetaDataModel.h b/source/MetaData/MetaDataModel.h index a96f75a..c019e1a 100644 --- a/source/MetaData/MetaDataModel.h +++ b/source/MetaData/MetaDataModel.h @@ -1,5 +1,6 @@ #pragma once +#include "../PrintableModel.h" #include "Age.h" #include "MetaDataModel.pb.h" @@ -10,7 +11,7 @@ #include #include -class MetaDataModel : public QAbstractTableModel +class MetaDataModel : public QAbstractTableModel, protected PrintableModel { Q_OBJECT @@ -25,19 +26,17 @@ public: MetaDataModel(QObject *parent); int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; - QVariant data( - const QModelIndex &index, int role = Qt::DisplayRole) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; Qt::ItemFlags flags(const QModelIndex &index) const override; - bool setData(const QModelIndex &index, const QVariant &value, - int role = Qt::EditRole) override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; void read(const ESGRAF48::MetaDataModel &model); void write(ESGRAF48::MetaDataModel &model) const; - void printTo(QTextCursor &cursor) const; + void printTo(QTextCursor &cursor) const override; Age getAge() const { - return { m_dateOfBirth, m_dateOfTest }; + return {m_dateOfBirth, m_dateOfTest}; } }; diff --git a/source/PrintableModel.cpp b/source/PrintableModel.cpp new file mode 100644 index 0000000..7176aba --- /dev/null +++ b/source/PrintableModel.cpp @@ -0,0 +1,11 @@ +#include "PrintableModel.h" + +QTextTableFormat PrintableModel::defaultTableFormat() +{ + QTextTableFormat tableFormat; + tableFormat.setCellPadding(2); + tableFormat.setCellSpacing(0); + + return tableFormat; +} + diff --git a/source/PrintableModel.h b/source/PrintableModel.h new file mode 100644 index 0000000..e3bea6a --- /dev/null +++ b/source/PrintableModel.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +class PrintableModel +{ +public: + virtual void printTo(QTextCursor &cursor) const = 0; + +protected: + static QTextTableFormat defaultTableFormat(); +}; diff --git a/source/ResultWidget/ResultModel.cpp b/source/ResultWidget/ResultModel.cpp index 02fab59..2c9fdc9 100644 --- a/source/ResultWidget/ResultModel.cpp +++ b/source/ResultWidget/ResultModel.cpp @@ -217,9 +217,7 @@ void ResultModel::printTo(QTextCursor &cursor) const cursor.insertBlock(); cursor.insertText("\nProzentränge (PR)"); - QTextTableFormat tableFormat; - tableFormat.setCellPadding(2); - tableFormat.setCellSpacing(0); + QTextTableFormat tableFormat = defaultTableFormat(); const unsigned int columnCount = 10; diff --git a/source/ResultWidget/ResultModel.h b/source/ResultWidget/ResultModel.h index 67d2692..3e58ba3 100644 --- a/source/ResultWidget/ResultModel.h +++ b/source/ResultWidget/ResultModel.h @@ -1,12 +1,13 @@ #pragma once +#include "../PrintableModel.h" #include "Age.h" #include "TestResult.h" #include #include -class ResultModel : public QAbstractTableModel +class ResultModel : public QAbstractTableModel, protected PrintableModel { Q_OBJECT @@ -36,5 +37,5 @@ public: void setPassivResult(unsigned int points); void setGenitivResult(unsigned int points); - void printTo(QTextCursor &cursor) const; + void printTo(QTextCursor &cursor) const override; }; diff --git a/source/SubTests/LateSkills/LateSkillsModel.cpp b/source/SubTests/LateSkills/LateSkillsModel.cpp index 74a824f..95ab46b 100644 --- a/source/SubTests/LateSkills/LateSkillsModel.cpp +++ b/source/SubTests/LateSkills/LateSkillsModel.cpp @@ -11,9 +11,7 @@ LateSkillsModel::LateSkillsModel(QObject *parent) void LateSkillsModel::printTableTo(QTextCursor &cursor) const { - QTextTableFormat tableFormat; - tableFormat.setCellPadding(2); - tableFormat.setCellSpacing(0); + QTextTableFormat tableFormat = defaultTableFormat(); tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::PercentageLength, 20), QTextLength(QTextLength::PercentageLength, 5), diff --git a/source/SubTests/Plural/PluralModel.cpp b/source/SubTests/Plural/PluralModel.cpp index 1bd733c..f3ccbd9 100644 --- a/source/SubTests/Plural/PluralModel.cpp +++ b/source/SubTests/Plural/PluralModel.cpp @@ -63,9 +63,7 @@ std::string PluralModel::getName() const void PluralModel::printTableTo(QTextCursor &cursor) const { - QTextTableFormat tableFormat; - tableFormat.setCellPadding(2); - tableFormat.setCellSpacing(0); + QTextTableFormat tableFormat = defaultTableFormat(); tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::PercentageLength, 10), QTextLength(QTextLength::PercentageLength, 10), diff --git a/source/SubTests/V2Svk/V2SvkModel.cpp b/source/SubTests/V2Svk/V2SvkModel.cpp index 2e64ef0..30b143c 100644 --- a/source/SubTests/V2Svk/V2SvkModel.cpp +++ b/source/SubTests/V2Svk/V2SvkModel.cpp @@ -209,9 +209,7 @@ std::string V2SvkModel::getName() const void V2SvkModel::printTableTo(QTextCursor &cursor) const { - QTextTableFormat tableFormat12; - tableFormat12.setCellPadding(2); - tableFormat12.setCellSpacing(0); + QTextTableFormat tableFormat12 = defaultTableFormat(); tableFormat12.setColumnWidthConstraints({QTextLength(QTextLength::PercentageLength, 20), QTextLength(QTextLength::PercentageLength, 5), @@ -231,9 +229,7 @@ void V2SvkModel::printTableTo(QTextCursor &cursor) const QTextLength(QTextLength::PercentageLength, 1), QTextLength(QTextLength::PercentageLength, 3)}); - QTextTableFormat tableFormat6; - tableFormat6.setCellPadding(2); - tableFormat6.setCellSpacing(0); + QTextTableFormat tableFormat6 = defaultTableFormat(); tableFormat6.setColumnWidthConstraints({QTextLength(QTextLength::PercentageLength, 20), QTextLength(QTextLength::PercentageLength, 10), @@ -329,9 +325,7 @@ void V2SvkModel::printTableTo(QTextCursor &cursor) const void V2SvkModel::printSummaryTo(QTextCursor &cursor) const { - QTextTableFormat tableFormat; - tableFormat.setCellPadding(2); - tableFormat.setCellSpacing(0); + QTextTableFormat tableFormat = defaultTableFormat(); tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::PercentageLength, 72), QTextLength(QTextLength::PercentageLength, 20), diff --git a/source/SubTests/VerbEnd/VerbEndModel.cpp b/source/SubTests/VerbEnd/VerbEndModel.cpp index 6fdd889..9061d23 100644 --- a/source/SubTests/VerbEnd/VerbEndModel.cpp +++ b/source/SubTests/VerbEnd/VerbEndModel.cpp @@ -131,9 +131,7 @@ std::string VerbEndModel::getName() const void VerbEndModel::printSummaryTo(QTextCursor &cursor) const { - QTextTableFormat tableFormat; - tableFormat.setCellPadding(2); - tableFormat.setCellSpacing(0); + QTextTableFormat tableFormat = defaultTableFormat(); tableFormat.setColumnWidthConstraints({QTextLength(QTextLength::PercentageLength, 46), QTextLength(QTextLength::PercentageLength, 25),