From c7bd71f805580411290c84d26114a691a3252e06 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 16 May 2018 07:02:12 +0200 Subject: [PATCH] implemented rudimentary load/save --- source/CMakeLists.txt | 1 + source/DataModel.cpp | 21 +++++++++++++++ source/DataModel.h | 19 ++++++++++++++ source/MetaDataModel.cpp | 2 +- source/MetaDataModel.h | 2 +- source/mainwindow.cpp | 55 ++++++++++++++++++++++++++++------------ source/mainwindow.h | 12 ++++++--- source/mainwindow.ui | 10 +++++--- 8 files changed, 98 insertions(+), 24 deletions(-) create mode 100644 source/DataModel.cpp create mode 100644 source/DataModel.h diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index e0d6234..db35231 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -13,6 +13,7 @@ qt5_wrap_ui(UI_HEADERS add_executable(${PROJECT_NAME} LogoTest.cpp testdata.cpp + DataModel.cpp MetaDataModel.cpp mainwindow.cpp ${UI_HEADERS} diff --git a/source/DataModel.cpp b/source/DataModel.cpp new file mode 100644 index 0000000..57b5c06 --- /dev/null +++ b/source/DataModel.cpp @@ -0,0 +1,21 @@ +#include "DataModel.h" + +DataModel::DataModel(QObject *parent) : QObject(parent), m_metaData(this) +{ +} + +void DataModel::write(QJsonObject &target) const +{ + QJsonObject metaData; + m_metaData.write(metaData); + target["MetaData"] = metaData; +} + +void DataModel::read(const QJsonObject &source) +{ + const auto &metaData = source["MetaData"]; + if (metaData.isObject()) + { + m_metaData.read(metaData.toObject()); + } +} diff --git a/source/DataModel.h b/source/DataModel.h new file mode 100644 index 0000000..02f8e29 --- /dev/null +++ b/source/DataModel.h @@ -0,0 +1,19 @@ +#pragma once + +#include "MetaDataModel.h" + +#include + +class DataModel : public QObject +{ + Q_OBJECT + +public: + MetaDataModel m_metaData; + +public: + DataModel(QObject *parent); + + void write(QJsonObject &target) const; + void read(const QJsonObject &source); +}; diff --git a/source/MetaDataModel.cpp b/source/MetaDataModel.cpp index f6b1276..ea04dfb 100644 --- a/source/MetaDataModel.cpp +++ b/source/MetaDataModel.cpp @@ -40,7 +40,7 @@ QVariant MetaDataModel::data(const QModelIndex &index, int role) const return QVariant(); } -void MetaDataModel::write(QJsonObject &json) +void MetaDataModel::write(QJsonObject &json) const { json["participant name"] = m_participant; json["instructor name"] = m_instructor; diff --git a/source/MetaDataModel.h b/source/MetaDataModel.h index ffc50f1..db179ae 100644 --- a/source/MetaDataModel.h +++ b/source/MetaDataModel.h @@ -23,6 +23,6 @@ public: QVariant data( const QModelIndex &index, int role = Qt::DisplayRole) const override; - void write(QJsonObject &json); + void write(QJsonObject &json) const; void read(const QJsonObject &json); }; diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index 802ce8f..ea05622 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -2,34 +2,35 @@ #include "ui_mainwindow.h" #include "testdata.h" +#include "DataModel.h" #include "MetaDataModel.h" + #include #include #include #include #include - MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent), ui(new Ui::MainWindow) + : QMainWindow(parent), ui(new Ui::MainWindow), + m_dataModel(new DataModel(parent)) + , m_widgetMapper(new QDataWidgetMapper(parent)) { ui->setupUi(this); ui->actionSave_as->setIcon(QIcon::fromTheme("document-save-as", QIcon("/usr/share/gtk-doc/html/gtk2/document-save-as.png"))); + ui->actionLoad->setIcon(QIcon::fromTheme("document-open", QIcon("/usr/share/gtk-doc/html/gtk2/document-open.png"))); + connect(ui->actionSave_as, SIGNAL(triggered()), this, SLOT(saveAs())); + connect(ui->actionLoad, SIGNAL(triggered()), this, SLOT(load())); - MetaDataModel *metaDataModel = new MetaDataModel(this); - - QDataWidgetMapper *mapper = new QDataWidgetMapper(this); - mapper->setModel(metaDataModel); - mapper->addMapping(ui->lineEdit, 0); - mapper->addMapping(ui->lineEdit_2, 1); - mapper->addMapping(ui->calendarWidget, 2); - mapper->addMapping(ui->calendarWidget_2, 3); - mapper->addMapping(ui->textEdit, 4); - mapper->toFirst(); - - //ui->tableView->setModel(metaDataModel); + m_widgetMapper->setModel(&m_dataModel->m_metaData); + m_widgetMapper->addMapping(ui->lineEdit, 0); + m_widgetMapper->addMapping(ui->lineEdit_2, 1); + m_widgetMapper->addMapping(ui->calendarWidget, 2); + m_widgetMapper->addMapping(ui->calendarWidget_2, 3); + m_widgetMapper->addMapping(ui->textEdit, 4); + m_widgetMapper->toFirst(); } MainWindow::~MainWindow() @@ -45,9 +46,8 @@ void MainWindow::saveAs() return; } - TestData testData; QJsonObject saveData; - testData.write(saveData); + m_dataModel->write(saveData); QJsonDocument saveDoc(saveData); @@ -56,3 +56,26 @@ void MainWindow::saveAs() saveFile.write(saveDoc.toJson()); saveFile.close(); } + +void MainWindow::load() +{ + QString filename = QFileDialog::getOpenFileName(this); + if (filename.isEmpty()) + { + return; + } + + QFile loadFile(filename); + if (!loadFile.open(QFile::ReadOnly)) + { + qWarning("Could not open file"); + return; + } + + QByteArray byteData = loadFile.readAll(); + QJsonDocument loadDoc = QJsonDocument::fromJson(byteData); + + m_dataModel->read(loadDoc.object()); + + m_widgetMapper->toFirst(); +} diff --git a/source/mainwindow.h b/source/mainwindow.h index a60c782..95d91cf 100644 --- a/source/mainwindow.h +++ b/source/mainwindow.h @@ -2,6 +2,9 @@ #include +class DataModel; +class QDataWidgetMapper; + namespace Ui { class MainWindow; }; @@ -10,13 +13,16 @@ class MainWindow : public QMainWindow { Q_OBJECT +private: + Ui::MainWindow *ui; + DataModel *m_dataModel; + QDataWidgetMapper *m_widgetMapper; + public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void saveAs(); - -private: - Ui::MainWindow *ui; + void load(); }; diff --git a/source/mainwindow.ui b/source/mainwindow.ui index a58e93e..6c8a1a0 100644 --- a/source/mainwindow.ui +++ b/source/mainwindow.ui @@ -104,9 +104,6 @@ - - - @@ -123,6 +120,7 @@ &File + @@ -138,6 +136,7 @@ false + @@ -147,6 +146,11 @@ Save as... + + + Load + +