Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
pokedexmodel.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
24#include <pse-db/pokemon.h>
25#include "./pokedexmodel.h"
27#include "../bridge/router.h"
28
30 : name(name),
31 dex(dex),
32 id(id)
33{}
34
38{
39 // Populate cache containing dex entries
40 for(auto el : PokemonDB::inst()->getStore()) {
41 if(!el->pokedex)
42 continue;
43
45 el->readable,
46 *el->pokedex,
47 el->ind
48 ));
49 }
50
51 // Now sort them via Pokedex number
52 dexSort();
53
54 // Connect dex changes
56 connect(this, &PokedexModel::dexSortSelectChanged, this, &PokedexModel::dexSort);
57
58 // When entering the page, it always needs to be in dex order
59 connect(this->router, &Router::closeNonModal, this, &PokedexModel::pageClosing);
60 connect(this->router, &Router::goHome, this, &PokedexModel::pageClosing);
61}
62
63int PokedexModel::rowCount(const QModelIndex& parent) const
64{
65 // Not a tree, just a list, there's no parent
66 Q_UNUSED(parent)
67
68 // Return list count
69 return pokemonDexCount;
70}
71
72QVariant PokedexModel::data(const QModelIndex& index, int role) const
73{
74 // If index is invalid in any way, return nothing
75 if (!index.isValid())
76 return QVariant();
77
78 if (index.row() >= pokemonDexCount)
79 return QVariant();
80
81 // Get Pokemon and return if invalid
82 //auto mon = PokemonDB::inst()->getIndAt("dex" + QString::number(index.row()), nullptr);
83 auto mon = PokemonDB::inst()->getIndAt("dex" + QString::number(dexListCache.at(index.row())->dex));
84 if(mon == nullptr)
85 return QVariant();
86
87 // Now return requested information
88 if (role == IndRole)
89 return *mon->pokedex;
90 else if (role == NameRole) {
91 if(mon->readable != "")
92 return mon->readable;
93
94 return mon->name;
95 }
96 else if(role == StateRole)
97 return pokedex->getState(*mon->pokedex);
98
99 // All else fails, return nothing
100 return QVariant();
101}
102
103QHash<int, QByteArray> PokedexModel::roleNames() const
104{
105 QHash<int, QByteArray> roles;
106
107 roles[IndRole] = "dexInd";
108 roles[NameRole] = "dexName";
109 roles[StateRole] = "dexState";
110
111 return roles;
112}
113
115{
116 auto indFixed = dexToListIndex(ind);
117
118 QAbstractListModel::dataChanged(
119 index(indFixed),
120 index(indFixed)
121 );
122}
123
125{
129
130 dexSortSelectChanged();
131}
132
134{
135 switch (dexSortSelect) {
136 case SortDex:
137 dexSortNum();
138 break;
139
140 case SortName:
141 dexSortName();
142 break;
143
144 case SortInternal:
146 break;
147
148 default:
149 // dexSortSelect is clamped to the Sort* set elsewhere; leave order unchanged
150 // for any unexpected value. (clang-tidy bugprone-switch-missing-default-case.)
151 break;
152 }
153
154 beginResetModel();
155 endResetModel();
156}
157
159{
160 QCollator collator;
161
162 // Setup Collator
163 collator.setNumericMode(true);
164 collator.setIgnorePunctuation(true);
165
166 std::sort(
167 dexListCache.begin(),
168 dexListCache.end(),
169 [&collator](PokedexEntryData* item1, PokedexEntryData* item2)
170 {
171 return collator.compare(item1->name, item2->name) < 0;
172 });
173}
174
176{
177 std::sort(
178 dexListCache.begin(),
179 dexListCache.end(),
180 [](PokedexEntryData* item1, PokedexEntryData* item2) {
181 return item1->dex < item2->dex;
182 });
183}
184
186{
187 std::sort(
188 dexListCache.begin(),
189 dexListCache.end(),
190 [](PokedexEntryData* item1, PokedexEntryData* item2) {
191 return item1->id < item2->id;
192 });
193}
194
196{
198 return;
199
201 dexSort();
202}
203
205{
206 int ret = -1;
207
208 for(int i = 0; i < dexListCache.size(); i++) {
209 if(ind != dexListCache.at(i)->dex)
210 continue;
211
212 ret = i;
213 break;
214 }
215
216 return ret;
217}
The player's Pokedex: a seen flag and an owned flag per species.
void dexItemChanged(int ind)
A single entry ind changed.
virtual int rowCount(const QModelIndex &parent) const override
Row count.
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name.
void pageClosing()
Hook for when the dex page closes.
void dexSortInternal()
Sort by internal id.
void dexSort()
Apply the current sort.
void dexSortCycle()
Advance to the next sort order.
virtual QVariant data(const QModelIndex &index, int role) const override
Row+role value.
QVector< PokedexEntryData * > dexListCache
Cached, currently-sorted rows.
int dexToListIndex(int ind)
Row index for species ind under the current sort.
void dexSortName()
Sort alphabetically.
PokedexModel(PlayerPokedex *pokedex, Router *router)
PlayerPokedex * pokedex
The save's dex.
Router * router
For page open/close hooks.
void dataChanged(int ind)
Notify that dex entry ind changed.
void dexSortNum()
Sort by dex number.
static PokemonDB * inst()
< Number of species.
Definition pokemon.cpp:183
PokemonDBEntry * getIndAt(const QString &key) const
Species by name key (for QML).
Definition pokemon.cpp:199
Screen navigation for the UI – the QML StackView's controller.
Definition router.h:74
constexpr var8 pokemonDexCount
Number of species.
Definition pokemon.h:28
One Pokedex grid row: name, dex number, internal id.
QString name
Species name.
int id
Internal id.
PokedexEntryData(QString name, int dex, int id)
int dex
Pokedex number.