implemented rudimentary load/save
This commit is contained in:
parent
b8fdf26628
commit
c7bd71f805
@ -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}
|
||||
|
21
source/DataModel.cpp
Normal file
21
source/DataModel.cpp
Normal 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
19
source/DataModel.h
Normal 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);
|
||||
};
|
@ -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;
|
||||
|
@ -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);
|
||||
};
|
||||
|
@ -2,34 +2,35 @@
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
#include "testdata.h"
|
||||
#include "DataModel.h"
|
||||
#include "MetaDataModel.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QFile>
|
||||
#include <QFileDialog>
|
||||
#include <QDataWidgetMapper>
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
@ -2,6 +2,9 @@
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
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();
|
||||
};
|
||||
|
@ -104,9 +104,6 @@
|
||||
<item>
|
||||
<widget class="QTextEdit" name="textEdit"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QTableView" name="tableView"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menubar">
|
||||
@ -123,6 +120,7 @@
|
||||
<string>&File</string>
|
||||
</property>
|
||||
<addaction name="actionSave_as"/>
|
||||
<addaction name="actionLoad"/>
|
||||
</widget>
|
||||
<addaction name="menuFile"/>
|
||||
</widget>
|
||||
@ -138,6 +136,7 @@
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
<addaction name="actionSave_as"/>
|
||||
<addaction name="actionLoad"/>
|
||||
</widget>
|
||||
<action name="actionSave_as">
|
||||
<property name="text">
|
||||
@ -147,6 +146,11 @@
|
||||
<string>Save as...</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionLoad">
|
||||
<property name="text">
|
||||
<string>Load</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
|
Reference in New Issue
Block a user