Added new-file function

This commit is contained in:
Michael Mandl 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)
{
m_dateOfBirth.setDate(2010, 01, 01);
m_dateOfTest = QDate::currentDate();
}
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)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_dataModel(new DataModel(parent))
{
ui->setupUi(this);
connect(ui->actionSave_as, SIGNAL(triggered()), this, SLOT(saveAs()));
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(load()));
connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
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()
@ -27,26 +27,13 @@ MainWindow::~MainWindow()
delete ui;
}
void MainWindow::saveAs()
void MainWindow::newFile()
{
QString filename = QFileDialog::getSaveFileName(this);
if (filename.isEmpty())
{
return;
m_dataModel = std::make_unique<DataModel>(this);
ui->metaDataWidget->setModel(&m_dataModel->m_metaData);
}
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);
if (filename.isEmpty())
@ -68,3 +55,22 @@ void MainWindow::load()
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 <memory>
class DataModel;
class QDataWidgetMapper;
@ -15,13 +17,14 @@ class MainWindow : public QMainWindow
private:
Ui::MainWindow *ui;
DataModel *m_dataModel;
std::unique_ptr<DataModel> m_dataModel;
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void saveAs();
void load();
void newFile();
void openFile();
void saveFileAs();
};

View file

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

View file

@ -52,8 +52,11 @@
<property name="title">
<string>&amp;File</string>
</property>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
<addaction name="actionSave_as"/>
<addaction name="separator"/>
<addaction name="actionQuit"/>
</widget>
<addaction name="menuFile"/>
</widget>
@ -67,6 +70,7 @@
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
<addaction name="actionSave_as"/>
</widget>
@ -97,6 +101,26 @@
<string>Ctrl+O</string>
</property>
</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>
<customwidgets>
<customwidget>
@ -109,5 +133,22 @@
<resources>
<include location="mainwindow.qrc"/>
</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>