Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
mapselectmodel.cpp
Go to the documentation of this file.
1
/*
2
* Copyright 2020 Twilight
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
21
22
#include <algorithm>
23
#include <QCollator>
24
25
#include <
pse-db/mapsdb.h
>
26
#include <
pse-db/entries/mapdbentry.h
>
27
#include <
pse-savefile/expanded/area/areamap.h
>
28
#include "
./mapselectmodel.h
"
29
30
MapSelectEntry::MapSelectEntry
(QString
name
,
int
ind
)
31
:
name
(
name
),
32
ind
(
ind
)
33
{}
34
35
MapSelectModel::MapSelectModel
(
AreaMap
*
map
)
36
:
map
(
map
)
37
{
38
// Rebuild Map
39
rebuild
();
40
41
// Listen for additional re-build events
42
connect(
map
, &
AreaMap::curMapChanged
,
this
, &
MapSelectModel::rebuild
);
43
}
44
45
int
MapSelectModel::rowCount
(
const
QModelIndex& parent)
const
46
{
47
// Not a tree, just a list, there's no parent
48
Q_UNUSED(parent)
49
50
// Return list count
51
return
mapListCache
.size();
52
}
53
54
QVariant
MapSelectModel::data
(
const
QModelIndex& index,
int
role)
const
55
{
56
// If index is invalid in any way, return nothing
57
if
(!index.isValid())
58
return
QVariant();
59
60
if
(index.row() >=
mapListCache
.size())
61
return
QVariant();
62
63
// Get Item from Item List Cache
64
auto
item =
mapListCache
.at(index.row());
65
66
if
(item ==
nullptr
)
67
return
QVariant();
68
69
// Now return requested information
70
if
(role ==
IndRole
)
71
return
item->ind;
72
else
if
(role ==
NameRole
)
73
return
item->name;
74
75
// All else fails, return nothing
76
return
QVariant();
77
}
78
79
QHash<int, QByteArray>
MapSelectModel::roleNames
()
const
80
{
81
QHash<int, QByteArray> roles;
82
83
roles[
IndRole
] =
"mapInd"
;
84
roles[
NameRole
] =
"mapName"
;
85
86
return
roles;
87
}
88
89
int
MapSelectModel::mapToListIndex
(
int
ind)
90
{
91
int
ret = -1;
92
93
for
(
int
i = 0; i <
mapListCache
.size(); i++) {
94
if
(ind !=
mapListCache
.at(i)->ind)
95
continue
;
96
97
ret = i;
98
break
;
99
}
100
101
return
ret;
102
}
103
104
void
MapSelectModel::rebuild
()
105
{
106
for
(
auto
el :
mapListCache
)
107
delete
el;
108
mapListCache
.clear();
109
110
// Setup Collator
111
QCollator collator;
112
collator.setNumericMode(
true
);
113
collator.setIgnorePunctuation(
true
);
114
115
// To prevent re-listing duplicate maps
116
QVector<MapDBEntry*> usedMaps;
117
118
// Add current map if there is one
119
auto
curMapData =
map
->toCurMap();
120
121
if
(curMapData !=
nullptr
) {
122
mapListCache
.append(
new
MapSelectEntry
(
"--- Current Map ---"
, -1));
123
mapListCache
.append(
new
MapSelectEntry
(curMapData->bestName(), curMapData->getInd()));
124
usedMaps.append(curMapData);
125
}
126
127
mapListCache
.append(
new
MapSelectEntry
(
"--- Normal Maps ---"
, -1));
128
129
// Add Daycare first given it's not the current map
130
// Daycare is a potential frequently used map
131
auto
dayCareMap =
MapsDB::inst
()->
getIndAt
(
"Daycare"
);
132
133
// Last map needs to go at the end, it's a special map
134
auto
lastMap =
MapsDB::inst
()->
getIndAt
(
"Last Map"
);
135
136
if
(curMapData != dayCareMap) {
137
mapListCache
.append(
new
MapSelectEntry
(dayCareMap->bestName(), dayCareMap->getInd()));
138
usedMaps.append(dayCareMap);
139
}
140
141
// Now add in rest
142
143
// Gather normal repeatable items and sort by name, then add into list
144
QVector<MapDBEntry*> tmp;
145
146
for
(
auto
el :
MapsDB::inst
()->getStore()) {
147
if
(!el->getGlitch() && !el->getSpecial() && el->getIncomplete() ==
""
&& !usedMaps.contains(el) && el != lastMap) {
148
tmp.append(el);
149
usedMaps.append(el);
150
}
151
}
152
153
std::sort(
154
tmp.begin(),
155
tmp.end(),
156
[&collator](
MapDBEntry
* mon1,
MapDBEntry
* mon2)
157
{
158
return collator.compare(mon1->bestName(), mon2->bestName()) < 0;
159
});
160
161
for
(
auto
el : tmp) {
162
mapListCache
.append(
new
MapSelectEntry
(el->bestName(), el->getInd()));
163
}
164
165
tmp.clear();
166
167
// Add in Last Map right before glitch maps
168
mapListCache
.append(
new
MapSelectEntry
(lastMap->bestName(), lastMap->getInd()));
169
usedMaps.append(lastMap);
170
171
// Add in all other maps
172
mapListCache
.append(
new
MapSelectEntry
(
"--- Glitch Maps ---"
, -1));
173
174
for
(
auto
el :
MapsDB::inst
()->getStore()) {
175
if
(!usedMaps.contains(el))
176
tmp.append(el);
177
}
178
179
std::sort(
180
tmp.begin(),
181
tmp.end(),
182
[&collator](
MapDBEntry
* mon1,
MapDBEntry
* mon2)
183
{
184
return collator.compare(mon1->bestName(), mon2->bestName()) < 0;
185
});
186
187
for
(
auto
el : tmp) {
188
189
// Get best name and append the incomplete map of name if there is one
190
QString name = el->bestName();
191
if
(el->getIncomplete() !=
""
)
192
name +=
" ("
+ el->getIncomplete() +
")"
;
193
194
mapListCache
.append(
new
MapSelectEntry
(el->bestName(), el->getInd()));
195
}
196
}
197
areamap.h
AreaMap
Identity, size, pointers, and edge connections of the current map.
Definition
areamap.h:44
AreaMap::curMapChanged
protected::void curMapChanged()
MapSelectModel::map
AreaMap * map
The live area map this picker targets.
Definition
mapselectmodel.h:64
MapSelectModel::rowCount
virtual int rowCount(const QModelIndex &parent) const override
Row count.
Definition
mapselectmodel.cpp:45
MapSelectModel::rebuild
void rebuild()
Rebuild the cached list.
Definition
mapselectmodel.cpp:104
MapSelectModel::NameRole
@ NameRole
Definition
mapselectmodel.h:47
MapSelectModel::IndRole
@ IndRole
Definition
mapselectmodel.h:46
MapSelectModel::roleNames
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name.
Definition
mapselectmodel.cpp:79
MapSelectModel::mapToListIndex
int mapToListIndex(int ind)
Row index for map ind.
Definition
mapselectmodel.cpp:89
MapSelectModel::data
virtual QVariant data(const QModelIndex &index, int role) const override
Row+role value.
Definition
mapselectmodel.cpp:54
MapSelectModel::MapSelectModel
MapSelectModel(AreaMap *map)
Definition
mapselectmodel.cpp:35
MapSelectModel::mapListCache
QVector< MapSelectEntry * > mapListCache
Cached picker rows.
Definition
mapselectmodel.h:56
MapsDB::inst
static MapsDB * inst()
< Number of maps.
Definition
mapsdb.cpp:35
MapsDB::getIndAt
MapDBEntry * getIndAt(const QString val) const
Map by name key (for QML).
Definition
mapsdb.cpp:155
mapdbentry.h
mapsdb.h
mapselectmodel.h
MapDBEntry
One map's complete static definition – the root of the MapDBEntry family.
Definition
mapdbentry.h:56
MapSelectEntry
One map picker row: display name + map index.
Definition
mapselectmodel.h:25
MapSelectEntry::MapSelectEntry
MapSelectEntry(QString name, int ind)
Definition
mapselectmodel.cpp:30
MapSelectEntry::ind
int ind
Map index.
Definition
mapselectmodel.h:29
MapSelectEntry::name
QString name
Display name.
Definition
mapselectmodel.h:28
projects
app
src
mvc
mapselectmodel.cpp
Generated by
1.17.0