Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
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"
28
30{
31 connect(file, &FileManagement::recentFilesChanged, this, &RecentFilesModel::onDataChange);
32}
33
34int 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
43QVariant 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
74QHash<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
85QString 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
103void 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}
Owns the on-disk side of a save: the current path, the recent-files list, and the live SaveFile.
void recentFilesChanged(QList< QString > files)
The recent-files list changed.
virtual int rowCount(const QModelIndex &parent) const override
Row count.
virtual QVariant data(const QModelIndex &index, int role) const override
Row+role value.
virtual QHash< int, QByteArray > roleNames() const override
Role -> QML name.
RecentFilesModel(FileManagement *file)