Open first command line argument as file

This commit is contained in:
mandlm 2018-12-02 15:22:44 +01:00
parent 458de36cbc
commit 4fefdc65c5
3 changed files with 61 additions and 29 deletions

View File

@ -4,8 +4,18 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show(); std::unique_ptr<MainWindow> mainWindow;
if (argc < 2)
{
mainWindow = std::make_unique<MainWindow>(nullptr);
}
else
{
mainWindow = std::make_unique<MainWindow>(nullptr, argv[1]);
}
mainWindow->show();
return app.exec(); return app.exec();
} }

View File

@ -1,8 +1,6 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "DataModel.h"
#include <QJsonObject> #include <QJsonObject>
#include <QJsonDocument> #include <QJsonDocument>
#include <QFile> #include <QFile>
@ -21,16 +19,45 @@
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
, m_dataModel(this)
{ {
ui->setupUi(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->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, &QAction::triggered, this, qOverload<>(&MainWindow::saveFile));
connect(ui->actionSave_as, &QAction::triggered, this, &MainWindow::saveFileAs); connect(ui->actionSave_as, &QAction::triggered, this, &MainWindow::saveFileAs);
connect(ui->actionPrint, &QAction::triggered, this, &MainWindow::print); connect(ui->actionPrint, &QAction::triggered, this, &MainWindow::print);
newFile(); connect(&m_dataModel, &DataModel::modelChanged, this, &MainWindow::dataModelChanged);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -42,21 +69,6 @@ void MainWindow::newFile()
{ {
closeFile(); closeFile();
m_dataModel = std::make_unique<DataModel>(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); setWindowModified(false);
setWindowTitle("untitled[*]"); setWindowTitle("untitled[*]");
m_filename = ""; m_filename = "";
@ -72,10 +84,15 @@ void MainWindow::openFile()
return; return;
} }
openFile(filename);
}
void MainWindow::openFile(const QString &filename)
{
closeFile(); closeFile();
std::fstream protoInFile(filename.toStdString(), std::ios::in | std::ios::binary); std::fstream protoInFile(filename.toStdString(), std::ios::in | std::ios::binary);
m_dataModel->read(protoInFile); m_dataModel.read(protoInFile);
setWindowModified(false); setWindowModified(false);
setWindowTitle(filename + "[*]"); setWindowTitle(filename + "[*]");
@ -133,7 +150,7 @@ void MainWindow::closeFile()
void MainWindow::print() const void MainWindow::print() const
{ {
//std::ofstream htmlfile("print.html"); //std::ofstream htmlfile("print.html");
//htmlfile << m_dataModel->toHtml(); //htmlfile << m_dataModel.toHtml();
QPrinter printer; QPrinter printer;
@ -144,7 +161,7 @@ void MainWindow::print() const
} }
QTextDocument printDoc; QTextDocument printDoc;
printDoc.setHtml(QString::fromStdString(m_dataModel->toHtml())); printDoc.setHtml(QString::fromStdString(m_dataModel.toHtml()));
printDoc.print(&printer); printDoc.print(&printer);
} }
@ -164,7 +181,7 @@ void MainWindow::saveFile(const QString &filename)
{ {
std::fstream protoOutFile(filename.toStdString(), std::fstream protoOutFile(filename.toStdString(),
std::ios::out | std::ios::trunc | std::ios::binary); std::ios::out | std::ios::trunc | std::ios::binary);
m_dataModel->write(protoOutFile); m_dataModel.write(protoOutFile);
qDebug() << "Wrote" << filename; qDebug() << "Wrote" << filename;

View File

@ -1,8 +1,10 @@
#pragma once #pragma once
#include <QMainWindow> #include "DataModel.h"
#include <QMainWindow>
#include <QString>
#include <memory>
class DataModel; class DataModel;
class QDataWidgetMapper; class QDataWidgetMapper;
@ -17,17 +19,19 @@ class MainWindow : public QMainWindow
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
std::unique_ptr<DataModel> m_dataModel; DataModel m_dataModel;
QString m_filename; QString m_filename;
bool m_saveOnClose = false; bool m_saveOnClose = false;
public: public:
MainWindow(QWidget *parent = nullptr); MainWindow(QWidget *parent);
MainWindow(QWidget *parent, const QString &filename);
~MainWindow(); ~MainWindow();
public slots: public slots:
void newFile(); void newFile();
void openFile(); void openFile();
void openFile(const QString &filename);
void saveFile(); void saveFile();
void saveFileAs(); void saveFileAs();
void closeFile(); void closeFile();
@ -38,5 +42,6 @@ protected:
void closeEvent(QCloseEvent *event) override; void closeEvent(QCloseEvent *event) override;
private: private:
void setupUi();
void saveFile(const QString &filename); void saveFile(const QString &filename);
}; };