tools/drawMapTest/mainwindow.cpp
author nemo
Sun, 03 Jun 2012 23:04:21 -0400
changeset 7174 80480d21e6ed
parent 4477 63a21fac8bf7
permissions -rw-r--r--
Workaround for bug #144. This workaround had occurred to me a while ago, but wasn't sure if placing them unfairly was better than not placing them at all. Argument for not placing at all is people should probably abort the game when they notice it. Argument for placing unfairly is people can still abort, and if we really wanted them to abort, we should probably just have halted launch if all hogs failed to spawn. This way at least play can continue.

#include <QFileDialog>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "drawmapscene.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    scene = new DrawMapScene(this);
    //ui->graphicsView->setScene(scene);
    ui->drawMapWidget->setScene(scene);

    connect(ui->pbUndo, SIGNAL(clicked()), scene, SLOT(undo()));
    connect(scene, SIGNAL(pathChanged()), this, SLOT(scene_pathChanged()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::scene_pathChanged()
{
    QString str = scene->encode().toBase64();
    ui->plainTextEdit->setPlainText(str);
    ui->sbBytes->setValue(str.size());
}

void MainWindow::on_pbSave_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save map"), ".");

    if(!fileName.isEmpty())
    {
        QFile f(fileName);

        f.open(QIODevice::WriteOnly);
        f.write(scene->encode());
    }
}

void MainWindow::on_pbLoad_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open map file"), ".");

    if(!fileName.isEmpty())
    {
        QFile f(fileName);

        f.open(QIODevice::ReadOnly);
        QByteArray data = f.readAll();
        scene->decode(data);
    }
}