2018-05-10 16:41:14 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "ui_mainwindow.h"
|
|
|
|
|
2018-05-16 05:02:12 +00:00
|
|
|
#include "DataModel.h"
|
|
|
|
|
2018-05-15 11:11:40 +00:00
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileDialog>
|
2018-05-15 12:24:15 +00:00
|
|
|
#include <QDataWidgetMapper>
|
2018-09-29 11:31:23 +00:00
|
|
|
#include <QCloseEvent>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QDebug>
|
2018-05-15 11:11:40 +00:00
|
|
|
|
2018-05-10 16:41:14 +00:00
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
2018-05-18 17:51:48 +00:00
|
|
|
: QMainWindow(parent)
|
|
|
|
, ui(new Ui::MainWindow)
|
2018-05-10 16:41:14 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2018-05-15 11:11:40 +00:00
|
|
|
|
2018-09-29 11:42:38 +00:00
|
|
|
connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newFile);
|
|
|
|
connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openFile);
|
|
|
|
connect(ui->actionSave, &QAction::triggered, this,
|
|
|
|
qOverload<>(&MainWindow::saveFile));
|
|
|
|
connect(
|
|
|
|
ui->actionSave_as, &QAction::triggered, this, &MainWindow::saveFileAs);
|
2018-05-15 12:24:15 +00:00
|
|
|
|
2018-05-20 10:04:21 +00:00
|
|
|
newFile();
|
2018-05-10 16:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2018-05-20 10:04:21 +00:00
|
|
|
void MainWindow::newFile()
|
2018-05-15 11:11:40 +00:00
|
|
|
{
|
2018-09-30 09:38:30 +00:00
|
|
|
closeFile();
|
|
|
|
|
2018-05-20 10:04:21 +00:00
|
|
|
m_dataModel = std::make_unique<DataModel>(this);
|
|
|
|
ui->metaDataWidget->setModel(&m_dataModel->m_metaData);
|
2018-05-23 16:43:57 +00:00
|
|
|
ui->verbEndWidget->setModel(&m_dataModel->m_verbEnd);
|
2018-06-25 21:04:32 +00:00
|
|
|
ui->genusWidget->setModel(&m_dataModel->m_genus);
|
2018-06-04 16:48:58 +00:00
|
|
|
ui->pluralWidget->setModel(&m_dataModel->m_plural);
|
2018-06-17 16:44:54 +00:00
|
|
|
ui->akkusativDativWidget->setAkkusativModel(&m_dataModel->m_akkusativ);
|
|
|
|
ui->akkusativDativWidget->setDativModel(&m_dataModel->m_dativ);
|
2018-06-25 21:04:32 +00:00
|
|
|
ui->v2SvkWidget->setV2SvkModel(&m_dataModel->m_v2Svk);
|
2018-09-29 11:31:23 +00:00
|
|
|
ui->lateSkillsWidget->setPassivModel(&m_dataModel->m_passiv);
|
|
|
|
ui->lateSkillsWidget->setGenitivModel(&m_dataModel->m_genitiv);
|
2018-06-17 16:44:54 +00:00
|
|
|
|
2018-06-06 16:05:30 +00:00
|
|
|
ui->resultWidget->setModel(&m_dataModel->m_results);
|
2018-05-24 09:54:24 +00:00
|
|
|
|
2018-09-29 11:42:38 +00:00
|
|
|
connect(&*m_dataModel, &DataModel::modelChanged, this,
|
|
|
|
&MainWindow::dataModelChanged);
|
2018-09-29 11:31:23 +00:00
|
|
|
|
|
|
|
setWindowModified(false);
|
|
|
|
setWindowTitle("untitled[*]");
|
2018-06-25 21:04:32 +00:00
|
|
|
m_filename = "";
|
2018-09-29 11:31:23 +00:00
|
|
|
m_saveOnClose = false;
|
2018-05-15 11:11:40 +00:00
|
|
|
}
|
2018-05-16 05:02:12 +00:00
|
|
|
|
2018-05-20 10:04:21 +00:00
|
|
|
void MainWindow::openFile()
|
2018-05-16 05:02:12 +00:00
|
|
|
{
|
2018-06-25 21:04:32 +00:00
|
|
|
QString filename = QFileDialog::getOpenFileName(
|
|
|
|
this, "Open file", "", "ESGRAF 4-8 (*.esgraf48)");
|
2018-05-16 05:02:12 +00:00
|
|
|
if (filename.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-30 09:38:30 +00:00
|
|
|
closeFile();
|
|
|
|
|
2018-05-16 05:02:12 +00:00
|
|
|
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());
|
|
|
|
|
2018-09-29 11:31:23 +00:00
|
|
|
setWindowModified(false);
|
|
|
|
setWindowTitle(filename + "[*]");
|
|
|
|
m_saveOnClose = false;
|
2018-06-25 21:04:32 +00:00
|
|
|
m_filename = filename;
|
2018-05-24 09:54:24 +00:00
|
|
|
|
2018-06-25 21:04:32 +00:00
|
|
|
ui->metaDataWidget->toFirst();
|
2018-05-24 09:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::saveFile()
|
|
|
|
{
|
2018-06-25 21:04:32 +00:00
|
|
|
if (m_filename.isEmpty())
|
|
|
|
{
|
|
|
|
saveFileAs();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
saveFile(m_filename);
|
|
|
|
}
|
2018-05-16 05:02:12 +00:00
|
|
|
}
|
2018-05-20 10:04:21 +00:00
|
|
|
|
|
|
|
void MainWindow::saveFileAs()
|
|
|
|
{
|
2018-06-25 21:04:32 +00:00
|
|
|
QString filename = QFileDialog::getSaveFileName(
|
|
|
|
this, "Save file", "", "ESGRAF 4-8 (*.esgraf48)");
|
2018-05-20 10:04:21 +00:00
|
|
|
if (filename.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-25 21:04:32 +00:00
|
|
|
saveFile(filename);
|
2018-05-24 09:54:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 09:38:30 +00:00
|
|
|
void MainWindow::closeFile()
|
2018-09-29 11:31:23 +00:00
|
|
|
{
|
2018-09-30 09:38:30 +00:00
|
|
|
if (m_saveOnClose == true)
|
|
|
|
{
|
|
|
|
QMessageBox msgBox;
|
|
|
|
msgBox.setText("The document has been modified.");
|
|
|
|
msgBox.setInformativeText("Do you want to save your changes?");
|
|
|
|
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
|
|
|
|
msgBox.setDefaultButton(QMessageBox::Save);
|
|
|
|
|
|
|
|
if (msgBox.exec() == QMessageBox::Save)
|
|
|
|
{
|
|
|
|
saveFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-29 11:31:23 +00:00
|
|
|
|
2018-09-30 09:38:30 +00:00
|
|
|
void MainWindow::dataModelChanged()
|
|
|
|
{
|
2018-09-29 11:31:23 +00:00
|
|
|
m_saveOnClose = true;
|
|
|
|
setWindowModified(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::closeEvent(QCloseEvent *event)
|
|
|
|
{
|
2018-09-30 09:38:30 +00:00
|
|
|
closeFile();
|
2018-09-29 11:31:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 09:54:24 +00:00
|
|
|
void MainWindow::saveFile(const QString &filename)
|
|
|
|
{
|
2018-06-25 21:04:32 +00:00
|
|
|
QJsonObject saveData;
|
|
|
|
m_dataModel->write(saveData);
|
2018-05-24 09:54:24 +00:00
|
|
|
|
2018-06-25 21:04:32 +00:00
|
|
|
QJsonDocument saveDoc(saveData);
|
2018-05-20 10:04:21 +00:00
|
|
|
|
2018-06-25 21:04:32 +00:00
|
|
|
QFile saveFile(filename);
|
|
|
|
saveFile.open(QFile::WriteOnly);
|
|
|
|
saveFile.write(saveDoc.toJson());
|
|
|
|
saveFile.close();
|
2018-05-20 10:04:21 +00:00
|
|
|
|
2018-09-30 09:38:30 +00:00
|
|
|
qDebug() << "Wrote" << filename;
|
|
|
|
|
2018-09-29 11:31:23 +00:00
|
|
|
setWindowTitle(filename + "[*]");
|
|
|
|
setWindowModified(false);
|
2018-06-25 21:04:32 +00:00
|
|
|
m_filename = filename;
|
2018-09-29 11:31:23 +00:00
|
|
|
m_saveOnClose = false;
|
2018-05-20 10:04:21 +00:00
|
|
|
}
|