From ee4e0002025099edd716bbd5b5c4f21b467ac7ab Mon Sep 17 00:00:00 2001 From: Lukáš Lalinský Date: Mon, 20 Jul 2009 10:33:14 +0200 Subject: Open file passed on the command line --- src/diagramdocument.cpp | 10 ++++++++-- src/diagramdocument.h | 2 +- src/main.cpp | 8 +++++++- src/mainwindow.cpp | 20 +++++++++++++++----- src/mainwindow.h | 2 +- 5 files changed, 32 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/diagramdocument.cpp b/src/diagramdocument.cpp index a697bc7..a541d7a 100644 --- a/src/diagramdocument.cpp +++ b/src/diagramdocument.cpp @@ -432,15 +432,20 @@ DiagramDocument::save(const QString &fileName) #include "diagramitemfactory.h" -void +bool DiagramDocument::load(const QString &fileName) { QDomDocument doc; QFile file(fileName); if (file.open(QIODevice::ReadOnly)) { - doc.setContent(&file); + if (!doc.setContent(&file)) { + return false; + } file.close(); } + else { + return false; + } setFileName(fileName); QDomElement root = doc.firstChildElement("diagram"); @@ -460,6 +465,7 @@ DiagramDocument::load(const QString &fileName) } itemElement = itemElement.nextSiblingElement("item"); } + return true; } DiagramItem * diff --git a/src/diagramdocument.h b/src/diagramdocument.h index fa061b1..4a3fd34 100644 --- a/src/diagramdocument.h +++ b/src/diagramdocument.h @@ -72,7 +72,7 @@ public: QList selectedItems(); void save(const QString &fileName); - void load(const QString &fileName); + bool load(const QString &fileName); QString fileName() { return m_fileName; } void setFileName(const QString &fileName) { m_fileName = fileName; } diff --git a/src/main.cpp b/src/main.cpp index 628856c..a76a90c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,7 +37,13 @@ main(int argc, char **argv) translator.load("dbmodel_" + QLocale::system().name(), ":/translations/"); app.installTranslator(&translator); - MainWindow window; + QString fileName; + QStringList args = app.arguments(); + if (args.size() > 1) { + fileName = args.at(1); + } + + MainWindow window(fileName); window.show(); return app.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index eac5f02..7d26984 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -68,14 +68,19 @@ public: ExporterList exporters; }; -MainWindow::MainWindow() +MainWindow::MainWindow(const QString &fileName) : QMainWindow(), d(new MainWindowPrivate), m_model(NULL) { setupUi(); setupActions(); setupToolBar(); setupMenuBar(); - newModel(); + if (fileName.isEmpty()) { + newModel(); + } + else { + loadFile(fileName); + } QIcon icon; icon.addFile(":/icons/16x16/item-table.png", QSize(32, 32)); @@ -448,9 +453,14 @@ void MainWindow::loadFile(const QString &fileName) { DiagramDocument *model = new DiagramDocument(this); - model->load(fileName); - newModel(model); - addRecentFile(fileName); + if (model->load(fileName)) { + newModel(model); + addRecentFile(fileName); + } + else { + QMessageBox::critical(this, tr("Error"), tr("Unknown format.")); + newModel(); + } } void diff --git a/src/mainwindow.h b/src/mainwindow.h index a1b7723..923f6cd 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -33,7 +33,7 @@ class MainWindow: public QMainWindow Q_OBJECT public: - MainWindow(); + MainWindow(const QString &fileName = QString()); ~MainWindow(); QIcon loadIcon(const QString &name); -- cgit v1.2.3-70-g09d2