-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilesaverandloader.cpp
More file actions
303 lines (195 loc) · 8.56 KB
/
Copy pathfilesaverandloader.cpp
File metadata and controls
303 lines (195 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "filesaverandloader.h"
#include <QByteArray>
#include <QFile>
#include <QList>
#include <QJsonArray>
#include <QJsonDocument>
//This file MUST be stored as UTF-8 because of headerString
#include <QJsonObject>
#include <QJsonParseError>
#include <QSaveFile>
#include <QtGlobal>
#include <QUrl>
#include <iostream>
#include <string>
#include <QVariant>
#include <QQuickItem>
FileSaverAndLoader::FileSaverAndLoader( QObject *parent ) : QObject( parent ) {
notecardManager = nullptr;
scriptPage = nullptr;
charactersPage = nullptr;
storylinesPage = nullptr;
headerString = QString::fromUtf8( u8"\U0001F409\U0001F432ScriptDragon\U0001F432\U0001F409\n" );
}
void FileSaverAndLoader::load( QUrl fileURL ) {
QString fileName = fileURL.toLocalFile();
QFile loadFile( fileName );
if( Q_UNLIKELY( !loadFile.exists() ) ) {
std::cerr << "Selected file \"" << fileName.toStdString() << "\" does not exist" << std::endl;
} else if( Q_UNLIKELY( !loadFile.open( QIODevice::ReadOnly ) ) ) {
std::cerr << "Selected file \"" << fileName.toStdString() << "\" could not be opened for reading" << std::endl;
} else {
QByteArray contents = loadFile.readAll();
QByteArray header = headerString.toUtf8();
if( Q_UNLIKELY( contents.isNull() || contents.isEmpty() ) ) {
std::cerr << "Error reading file \"" << fileName.toStdString() << "\"" << std::endl;
} else if( Q_UNLIKELY( !contents.startsWith( header ) ) ) {
std::cerr << "Error reading file \"" << fileName.toStdString() << "\": Header not found" << std::endl;
} else {
contents.remove( 0, header.length() );
QJsonParseError errorHolder;
QJsonDocument jsonDoc = QJsonDocument::fromJson( contents, &errorHolder );
if( errorHolder.error != QJsonParseError::NoError ) {
std::cerr << "Error reading file \"" << fileName.toStdString() << "\": " << errorHolder.errorString().toStdString() << std::endl;
} else if( !jsonDoc.isObject() ) {
std::cerr << "Error reading file \"" << fileName.toStdString() << "\": Incorrect file format" << std::endl;
} else {
QJsonObject json = jsonDoc.object();
//script
if( Q_LIKELY( scriptPage != nullptr ) && Q_LIKELY( json.contains( "script" ) ) ) {
QString scriptText = json.take( "script" ).toString();
scriptPage->setProperty( "text", scriptText );
/*auto testing = scriptPage->findChild<QTextArea *>("scriptTA");
std::cout << 5 << std::endl;*/
}
//characters
if( Q_LIKELY( charactersPage != nullptr ) && Q_LIKELY( json.contains( "characters" ) ) ) {
QJsonArray characters = json.take( "characters" ).toArray();
for( auto i = characters.begin(); i != characters.end(); ++i ) {
QString characterName = (*i).toString();
emit addCharacterName( characterName );
}
}
//locations
if( Q_LIKELY( locationsPage != nullptr ) && Q_LIKELY( json.contains( "locations" ) ) ) {
QJsonArray locations = json.take( "locations" ).toArray();
for( auto i = locations.begin(); i != locations.end(); ++i ) {
QString locationName = (*i).toString();
emit addLocationName( locationName );
}
}
//storylines
if( Q_LIKELY( storylinesPage != nullptr ) && Q_LIKELY( json.contains( "storylines" ) ) ) {
QJsonArray storylines = json.take( "storylines" ).toArray();
for( auto i = storylines.begin(); i != storylines.end(); ++i ) {
QString storylineName = (*i).toString();
emit addStorylineName( storylineName );
}
}
//notecards
if( Q_LIKELY( notecardManager != nullptr ) && Q_LIKELY( json.contains( "notecards" ) ) ) {
QJsonArray notecards = json.take( "notecards" ).toArray();
for( auto i = notecards.begin(); i != notecards.end(); ++i ) {
QJsonObject notecard = (*i).toObject();
NotecardManager::associationType associationType = (NotecardManager::associationType) ( (uint_fast8_t) notecard.take( "associationType" ).toInt() );
int associatedID = notecard.take( "associatedID" ).toInt();
QString color = notecard.take( "color" ).toString();
QString title = notecard.take( "title" ).toString();
QString text = notecard.take( "text" ).toString();
int idWithinAssociatedThing = notecard.take( "idWithinAssociatedThing" ).toInt();
notecardManager->addNotecard( title, text, associationType, associatedID, idWithinAssociatedThing, color );
}
}
}
}
}
emit fileLoaded( fileURL );
}
void FileSaverAndLoader::save( QUrl fileURL ) {
QString fileName = fileURL.toLocalFile();
if( !fileName.endsWith( ".scriptdragon", Qt::CaseInsensitive ) ) {
fileName.append( ".scriptdragon" );
}
QSaveFile saveFile( fileName );
std::cout << "saving to \"" << saveFile.fileName().toStdString().c_str() << "\"" << std::endl;
saveFile.setDirectWriteFallback( true );
if( Q_UNLIKELY( !saveFile.open( QIODevice::WriteOnly ) ) ) {
std::cerr << "Couldn't open file for writing" << std::endl;
saveFile.cancelWriting(); //I doubt this is needed since open() failed
} else {
{
QJsonObject json;
//script
if( Q_LIKELY( scriptPage != nullptr ) ) {
json.insert( "script", scriptPage->property( "text" ).toString() );
}
//notecards
if( Q_LIKELY( notecardManager != nullptr ) ) {
QJsonArray notecardArray;
foreach( QObject* notecard, notecardManager->getAllNotecards() ) {
QJsonObject cardJson;
QList< QString > stringProperties( { "title", "text", "color" } );
foreach( QString propertyName, stringProperties ) {
cardJson.insert( propertyName, notecard->property( propertyName.toStdString().c_str() ).toString() );
}
QList< QString > intProperties( { "associationType", "associatedID", "idWithinAssociatedThing" } );
foreach( QString propertyName, intProperties ) {
cardJson.insert( propertyName, notecard->property( propertyName.toStdString().c_str() ).toInt() );
}
notecardArray.append( cardJson );
}
json.insert( "notecards", notecardArray );
}
//characters
if( Q_LIKELY( charactersPage != nullptr ) ) {
QJsonArray characterArray;
auto characterView = charactersPage->findChild< QObject* >( "characterView" );
auto children = characterView->children();
for( uint_fast8_t i = 0; i < children.size(); ++i ) {
QObject* child = children[ i ];
QString name = child->property( "name" ).toString();
characterArray.append( QJsonValue( name ) );
}
json.insert( "characters", characterArray );
}
//locations
if( Q_LIKELY( locationsPage != nullptr ) ) {
QJsonArray locationArray;
auto locationView = locationsPage->findChild< QObject* >( "locationView" );
auto children = locationView->children();
for( uint_fast8_t i = 0; i < children.size(); ++i ) {
QObject* child = children[ i ];
QString name = child->property( "name" ).toString();
locationArray.append( QJsonValue( name ) );
}
json.insert( "locations", locationArray );
}
//storylines
if( Q_LIKELY( storylinesPage != nullptr ) ) {
QJsonArray storylineArray;
auto storylineView = storylinesPage->findChild< QObject* >( "storylineView" );
auto children = storylineView->children();
for( uint_fast8_t i = 0; i < children.size(); ++i ) {
QObject* child = children[ i ];
QString name = child->property( "name" ).toString();
storylineArray.append( QJsonValue( name ) );
}
json.insert( "storylines", storylineArray );
}
QByteArray fileContent = headerString.toUtf8();
QJsonDocument jsonDoc( json );
fileContent.append( jsonDoc.toJson( QJsonDocument::Indented ) );
saveFile.write( fileContent );
}
//..then when we're done writing data, we commit.
if( Q_UNLIKELY( !saveFile.commit() ) ) {
std::cerr << "Error saving file" << std::endl;
}
}
emit fileSaved( fileURL );
}
void FileSaverAndLoader::setCharactersPage( QObject* newCharactersPage ) {
charactersPage = newCharactersPage;
}
void FileSaverAndLoader::setLocationsPage( QObject* newLocationsPage ) {
locationsPage = newLocationsPage;
}
void FileSaverAndLoader::setNotecardManager( NotecardManager* newNM ) {
notecardManager = newNM;
}
void FileSaverAndLoader::setScriptPage( QObject* newScriptPage ) {
scriptPage = newScriptPage;
}
void FileSaverAndLoader::setStorylinesPage( QObject* newStorylinesPage ) {
storylinesPage = newStorylinesPage;
}