Read and write some protobuf data

feature/build-protobuf
mandlm 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
{
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
)
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}

View File

@ -1,7 +1,10 @@
#include "DataModel.h"
#include "DataModel.pb.h"
#include <QDebug>
#include <google/protobuf/text_format.h>
#include <sstream>
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;

View File

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

View File

@ -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}
)

View File

@ -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;

View File

@ -2,6 +2,8 @@
#include "../Age.h"
//#include "MetaDataModel.pb.h"
#include <QAbstractTableModel>
#include <QString>
#include <QDate>
@ -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

View File

@ -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}

View File

@ -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));
}

View File

@ -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);
};

View File

@ -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 + "[*]");