Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
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>
28#include "./mapselectmodel.h"
29
31 : name(name),
32 ind(ind)
33{}
34
36 : map(map)
37{
38 // Rebuild Map
39 rebuild();
40
41 // Listen for additional re-build events
43}
44
45int 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
54QVariant 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
79QHash<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
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
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
Identity, size, pointers, and edge connections of the current map.
Definition areamap.h:44
protected::void curMapChanged()
AreaMap * map
The live area map this picker targets.
virtual int rowCount(const QModelIndex &parent) const override
Row count.
void rebuild()
Rebuild the cached list.
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name.
int mapToListIndex(int ind)
Row index for map ind.
virtual QVariant data(const QModelIndex &index, int role) const override
Row+role value.
MapSelectModel(AreaMap *map)
QVector< MapSelectEntry * > mapListCache
Cached picker rows.
static MapsDB * inst()
< Number of maps.
Definition mapsdb.cpp:35
MapDBEntry * getIndAt(const QString val) const
Map by name key (for QML).
Definition mapsdb.cpp:155
One map's complete static definition – the root of the MapDBEntry family.
Definition mapdbentry.h:56
One map picker row: display name + map index.
MapSelectEntry(QString name, int ind)
int ind
Map index.
QString name
Display name.