Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
abstractrandomstring.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
#include "
abstractrandomstring.h
"
22
23
#include <QVector>
24
#include <QJsonArray>
25
#include <QtMath>
26
#include <QQmlEngine>
27
28
#include "
../util/gamedata.h
"
29
#include <
pse-common/random.h
>
30
#include <
pse-common/utility.h
>
31
32
void
AbstractRandomString::load
()
33
{
34
store
.clear();
35
36
// Grab Event Pokemon Data
37
auto
jsonData =
GameData::inst
()->
json
(
fileName
);
38
39
// Go through each event Pokemon
40
for
(QJsonValue jsonEntry : jsonData.array())
41
{
42
// Add to array
43
store
.append(jsonEntry.toString());
44
}
45
46
listChanged();
47
}
48
49
void
AbstractRandomString::qmlProtect
(
const
QQmlEngine*
const
engine)
const
50
{
51
Utility::qmlProtectUtil
(
this
, engine);
52
}
53
54
AbstractRandomString::AbstractRandomString
(QString
fileName
)
55
:
fileName
(
fileName
)
56
{
57
load
();
58
}
59
60
const
QVector<QString>
AbstractRandomString::getStore
()
const
61
{
62
return
store
;
63
}
64
65
int
AbstractRandomString::getStoreSize
()
const
66
{
67
return
store
.size();
68
}
69
70
const
QString
AbstractRandomString::getStoreAt
(
const
int
ind)
const
71
{
72
// Guard both ends -- a negative ind (e.g. QML's -1 "no selection") must not
73
// reach store.at(). Matches the canonical guard used by the DB store accessors.
74
if
(ind < 0 || ind >=
store
.size())
75
return
QString();
76
77
return
store
.at(ind);
78
}
79
80
QString
AbstractRandomString::randomExample
()
81
{
82
int
index =
Random::inst
()->
rangeExclusive
(0,
store
.size());
83
QString ret =
store
.at(index);
84
store
.removeAt(index);
85
86
if
(
store
.size() == 0)
87
load
();
88
else
89
listChanged();
90
91
return
ret;
92
}
abstractrandomstring.h
AbstractRandomString::qmlProtect
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition
abstractrandomstring.cpp:49
AbstractRandomString::getStore
const QVector< QString > getStore() const
All strings.
Definition
abstractrandomstring.cpp:60
AbstractRandomString::randomExample
QString randomExample()
A random string from the list.
Definition
abstractrandomstring.cpp:80
AbstractRandomString::AbstractRandomString
AbstractRandomString(const QString fileName)
Definition
abstractrandomstring.cpp:54
AbstractRandomString::getStoreSize
int getStoreSize() const
String count.
Definition
abstractrandomstring.cpp:65
AbstractRandomString::getStoreAt
const QString getStoreAt(const int ind) const
String at ind (for QML).
Definition
abstractrandomstring.cpp:70
AbstractRandomString::fileName
const QString fileName
Asset path (set by the subclass).
Definition
abstractrandomstring.h:64
AbstractRandomString::load
void load()
Load the strings from fileName.
Definition
abstractrandomstring.cpp:32
AbstractRandomString::store
QVector< QString > store
The loaded strings.
Definition
abstractrandomstring.h:63
GameData::json
const QJsonDocument json(const QString filename) const
Parsed document for filename.
Definition
gamedata.cpp:45
GameData::inst
static GameData * inst()
The process-wide GameData singleton.
Definition
gamedata.cpp:71
Random::inst
static Random * inst()
< Convenience 50% coin flip (integer path), readable from QML.
Definition
random.cpp:31
Random::rangeExclusive
int rangeExclusive(const int start, const int end) const
Random integer in the half-open interval [start, end).
Definition
random.cpp:53
Utility::qmlProtectUtil
static void qmlProtectUtil(const QObject *const obj, const QQmlEngine *const engine)
Pin obj to C++ ownership so the QML engine never garbage-collects it.
Definition
utility.cpp:63
gamedata.h
random.h
utility.h
projects
db
src
pse-db
entries
abstractrandomstring.cpp
Generated by
1.17.0