Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
speciesselectmodel.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/pokemon.h>
27
32
34{
35 // Setup Collator
36 QCollator collator;
37 collator.setNumericMode(true);
38 collator.setIgnorePunctuation(true);
39
40 // Add first category
41 speciesListCache.append(new SpeciesSelectEntry("--- Pokedex Pokemon ---", -1));
42
43 // Gather normal repeatable items and sort by name, then add into list
44 QVector<PokemonDBEntry*> tmp;
45
46 for(auto el : PokemonDB::inst()->getStore()) {
47 if(el->pokedex)
48 tmp.append(el);
49 }
50
51 std::sort(
52 tmp.begin(),
53 tmp.end(),
54 [&collator](const PokemonDBEntry* mon1, const PokemonDBEntry* mon2)
55 {
56 return collator.compare(mon1->readable, mon2->readable) < 0;
57 });
58
59 for(auto el : tmp) {
60 speciesListCache.append(new SpeciesSelectEntry(el->readable, el->ind));
61 }
62
63 tmp.clear();
64
65 // Add 2nd category
66 speciesListCache.append(new SpeciesSelectEntry("--- Glitch Pokemon ---", -1));
67
68 for(auto el : PokemonDB::inst()->getStore()) {
69 if(!el->pokedex)
70 tmp.append(el);
71 }
72
73 std::sort(
74 tmp.begin(),
75 tmp.end(),
76 [&collator](const PokemonDBEntry* mon1, const PokemonDBEntry* mon2)
77 {
78 return collator.compare(mon1->readable, mon2->readable) < 0;
79 });
80
81 for(auto el : tmp) {
82 speciesListCache.append(new SpeciesSelectEntry(el->readable, el->ind));
83 }
84
85 tmp.clear();
86}
87
88int SpeciesSelectModel::rowCount(const QModelIndex& parent) const
89{
90 // Not a tree, just a list, there's no parent
91 Q_UNUSED(parent)
92
93 // Return list count
94 return speciesListCache.size();
95}
96
97QVariant SpeciesSelectModel::data(const QModelIndex& index, int role) const
98{
99 // If index is invalid in any way, return nothing
100 if (!index.isValid())
101 return QVariant();
102
103 if (index.row() >= speciesListCache.size())
104 return QVariant();
105
106 // Get Item from Item List Cache
107 auto item = speciesListCache.at(index.row());
108
109 if(item == nullptr)
110 return QVariant();
111
112 // Now return requested information
113 if (role == IndRole)
114 return item->ind;
115 else if (role == NameRole)
116 return item->name;
117
118 // All else fails, return nothing
119 return QVariant();
120}
121
122QHash<int, QByteArray> SpeciesSelectModel::roleNames() const
123{
124 QHash<int, QByteArray> roles;
125
126 roles[IndRole] = "speciesInd";
127 roles[NameRole] = "speciesName";
128
129 return roles;
130}
131
133{
134 int ret = -1;
135
136 for(int i = 0; i < speciesListCache.size(); i++) {
137 if(ind != speciesListCache.at(i)->ind)
138 continue;
139
140 ret = i;
141 break;
142 }
143
144 return ret;
145}
static PokemonDB * inst()
< Number of species.
Definition pokemon.cpp:183
virtual QVariant data(const QModelIndex &index, int role) const override
Value for a row + role.
virtual int rowCount(const QModelIndex &parent) const override
Number of species rows.
QVector< SpeciesSelectEntry * > speciesListCache
Cached picker rows.
SpeciesSelectModel()
Build the cache from the species DB.
int speciesToListIndex(int ind)
Row index for species ind (for combo highlighting).
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name map.
One species' complete static data – the richest entry in the db layer.
Definition pokemon.h:98
One species picker row: display name + species index.
QString name
Display name.
int ind
Species index.
SpeciesSelectEntry(QString name, int ind)