From 68688fb53971252ad0234af0a18d47cf47ecbef4 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 <0xFEEDC0DE64@gmail.com> Date: Thu, 12 Apr 2018 20:23:36 +0200 Subject: [PATCH] Implemented SketchMainWindow --- plugins/sketchplugin/sketchmainwindow.cpp | 59 ++++++++++++++++++++++- plugins/sketchplugin/sketchmainwindow.h | 2 + 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/plugins/sketchplugin/sketchmainwindow.cpp b/plugins/sketchplugin/sketchmainwindow.cpp index c9e23c4..04d057b 100644 --- a/plugins/sketchplugin/sketchmainwindow.cpp +++ b/plugins/sketchplugin/sketchmainwindow.cpp @@ -2,6 +2,15 @@ #include "ui_sketchmainwindow.h" #include +#include +#include +#include +#include + +#include "sketchfile.h" +#include "container/document.h" +#include "container/page.h" +#include "container/msjsonfilereference.h" SketchMainWindow::SketchMainWindow(QWidget *parent) : QMainWindow(parent), @@ -22,5 +31,53 @@ SketchMainWindow::~SketchMainWindow() void SketchMainWindow::openPressed() { - QFileDialog::getOpenFileName(this, tr("Select a sketch file"), QString(), QStringLiteral("%0 (*.sketch)").arg(tr("Sketch file"))); + auto filename = QFileDialog::getOpenFileName(this, tr("Select a sketch file"), QString(), QStringLiteral("%0 (*.sketch)").arg(tr("Sketch file"))); + if(filename.isEmpty()) + return; + + load(filename); +} + +void SketchMainWindow::load(const QString &filename) +{ + while(ui->tabWidget->count()) + { + auto widget = ui->tabWidget->widget(0); + ui->tabWidget->removeTab(0); + widget->deleteLater(); + } + + SketchFile file; + try + { + file.open(filename); + } + catch(const QString &msg) + { + QMessageBox::warning(this, tr("Could not load sketch file!"), tr("Could not load sketch file!") % "\n\n" % msg); + return; + } + + for(auto pageRef : file.document()->pages()) + { + Page *page; + try + { + page = file.loadPage(pageRef->_ref()); + } catch (QString msg) { + ui->tabWidget->addTab(new QLabel(QStringLiteral("Could not parse page: %0").arg(msg), ui->tabWidget), pageRef->_ref()); + continue; + } + + QGraphicsScene *scene; + try + { + scene = file.createScene(page); + } catch (QString msg) { + ui->tabWidget->addTab(new QLabel(QStringLiteral("Could not render page: %0").arg(msg), ui->tabWidget), page->name()); + continue; + } + + ui->tabWidget->addTab(new QGraphicsView(scene, ui->tabWidget), page->name()); + } } diff --git a/plugins/sketchplugin/sketchmainwindow.h b/plugins/sketchplugin/sketchmainwindow.h index d2970a4..f29296b 100644 --- a/plugins/sketchplugin/sketchmainwindow.h +++ b/plugins/sketchplugin/sketchmainwindow.h @@ -19,6 +19,8 @@ private Q_SLOTS: void openPressed(); private: + void load(const QString &filename); + Ui::SketchMainWindow *ui; };