Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
filemanagement.h
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#pragma once
17#include <QList>
18#include <QFile>
19#include <QSettings>
20
21#include <pse-common/types.h>
22#include "savefile_autoport.h"
23
24#include "savefile.h"
25
26class SaveFile;
27
28constexpr var8 MAX_RECENT_FILES{5};
29constexpr const char* KEY_RECENT_FILES = "recentFiles";
30constexpr const char* KEY_LAST_FILE = "lastFile";
31
45class SAVEFILE_AUTOPORT FileManagement : public QObject
46{
47 Q_OBJECT
48
50 Q_PROPERTY(QString path READ getPath WRITE setPath NOTIFY pathChanged)
52 Q_PROPERTY(SaveFile* data MEMBER data NOTIFY dataChanged)
54 Q_PROPERTY(QString lastErrorMessage READ getLastErrorMessage NOTIFY loadError)
58 Q_PROPERTY(QString lastErrorDetail READ getLastErrorDetail NOTIFY loadError)
59
60public:
61 FileManagement(QObject* parent = nullptr);
62 virtual ~FileManagement();
63
64 QString getLastErrorMessage();
65 QString getLastErrorDetail();
66
67 QString getPath();
68 Q_INVOKABLE QString getRecentFile(int index = 0);
69 QList<QString> getRecentFiles();
70
72 SaveFile* data = nullptr;
73
74 Q_INVOKABLE int recentFilesCount();
75 Q_INVOKABLE int recentFilesMax();
76 Q_INVOKABLE void recentFilesSwap(int from, int to);
77 Q_INVOKABLE void recentFilesRemove(int ind);
78
79signals:
80 void pathChanged(QString newPath, QString oldPath);
81 void recentFilesChanged(QList<QString> files);
82 void dataChanged();
86 void loadError();
87
88public slots:
89 void reset();
90
91 void newFile();
92 bool openFile();
93 bool openFileRecent(int index);
94 void reopenFile();
95
96 void addRecentFile(QString path);
97 void setPath(QString path);
98
99 bool saveFile();
100 bool saveFileAs();
101 bool saveFileCopy();
102
103 void wipeUnusedSpace();
104 void clearRecentFiles();
105
106private:
107 void processRecentFileChanges();
108 void pruneRecentFiles();
109
110 QString openFileDialog(QString title);
111 QString saveFileDialog(QString title);
112
116 bool loadData(const QString& filePath);
117 void reportLoadError();
118
119 var8* readSaveData(QString filePath);
120 void writeSaveData(QString filePath, var8* data);
121
122 void expandRecentFiles(QString files);
123
124 QString path;
125 QList<QString> recentFiles;
126 QSettings settings;
127
130 enum FileLoadError { LoadErrorNone, LoadErrorCannotOpen, LoadErrorTooShort };
131
132 FileLoadError lastError = LoadErrorNone;
133 QString lastErrorMessage;
134 QString lastErrorDetail;
135};
void setPath(QString path)
Set the active path (backs the path property).
void newFile()
Start a fresh blank save.
int recentFilesMax()
The cap (MAX_RECENT_FILES).
QString getLastErrorDetail()
void clearRecentFiles()
Forget the entire recent-files list.
SaveFile * data
The live save file these operations load into / save from.
QString getLastErrorMessage()
bool saveFileAs()
Prompt for a new path and save there.
void recentFilesRemove(int ind)
Drop one entry from the recent list.
QList< QString > getRecentFiles()
The whole recent-files list.
protected::void pathChanged(QString newPath, QString oldPath)
The active path changed.
int recentFilesCount()
How many recent files are currently remembered.
void recentFilesChanged(QList< QString > files)
The recent-files list changed.
QString getRecentFile(int index=0)
Recent path at index (0 = most recent).
void addRecentFile(QString path)
Push a path onto the recent list (de-duped, capped).
void loadError()
A load failed on a file that exists (unreadable / truncated).
FileManagement(QObject *parent=nullptr)
Current file path. Setting it triggers a load via setPath().
void wipeUnusedSpace()
Zero out save regions that aren't meaningfully used.
bool openFileRecent(int index)
Open an entry from the recent-files list.
void reset()
Clear path + data back to a blank starting state.
void reopenFile()
Reload the current path from disk, discarding edits.
bool saveFile()
Save to the current path.
void recentFilesSwap(int from, int to)
Reorder the recent list (e.g. drag to reorder).
bool saveFileCopy()
Save a copy elsewhere without changing the active path.
QString getPath()
Current file path.
bool openFile()
Prompt for and open a save.
void dataChanged()
The live SaveFile was replaced.
One loaded save: the raw 32 KB bytes, their expanded object tree, and the tools that move between the...
Definition savefile.h:46
Project-wide fixed-width integer aliases (var8, var16, ...).
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition types.h:124
constexpr const char * KEY_LAST_FILE
QSettings key for the most-recent path.
constexpr var8 MAX_RECENT_FILES
How many recent paths to remember.
constexpr const char * KEY_RECENT_FILES
QSettings key for the recent-files list.
Import/export macro for the savefile library, plus the central list of QObject types kept deliberatel...
#define SAVEFILE_AUTOPORT
Expands to the correct dllexport/dllimport decoration for this library.