Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
natureselectmodel.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 "./natureselectmodel.h"
26
31
33{
34 // Setup Collator
35 QCollator collator;
36 collator.setNumericMode(true);
37 collator.setIgnorePunctuation(true);
38
39 // Prepare list of natures in correct order
40 QString tmpNatures[] = {
41 "Hardy",
42 "Lonely",
43 "Brave",
44 "Adamant",
45 "Naughty",
46 "Bold",
47 "Docile",
48 "Relaxed",
49 "Impish",
50 "Lax",
51 "Timid",
52 "Hasty",
53 "Serious",
54 "Jolly",
55 "Naive",
56 "Modest",
57 "Mild",
58 "Quiet",
59 "Bashful",
60 "Rash",
61 "Calm",
62 "Gentle",
63 "Sassy",
64 "Careful",
65 "Quirky"
66 };
67
68 // Numerize them automatically
69 for(int i = 0; i < 25; i++) {
70 natureListCache.append(new NatureSelectEntry(tmpNatures[i], i));
71 }
72
73 // Now sort them with their proper ordering preserved from earlier when they
74 // were numerized
75 std::sort(
76 natureListCache.begin(),
77 natureListCache.end(),
78 [&collator](const NatureSelectEntry* item1, const NatureSelectEntry* item2)
79 {
80 return collator.compare(item1->name, item2->name) < 0;
81 });
82}
83
84int NatureSelectModel::rowCount(const QModelIndex& parent) const
85{
86 // Not a tree, just a list, there's no parent
87 Q_UNUSED(parent)
88
89 // Return list count
90 return natureListCache.size();
91}
92
93QVariant NatureSelectModel::data(const QModelIndex& index, int role) const
94{
95 // If index is invalid in any way, return nothing
96 if (!index.isValid())
97 return QVariant();
98
99 if (index.row() >= natureListCache.size())
100 return QVariant();
101
102 // Get Item from Item List Cache
103 auto item = natureListCache.at(index.row());
104
105 if(item == nullptr)
106 return QVariant();
107
108 // Now return requested information
109 if (role == IndRole)
110 return item->ind;
111 else if (role == NameRole)
112 return item->name;
113
114 // All else fails, return nothing
115 return QVariant();
116}
117
118QHash<int, QByteArray> NatureSelectModel::roleNames() const
119{
120 QHash<int, QByteArray> roles;
121
122 roles[IndRole] = "natureInd";
123 roles[NameRole] = "natureName";
124
125 return roles;
126}
127
129{
130 int ret = -1;
131
132 for(int i = 0; i < natureListCache.size(); i++) {
133 if(ind != natureListCache.at(i)->ind)
134 continue;
135
136 ret = i;
137 break;
138 }
139
140 return ret;
141}
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name.
int natureToListIndex(int ind)
Row index for nature ind.
virtual QVariant data(const QModelIndex &index, int role) const override
Row+role value.
virtual int rowCount(const QModelIndex &parent) const override
Row count.
QVector< NatureSelectEntry * > natureListCache
Cached picker rows.
One nature picker row: display name + nature index.
NatureSelectEntry(QString name, int ind)
QString name
Display name.
int ind
Nature index.