Skip to content
Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
itemexchangemodel.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 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#pragma once
17#include <QObject>
18#include <QString>
19#include <QHash>
20#include <QVariantList>
21
22class ItemStorageBox;
23class PlayerBasics;
24
50class ItemExchangeModel : public QObject
51{
52 Q_OBJECT
53
54 // The two selected item indices (-1 = none). The dropdowns write these.
55 Q_PROPERTY(int itemAInd READ itemAInd WRITE setItemAInd NOTIFY selectionChanged)
56 Q_PROPERTY(int itemBInd READ itemBInd WRITE setItemBInd NOTIFY selectionChanged)
57
58 Q_PROPERTY(bool valid READ valid NOTIFY changed)
59 Q_PROPERTY(int net READ net NOTIFY changed)
60
61 Q_PROPERTY(QString aName READ aName NOTIFY changed)
62 Q_PROPERTY(QString bName READ bName NOTIFY changed)
63 Q_PROPERTY(int aBuy READ aBuy NOTIFY changed)
64 Q_PROPERTY(int bBuy READ bBuy NOTIFY changed)
65
66 Q_PROPERTY(int aStart READ aStart NOTIFY changed)
67 Q_PROPERTY(int aAfter READ aAfter NOTIFY changed)
68 Q_PROPERTY(int bStart READ bStart NOTIFY changed)
69 Q_PROPERTY(int bAfter READ bAfter NOTIFY changed)
70 Q_PROPERTY(int moneyStart READ moneyStart NOTIFY changed)
71 Q_PROPERTY(int moneyAfter READ moneyAfter NOTIFY changed)
72
73 Q_PROPERTY(bool canAddA READ canAddA NOTIFY changed)
74 Q_PROPERTY(bool canAddB READ canAddB NOTIFY changed)
75
76
79 Q_PROPERTY(int revision READ revision NOTIFY changed)
80
81public:
82 static constexpr int MoneyCap = 999999;
83 static constexpr int StackCap = 99;
84
86
87 int itemAInd() const { return m_itemAInd; }
88 int itemBInd() const { return m_itemBInd; }
89 void setItemAInd(int v);
90 void setItemBInd(int v);
91
92 int net() const { return m_net; }
93 int revision() const { return m_revision; }
94 bool valid() const;
95
96 QString aName() const { return readableOf(m_itemAInd); }
97 QString bName() const { return readableOf(m_itemBInd); }
98 int aBuy() const { return buyOf(m_itemAInd); }
99 int bBuy() const { return buyOf(m_itemBInd); }
100
101 int aStart() const { return combinedAmount(m_itemAInd); }
102 int bStart() const { return combinedAmount(m_itemBInd); }
103 int moneyStart() const;
104 int aAfter() const { return aAfterFor(m_net); }
105 int bAfter() const { return bAfterFor(m_net); }
106 int moneyAfter() const { return moneyAfterFor(m_net); }
107
113 Q_INVOKABLE int giveFor(int dir, int steps) const;
114
118 Q_INVOKABLE int refundFor(int dir, int steps) const;
119
120 bool canAddA() const { return valid() && stateValid(m_net + 1); }
121 bool canAddB() const { return valid() && stateValid(m_net - 1); }
122
126 Q_INVOKABLE QVariantList sourceItems(bool healingOnly, int excludeInd) const;
127
134 Q_INVOKABLE QVariantList targetItems(bool healingOnly, int excludeInd) const;
135
137 Q_INVOKABLE bool canGainTarget(int bInd) const;
138
143 Q_INVOKABLE void pickDefaults(bool healingOnly);
144
145 Q_INVOKABLE void adjust(int dir);
146 Q_INVOKABLE void reset();
147 Q_INVOKABLE void refresh();
148
149public slots:
150 void checkout();
151
152signals:
154 void changed();
155
156private:
157 int combinedAmount(int ind) const;
158 int combinedCapacity(int ind) const;
159 int buyOf(int ind) const;
160 QString readableOf(int ind) const;
161 bool isHealing(int ind) const;
162 int indOfName(const QString& name) const;
163 static void sortByName(QVariantList& list);
164
165 static int nA(int m) { return m > 0 ? m : 0; }
166 static int nB(int m) { return m < 0 ? -m : 0; }
167 int aAfterFor(int m) const;
168 int bAfterFor(int m) const;
169 int moneyAfterFor(int m) const;
170 bool stateValid(int m) const;
171 void clampNet();
172 void emitChanged();
173
174 ItemStorageBox* bag = nullptr;
175 ItemStorageBox* storage = nullptr;
176 PlayerBasics* basics = nullptr;
177
178 int m_itemAInd = -1;
179 int m_itemBInd = -1;
180 int m_net = 0;
181 int m_revision = 0;
182
184 struct Info { QString name; QString readable; int buy = 0; bool healing = false; };
185 QHash<int, Info> m_info;
186};
QVariantList targetItems(bool healingOnly, int excludeInd) const
RIGHT list – what you can GET: EVERY exchangeable item (owned or not), optionally healing-only,...
bool canGainTarget(int bInd) const
Can we gain at least one of item bInd right now, paying with the selected source?
void changed()
Any derived value changed.
ItemExchangeModel(ItemStorageBox *bag, ItemStorageBox *storage, PlayerBasics *basics)
QString bName() const
int giveFor(int dir, int steps) const
How many of the OTHER item steps of this direction consume, priced as ONE whole trade rather than ste...
void refresh()
Re-read counts/money + re-emit (tab open / external edit).
void pickDefaults(bool healingOnly)
Choose a sensible starting pair.
static constexpr int StackCap
Gen 1 per-stack cap.
void adjust(int dir)
+1 = one gained-A step, -1 = one gained-B step (gated).
QVariantList sourceItems(bool healingOnly, int excludeInd) const
LEFT list – what you can GIVE: items you actually own (amount > 0) and that are exchangeable (buy pri...
static constexpr int MoneyCap
< Both picked, distinct, both have a buy price.
QString aName() const
void reset()
Clear the net axis.
protected::void selectionChanged()
A or B selection changed.
void checkout()
Apply the previewed trade to the save.
int refundFor(int dir, int steps) const
The leftover value of steps in direction dir, refunded as money – i.e.
A container of Items – either the trainer's bag or a PC item box.
The trainer's headline values: name, ID, money, coins, badges, starter.