Open first command line argument as file

This commit is contained in:
Michael Mandl 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)
{
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();
}

View file

@ -1,8 +1,6 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "DataModel.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
@ -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<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);
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;

View file

@ -1,8 +1,10 @@
#pragma once
#include <QMainWindow>
#include "DataModel.h"
#include <QMainWindow>
#include <QString>
#include <memory>
class DataModel;
class QDataWidgetMapper;
@ -17,17 +19,19 @@ class MainWindow : public QMainWindow
private:
Ui::MainWindow *ui;
std::unique_ptr<DataModel> 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);
};