From 221368b3ca6958bb93a855f7a045f62d777ac098 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 14 Nov 2018 00:17:29 +0100 Subject: [PATCH] Read and write some protobuf data --- proto/V2SvkModel.proto | 45 ++++++++++++++++++- source/CMakeLists.txt | 6 +-- source/DataModel.cpp | 23 ++++++++++ source/DataModel.h | 3 ++ source/MetaData/CMakeLists.txt | 13 ++++++ source/MetaData/MetaDataModel.cpp | 4 ++ source/MetaData/MetaDataModel.h | 4 ++ source/SubTests/V2Svk/CMakeLists.txt | 6 +-- source/SubTests/V2Svk/V2SvkModel.cpp | 64 ++++++++++++++++++++++++++++ source/SubTests/V2Svk/V2SvkModel.h | 4 ++ source/mainwindow.cpp | 46 +++++++++++++------- 11 files changed, 196 insertions(+), 22 deletions(-) diff --git a/proto/V2SvkModel.proto b/proto/V2SvkModel.proto index 9f63d8c..8a8c268 100644 --- a/proto/V2SvkModel.proto +++ b/proto/V2SvkModel.proto @@ -4,5 +4,48 @@ package ESGRAF48; message V2SvkModel { - string dummy = 1; + message OneEach + { + bool Affe = 1; + bool Schwein = 2; + bool Gans = 3; + } + + message TwoEach + { + bool Affe1 = 1; + bool Affe2 = 2; + bool Schwein1 = 3; + bool Schwein2 = 4; + bool Gans1 = 5; + bool Gans2 = 6; + } + + message FourEach + { + bool Affe1 = 1; + bool Affe2 = 2; + bool Affe3 = 3; + bool Affe4 = 4; + bool Schwein1 = 5; + bool Schwein2 = 6; + bool Schwein3 = 7; + bool Schwein4 = 8; + bool Gans1 = 9; + bool Gans2 = 10; + bool Gans3 = 11; + bool Gans4 = 12; + } + + FourEach WFrage = 1; + OneEach Verbtrennung1 = 2; + FourEach SvkSt = 3; + FourEach ObjTop = 4; + FourEach SvkStamm = 5; + TwoEach TempPraes = 6; + TwoEach SvkE1 = 7; + TwoEach TempPerf = 8; + TwoEach Verbtrennung2 = 9; + TwoEach SvkE2 = 10; + TwoEach Partizip = 11; } diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 5bdd8f2..410e0fe 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -23,7 +23,7 @@ set(DataModel_PROTO_FILES ../proto/V2SvkModel.proto ) -protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS +protobuf_generate_cpp(DataModel_PROTO_SRCS DataModel_PROTO_HDRS ${DataModel_PROTO_FILES}) add_executable(${PROJECT_NAME} WIN32 @@ -33,8 +33,8 @@ add_executable(${PROJECT_NAME} WIN32 Age.cpp ${LOGO_TEST_UI} ${LOGO_TEST_QRC} - ${PROTO_SRCS} - ${PROTO_HDRS} + ${DataModel_PROTO_SRCS} + ${DataModel_PROTO_HDRS} ) set_target_properties(${PROJECT_NAME} diff --git a/source/DataModel.cpp b/source/DataModel.cpp index f5fc181..2f571c1 100644 --- a/source/DataModel.cpp +++ b/source/DataModel.cpp @@ -1,7 +1,10 @@ #include "DataModel.h" +#include "DataModel.pb.h" #include +#include + #include DataModel::DataModel(QObject *parent) @@ -54,6 +57,26 @@ void DataModel::read(const QJsonObject &source) read(m_genitiv, source, "Genitiv"); } +void DataModel::writeProtoBuf(std::ostream &outStream) const +{ + ESGRAF48::DataModel dataModel; + //m_metaData.writeProtoBuf(dataModel.mutable_metadata); + m_v2Svk.writeProtoBuf(*dataModel.mutable_v2svk()); + dataModel.SerializeToOstream(&outStream); + + std::string saveData; + google::protobuf::TextFormat::PrintToString(dataModel, &saveData); + qDebug() << "Wrote:" << QString::fromStdString(saveData); +} + +void DataModel::readProtoBuf(std::istream &inStream) +{ + ESGRAF48::DataModel dataModel; + dataModel.ParseFromIstream(&inStream); + + m_v2Svk.readProtoBuf(dataModel.v2svk()); +} + std::string DataModel::toHtml() const { std::stringstream out; diff --git a/source/DataModel.h b/source/DataModel.h index b2d6f13..2ac876d 100644 --- a/source/DataModel.h +++ b/source/DataModel.h @@ -39,6 +39,9 @@ public: std::string toHtml() const; + void writeProtoBuf(std::ostream &outStream) const; + void readProtoBuf(std::istream &inStream); + signals: void modelChanged(); diff --git a/source/MetaData/CMakeLists.txt b/source/MetaData/CMakeLists.txt index 01aa53f..d8a6379 100644 --- a/source/MetaData/CMakeLists.txt +++ b/source/MetaData/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.6) project(MetaData LANGUAGES CXX) find_package(Qt5Widgets REQUIRED) +find_package(Protobuf REQUIRED) set(CMAKE_AUTOMOC ON) @@ -10,10 +11,20 @@ qt5_wrap_ui(META_DATA_UI MetaDataWidget.ui ) +set(MetaDataModel_PROTO_FILES + ../../proto/MetaDataModel.proto +) + +protobuf_generate_cpp(MetaDataModel_PROTO_SRCS MetaDataModel_PROTO_HDRS + ${MetaDataModel_PROTO_FILES} +) + add_library(${PROJECT_NAME} MetaDataModel.cpp MetaDataWidget.cpp ${META_DATA_UI} + ${MetaDateModel_PROTO_SRCS} + ${MetaDateModel_PROTO_HDRS} ) set_target_properties(${PROJECT_NAME} @@ -23,6 +34,7 @@ set_target_properties(${PROJECT_NAME} target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} + ${Protobuf_INCLUDE_DIRS} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} ) @@ -30,4 +42,5 @@ target_include_directories(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Widgets + ${Protobuf_LIBRARIES} ) diff --git a/source/MetaData/MetaDataModel.cpp b/source/MetaData/MetaDataModel.cpp index 7219715..1877777 100644 --- a/source/MetaData/MetaDataModel.cpp +++ b/source/MetaData/MetaDataModel.cpp @@ -130,6 +130,10 @@ void MetaDataModel::read(const QJsonObject &json) setData(index(0, 4), json["remarks"].toVariant(), Qt::EditRole); } +//void MetaDataModel::writeProtoBuf(ESGRAF48::MetaDataModel &model) const +//{ +//} + std::string MetaDataModel::toHtml() const { std::ostringstream out; diff --git a/source/MetaData/MetaDataModel.h b/source/MetaData/MetaDataModel.h index 6c81f8a..1de7a0f 100644 --- a/source/MetaData/MetaDataModel.h +++ b/source/MetaData/MetaDataModel.h @@ -2,6 +2,8 @@ #include "../Age.h" +//#include "MetaDataModel.pb.h" + #include #include #include @@ -31,6 +33,8 @@ public: void write(QJsonObject &json) const; void read(const QJsonObject &json); + //void writeProtoBuf(ESGRAF48::MetaDataModel &model) const; + std::string toHtml() const; Age getAge() const diff --git a/source/SubTests/V2Svk/CMakeLists.txt b/source/SubTests/V2Svk/CMakeLists.txt index fdd3fe6..caf6c8c 100644 --- a/source/SubTests/V2Svk/CMakeLists.txt +++ b/source/SubTests/V2Svk/CMakeLists.txt @@ -15,14 +15,14 @@ set(V2Svk_PROTO_FILES ../../../proto/V2SvkModel.proto ) -protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${V2Svk_PROTO_FILES}) +protobuf_generate_cpp(V2Svk_PROTO_SRCS V2Svk_PROTO_HDRS ${V2Svk_PROTO_FILES}) add_library(${PROJECT_NAME} V2SvkWidget.cpp V2SvkModel.cpp ${UI_HEADERS} - ${PROTO_SRCS} - ${PROTO_HDRS} + ${V2Svk_PROTO_SRCS} + ${V2Svk_PROTO_HDRS} ) set_target_properties(${PROJECT_NAME} diff --git a/source/SubTests/V2Svk/V2SvkModel.cpp b/source/SubTests/V2Svk/V2SvkModel.cpp index 249fe91..b86cef9 100644 --- a/source/SubTests/V2Svk/V2SvkModel.cpp +++ b/source/SubTests/V2Svk/V2SvkModel.cpp @@ -75,3 +75,67 @@ unsigned int V2SvkModel::getSvkPoints() return points; } + +void V2SvkModel::writeProtoBuf(ESGRAF48::V2SvkModel &model) const +{ + auto *wfrage = model.mutable_wfrage(); + if (wfrage != nullptr) + { + const auto &items = m_tests.at(0).items(); + + wfrage->set_affe1(items[0].isChecked()); + wfrage->set_affe2(items[1].isChecked()); + wfrage->set_affe3(items[2].isChecked()); + wfrage->set_affe4(items[3].isChecked()); + wfrage->set_schwein1(items[4].isChecked()); + wfrage->set_schwein2(items[5].isChecked()); + wfrage->set_schwein3(items[6].isChecked()); + wfrage->set_schwein4(items[7].isChecked()); + wfrage->set_gans1(items[8].isChecked()); + wfrage->set_gans2(items[9].isChecked()); + wfrage->set_gans3(items[10].isChecked()); + wfrage->set_gans4(items[11].isChecked()); + } + + auto *verbtrennung = model.mutable_verbtrennung1(); + if (verbtrennung != nullptr) + { + const auto &testItems = m_tests.at(1).items(); + + verbtrennung->set_affe(testItems[1].isChecked()); + verbtrennung->set_schwein(testItems[7].isChecked()); + verbtrennung->set_gans(testItems[10].isChecked()); + } +} + +void V2SvkModel::readProtoBuf(const ESGRAF48::V2SvkModel &model) +{ + const auto &wfrage = model.wfrage(); + { + auto &testItems = m_tests.at(0).items(); + + testItems[0].setState(wfrage.affe1()); + testItems[1].setState(wfrage.affe2()); + testItems[2].setState(wfrage.affe3()); + testItems[3].setState(wfrage.affe4()); + testItems[4].setState(wfrage.schwein1()); + testItems[5].setState(wfrage.schwein2()); + testItems[6].setState(wfrage.schwein3()); + testItems[7].setState(wfrage.schwein4()); + testItems[8].setState(wfrage.gans1()); + testItems[9].setState(wfrage.gans2()); + testItems[10].setState(wfrage.gans3()); + testItems[11].setState(wfrage.gans4()); + } + + const auto &verbtrennung = model.verbtrennung1(); + { + auto &testItems = m_tests.at(1).items(); + + testItems[1].setState(verbtrennung.affe()); + testItems[7].setState(verbtrennung.schwein()); + testItems[10].setState(verbtrennung.gans()); + } + + emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1)); +} diff --git a/source/SubTests/V2Svk/V2SvkModel.h b/source/SubTests/V2Svk/V2SvkModel.h index 456c572..091e5b6 100644 --- a/source/SubTests/V2Svk/V2SvkModel.h +++ b/source/SubTests/V2Svk/V2SvkModel.h @@ -1,6 +1,7 @@ #pragma once #include "CheckableTestModel.h" +#include "V2SvkModel.pb.h" class V2SvkModel : public CheckableTestModel { @@ -11,4 +12,7 @@ public: unsigned int getV2Points(); unsigned int getSvkPoints(); + + void writeProtoBuf(ESGRAF48::V2SvkModel &model) const; + void readProtoBuf(const ESGRAF48::V2SvkModel &model); }; diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index 12bc99f..18642ab 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -74,17 +74,25 @@ void MainWindow::openFile() closeFile(); - QFile loadFile(filename); - if (!loadFile.open(QFile::ReadOnly)) + if (true) { - qWarning("Could not open file"); - return; + std::fstream protoInFile(filename.toStdString(), std::ios::in | std::ios::binary); + m_dataModel->readProtoBuf(protoInFile); } + else + { + QFile loadFile(filename); + if (!loadFile.open(QFile::ReadOnly)) + { + qWarning("Could not open file"); + return; + } - QByteArray byteData = loadFile.readAll(); - QJsonDocument loadDoc = QJsonDocument::fromJson(byteData); + QByteArray byteData = loadFile.readAll(); + QJsonDocument loadDoc = QJsonDocument::fromJson(byteData); - m_dataModel->read(loadDoc.object()); + m_dataModel->read(loadDoc.object()); + } setWindowModified(false); setWindowTitle(filename + "[*]"); @@ -167,16 +175,24 @@ void MainWindow::closeEvent(QCloseEvent *event) void MainWindow::saveFile(const QString &filename) { - QJsonObject saveData; - m_dataModel->write(saveData); + if (true) + { + std::fstream protoOutFile(filename.toStdString(), + std::ios::out | std::ios::trunc | std::ios::binary); + m_dataModel->writeProtoBuf(protoOutFile); + } + else + { + QJsonObject saveData; + m_dataModel->write(saveData); - QJsonDocument saveDoc(saveData); - - QFile saveFile(filename); - saveFile.open(QFile::WriteOnly); - saveFile.write(saveDoc.toJson()); - saveFile.close(); + QJsonDocument saveDoc(saveData); + QFile saveFile(filename); + saveFile.open(QFile::WriteOnly); + saveFile.write(saveDoc.toJson()); + saveFile.close(); + } qDebug() << "Wrote" << filename; setWindowTitle(filename + "[*]");