Example #1
0
void MainWindow::on_actionOpen_triggered()
{
    if (!promptUnsavedWork()) return;

    QString fileName =
        QFileDialog::getOpenFileName(this,
                                     tr("Open Canvas"),
                                     QDir::currentPath(),
                                     tr("Json (*.json);;All File Types (*.*)"));

    if (fileName.isEmpty()) return;

    QString ext = QFileInfo(fileName).suffix();
    IFileReader *reader = FileReaderFactory::create(ext.toStdString());

    if (reader == nullptr) {
        QMessageBox::critical(this,
                              "Unsupported file type",
                              QString("I can't read *.%1 files.").arg(ext));
        return;
    }

    Group *readCanvas = new Group();
    reader->setup(readCanvas);

    bool canRead = reader->read(fileName.toStdString());
    delete reader;

    if (canRead) {
        m_canvas->setMainGroup(readCanvas);
        ui->statusBar->showMessage(QString("Opened file: \"%1\"").arg(fileName));
    } else {
        delete readCanvas;
        QMessageBox::critical(this,
                              "File Read Error",
                              "I can't read this file ):");
        return;
    }

    setCanvasFile(fileName);

    resetCommandStack();

    m_canvas->repaint();
}