implemented rudimentary load/save

This commit is contained in:
mandlm 2018-05-16 07:02:12 +02:00
parent b8fdf26628
commit c7bd71f805
8 changed files with 98 additions and 24 deletions

View File

@ -13,6 +13,7 @@ qt5_wrap_ui(UI_HEADERS
add_executable(${PROJECT_NAME} add_executable(${PROJECT_NAME}
LogoTest.cpp LogoTest.cpp
testdata.cpp testdata.cpp
DataModel.cpp
MetaDataModel.cpp MetaDataModel.cpp
mainwindow.cpp mainwindow.cpp
${UI_HEADERS} ${UI_HEADERS}

21
source/DataModel.cpp Normal file
View File

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

19
source/DataModel.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include "MetaDataModel.h"
#include <QJsonObject>
class DataModel : public QObject
{
Q_OBJECT
public:
MetaDataModel m_metaData;
public:
DataModel(QObject *parent);
void write(QJsonObject &target) const;
void read(const QJsonObject &source);
};

View File

@ -40,7 +40,7 @@ QVariant MetaDataModel::data(const QModelIndex &index, int role) const
return QVariant(); return QVariant();
} }
void MetaDataModel::write(QJsonObject &json) void MetaDataModel::write(QJsonObject &json) const
{ {
json["participant name"] = m_participant; json["participant name"] = m_participant;
json["instructor name"] = m_instructor; json["instructor name"] = m_instructor;

View File

@ -23,6 +23,6 @@ public:
QVariant data( QVariant data(
const QModelIndex &index, int role = Qt::DisplayRole) const override; const QModelIndex &index, int role = Qt::DisplayRole) const override;
void write(QJsonObject &json); void write(QJsonObject &json) const;
void read(const QJsonObject &json); void read(const QJsonObject &json);
}; };

View File

@ -2,34 +2,35 @@
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "testdata.h" #include "testdata.h"
#include "DataModel.h"
#include "MetaDataModel.h" #include "MetaDataModel.h"
#include <QJsonObject> #include <QJsonObject>
#include <QJsonDocument> #include <QJsonDocument>
#include <QFile> #include <QFile>
#include <QFileDialog> #include <QFileDialog>
#include <QDataWidgetMapper> #include <QDataWidgetMapper>
MainWindow::MainWindow(QWidget *parent) 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->setupUi(this);
ui->actionSave_as->setIcon(QIcon::fromTheme("document-save-as", QIcon("/usr/share/gtk-doc/html/gtk2/document-save-as.png"))); 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->actionSave_as, SIGNAL(triggered()), this, SLOT(saveAs()));
connect(ui->actionLoad, SIGNAL(triggered()), this, SLOT(load()));
MetaDataModel *metaDataModel = new MetaDataModel(this); m_widgetMapper->setModel(&m_dataModel->m_metaData);
m_widgetMapper->addMapping(ui->lineEdit, 0);
QDataWidgetMapper *mapper = new QDataWidgetMapper(this); m_widgetMapper->addMapping(ui->lineEdit_2, 1);
mapper->setModel(metaDataModel); m_widgetMapper->addMapping(ui->calendarWidget, 2);
mapper->addMapping(ui->lineEdit, 0); m_widgetMapper->addMapping(ui->calendarWidget_2, 3);
mapper->addMapping(ui->lineEdit_2, 1); m_widgetMapper->addMapping(ui->textEdit, 4);
mapper->addMapping(ui->calendarWidget, 2); m_widgetMapper->toFirst();
mapper->addMapping(ui->calendarWidget_2, 3);
mapper->addMapping(ui->textEdit, 4);
mapper->toFirst();
//ui->tableView->setModel(metaDataModel);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -45,9 +46,8 @@ void MainWindow::saveAs()
return; return;
} }
TestData testData;
QJsonObject saveData; QJsonObject saveData;
testData.write(saveData); m_dataModel->write(saveData);
QJsonDocument saveDoc(saveData); QJsonDocument saveDoc(saveData);
@ -56,3 +56,26 @@ void MainWindow::saveAs()
saveFile.write(saveDoc.toJson()); saveFile.write(saveDoc.toJson());
saveFile.close(); 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();
}

View File

@ -2,6 +2,9 @@
#include <QMainWindow> #include <QMainWindow>
class DataModel;
class QDataWidgetMapper;
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
}; };
@ -10,13 +13,16 @@ class MainWindow : public QMainWindow
{ {
Q_OBJECT Q_OBJECT
private:
Ui::MainWindow *ui;
DataModel *m_dataModel;
QDataWidgetMapper *m_widgetMapper;
public: public:
MainWindow(QWidget *parent = nullptr); MainWindow(QWidget *parent = nullptr);
~MainWindow(); ~MainWindow();
public slots: public slots:
void saveAs(); void saveAs();
void load();
private:
Ui::MainWindow *ui;
}; };

View File

@ -104,9 +104,6 @@
<item> <item>
<widget class="QTextEdit" name="textEdit"/> <widget class="QTextEdit" name="textEdit"/>
</item> </item>
<item>
<widget class="QTableView" name="tableView"/>
</item>
</layout> </layout>
</widget> </widget>
<widget class="QMenuBar" name="menubar"> <widget class="QMenuBar" name="menubar">
@ -123,6 +120,7 @@
<string>&amp;File</string> <string>&amp;File</string>
</property> </property>
<addaction name="actionSave_as"/> <addaction name="actionSave_as"/>
<addaction name="actionLoad"/>
</widget> </widget>
<addaction name="menuFile"/> <addaction name="menuFile"/>
</widget> </widget>
@ -138,6 +136,7 @@
<bool>false</bool> <bool>false</bool>
</attribute> </attribute>
<addaction name="actionSave_as"/> <addaction name="actionSave_as"/>
<addaction name="actionLoad"/>
</widget> </widget>
<action name="actionSave_as"> <action name="actionSave_as">
<property name="text"> <property name="text">
@ -147,6 +146,11 @@
<string>Save as...</string> <string>Save as...</string>
</property> </property>
</action> </action>
<action name="actionLoad">
<property name="text">
<string>Load</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>