From ef54685bce7ee9cf7311a9c15b48c1935d0d238b Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sun, 18 Nov 2018 15:27:00 +0100 Subject: [PATCH] Ported GenusModel from json to protobuf --- proto/DataModel.proto | 2 + proto/GenusModel.proto | 42 ++++++++++++ source/CMakeLists.txt | 1 + source/DataModel.cpp | 4 +- source/SubTests/Genus/CMakeLists.txt | 12 ++++ source/SubTests/Genus/GenusModel.cpp | 96 ++++++++++++++++++++++++++-- source/SubTests/Genus/GenusModel.h | 4 ++ 7 files changed, 153 insertions(+), 8 deletions(-) create mode 100644 proto/GenusModel.proto diff --git a/proto/DataModel.proto b/proto/DataModel.proto index a0b0128..c5df198 100644 --- a/proto/DataModel.proto +++ b/proto/DataModel.proto @@ -5,10 +5,12 @@ package ESGRAF48; import "MetaDataModel.proto"; import "V2SvkModel.proto"; import "VerbEndModel.proto"; +import "GenusModel.proto"; message DataModel { MetaDataModel MetaData = 1; V2SvkModel V2Svk = 2; VerbEndModel VerbEnd = 3; + GenusModel Genus = 4; } diff --git a/proto/GenusModel.proto b/proto/GenusModel.proto new file mode 100644 index 0000000..26c24cf --- /dev/null +++ b/proto/GenusModel.proto @@ -0,0 +1,42 @@ +syntax = "proto3"; + +package ESGRAF48; + +message GenusModel +{ + message TiereModel + { + bool Tiger = 1; + bool Baer = 2; + bool Katze = 3; + bool Pferd = 4; + bool Gans = 5; + bool Elefant = 6; + bool Affe = 7; + bool Hund = 8; + } + + message FutterModel + { + bool Salat = 1; + bool Fleisch = 2; + bool Knochen = 3; + bool Banane = 4; + bool Apfel = 5; + bool Karotte = 6; + bool Honig = 7; + bool Zucker = 8; + } + + message ZirkusModel + { + bool Kiste = 1; + bool Holz = 2; + bool Vorhang = 3; + bool Baum = 4; + } + + TiereModel Tiere = 1; + FutterModel Futter = 2; + ZirkusModel Zirkus = 3; +} diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 61e5d50..33df521 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -22,6 +22,7 @@ set(DataModel_PROTO_FILES ../proto/MetaDataModel.proto ../proto/V2SvkModel.proto ../proto/VerbEndModel.proto + ../proto/GenusModel.proto ) protobuf_generate_cpp(DataModel_PROTO_SRCS DataModel_PROTO_HDRS diff --git a/source/DataModel.cpp b/source/DataModel.cpp index d0119e7..7c346e4 100644 --- a/source/DataModel.cpp +++ b/source/DataModel.cpp @@ -1,7 +1,5 @@ #include "DataModel.h" - #include "DataModel.pb.h" -#include #include @@ -36,6 +34,7 @@ void DataModel::writeProtoBuf(std::ostream &outStream) const m_metaData.writeProtoBuf(*dataModel.mutable_metadata()); m_v2Svk.writeProtoBuf(*dataModel.mutable_v2svk()); m_verbEnd.writeProtoBuf(*dataModel.mutable_verbend()); + m_genus.writeProtoBuf(*dataModel.mutable_genus()); dataModel.SerializeToOstream(&outStream); } @@ -48,6 +47,7 @@ void DataModel::readProtoBuf(std::istream &inStream) m_metaData.readProtoBuf(dataModel.metadata()); m_v2Svk.readProtoBuf(dataModel.v2svk()); m_verbEnd.readProtoBuf(dataModel.verbend()); + m_genus.readProtoBuf(dataModel.genus()); } std::string DataModel::toHtml() const diff --git a/source/SubTests/Genus/CMakeLists.txt b/source/SubTests/Genus/CMakeLists.txt index 96bad12..28fe2ef 100644 --- a/source/SubTests/Genus/CMakeLists.txt +++ b/source/SubTests/Genus/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.6) project(Genus LANGUAGES CXX) find_package(Qt5Widgets REQUIRED) +find_package(Protobuf REQUIRED) set(CMAKE_AUTOMOC ON) @@ -10,10 +11,19 @@ qt5_wrap_ui(GENUS_UI GenusWidget.ui ) + +set(Genus_PROTO_FILES + ../../../proto/GenusModel.proto +) + +protobuf_generate_cpp(Genus_PROTO_SRCS Genus_PROTO_HDRS ${Genus_PROTO_FILES}) + add_library(${PROJECT_NAME} GenusWidget.cpp GenusModel.cpp ${GENUS_UI} + ${Genus_PROTO_SRCS} + ${Genus_PROTO_HDRS} ) set_target_properties(${PROJECT_NAME} @@ -25,6 +35,7 @@ target_include_directories(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} + ${Protobuf_INCLUDE_DIRS} ) target_link_libraries(${PROJECT_NAME} @@ -33,4 +44,5 @@ target_link_libraries(${PROJECT_NAME} CheckableTest CheckableTestModel Qt5::Widgets + ${Protobuf_LIBRARIES} ) diff --git a/source/SubTests/Genus/GenusModel.cpp b/source/SubTests/Genus/GenusModel.cpp index 797e5a1..0977ce9 100644 --- a/source/SubTests/Genus/GenusModel.cpp +++ b/source/SubTests/Genus/GenusModel.cpp @@ -1,11 +1,95 @@ #include "GenusModel.h" GenusModel::GenusModel(QObject *parent) - : CheckableTestModel(parent) + : CheckableTestModel(parent) { - m_tests = { { "Tiere", { "Tiger", "Bär", "Katze", "Pferd", "Gans", - "Elefant", "Affe", "Hund" } }, - { "Futter", { "Salat", "Fleisch", "Knochen", "Banane", "Apfel", "Karotte", - "Honig", "Zucker" } }, - { "Zirkus", { "Kiste", "Holz", "Vorhang", "Baum" } } }; + m_tests = {{"Tiere", {"Tiger", "Bär", "Katze", "Pferd", "Gans", "Elefant", "Affe", "Hund"}}, + {"Futter", + {"Salat", "Fleisch", "Knochen", "Banane", "Apfel", "Karotte", "Honig", "Zucker"}}, + {"Zirkus", {"Kiste", "Holz", "Vorhang", "Baum"}}}; +} + +void GenusModel::readProtoBuf(const ESGRAF48::GenusModel &model) +{ + const auto &tiereModel = model.tiere(); + { + auto &testItems = m_tests.at(0).items(); + + testItems[0].setState(tiereModel.tiger()); + testItems[1].setState(tiereModel.baer()); + testItems[2].setState(tiereModel.katze()); + testItems[3].setState(tiereModel.pferd()); + testItems[4].setState(tiereModel.gans()); + testItems[5].setState(tiereModel.elefant()); + testItems[6].setState(tiereModel.affe()); + testItems[7].setState(tiereModel.hund()); + } + + const auto &futterModel = model.futter(); + { + auto &testItems = m_tests.at(1).items(); + + testItems[0].setState(futterModel.salat()); + testItems[1].setState(futterModel.fleisch()); + testItems[2].setState(futterModel.knochen()); + testItems[3].setState(futterModel.banane()); + testItems[4].setState(futterModel.apfel()); + testItems[5].setState(futterModel.karotte()); + testItems[6].setState(futterModel.honig()); + testItems[7].setState(futterModel.zucker()); + } + + const auto &zirkusModel = model.zirkus(); + { + auto &testItems = m_tests.at(2).items(); + + testItems[0].setState(zirkusModel.kiste()); + testItems[1].setState(zirkusModel.holz()); + testItems[2].setState(zirkusModel.vorhang()); + testItems[3].setState(zirkusModel.baum()); + } +} + +void GenusModel::writeProtoBuf(ESGRAF48::GenusModel &model) const +{ + auto *tiereModel = model.mutable_tiere(); + if (tiereModel != nullptr) + { + const auto &testItems = m_tests.at(0).items(); + + tiereModel->set_tiger(testItems[0].isChecked()); + tiereModel->set_baer(testItems[1].isChecked()); + tiereModel->set_katze(testItems[2].isChecked()); + tiereModel->set_pferd(testItems[3].isChecked()); + tiereModel->set_gans(testItems[4].isChecked()); + tiereModel->set_elefant(testItems[5].isChecked()); + tiereModel->set_affe(testItems[6].isChecked()); + tiereModel->set_hund(testItems[7].isChecked()); + } + + auto *futterModel = model.mutable_futter(); + if (futterModel != nullptr) + { + const auto &testItems = m_tests.at(1).items(); + + futterModel->set_salat(testItems[0].isChecked()); + futterModel->set_fleisch(testItems[1].isChecked()); + futterModel->set_knochen(testItems[2].isChecked()); + futterModel->set_banane(testItems[3].isChecked()); + futterModel->set_apfel(testItems[4].isChecked()); + futterModel->set_karotte(testItems[5].isChecked()); + futterModel->set_honig(testItems[6].isChecked()); + futterModel->set_zucker(testItems[7].isChecked()); + } + + auto *zirkusModel = model.mutable_zirkus(); + if (zirkusModel != nullptr) + { + const auto &testItems = m_tests.at(2).items(); + + zirkusModel->set_kiste(testItems[0].isChecked()); + zirkusModel->set_holz(testItems[1].isChecked()); + zirkusModel->set_vorhang(testItems[2].isChecked()); + zirkusModel->set_baum(testItems[3].isChecked()); + } } diff --git a/source/SubTests/Genus/GenusModel.h b/source/SubTests/Genus/GenusModel.h index 24f297a..3dd8dc9 100644 --- a/source/SubTests/Genus/GenusModel.h +++ b/source/SubTests/Genus/GenusModel.h @@ -1,6 +1,7 @@ #pragma once #include "CheckableTestModel.h" +#include "GenusModel.pb.h" class GenusModel : public CheckableTestModel { @@ -8,4 +9,7 @@ class GenusModel : public CheckableTestModel public: GenusModel(QObject *parent); + + void readProtoBuf(const ESGRAF48::GenusModel &model); + void writeProtoBuf(ESGRAF48::GenusModel &model) const; };