blob: 30824edb4df6094e930af45c79f4c7753ea73432 [file] [log] [blame]
swissChilia44bf722022-04-16 18:41:54 -07001#include "RecentModel.h"
2#include <QSettings>
3
4RecentModel::RecentModel(QObject *parent)
5 : QAbstractListModel(parent)
6{
7 _recents = _settings.value("recents").toStringList();
8}
9
10RecentModel::RecentModel(const RecentModel &other, QObject *parent)
11 : RecentModel(parent)
12{
13 _recents = other._recents;
14}
15
16int RecentModel::rowCount(const QModelIndex &parent) const
17{
18 if (parent.isValid())
19 return 0;
20
21 return _recents.size();
22}
23
24QVariant RecentModel::data(const QModelIndex &index, int role) const
25{
26 if (!index.isValid())
27 return QVariant();
28
29 if (role == PathRole)
30 return _recents[index.row()];
31
32 return QVariant();
33}
34
35QHash<int, QByteArray> RecentModel::roleNames() const
36{
37 return {{PathRole, "path"}};
38}
39
40void RecentModel::add(QString path)
41{
42 remove(path);
43
44 beginInsertRows(QModelIndex(), 0, 0);
45 _recents.prepend(path);
46 endInsertRows();
47
48 _settings.setValue("recents", _recents);
49}
50
51void RecentModel::remove(QString path)
52{
53 if (_recents.contains(path))
54 {
55 int index = _recents.indexOf(path);
56 beginRemoveRows(QModelIndex(), index, index);
57 _recents.removeAt(index);
58 endRemoveRows();
59
60 _settings.setValue("recents", _recents);
61 }
62}