1 /*
2 SPDX-FileCopyrightText: 2014 Jean-Baptiste Mardelle <jb@kdenlive.org>
3 This file is part of Kdenlive. See www.kdenlive.org.
4
5 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
6 */
7
8 #include "projectsortproxymodel.h"
9 #include "abstractprojectitem.h"
10
11 #include <QItemSelectionModel>
12
ProjectSortProxyModel(QObject * parent)13 ProjectSortProxyModel::ProjectSortProxyModel(QObject *parent)
14 : QSortFilterProxyModel(parent)
15 {
16 m_collator.setLocale(QLocale()); // Locale used for sorting → OK
17 m_collator.setCaseSensitivity(Qt::CaseInsensitive);
18 m_collator.setNumericMode(true);
19 m_selection = new QItemSelectionModel(this);
20 connect(m_selection, &QItemSelectionModel::selectionChanged, this, &ProjectSortProxyModel::onCurrentRowChanged);
21 setDynamicSortFilter(true);
22 }
23
24 // Responsible for item sorting!
filterAcceptsRow(int sourceRow,const QModelIndex & sourceParent) const25 bool ProjectSortProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
26 {
27 if (filterAcceptsRowItself(sourceRow, sourceParent)) {
28 return true;
29 }
30 // accept if any of the children is accepted on it's own merits
31 return hasAcceptedChildren(sourceRow, sourceParent);
32 }
33
filterAcceptsRowItself(int sourceRow,const QModelIndex & sourceParent) const34 bool ProjectSortProxyModel::filterAcceptsRowItself(int sourceRow, const QModelIndex &sourceParent) const
35 {
36 if (m_unusedFilter) {
37 // Column 8 contains the usage
38 QModelIndex indexTag = sourceModel()->index(sourceRow, 8, sourceParent);
39 if (sourceModel()->data(indexTag).toInt() > 0) {
40 return false;
41 }
42 }
43 if (m_searchRating > 0) {
44 // Column 7 contains the rating
45 QModelIndex indexTag = sourceModel()->index(sourceRow, 7, sourceParent);
46 if (sourceModel()->data(indexTag).toInt() != m_searchRating) {
47 return false;
48 }
49 }
50 if (m_searchType > 0) {
51 // Column 3 contains the item type (video, image, title, etc)
52 QModelIndex indexTag = sourceModel()->index(sourceRow, 3, sourceParent);
53 if (sourceModel()->data(indexTag).toInt() != m_searchType) {
54 return false;
55 }
56 }
57 if (!m_searchTag.isEmpty()) {
58 // Column 4 contains the item tag data
59 QModelIndex indexTag = sourceModel()->index(sourceRow, 4, sourceParent);
60 auto tagData = sourceModel()->data(indexTag);
61 for (const QString &tag : m_searchTag) {
62 if (!tagData.toString().contains(tag, Qt::CaseInsensitive)) {
63 return false;
64 }
65 }
66 }
67
68 for (int i = 0; i < 3; i++) {
69 QModelIndex index0 = sourceModel()->index(sourceRow, i, sourceParent);
70 if (!index0.isValid()) {
71 return false;
72 }
73 auto data = sourceModel()->data(index0);
74 if (data.toString().contains(m_searchString, Qt::CaseInsensitive)) {
75 return true;
76 }
77 }
78 return false;
79 }
80
hasAcceptedChildren(int sourceRow,const QModelIndex & source_parent) const81 bool ProjectSortProxyModel::hasAcceptedChildren(int sourceRow, const QModelIndex &source_parent) const
82 {
83 QModelIndex item = sourceModel()->index(sourceRow, 0, source_parent);
84 if (!item.isValid()) {
85 return false;
86 }
87
88 // check if there are children
89 int childCount = item.model()->rowCount(item);
90 if (childCount == 0) {
91 return false;
92 }
93
94 for (int i = 0; i < childCount; ++i) {
95 if (filterAcceptsRowItself(i, item)) {
96 return true;
97 }
98 // recursive call -> NOTICE that this is depth-first searching, you're probably better off with breadth first search...
99 if (hasAcceptedChildren(i, item)) {
100 return true;
101 }
102 }
103 return false;
104 }
105
lessThan(const QModelIndex & left,const QModelIndex & right) const106 bool ProjectSortProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
107 {
108 // Check item type (folder or clip) as defined in projectitemmodel
109 int leftType = sourceModel()->data(left, AbstractProjectItem::ItemTypeRole).toInt();
110 int rightType = sourceModel()->data(right, AbstractProjectItem::ItemTypeRole).toInt();
111 if (leftType == rightType) {
112 // Let the normal alphabetical sort happen
113 const QVariant leftData = sourceModel()->data(left, Qt::DisplayRole);
114 const QVariant rightData = sourceModel()->data(right, Qt::DisplayRole);
115 if (leftData.type() == QVariant::DateTime) {
116 return leftData.toDateTime() < rightData.toDateTime();
117 }
118 if (leftData.type() == QVariant::Int) {
119 return leftData.toInt() < rightData.toInt();
120 }
121 return m_collator.compare(leftData.toString(), rightData.toString()) < 0;
122 }
123 if (sortOrder() == Qt::AscendingOrder) {
124 return leftType < rightType;
125 }
126 return leftType > rightType;
127 }
128
selectionModel()129 QItemSelectionModel *ProjectSortProxyModel::selectionModel()
130 {
131 return m_selection;
132 }
133
slotSetSearchString(const QString & str)134 void ProjectSortProxyModel::slotSetSearchString(const QString &str)
135 {
136 m_searchString = str;
137 invalidateFilter();
138 }
139
slotSetFilters(const QStringList tagFilters,const int rateFilters,const int typeFilters,bool unusedFilter)140 void ProjectSortProxyModel::slotSetFilters(const QStringList tagFilters, const int rateFilters, const int typeFilters, bool unusedFilter)
141 {
142 m_searchType = typeFilters;
143 m_searchRating = rateFilters;
144 m_searchTag = tagFilters;
145 m_unusedFilter = unusedFilter;
146 invalidateFilter();
147 }
148
slotClearSearchFilters()149 void ProjectSortProxyModel::slotClearSearchFilters()
150 {
151 m_searchTag.clear();
152 m_searchRating = 0;
153 m_searchType = 0;
154 m_unusedFilter = false;
155 invalidateFilter();
156 }
157
onCurrentRowChanged(const QItemSelection & current,const QItemSelection & previous)158 void ProjectSortProxyModel::onCurrentRowChanged(const QItemSelection ¤t, const QItemSelection &previous)
159 {
160 Q_UNUSED(previous)
161 QModelIndexList indexes = current.indexes();
162 if (indexes.isEmpty()) {
163 emit selectModel(QModelIndex());
164 return;
165 }
166 for (int ix = 0; ix < indexes.count(); ix++) {
167 if (indexes.at(ix).column() == 0 || indexes.at(ix).column() == 7) {
168 emit selectModel(indexes.at(ix));
169 break;
170 }
171 }
172 }
173
slotDataChanged(const QModelIndex & ix1,const QModelIndex & ix2,const QVector<int> & roles)174 void ProjectSortProxyModel::slotDataChanged(const QModelIndex &ix1, const QModelIndex &ix2, const QVector<int> &roles)
175 {
176 emit dataChanged(ix1, ix2, roles);
177 }
178
179
selectAll(QModelIndex rootIndex)180 void ProjectSortProxyModel::selectAll(QModelIndex rootIndex)
181 {
182 QModelIndex topLeft = index(0, 0, rootIndex);
183 QModelIndex bottomRight = index(rowCount(rootIndex) - 1,
184 columnCount(rootIndex) - 1, rootIndex);
185 QItemSelection selection(topLeft, bottomRight);
186 m_selection->select(selection, QItemSelectionModel::Select);
187 }
188