Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 130 lines (112 sloc) 3.807 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#include <QtGui>
#include "MapExploration.h"
#include "Visualisation.h"
#include "editor/MapEditor.h"

MapExploration::MapExploration(int vwidth_, int vheight_, QWidget *parent):
    QWidget(parent),
    name("Map exploration"),
    mapEditor(NULL),
    visualisation(NULL),
    loadMapBtn(new QPushButton("Load...")),
    reloadMapBtn(new QPushButton("Reload map")),
    startMapEditorBtn(new QPushButton("Edit map")),
    pauseVisualisationBtn(new QPushButton("Pause visualisation")),
    toggleManualControlBtn(new QPushButton("Toggle manual control")),
    vwidth(vwidth_), vheight(vheight_)
{
    setWindowTitle(name + " - " + "Empty map");


    connect(loadMapBtn, SIGNAL(clicked()), this, SLOT(loadFromFile()));
    connect(reloadMapBtn, SIGNAL(clicked()), this, SLOT(reloadMap()));
    connect(startMapEditorBtn, SIGNAL(clicked()), this, SLOT(editMap()));

    QVBoxLayout *mapControls = new QVBoxLayout();
    mapControls->addWidget(loadMapBtn);
    mapControls->addWidget(reloadMapBtn);
    mapControls->addWidget(startMapEditorBtn);
    mapControls->addStretch(1);

    QHBoxLayout *visControls = new QHBoxLayout();
    visControls->addWidget(pauseVisualisationBtn);
    visControls->addWidget(toggleManualControlBtn);
    visControls->addStretch(1);

    mainLayout = new QGridLayout();
    setVisualisation(new Visualisation(vwidth, vheight, QVector<QVector<QPointF> > ()));
    mainLayout->addLayout(visControls, 1, 0);
    mainLayout->addLayout(mapControls, 0, 1);
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);

    setLayout(mainLayout);
}

void MapExploration::closeEvent(QCloseEvent *)
{
    delete mapEditor;
}

void MapExploration::unBlockEditMap()
{
    startMapEditorBtn->setEnabled(true);
    mapEditor = NULL;
}

void MapExploration::editMap()
{
    if (mapEditor != NULL)
    {
        return;
    }
#ifdef DEBUG
    qDebug() << "Creating a MapEditor window..." << endl;
#endif
    mapEditor = new MapEditor(vwidth, vheight, curMap);
    startMapEditorBtn->setDisabled(true);
    connect(mapEditor, SIGNAL(gonnaDie()), this, SLOT(unBlockEditMap()));
    mapEditor->show();
}

namespace{

QVector<QVector<QPointF> > getMapFromFile(const QString &fileName)
{
    QFile file(fileName);
    file.open(QIODevice::ReadOnly);
    QDataStream in(&file);
    QVector<QVector<QPointF> > m;
    in >> m;
    file.close();
    return m;
}

}

void MapExploration::loadFromFile()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    "Open map",
                                                    "",
                                                    "Map files (*.map);;All files (*)");

    if (fileName.isEmpty())
        return;
    else
    {
        curMap = fileName;
        setWindowTitle(name + " - " + fileName);
        QVector<QVector<QPointF> > m = getMapFromFile(fileName);
#ifdef DEBUG
        qDebug() << "File " + fileName + " has been loaded: " << endl << m << endl;
#endif
        setVisualisation(new Visualisation(vwidth, vheight, m));
    }
}

void MapExploration::reloadMap()
{
    if (curMap.isEmpty())
        return;
    QVector<QVector<QPointF> > m = getMapFromFile(curMap);
    setVisualisation(new Visualisation(vwidth, vheight, m));
#ifdef DEBUG
    qDebug() << "File " + curMap + " has been reloaded: " << endl << m << endl;
#endif
}

void MapExploration::setVisualisation(Visualisation *newvis)
{
    if (visualisation != NULL)
    {
        mainLayout->removeWidget(visualisation);
        delete visualisation;
    }
    visualisation = newvis;
    mainLayout->addWidget(visualisation, 0, 0);
    connect(pauseVisualisationBtn, SIGNAL(clicked()), visualisation, SLOT(togglePause()));
    connect(toggleManualControlBtn, SIGNAL(clicked()), visualisation, SLOT(toggleManualControl()));
}
Something went wrong with that request. Please try again.