Added new-file function

This commit is contained in:
mandlm 2018-05-20 12:04:21 +02:00
parent 837a4f68bc
commit 1aa0fdfd22
6 changed files with 78 additions and 25 deletions

View File

@ -4,6 +4,8 @@
MetaDataModel::MetaDataModel(QObject *parent) : QAbstractTableModel(parent) MetaDataModel::MetaDataModel(QObject *parent) : QAbstractTableModel(parent)
{ {
m_dateOfBirth.setDate(2010, 01, 01);
m_dateOfTest = QDate::currentDate();
} }
int MetaDataModel::rowCount(const QModelIndex &parent) const int MetaDataModel::rowCount(const QModelIndex &parent) const

Binary file not shown.

After

Width:  |  Height:  |  Size: 736 B

View File

@ -12,14 +12,14 @@
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
, m_dataModel(new DataModel(parent))
{ {
ui->setupUi(this); ui->setupUi(this);
connect(ui->actionSave_as, SIGNAL(triggered()), this, SLOT(saveAs())); connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(load())); connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile()));
connect(ui->actionSave_as, SIGNAL(triggered()), this, SLOT(saveFileAs()));
ui->metaDataWidget->setModel(&m_dataModel->m_metaData); newFile();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -27,26 +27,13 @@ MainWindow::~MainWindow()
delete ui; delete ui;
} }
void MainWindow::saveAs() void MainWindow::newFile()
{ {
QString filename = QFileDialog::getSaveFileName(this); m_dataModel = std::make_unique<DataModel>(this);
if (filename.isEmpty()) ui->metaDataWidget->setModel(&m_dataModel->m_metaData);
{
return;
}
QJsonObject saveData;
m_dataModel->write(saveData);
QJsonDocument saveDoc(saveData);
QFile saveFile(filename);
saveFile.open(QFile::WriteOnly);
saveFile.write(saveDoc.toJson());
saveFile.close();
} }
void MainWindow::load() void MainWindow::openFile()
{ {
QString filename = QFileDialog::getOpenFileName(this); QString filename = QFileDialog::getOpenFileName(this);
if (filename.isEmpty()) if (filename.isEmpty())
@ -68,3 +55,22 @@ void MainWindow::load()
ui->metaDataWidget->toFirst(); ui->metaDataWidget->toFirst();
} }
void MainWindow::saveFileAs()
{
QString filename = QFileDialog::getSaveFileName(this);
if (filename.isEmpty())
{
return;
}
QJsonObject saveData;
m_dataModel->write(saveData);
QJsonDocument saveDoc(saveData);
QFile saveFile(filename);
saveFile.open(QFile::WriteOnly);
saveFile.write(saveDoc.toJson());
saveFile.close();
}

View File

@ -2,6 +2,8 @@
#include <QMainWindow> #include <QMainWindow>
#include <memory>
class DataModel; class DataModel;
class QDataWidgetMapper; class QDataWidgetMapper;
@ -15,13 +17,14 @@ class MainWindow : public QMainWindow
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
DataModel *m_dataModel; std::unique_ptr<DataModel> m_dataModel;
public: public:
MainWindow(QWidget *parent = nullptr); MainWindow(QWidget *parent = nullptr);
~MainWindow(); ~MainWindow();
public slots: public slots:
void saveAs(); void newFile();
void load(); void openFile();
void saveFileAs();
}; };

View File

@ -1,5 +1,6 @@
<RCC> <RCC>
<qresource> <qresource>
<file>images/document-new.png</file>
<file>images/document-open.png</file> <file>images/document-open.png</file>
<file>images/document-save-as.png</file> <file>images/document-save-as.png</file>
</qresource> </qresource>

View File

@ -52,8 +52,11 @@
<property name="title"> <property name="title">
<string>&amp;File</string> <string>&amp;File</string>
</property> </property>
<addaction name="actionNew"/>
<addaction name="actionOpen"/> <addaction name="actionOpen"/>
<addaction name="actionSave_as"/> <addaction name="actionSave_as"/>
<addaction name="separator"/>
<addaction name="actionQuit"/>
</widget> </widget>
<addaction name="menuFile"/> <addaction name="menuFile"/>
</widget> </widget>
@ -67,6 +70,7 @@
<attribute name="toolBarBreak"> <attribute name="toolBarBreak">
<bool>false</bool> <bool>false</bool>
</attribute> </attribute>
<addaction name="actionNew"/>
<addaction name="actionOpen"/> <addaction name="actionOpen"/>
<addaction name="actionSave_as"/> <addaction name="actionSave_as"/>
</widget> </widget>
@ -97,6 +101,26 @@
<string>Ctrl+O</string> <string>Ctrl+O</string>
</property> </property>
</action> </action>
<action name="actionNew">
<property name="icon">
<iconset resource="mainwindow.qrc">
<normaloff>:/images/document-new.png</normaloff>:/images/document-new.png</iconset>
</property>
<property name="text">
<string>New</string>
</property>
<property name="shortcut">
<string>Ctrl+N</string>
</property>
</action>
<action name="actionQuit">
<property name="text">
<string>Quit</string>
</property>
<property name="shortcut">
<string>Ctrl+Q</string>
</property>
</action>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>
@ -109,5 +133,22 @@
<resources> <resources>
<include location="mainwindow.qrc"/> <include location="mainwindow.qrc"/>
</resources> </resources>
<connections/> <connections>
<connection>
<sender>actionQuit</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>399</x>
<y>177</y>
</hint>
</hints>
</connection>
</connections>
</ui> </ui>