qBittorrent
transferlistsortmodel.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2013 Nick Tiskov <daymansmail@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL". If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #include "transferlistsortmodel.h"
30 
31 #include <type_traits>
32 
33 #include <QDateTime>
34 
37 #include "transferlistmodel.h"
38 
39 namespace
40 {
41  template <typename T>
42  int threeWayCompare(const T &left, const T &right)
43  {
44  if (left == right)
45  return 0;
46  return (left < right) ? -1 : 1;
47  }
48 
49  int customCompare(const QDateTime &left, const QDateTime &right)
50  {
51  const bool isLeftValid = left.isValid();
52  const bool isRightValid = right.isValid();
53 
54  if (isLeftValid == isRightValid)
55  return threeWayCompare(left, right);
56  return isLeftValid ? -1 : 1;
57  }
58 
59  int customCompare(const TagSet &left, const TagSet &right, const Utils::Compare::NaturalCompare<Qt::CaseInsensitive> &compare)
60  {
61  for (auto leftIter = left.cbegin(), rightIter = right.cbegin();
62  (leftIter != left.cend()) && (rightIter != right.cend());
63  ++leftIter, ++rightIter)
64  {
65  const int result = compare(*leftIter, *rightIter);
66  if (result != 0)
67  return result;
68  }
69  return threeWayCompare(left.size(), right.size());
70  }
71 
72  template <typename T>
73  int customCompare(const T left, const T right)
74  {
75  static_assert(std::is_arithmetic_v<T>);
76 
77  const bool isLeftValid = (left >= 0);
78  const bool isRightValid = (right >= 0);
79 
80  if (isLeftValid && isRightValid)
81  return threeWayCompare(left, right);
82  if (!isLeftValid && !isRightValid)
83  return 0;
84  return isLeftValid ? -1 : 1;
85  }
86 
87  int adjustSubSortColumn(const int column)
88  {
89  return ((column >= 0) && (column < TransferListModel::NB_COLUMNS))
90  ? column : TransferListModel::TR_NAME;
91  }
92 }
93 
95  : QSortFilterProxyModel {parent}
96  , m_subSortColumn {"TransferList/SubSortColumn", TransferListModel::TR_NAME, adjustSubSortColumn}
97  , m_subSortOrder {"TransferList/SubSortOrder", 0}
98 {
100 }
101 
102 void TransferListSortModel::sort(const int column, const Qt::SortOrder order)
103 {
104  if ((m_lastSortColumn != column) && (m_lastSortColumn != -1))
105  {
108  }
109  m_lastSortColumn = column;
110  m_lastSortOrder = ((order == Qt::AscendingOrder) ? 0 : 1);
111 
112  QSortFilterProxyModel::sort(column, order);
113 }
114 
116 {
117  if (m_filter.setType(filter))
118  invalidateFilter();
119 }
120 
121 void TransferListSortModel::setCategoryFilter(const QString &category)
122 {
123  if (m_filter.setCategory(category))
124  invalidateFilter();
125 }
126 
128 {
130  invalidateFilter();
131 }
132 
133 void TransferListSortModel::setTagFilter(const QString &tag)
134 {
135  if (m_filter.setTag(tag))
136  invalidateFilter();
137 }
138 
140 {
142  invalidateFilter();
143 }
144 
145 void TransferListSortModel::setTrackerFilter(const QSet<BitTorrent::TorrentID> &torrentIDs)
146 {
147  if (m_filter.setTorrentIDSet(torrentIDs))
148  invalidateFilter();
149 }
150 
152 {
154  invalidateFilter();
155 }
156 
157 int TransferListSortModel::compare(const QModelIndex &left, const QModelIndex &right) const
158 {
159  const int compareColumn = left.column();
160  const QVariant leftValue = left.data(TransferListModel::UnderlyingDataRole);
161  const QVariant rightValue = right.data(TransferListModel::UnderlyingDataRole);
162 
163  switch (compareColumn)
164  {
169  return m_naturalCompare(leftValue.toString(), rightValue.toString());
170 
172  return customCompare(leftValue.value<TagSet>(), rightValue.value<TagSet>(), m_naturalCompare);
173 
185  return customCompare(leftValue.toLongLong(), rightValue.toLongLong());
186 
191  return customCompare(leftValue.toReal(), rightValue.toReal());
192 
194  return threeWayCompare(leftValue.toInt(), rightValue.toInt());
195 
199  return customCompare(leftValue.toDateTime(), rightValue.toDateTime());
200 
206  return customCompare(leftValue.toInt(), rightValue.toInt());
207 
210  {
211  // Active peers/seeds take precedence over total peers/seeds
212  const auto activeL = leftValue.toInt();
213  const auto activeR = rightValue.toInt();
214  if (activeL != activeR)
215  return threeWayCompare(activeL, activeR);
216 
217  const auto totalL = left.data(TransferListModel::AdditionalUnderlyingDataRole).toInt();
218  const auto totalR = right.data(TransferListModel::AdditionalUnderlyingDataRole).toInt();
219  return threeWayCompare(totalL, totalR);
220  }
221 
222  default:
223  Q_ASSERT_X(false, Q_FUNC_INFO, "Missing comparison case");
224  break;
225  }
226 
227  return 0;
228 }
229 
230 bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
231 {
232  Q_ASSERT(left.column() == right.column());
233 
234  const int result = compare(left, right);
235  if (result == 0)
236  {
237  const int subResult = compare(left.sibling(left.row(), m_subSortColumn), right.sibling(right.row(), m_subSortColumn));
238  // Qt inverses lessThan() result when ordered descending.
239  // For sub-sorting we have to do it manually.
240  // When both are ordered descending subResult must be double-inversed, which is the same as no inversion.
241  const bool inverseSubResult = (m_lastSortOrder != m_subSortOrder); // exactly one is descending
242  return (inverseSubResult ? (subResult > 0) : (subResult < 0));
243  }
244 
245  return result < 0;
246 }
247 
248 bool TransferListSortModel::filterAcceptsRow(const int sourceRow, const QModelIndex &sourceParent) const
249 {
250  return matchFilter(sourceRow, sourceParent)
251  && QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
252 }
253 
254 bool TransferListSortModel::matchFilter(const int sourceRow, const QModelIndex &sourceParent) const
255 {
256  const auto *model = qobject_cast<TransferListModel *>(sourceModel());
257  if (!model) return false;
258 
259  const BitTorrent::Torrent *torrent = model->torrentHandle(model->index(sourceRow, 0, sourceParent));
260  if (!torrent) return false;
261 
262  return m_filter.match(torrent);
263 }
bool setType(Type type)
static const QString AnyCategory
Definition: torrentfilter.h:64
bool setCategory(const QString &category)
static const TorrentIDSet AnyID
Definition: torrentfilter.h:65
bool setTag(const QString &tag)
static const QString AnyTag
Definition: torrentfilter.h:66
bool match(const BitTorrent::Torrent *torrent) const
bool setTorrentIDSet(const TorrentIDSet &idSet)
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
TransferListSortModel(QObject *parent=nullptr)
void setStatusFilter(TorrentFilter::Type filter)
void setTagFilter(const QString &tag)
int compare(const QModelIndex &left, const QModelIndex &right) const
void setCategoryFilter(const QString &category)
CachedSettingValue< int > m_subSortColumn
void setTrackerFilter(const QSet< BitTorrent::TorrentID > &torrentIDs)
bool matchFilter(int sourceRow, const QModelIndex &sourceParent) const
Utils::Compare::NaturalCompare< Qt::CaseInsensitive > m_naturalCompare
CachedSettingValue< int > m_subSortOrder
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
int threeWayCompare(const T &left, const T &right)