Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
recentfilesmodel.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 <QDir>
23
#include <QString>
24
#include <QStringList>
25
26
#include "
./recentfilesmodel.h
"
27
#include <
pse-savefile/filemanagement.h
>
28
29
RecentFilesModel::RecentFilesModel
(
FileManagement
* file) : file(file)
30
{
31
connect(file, &
FileManagement::recentFilesChanged
,
this
, &RecentFilesModel::onDataChange);
32
}
33
34
int
RecentFilesModel::rowCount
(
const
QModelIndex& parent)
const
35
{
36
// Not a tree, just a list, there's no parent
37
Q_UNUSED(parent)
38
39
// Return list count
40
return
file->recentFilesCount() + 1;
41
}
42
43
QVariant
RecentFilesModel::data
(
const
QModelIndex& index,
int
role)
const
44
{
45
// If index is invalid in any way, return nothing
46
if
(!index.isValid())
47
return
QVariant();
48
49
if
(index.row() >= (file->recentFilesCount() + 1))
50
return
QVariant();
51
52
// If it's the special clear button then treat that specially
53
if
(index.row() == 0 && role ==
PathRole
)
54
return
tr(
"Clear Recent Files"
);
55
else
if
(index.row() == 0 && role ==
EnabledRole
)
56
return
file->recentFilesCount() > 0;
57
else
if
(index.row() == 0 && role ==
FileIndexRole
)
58
return
-1;
59
60
// else return recent file, actual recent files are always enabled
61
if
(role ==
PathRole
)
62
return
getDisplayPath(index.row() - 1, file->getRecentFile(index.row() - 1));
63
64
if
(role ==
EnabledRole
)
65
return
true
;
66
67
if
(role ==
FileIndexRole
)
68
return
index.row() - 1;
69
70
// All else fails, return nothing
71
return
QVariant();
72
}
73
74
QHash<int, QByteArray>
RecentFilesModel::roleNames
()
const
75
{
76
QHash<int, QByteArray> roles;
77
78
roles[
EnabledRole
] =
"isEnabled"
;
79
roles[
PathRole
] =
"path"
;
80
roles[
FileIndexRole
] =
"fileIndex"
;
81
82
return
roles;
83
}
84
85
QString RecentFilesModel::getDisplayPath(
int
index, QString path)
const
86
{
87
auto
parts = path.split(
"/"
, Qt::SkipEmptyParts);
88
QString display =
"["
+ QString::number(index) +
"] "
;
89
90
if
(parts.size() >= 3)
91
display += parts.at(0) +
"/..."
92
+ parts.at(parts.size() - 2) +
"/"
93
+ parts.at(parts.size() - 1);
94
else
if
(parts.size() == 2)
95
display += parts.at(parts.size() - 2) +
"/"
96
+ parts.at(parts.size() - 1);
97
else
98
display += parts.at(parts.size() - 1);
99
100
return
QDir::toNativeSeparators(display);
101
}
102
103
void
RecentFilesModel::onDataChange()
104
{
105
// I'm just trying to say refresh everything, wow this is complicated
106
// None of my data handpicks which exact bits and pieces have changed because
107
// with the small datasets I have it'd be more expensive and error prone to
108
// do that than to just say everything has changed
109
// EDIT: It's 5 Rows, Literally no more than 5 entries!
110
beginResetModel();
111
endResetModel();
112
}
FileManagement
Owns the on-disk side of a save: the current path, the recent-files list, and the live SaveFile.
Definition
filemanagement.h:46
FileManagement::recentFilesChanged
void recentFilesChanged(QList< QString > files)
The recent-files list changed.
RecentFilesModel::rowCount
virtual int rowCount(const QModelIndex &parent) const override
Row count.
Definition
recentfilesmodel.cpp:34
RecentFilesModel::data
virtual QVariant data(const QModelIndex &index, int role) const override
Row+role value.
Definition
recentfilesmodel.cpp:43
RecentFilesModel::roleNames
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name.
Definition
recentfilesmodel.cpp:74
RecentFilesModel::RecentFilesModel
RecentFilesModel(FileManagement *file)
Definition
recentfilesmodel.cpp:29
RecentFilesModel::EnabledRole
@ EnabledRole
Definition
recentfilesmodel.h:37
RecentFilesModel::FileIndexRole
@ FileIndexRole
Definition
recentfilesmodel.h:38
RecentFilesModel::PathRole
@ PathRole
Definition
recentfilesmodel.h:36
filemanagement.h
recentfilesmodel.h
projects
app
src
mvc
recentfilesmodel.cpp
Generated by
1.17.0