Merge branch 'feature/save-on-exit' into develop

This commit is contained in:
Michael Mandl 2018-09-29 13:42:55 +02:00
commit e3b3ed064b
4 changed files with 92 additions and 6 deletions

View file

@ -64,45 +64,63 @@ void DataModel::read(const QJsonObject &source)
void DataModel::pluralModelChanged() void DataModel::pluralModelChanged()
{ {
m_results.setPluralResult(m_plural.getPoints()); m_results.setPluralResult(m_plural.getPoints());
emit modelChanged();
} }
void DataModel::metaDataChanged() void DataModel::metaDataChanged()
{ {
m_results.setAge(m_metaData.getAge()); m_results.setAge(m_metaData.getAge());
emit modelChanged();
} }
void DataModel::genusModelChanged() void DataModel::genusModelChanged()
{ {
m_results.setGenusResult(m_genus.getPoints()); m_results.setGenusResult(m_genus.getPoints());
emit modelChanged();
} }
void DataModel::verbEndModelChanged() void DataModel::verbEndModelChanged()
{ {
m_results.setVerbEndResult(m_verbEnd.getPoints()); m_results.setVerbEndResult(m_verbEnd.getPoints());
emit modelChanged();
} }
void DataModel::akkusativModelChanged() void DataModel::akkusativModelChanged()
{ {
m_results.setAkkusativResult(m_akkusativ.getPoints()); m_results.setAkkusativResult(m_akkusativ.getPoints());
emit modelChanged();
} }
void DataModel::dativModelChanged() void DataModel::dativModelChanged()
{ {
m_results.setDativResult(m_dativ.getPoints()); m_results.setDativResult(m_dativ.getPoints());
emit modelChanged();
} }
void DataModel::v2SvkModelChanged() void DataModel::v2SvkModelChanged()
{ {
m_results.setV2Result(m_v2Svk.getV2Points()); m_results.setV2Result(m_v2Svk.getV2Points());
m_results.setSvkResult(m_v2Svk.getSvkPoints()); m_results.setSvkResult(m_v2Svk.getSvkPoints());
emit modelChanged();
} }
void DataModel::passivModelChanged() void DataModel::passivModelChanged()
{ {
m_results.setPassivResult(m_passiv.getPoints()); m_results.setPassivResult(m_passiv.getPoints());
emit modelChanged();
} }
void DataModel::genitivModelChanged() void DataModel::genitivModelChanged()
{ {
m_results.setGenitivResult(m_genitiv.getPoints()); m_results.setGenitivResult(m_genitiv.getPoints());
emit modelChanged();
} }

View file

@ -37,6 +37,9 @@ public:
void write(QJsonObject &target) const; void write(QJsonObject &target) const;
void read(const QJsonObject &source); void read(const QJsonObject &source);
signals:
void modelChanged();
private: private:
template <class ModelType> template <class ModelType>
void write( void write(

View file

@ -8,6 +8,9 @@
#include <QFile> #include <QFile>
#include <QFileDialog> #include <QFileDialog>
#include <QDataWidgetMapper> #include <QDataWidgetMapper>
#include <QCloseEvent>
#include <QMessageBox>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
@ -15,10 +18,12 @@ MainWindow::MainWindow(QWidget *parent)
{ {
ui->setupUi(this); ui->setupUi(this);
connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile())); connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newFile);
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile())); connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openFile);
connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(saveFile())); connect(ui->actionSave, &QAction::triggered, this,
connect(ui->actionSave_as, SIGNAL(triggered()), this, SLOT(saveFileAs())); qOverload<>(&MainWindow::saveFile));
connect(
ui->actionSave_as, &QAction::triggered, this, &MainWindow::saveFileAs);
newFile(); newFile();
} }
@ -43,7 +48,13 @@ void MainWindow::newFile()
ui->resultWidget->setModel(&m_dataModel->m_results); ui->resultWidget->setModel(&m_dataModel->m_results);
connect(&*m_dataModel, &DataModel::modelChanged, this,
&MainWindow::dataModelChanged);
setWindowModified(false);
setWindowTitle("untitled[*]");
m_filename = ""; m_filename = "";
m_saveOnClose = false;
} }
void MainWindow::openFile() void MainWindow::openFile()
@ -67,6 +78,9 @@ void MainWindow::openFile()
m_dataModel->read(loadDoc.object()); m_dataModel->read(loadDoc.object());
setWindowModified(false);
setWindowTitle(filename + "[*]");
m_saveOnClose = false;
m_filename = filename; m_filename = filename;
ui->metaDataWidget->toFirst(); ui->metaDataWidget->toFirst();
@ -96,6 +110,49 @@ void MainWindow::saveFileAs()
saveFile(filename); saveFile(filename);
} }
void MainWindow::dataModelChanged()
{
qDebug() << "data model changed";
m_saveOnClose = true;
setWindowModified(true);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (m_saveOnClose == false)
{
event->accept();
return;
}
QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret)
{
case QMessageBox::Save:
saveFile();
if (m_saveOnClose == true)
{
event->ignore();
break;
}
case QMessageBox::Discard:
event->accept();
break;
case QMessageBox::Cancel:
default:
event->ignore();
break;
}
}
void MainWindow::saveFile(const QString &filename) void MainWindow::saveFile(const QString &filename)
{ {
QJsonObject saveData; QJsonObject saveData;
@ -108,5 +165,8 @@ void MainWindow::saveFile(const QString &filename)
saveFile.write(saveDoc.toJson()); saveFile.write(saveDoc.toJson());
saveFile.close(); saveFile.close();
setWindowTitle(filename + "[*]");
setWindowModified(false);
m_filename = filename; m_filename = filename;
m_saveOnClose = false;
} }

View file

@ -19,6 +19,7 @@ private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
std::unique_ptr<DataModel> m_dataModel; std::unique_ptr<DataModel> m_dataModel;
QString m_filename; QString m_filename;
bool m_saveOnClose = false;
public: public:
MainWindow(QWidget *parent = nullptr); MainWindow(QWidget *parent = nullptr);
@ -29,6 +30,10 @@ public slots:
void openFile(); void openFile();
void saveFile(); void saveFile();
void saveFileAs(); void saveFileAs();
void dataModelChanged();
protected:
void closeEvent(QCloseEvent *event) override;
private: private:
void saveFile(const QString &filename); void saveFile(const QString &filename);