From 4fefdc65c5f7df1168c71255f3019aeebef1b7e2 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sun, 2 Dec 2018 15:22:44 +0100 Subject: [PATCH] Open first command line argument as file --- source/LogoTest.cpp | 14 ++++++++-- source/mainwindow.cpp | 63 +++++++++++++++++++++++++++---------------- source/mainwindow.h | 13 ++++++--- 3 files changed, 61 insertions(+), 29 deletions(-) diff --git a/source/LogoTest.cpp b/source/LogoTest.cpp index 30539a1..1f15804 100644 --- a/source/LogoTest.cpp +++ b/source/LogoTest.cpp @@ -4,8 +4,18 @@ int main(int argc, char **argv) { QApplication app(argc, argv); - MainWindow mainWindow; - mainWindow.show(); + + std::unique_ptr mainWindow; + if (argc < 2) + { + mainWindow = std::make_unique(nullptr); + } + else + { + mainWindow = std::make_unique(nullptr, argv[1]); + } + + mainWindow->show(); return app.exec(); } diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index 20d877a..cf307db 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -1,8 +1,6 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -#include "DataModel.h" - #include #include #include @@ -21,16 +19,45 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) + , m_dataModel(this) { ui->setupUi(this); + setupUi(); + + newFile(); +} + +MainWindow::MainWindow(QWidget *parent, const QString &filename) + : QMainWindow(parent) + , ui(new Ui::MainWindow) + , m_dataModel(this) +{ + ui->setupUi(this); + setupUi(); + + openFile(filename); +} + +void MainWindow::setupUi() +{ + ui->metaDataWidget->setModel(&m_dataModel.m_metaData); + ui->verbEndWidget->setModel(&m_dataModel.m_verbEnd); + ui->genusWidget->setModel(&m_dataModel.m_genus); + ui->pluralWidget->setModel(&m_dataModel.m_plural); + ui->akkusativDativWidget->setAkkusativModel(&m_dataModel.m_akkusativ); + ui->akkusativDativWidget->setDativModel(&m_dataModel.m_dativ); + ui->v2SvkWidget->setV2SvkModel(&m_dataModel.m_v2Svk); + ui->lateSkillsWidget->setPassivModel(&m_dataModel.m_passiv); + ui->lateSkillsWidget->setGenitivModel(&m_dataModel.m_genitiv); + ui->resultWidget->setModel(&m_dataModel.m_results); connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newFile); - connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openFile); + connect(ui->actionOpen, &QAction::triggered, this, qOverload<>(&MainWindow::openFile)); connect(ui->actionSave, &QAction::triggered, this, qOverload<>(&MainWindow::saveFile)); connect(ui->actionSave_as, &QAction::triggered, this, &MainWindow::saveFileAs); connect(ui->actionPrint, &QAction::triggered, this, &MainWindow::print); - newFile(); + connect(&m_dataModel, &DataModel::modelChanged, this, &MainWindow::dataModelChanged); } MainWindow::~MainWindow() @@ -42,21 +69,6 @@ void MainWindow::newFile() { closeFile(); - m_dataModel = std::make_unique(this); - ui->metaDataWidget->setModel(&m_dataModel->m_metaData); - ui->verbEndWidget->setModel(&m_dataModel->m_verbEnd); - ui->genusWidget->setModel(&m_dataModel->m_genus); - ui->pluralWidget->setModel(&m_dataModel->m_plural); - ui->akkusativDativWidget->setAkkusativModel(&m_dataModel->m_akkusativ); - ui->akkusativDativWidget->setDativModel(&m_dataModel->m_dativ); - ui->v2SvkWidget->setV2SvkModel(&m_dataModel->m_v2Svk); - ui->lateSkillsWidget->setPassivModel(&m_dataModel->m_passiv); - ui->lateSkillsWidget->setGenitivModel(&m_dataModel->m_genitiv); - - ui->resultWidget->setModel(&m_dataModel->m_results); - - connect(&*m_dataModel, &DataModel::modelChanged, this, &MainWindow::dataModelChanged); - setWindowModified(false); setWindowTitle("untitled[*]"); m_filename = ""; @@ -72,10 +84,15 @@ void MainWindow::openFile() return; } + openFile(filename); +} + +void MainWindow::openFile(const QString &filename) +{ closeFile(); std::fstream protoInFile(filename.toStdString(), std::ios::in | std::ios::binary); - m_dataModel->read(protoInFile); + m_dataModel.read(protoInFile); setWindowModified(false); setWindowTitle(filename + "[*]"); @@ -133,7 +150,7 @@ void MainWindow::closeFile() void MainWindow::print() const { //std::ofstream htmlfile("print.html"); - //htmlfile << m_dataModel->toHtml(); + //htmlfile << m_dataModel.toHtml(); QPrinter printer; @@ -144,7 +161,7 @@ void MainWindow::print() const } QTextDocument printDoc; - printDoc.setHtml(QString::fromStdString(m_dataModel->toHtml())); + printDoc.setHtml(QString::fromStdString(m_dataModel.toHtml())); printDoc.print(&printer); } @@ -164,7 +181,7 @@ void MainWindow::saveFile(const QString &filename) { std::fstream protoOutFile(filename.toStdString(), std::ios::out | std::ios::trunc | std::ios::binary); - m_dataModel->write(protoOutFile); + m_dataModel.write(protoOutFile); qDebug() << "Wrote" << filename; diff --git a/source/mainwindow.h b/source/mainwindow.h index 78fa2bb..c2cf130 100644 --- a/source/mainwindow.h +++ b/source/mainwindow.h @@ -1,8 +1,10 @@ #pragma once -#include +#include "DataModel.h" + +#include +#include -#include class DataModel; class QDataWidgetMapper; @@ -17,17 +19,19 @@ class MainWindow : public QMainWindow private: Ui::MainWindow *ui; - std::unique_ptr m_dataModel; + DataModel m_dataModel; QString m_filename; bool m_saveOnClose = false; public: - MainWindow(QWidget *parent = nullptr); + MainWindow(QWidget *parent); + MainWindow(QWidget *parent, const QString &filename); ~MainWindow(); public slots: void newFile(); void openFile(); + void openFile(const QString &filename); void saveFile(); void saveFileAs(); void closeFile(); @@ -38,5 +42,6 @@ protected: void closeEvent(QCloseEvent *event) override; private: + void setupUi(); void saveFile(const QString &filename); };