Changeset View
Changeset View
Standalone View
Standalone View
wallpapers/image/slidefiltermodel.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2019 David Redondo <kde@david-redondo.de> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 2.010-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "slidefiltermodel.h" | ||||
20 | | ||||
21 | #include "backgroundlistmodel.h" | ||||
22 | #include "slidemodel.h" | ||||
23 | | ||||
24 | #include <QRandomGenerator> | ||||
25 | #include <QDebug> | ||||
26 | #include <QFileInfo> | ||||
27 | | ||||
28 | SlideFilterModel::SlideFilterModel(QObject* parent) | ||||
29 | : QSortFilterProxyModel{parent} | ||||
30 | , m_SortingMode{Image::Random} | ||||
31 | , m_usedInConfig{false} | ||||
32 | { | ||||
33 | setSortCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive); | ||||
34 | connect(this, &SlideFilterModel::usedInConfigChanged, this, &SlideFilterModel::invalidateFilter); | ||||
35 | } | ||||
36 | | ||||
37 | bool SlideFilterModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const | ||||
38 | { | ||||
39 | auto index = sourceModel()->index(source_row, 0, source_parent); | ||||
40 | return m_usedInConfig || index.data(BackgroundListModel::ToggleRole).toBool(); | ||||
41 | } | ||||
42 | | ||||
43 | bool SlideFilterModel::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const | ||||
44 | { | ||||
45 | switch (m_SortingMode) { | ||||
46 | case Image::Random: | ||||
47 | return (*QRandomGenerator::system())() % 2 == 0; | ||||
48 | case Image::Alphabetical: | ||||
49 | return QSortFilterProxyModel::lessThan(source_left, source_right); | ||||
50 | case Image::AlphabeticalReversed: | ||||
51 | return !QSortFilterProxyModel::lessThan(source_left, source_right); | ||||
52 | case Image::Modified: // oldest first | ||||
53 | { | ||||
54 | QFileInfo f1(source_left.data(BackgroundListModel::PathRole).toUrl().toLocalFile()); | ||||
55 | QFileInfo f2(source_right.data(BackgroundListModel::PathRole).toUrl().toLocalFile()); | ||||
56 | return f1.lastModified() < f2.lastModified(); | ||||
57 | } | ||||
58 | case Image::ModifiedReversed: // newest first | ||||
59 | { | ||||
60 | QFileInfo f1(source_left.data(BackgroundListModel::PathRole).toUrl().toLocalFile()); | ||||
61 | QFileInfo f2(source_right.data(BackgroundListModel::PathRole).toUrl().toLocalFile()); | ||||
62 | return !(f1.lastModified() < f2.lastModified()); | ||||
63 | } | ||||
64 | } | ||||
65 | Q_UNREACHABLE(); | ||||
66 | } | ||||
67 | | ||||
68 | void SlideFilterModel::setSortingMode(Image::SlideshowMode mode) | ||||
69 | { | ||||
70 | m_SortingMode = mode; | ||||
71 | if (!(m_usedInConfig && mode == Image::Random)) { | ||||
72 | QSortFilterProxyModel::invalidate(); | ||||
73 | } | ||||
74 | } | ||||
75 | | ||||
76 | void SlideFilterModel::invalidateFilter() | ||||
77 | { | ||||
78 | QSortFilterProxyModel::invalidateFilter(); | ||||
79 | } | ||||
80 | | ||||
81 | int SlideFilterModel::indexOf(const QString& path) | ||||
82 | { | ||||
83 | auto sourceIndex = sourceModel()->index(static_cast<SlideModel*>(sourceModel())->indexOf(path), 0); | ||||
84 | return mapFromSource(sourceIndex).row(); | ||||
85 | } | ||||
86 | | ||||
87 | void SlideFilterModel::openContainingFolder(int rowIndex) | ||||
88 | { | ||||
89 | auto sourceIndex = mapToSource(index(rowIndex, 0)); | ||||
90 | static_cast<SlideModel*>(sourceModel())->openContainingFolder(sourceIndex.row()); | ||||
91 | } |