summaryrefslogtreecommitdiffstats
path: root/pokemodr/MapUI.cpp
blob: 749b2bf5668d5bbf7f6ae1ff67c03e2f8434541a (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright 2008 Ben Boeckel <MathStuf@gmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Header include
#include "MapUI.h"

// PokeModr includes
#include "TileDelegate.h"
#include "TilemapModel.h"

// Pokemod includes
#include "../pokemod/Map.h"
#include "../pokemod/MapWarp.h"
#include "../pokemod/Pokemod.h"

// Qt includes
#include <QHeaderView>

// KDE includes
#include <KIcon>

MapUI::MapUI(Map* map, QWidget* parent) :
        ObjectUI(parent),
        m_delegate(new TileDelegate(this))
{
    setupUi(this);
    QMetaObject::connectSlotsByName(this);
    setObjects(map, new Map(*map));
    m_model = new TilemapModel(this, static_cast<Map*>(modified())->map(), static_cast<const Pokemod*>(original()->pokemod()));
    connect(modified(), SIGNAL(error(const QString&)), this, SLOT(setGui()));
    connect(modified(), SIGNAL(error(const QString&)), this, SLOT(errorMessage(const QString&)));
    connect(modified(), SIGNAL(warning(const QString&)), this, SLOT(warningMessage(const QString&)));
    connect(modified(), SIGNAL(changed()), this, SLOT(setChanged()));
    init();
}

MapUI::~MapUI()
{
    delete m_model;
    delete m_delegate;
}

void MapUI::initGui()
{
    varType->addItems(Map::TypeStr);
    varTilemap->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
    varTilemap->verticalHeader()->setResizeMode(QHeaderView::Fixed);
    varTilemap->horizontalHeader()->setDefaultSectionSize(64);
    varTilemap->verticalHeader()->setDefaultSectionSize(64);
    varTilemap->setModel(m_model);
    varTilemap->setItemDelegate(m_delegate);
}

void MapUI::refreshGui()
{
    varFlyWarp->clear();
    for (int i = 0; i < static_cast<Map*>(original())->warpCount(); ++i)
    {
        const MapWarp* warp = static_cast<Map*>(original())->warp(i);
        varFlyWarp->addItem(warp->name());
        varFlyWarp->setItemData(i, warp->id());
    }
}

void MapUI::setGui()
{
    varName->setText(static_cast<Map*>(modified())->name());
    boxFlyWarp->setChecked((static_cast<Map*>(modified())->flyWarp() == INT_MAX) ? Qt::Unchecked : Qt::Checked);
    varFlyWarp->setCurrentIndex(varFlyWarp->findData(static_cast<Map*>(modified())->flyWarp()));
    varType->setCurrentIndex(static_cast<Map*>(modified())->type());
    buttonDeleteColumn->setEnabled(0 < m_model->columnCount());
    buttonDeleteRow->setEnabled(0 < m_model->rowCount());
}

void MapUI::apply()
{
    *static_cast<Map*>(original()) = *static_cast<Map*>(modified());
    emit(changed(false));
}

void MapUI::discard()
{
    *static_cast<Map*>(modified()) = *static_cast<Map*>(original());
    setGui();
    emit(changed(false));
}

void MapUI::on_varName_textChanged(const QString& name)
{
    static_cast<Map*>(modified())->setName(name);
}

void MapUI::on_boxFlyWarp_toggled()
{
    static_cast<Map*>(modified())->setFlyWarp(-1);
}

void MapUI::on_varFlyWarp_currentIndexChanged(const int flyWarp)
{
    static_cast<Map*>(modified())->setFlyWarp(varFlyWarp->itemData(flyWarp).toInt());
}

void MapUI::on_varType_currentIndexChanged(const int type)
{
    static_cast<Map*>(modified())->setType(type);
}

void MapUI::on_buttonAddColumn_pressed()
{
    m_model->addColumn();
    emit(changed(true));
}

void MapUI::on_buttonAddRow_pressed()
{
    m_model->addRow();
    emit(changed(true));
}

void MapUI::on_buttonDeleteColumn_pressed()
{
    m_model->removeColumns(varTilemap->currentIndex().column(), 1);
    emit(changed(true));
}

void MapUI::on_buttonDeleteRow_pressed()
{
    m_model->removeRows(varTilemap->currentIndex().row(), 1);
    emit(changed(true));
}

void MapUI::on_buttonInsertColumn_pressed()
{
    m_model->insertColumns(varTilemap->currentIndex().column(), 1);
    emit(changed(true));
}

void MapUI::on_buttonInsertRow_pressed()
{
    m_model->insertRows(varTilemap->currentIndex().row(), 1);
    emit(changed(true));
}