swissChili | a44bf72 | 2022-04-16 18:41:54 -0700 | [diff] [blame] | 1 | #include "RecentModel.h" |
| 2 | #include <QSettings> |
| 3 | |
| 4 | RecentModel::RecentModel(QObject *parent) |
| 5 | : QAbstractListModel(parent) |
| 6 | { |
| 7 | _recents = _settings.value("recents").toStringList(); |
| 8 | } |
| 9 | |
| 10 | RecentModel::RecentModel(const RecentModel &other, QObject *parent) |
| 11 | : RecentModel(parent) |
| 12 | { |
| 13 | _recents = other._recents; |
| 14 | } |
| 15 | |
| 16 | int RecentModel::rowCount(const QModelIndex &parent) const |
| 17 | { |
| 18 | if (parent.isValid()) |
| 19 | return 0; |
| 20 | |
| 21 | return _recents.size(); |
| 22 | } |
| 23 | |
| 24 | QVariant 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 | |
| 35 | QHash<int, QByteArray> RecentModel::roleNames() const |
| 36 | { |
| 37 | return {{PathRole, "path"}}; |
| 38 | } |
| 39 | |
| 40 | void 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 | |
| 51 | void 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 | } |