Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
itemselectmodel.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/itemsdb.h>
27#include "./itemselectmodel.h"
28
33
35{
36 // Setup Collator
37 QCollator collator;
38 collator.setNumericMode(true);
39 collator.setIgnorePunctuation(true);
40
41 // Add first category
42 itemListCache.append(new ItemSelectEntryData("--- Normal Items ---", -1));
43
44 // Gather normal repeatable items and sort by name, then add into list
45 QVector<ItemDBEntry*> tmp;
46
47 for(auto el : ItemsDB::inst()->getStore()) {
48 if(!el->getOnce() && !el->getGlitch())
49 tmp.append(el);
50 }
51
52 std::sort(
53 tmp.begin(),
54 tmp.end(),
55 [&collator](const ItemDBEntry* item1, const ItemDBEntry* item2)
56 {
57 return collator.compare(item1->getReadable(), item2->getReadable()) < 0;
58 });
59
60 for(auto el : tmp) {
61 itemListCache.append(new ItemSelectEntryData(el->getReadable(), el->getInd()));
62 }
63
64 tmp.clear();
65
66 // Add 2nd category
67 itemListCache.append(new ItemSelectEntryData("--- Special Items ---", -1));
68
69 for(auto el : ItemsDB::inst()->getStore()) {
70 if(el->getOnce() && !el->getGlitch())
71 tmp.append(el);
72 }
73
74 std::sort(
75 tmp.begin(),
76 tmp.end(),
77 [&collator](const ItemDBEntry* item1, const ItemDBEntry* item2)
78 {
79 return collator.compare(item1->getReadable(), item2->getReadable()) < 0;
80 });
81
82 for(auto el : tmp) {
83 itemListCache.append(new ItemSelectEntryData(el->getReadable(), el->getInd()));
84 }
85
86 tmp.clear();
87
88 // Add 3rd category
89 itemListCache.append(new ItemSelectEntryData("--- Glitch Items ---", -1));
90
91 for(auto el : ItemsDB::inst()->getStore()) {
92 if(el->getGlitch())
93 tmp.append(el);
94 }
95
96 std::sort(
97 tmp.begin(),
98 tmp.end(),
99 [&collator](const ItemDBEntry* item1, const ItemDBEntry* item2)
100 {
101 return collator.compare(item1->getReadable(), item2->getReadable()) < 0;
102 });
103
104 for(auto el : tmp) {
105 itemListCache.append(new ItemSelectEntryData(el->getReadable(), el->getInd()));
106 }
107
108 tmp.clear();
109}
110
111int ItemSelectModel::rowCount(const QModelIndex& parent) const
112{
113 // Not a tree, just a list, there's no parent
114 Q_UNUSED(parent)
115
116 // Return list count
117 return itemListCache.size();
118}
119
120QVariant ItemSelectModel::data(const QModelIndex& index, int role) const
121{
122 // If index is invalid in any way, return nothing
123 if (!index.isValid())
124 return QVariant();
125
126 if (index.row() >= itemListCache.size())
127 return QVariant();
128
129 // Get Item from Item List Cache
130 auto item = itemListCache.at(index.row());
131
132 if(item == nullptr)
133 return QVariant();
134
135 // Now return requested information
136 if (role == IndRole)
137 return item->ind;
138 else if (role == NameRole)
139 return item->name;
140 else if (role == InfoRole)
141 return infoForInd(item->ind);
142
143 // All else fails, return nothing
144 return QVariant();
145}
146
147QHash<int, QByteArray> ItemSelectModel::roleNames() const
148{
149 QHash<int, QByteArray> roles;
150
151 roles[IndRole] = "itemSelectInd";
152 roles[NameRole] = "itemSelectName";
153 roles[InfoRole] = "itemSelectInfo";
154
155 return roles;
156}
157
158QString ItemSelectModel::infoForInd(int ind) const
159{
160 if(ind < 0)
161 return QString();
162 auto el = ItemsDB::inst()->getIndAt(QString::number(ind));
163 return (el != nullptr) ? el->getInfo() : QString();
164}
165
167{
168 int ret = -1;
169
170 for(int i = 0; i < itemListCache.size(); i++) {
171 if(itemListCache.at(i)->ind == ind) {
172 ret = i;
173 break;
174 }
175 }
176
177 return ret;
178}
QString infoForInd(int ind) const
Detailed-tooltip text for item ind (empty if none).
virtual int rowCount(const QModelIndex &parent) const override
Row count.
int itemToListIndex(int ind)
Row index for item ind.
virtual QVariant data(const QModelIndex &index, int role) const override
Row+role value.
QVector< ItemSelectEntryData * > itemListCache
Cached picker rows.
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name.
ItemDBEntry * getIndAt(const QString val) const
Item by name key (for QML).
Definition itemsdb.cpp:66
static ItemsDB * inst()
< Number of items.
Definition itemsdb.cpp:37
One item's static data: name/flags, pricing, and where it's used.
Definition itemdbentry.h:46
QString getInfo() const
One item picker row: display name + item index.
QString name
Display name.
ItemSelectEntryData(QString name, int ind)