Read and write some protobuf data

This commit is contained in:
Michael Mandl 2018-11-14 00:17:29 +01:00
parent 7b02a34cf3
commit 221368b3ca
11 changed files with 196 additions and 22 deletions

View file

@ -4,5 +4,48 @@ package ESGRAF48;
message V2SvkModel 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;
} }

View file

@ -23,7 +23,7 @@ set(DataModel_PROTO_FILES
../proto/V2SvkModel.proto ../proto/V2SvkModel.proto
) )
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS protobuf_generate_cpp(DataModel_PROTO_SRCS DataModel_PROTO_HDRS
${DataModel_PROTO_FILES}) ${DataModel_PROTO_FILES})
add_executable(${PROJECT_NAME} WIN32 add_executable(${PROJECT_NAME} WIN32
@ -33,8 +33,8 @@ add_executable(${PROJECT_NAME} WIN32
Age.cpp Age.cpp
${LOGO_TEST_UI} ${LOGO_TEST_UI}
${LOGO_TEST_QRC} ${LOGO_TEST_QRC}
${PROTO_SRCS} ${DataModel_PROTO_SRCS}
${PROTO_HDRS} ${DataModel_PROTO_HDRS}
) )
set_target_properties(${PROJECT_NAME} set_target_properties(${PROJECT_NAME}

View file

@ -1,7 +1,10 @@
#include "DataModel.h" #include "DataModel.h"
#include "DataModel.pb.h"
#include <QDebug> #include <QDebug>
#include <google/protobuf/text_format.h>
#include <sstream> #include <sstream>
DataModel::DataModel(QObject *parent) DataModel::DataModel(QObject *parent)
@ -54,6 +57,26 @@ void DataModel::read(const QJsonObject &source)
read(m_genitiv, source, "Genitiv"); 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::string DataModel::toHtml() const
{ {
std::stringstream out; std::stringstream out;

View file

@ -39,6 +39,9 @@ public:
std::string toHtml() const; std::string toHtml() const;
void writeProtoBuf(std::ostream &outStream) const;
void readProtoBuf(std::istream &inStream);
signals: signals:
void modelChanged(); void modelChanged();

View file

@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.6)
project(MetaData LANGUAGES CXX) project(MetaData LANGUAGES CXX)
find_package(Qt5Widgets REQUIRED) find_package(Qt5Widgets REQUIRED)
find_package(Protobuf REQUIRED)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
@ -10,10 +11,20 @@ qt5_wrap_ui(META_DATA_UI
MetaDataWidget.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} add_library(${PROJECT_NAME}
MetaDataModel.cpp MetaDataModel.cpp
MetaDataWidget.cpp MetaDataWidget.cpp
${META_DATA_UI} ${META_DATA_UI}
${MetaDateModel_PROTO_SRCS}
${MetaDateModel_PROTO_HDRS}
) )
set_target_properties(${PROJECT_NAME} set_target_properties(${PROJECT_NAME}
@ -23,6 +34,7 @@ set_target_properties(${PROJECT_NAME}
target_include_directories(${PROJECT_NAME} target_include_directories(${PROJECT_NAME}
PRIVATE PRIVATE
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}
${Protobuf_INCLUDE_DIRS}
INTERFACE INTERFACE
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
) )
@ -30,4 +42,5 @@ target_include_directories(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
PRIVATE PRIVATE
Qt5::Widgets Qt5::Widgets
${Protobuf_LIBRARIES}
) )

View file

@ -130,6 +130,10 @@ void MetaDataModel::read(const QJsonObject &json)
setData(index(0, 4), json["remarks"].toVariant(), Qt::EditRole); setData(index(0, 4), json["remarks"].toVariant(), Qt::EditRole);
} }
//void MetaDataModel::writeProtoBuf(ESGRAF48::MetaDataModel &model) const
//{
//}
std::string MetaDataModel::toHtml() const std::string MetaDataModel::toHtml() const
{ {
std::ostringstream out; std::ostringstream out;

View file

@ -2,6 +2,8 @@
#include "../Age.h" #include "../Age.h"
//#include "MetaDataModel.pb.h"
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QString> #include <QString>
#include <QDate> #include <QDate>
@ -31,6 +33,8 @@ public:
void write(QJsonObject &json) const; void write(QJsonObject &json) const;
void read(const QJsonObject &json); void read(const QJsonObject &json);
//void writeProtoBuf(ESGRAF48::MetaDataModel &model) const;
std::string toHtml() const; std::string toHtml() const;
Age getAge() const Age getAge() const

View file

@ -15,14 +15,14 @@ set(V2Svk_PROTO_FILES
../../../proto/V2SvkModel.proto ../../../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} add_library(${PROJECT_NAME}
V2SvkWidget.cpp V2SvkWidget.cpp
V2SvkModel.cpp V2SvkModel.cpp
${UI_HEADERS} ${UI_HEADERS}
${PROTO_SRCS} ${V2Svk_PROTO_SRCS}
${PROTO_HDRS} ${V2Svk_PROTO_HDRS}
) )
set_target_properties(${PROJECT_NAME} set_target_properties(${PROJECT_NAME}

View file

@ -75,3 +75,67 @@ unsigned int V2SvkModel::getSvkPoints()
return points; 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));
}

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "CheckableTestModel.h" #include "CheckableTestModel.h"
#include "V2SvkModel.pb.h"
class V2SvkModel : public CheckableTestModel class V2SvkModel : public CheckableTestModel
{ {
@ -11,4 +12,7 @@ public:
unsigned int getV2Points(); unsigned int getV2Points();
unsigned int getSvkPoints(); unsigned int getSvkPoints();
void writeProtoBuf(ESGRAF48::V2SvkModel &model) const;
void readProtoBuf(const ESGRAF48::V2SvkModel &model);
}; };

View file

@ -74,6 +74,13 @@ void MainWindow::openFile()
closeFile(); closeFile();
if (true)
{
std::fstream protoInFile(filename.toStdString(), std::ios::in | std::ios::binary);
m_dataModel->readProtoBuf(protoInFile);
}
else
{
QFile loadFile(filename); QFile loadFile(filename);
if (!loadFile.open(QFile::ReadOnly)) if (!loadFile.open(QFile::ReadOnly))
{ {
@ -85,6 +92,7 @@ void MainWindow::openFile()
QJsonDocument loadDoc = QJsonDocument::fromJson(byteData); QJsonDocument loadDoc = QJsonDocument::fromJson(byteData);
m_dataModel->read(loadDoc.object()); m_dataModel->read(loadDoc.object());
}
setWindowModified(false); setWindowModified(false);
setWindowTitle(filename + "[*]"); setWindowTitle(filename + "[*]");
@ -166,6 +174,14 @@ void MainWindow::closeEvent(QCloseEvent *event)
} }
void MainWindow::saveFile(const QString &filename) void MainWindow::saveFile(const QString &filename)
{
if (true)
{
std::fstream protoOutFile(filename.toStdString(),
std::ios::out | std::ios::trunc | std::ios::binary);
m_dataModel->writeProtoBuf(protoOutFile);
}
else
{ {
QJsonObject saveData; QJsonObject saveData;
m_dataModel->write(saveData); m_dataModel->write(saveData);
@ -176,7 +192,7 @@ void MainWindow::saveFile(const QString &filename)
saveFile.open(QFile::WriteOnly); saveFile.open(QFile::WriteOnly);
saveFile.write(saveDoc.toJson()); saveFile.write(saveDoc.toJson());
saveFile.close(); saveFile.close();
}
qDebug() << "Wrote" << filename; qDebug() << "Wrote" << filename;
setWindowTitle(filename + "[*]"); setWindowTitle(filename + "[*]");