Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
pokemonboxselectmodel.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 "
./pokemonboxselectmodel.h
"
23
#include "
./pokemonstoragemodel.h
"
24
#include <
pse-savefile/expanded/storage.h
>
25
#include <
pse-savefile/expanded/player/playerpokemon.h
>
26
#include <
pse-savefile/expanded/fragments/pokemonbox.h
>
27
#include <
pse-savefile/expanded/fragments/pokemonparty.h
>
28
#include <
pse-savefile/expanded/fragments/pokemonstoragebox.h
>
29
30
PokemonBoxSelectModel::PokemonBoxSelectModel
(
PokemonStorageModel
*
pairedModel
)
31
:
pairedModel
(
pairedModel
)
32
{
33
storage
=
pairedModel
->storage;
34
party
=
pairedModel
->party;
35
36
// The box list has to know all the up-to-date information on all the pokemon
37
// boxes because the list that is displayed contains more information than
38
// just a box number
39
40
// Subscribe to all changes in all PokemonStorageBox's
41
for
(
int
i = 0; i < 12; i++) {
42
connect(
storage
->boxAt(i), &
PokemonStorageBox::pokemonChanged
,
this
, &
PokemonBoxSelectModel::onBoxChange
);
43
}
44
45
// Subscribe to all changes in the players party
46
connect(
party
, &
PlayerPokemon::pokemonChanged
,
this
, &
PokemonBoxSelectModel::onBoxChange
);
47
48
// Subscribe to certain changes in storage
49
connect(
storage
, &
Storage::pokemonChanged
,
this
, &
PokemonBoxSelectModel::onBoxChange
);
50
connect(
storage
, &
Storage::curBoxChanged
,
this
, &
PokemonBoxSelectModel::onBoxChange
);
51
connect(
storage
, &
Storage::boxesFormattedChanged
,
this
, &
PokemonBoxSelectModel::onBoxChange
);
52
}
53
54
int
PokemonBoxSelectModel::rowCount
(
const
QModelIndex& parent)
const
55
{
56
// Not a tree, just a list, there's no parent
57
Q_UNUSED(parent)
58
59
// Return list count
60
return
13;
61
}
62
63
QVariant
PokemonBoxSelectModel::data
(
const
QModelIndex& index,
int
role)
const
64
{
65
// If index is invalid in any way, return nothing
66
if
(!index.isValid())
67
return
QVariant();
68
69
if
(index.row() >= 13)
70
return
QVariant();
71
72
// Now return requested information
73
74
// Return the display name decorated accordingly
75
if
(role ==
NameRole
)
76
return
getDecoratedName
(index.row());
77
78
// Return the value, just the row number
79
else
if
(role ==
ValueRole
)
80
return
index.row();
81
82
// Return the value, just the row number
83
else
if
(role ==
IndRole
)
84
return
pairedModel
->curBox;
85
86
// Return whether it's disabled or not
87
// It's disabled
88
// 1) It's the same box as the other side half
89
// 2) The boxes are not formatted and the non-party box row is not the
90
// current non-party box.
91
else
if
(role ==
DisabledRole
) {
92
bool
ret =
false
;
93
94
if
((index.row() - 1) ==
pairedModel
->otherModel->curBox)
95
ret =
true
;
96
else
if
(!
storage
->boxesFormatted && (index.row() - 1) !=
storage
->curBox && index.row() != 0)
97
ret =
true
;
98
99
return
ret;
100
}
101
102
// All else fails, return nothing
103
return
QVariant();
104
}
105
106
QHash<int, QByteArray>
PokemonBoxSelectModel::roleNames
()
const
107
{
108
QHash<int, QByteArray> roles;
109
110
roles[
IndRole
] =
"boxInd"
;
111
roles[
NameRole
] =
"boxName"
;
112
roles[
ValueRole
] =
"boxValue"
;
113
roles[
DisabledRole
] =
"boxDisabled"
;
114
115
return
roles;
116
}
117
118
bool
PokemonBoxSelectModel::setData
(
const
QModelIndex& index,
const
QVariant& value,
int
role)
119
{
120
if
(!index.isValid())
121
return
false
;
122
123
if
(index.row() >= 13)
124
return
false
;
125
126
// Now set requested information
127
if
(role ==
IndRole
) {
128
pairedModel
->switchBox(value.toInt() - 1);
129
return
true
;
130
}
131
132
return
false
;
133
}
134
135
void
PokemonBoxSelectModel::onBoxChange
()
136
{
137
// Ask all the data to re-update
138
dataChanged(index(0), index(12));
139
}
140
141
QString
PokemonBoxSelectModel::getDecoratedName
(
int
box)
const
142
{
143
// First get box in question
144
PokemonStorageBox
* selBox = (box == 0)
145
?
party
146
:
storage
->boxAt(box - 1);
147
148
// Get name of box
149
QString name =
boxSelect
[box];
150
151
// Prepend the current-box marker (points at the label), where the old fill
152
// circle used to sit; otherwise pad with equal-width blank so every row stays
153
// left-aligned. The fill circle is gone -- the (N/Max) count below already
154
// conveys how full a box is, and more precisely than empty/partly/full did.
155
if
(box > 0 &&
storage
->curBox == (box - 1))
156
name =
curBoxSym
+
" "
+ name;
157
else
158
name =
" "
+ name;
159
160
// Next append box count
161
name +=
" ("
+
162
QString::number(selBox->
pokemonCount
()) +
163
"/"
+
164
QString::number(selBox->
pokemonMax
()) +
165
")"
;
166
167
// Return
168
return
name;
169
}
PokemonBoxSelectModel::onBoxChange
void onBoxChange()
React to this selector changing.
Definition
pokemonboxselectmodel.cpp:135
PokemonBoxSelectModel::party
PlayerPokemon * party
The party (box 0).
Definition
pokemonboxselectmodel.h:83
PokemonBoxSelectModel::DisabledRole
@ DisabledRole
Definition
pokemonboxselectmodel.h:45
PokemonBoxSelectModel::IndRole
@ IndRole
Definition
pokemonboxselectmodel.h:46
PokemonBoxSelectModel::ValueRole
@ ValueRole
Definition
pokemonboxselectmodel.h:44
PokemonBoxSelectModel::NameRole
@ NameRole
Definition
pokemonboxselectmodel.h:43
PokemonBoxSelectModel::boxSelect
QString boxSelect[13]
Row labels: Party plus the 12 boxes.
Definition
pokemonboxselectmodel.h:53
PokemonBoxSelectModel::getDecoratedName
QString getDecoratedName(int box) const
Row label with its fill-status symbol.
Definition
pokemonboxselectmodel.cpp:141
PokemonBoxSelectModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override
Selection write-back.
Definition
pokemonboxselectmodel.cpp:118
PokemonBoxSelectModel::roleNames
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name.
Definition
pokemonboxselectmodel.cpp:106
PokemonBoxSelectModel::pairedModel
PokemonStorageModel * pairedModel
The storage model this selector drives (see note).
Definition
pokemonboxselectmodel.h:81
PokemonBoxSelectModel::storage
Storage * storage
The PC storage (for fill status).
Definition
pokemonboxselectmodel.h:82
PokemonBoxSelectModel::rowCount
virtual int rowCount(const QModelIndex &parent) const override
Row count (13).
Definition
pokemonboxselectmodel.cpp:54
PokemonBoxSelectModel::PokemonBoxSelectModel
PokemonBoxSelectModel(PokemonStorageModel *pairedModel)
Definition
pokemonboxselectmodel.cpp:30
PokemonBoxSelectModel::curBoxSym
QString curBoxSym
Decoration: current-box marker (points at the label). Fill status is conveyed by the (N/Max) count.
Definition
pokemonboxselectmodel.h:51
PokemonBoxSelectModel::data
virtual QVariant data(const QModelIndex &index, int role) const override
Row+role value.
Definition
pokemonboxselectmodel.cpp:63
PokemonStorageBox
Holds contents of a single Pokemon storage box.
Definition
pokemonstoragebox.h:49
PokemonStorageBox::pokemonChanged
void pokemonChanged()
Box contents changed.
PokemonStorageBox::pokemonMax
int pokemonMax()
Capacity (maxSize).
Definition
pokemonstoragebox.cpp:64
PokemonStorageBox::pokemonCount
int pokemonCount()
Number of mons present.
Definition
pokemonstoragebox.cpp:59
PokemonStorageModel
Editable list model for a PC box (or the party), with checkbox selection.
Definition
pokemonstoragemodel.h:49
Storage::pokemonChanged
void pokemonChanged()
Storage::curBoxChanged
void curBoxChanged()
Storage::boxesFormattedChanged
void boxesFormattedChanged()
playerpokemon.h
pokemonbox.h
pokemonboxselectmodel.h
pokemonparty.h
pokemonstoragebox.h
pokemonstoragemodel.h
storage.h
projects
app
src
mvc
pokemonboxselectmodel.cpp
Generated by
1.17.0