qBittorrent
TransferListSortModel Class Referencefinal

#include <transferlistsortmodel.h>

Inheritance diagram for TransferListSortModel:
Collaboration diagram for TransferListSortModel:

Public Member Functions

 TransferListSortModel (QObject *parent=nullptr)
 
void sort (int column, Qt::SortOrder order=Qt::AscendingOrder) override
 
void setStatusFilter (TorrentFilter::Type filter)
 
void setCategoryFilter (const QString &category)
 
void disableCategoryFilter ()
 
void setTagFilter (const QString &tag)
 
void disableTagFilter ()
 
void setTrackerFilter (const QSet< BitTorrent::TorrentID > &torrentIDs)
 
void disableTrackerFilter ()
 

Private Member Functions

int compare (const QModelIndex &left, const QModelIndex &right) const
 
bool lessThan (const QModelIndex &left, const QModelIndex &right) const override
 
bool filterAcceptsRow (int sourceRow, const QModelIndex &sourceParent) const override
 
bool matchFilter (int sourceRow, const QModelIndex &sourceParent) const
 

Private Attributes

TorrentFilter m_filter
 
CachedSettingValue< int > m_subSortColumn
 
CachedSettingValue< int > m_subSortOrder
 
int m_lastSortColumn = -1
 
int m_lastSortOrder = 0
 
Utils::Compare::NaturalCompare< Qt::CaseInsensitive > m_naturalCompare
 

Detailed Description

Definition at line 42 of file transferlistsortmodel.h.

Constructor & Destructor Documentation

◆ TransferListSortModel()

TransferListSortModel::TransferListSortModel ( QObject *  parent = nullptr)
explicit

Definition at line 94 of file transferlistsortmodel.cpp.

95  : QSortFilterProxyModel {parent}
96  , m_subSortColumn {"TransferList/SubSortColumn", TransferListModel::TR_NAME, adjustSubSortColumn}
97  , m_subSortOrder {"TransferList/SubSortOrder", 0}
98 {
100 }
CachedSettingValue< int > m_subSortColumn
CachedSettingValue< int > m_subSortOrder

References TransferListModel::UnderlyingDataRole.

Member Function Documentation

◆ compare()

int TransferListSortModel::compare ( const QModelIndex &  left,
const QModelIndex &  right 
) const
private

Definition at line 157 of file transferlistsortmodel.cpp.

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 }
Utils::Compare::NaturalCompare< Qt::CaseInsensitive > m_naturalCompare
int threeWayCompare(const T &left, const T &right)

References TransferListModel::AdditionalUnderlyingDataRole, anonymous_namespace{transferlistsortmodel.cpp}::customCompare(), m_naturalCompare, anonymous_namespace{transferlistsortmodel.cpp}::threeWayCompare(), TransferListModel::TR_ADD_DATE, TransferListModel::TR_AMOUNT_DOWNLOADED, TransferListModel::TR_AMOUNT_DOWNLOADED_SESSION, TransferListModel::TR_AMOUNT_LEFT, TransferListModel::TR_AMOUNT_UPLOADED, TransferListModel::TR_AMOUNT_UPLOADED_SESSION, TransferListModel::TR_AVAILABILITY, TransferListModel::TR_CATEGORY, TransferListModel::TR_COMPLETED, TransferListModel::TR_DLLIMIT, TransferListModel::TR_DLSPEED, TransferListModel::TR_ETA, TransferListModel::TR_LAST_ACTIVITY, TransferListModel::TR_NAME, TransferListModel::TR_PEERS, TransferListModel::TR_PROGRESS, TransferListModel::TR_QUEUE_POSITION, TransferListModel::TR_RATIO, TransferListModel::TR_RATIO_LIMIT, TransferListModel::TR_SAVE_PATH, TransferListModel::TR_SEED_DATE, TransferListModel::TR_SEEDS, TransferListModel::TR_SEEN_COMPLETE_DATE, TransferListModel::TR_SIZE, TransferListModel::TR_STATUS, TransferListModel::TR_TAGS, TransferListModel::TR_TIME_ELAPSED, TransferListModel::TR_TOTAL_SIZE, TransferListModel::TR_TRACKER, TransferListModel::TR_UPLIMIT, TransferListModel::TR_UPSPEED, and TransferListModel::UnderlyingDataRole.

Referenced by lessThan().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ disableCategoryFilter()

void TransferListSortModel::disableCategoryFilter ( )

Definition at line 127 of file transferlistsortmodel.cpp.

128 {
130  invalidateFilter();
131 }
static const QString AnyCategory
Definition: torrentfilter.h:64
bool setCategory(const QString &category)

References TorrentFilter::AnyCategory, m_filter, and TorrentFilter::setCategory().

Referenced by TransferListWidget::applyCategoryFilter().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ disableTagFilter()

void TransferListSortModel::disableTagFilter ( )

Definition at line 139 of file transferlistsortmodel.cpp.

140 {
142  invalidateFilter();
143 }
bool setTag(const QString &tag)
static const QString AnyTag
Definition: torrentfilter.h:66

References TorrentFilter::AnyTag, m_filter, and TorrentFilter::setTag().

Referenced by TransferListWidget::applyTagFilter().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ disableTrackerFilter()

void TransferListSortModel::disableTrackerFilter ( )

Definition at line 151 of file transferlistsortmodel.cpp.

152 {
154  invalidateFilter();
155 }
static const TorrentIDSet AnyID
Definition: torrentfilter.h:65
bool setTorrentIDSet(const TorrentIDSet &idSet)

References TorrentFilter::AnyID, m_filter, and TorrentFilter::setTorrentIDSet().

Referenced by TransferListWidget::applyTrackerFilterAll().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ filterAcceptsRow()

bool TransferListSortModel::filterAcceptsRow ( int  sourceRow,
const QModelIndex &  sourceParent 
) const
overrideprivate

Definition at line 248 of file transferlistsortmodel.cpp.

249 {
250  return matchFilter(sourceRow, sourceParent)
251  && QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
252 }
bool matchFilter(int sourceRow, const QModelIndex &sourceParent) const

References matchFilter().

Here is the call graph for this function:

◆ lessThan()

bool TransferListSortModel::lessThan ( const QModelIndex &  left,
const QModelIndex &  right 
) const
overrideprivate

Definition at line 230 of file transferlistsortmodel.cpp.

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 }
int compare(const QModelIndex &left, const QModelIndex &right) const

References compare(), m_lastSortOrder, m_subSortColumn, and m_subSortOrder.

Here is the call graph for this function:

◆ matchFilter()

bool TransferListSortModel::matchFilter ( int  sourceRow,
const QModelIndex &  sourceParent 
) const
private

Definition at line 254 of file transferlistsortmodel.cpp.

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 match(const BitTorrent::Torrent *torrent) const

References m_filter, and TorrentFilter::match().

Referenced by filterAcceptsRow().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setCategoryFilter()

void TransferListSortModel::setCategoryFilter ( const QString &  category)

Definition at line 121 of file transferlistsortmodel.cpp.

122 {
123  if (m_filter.setCategory(category))
124  invalidateFilter();
125 }

References m_filter, and TorrentFilter::setCategory().

Referenced by TransferListWidget::applyCategoryFilter().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setStatusFilter()

void TransferListSortModel::setStatusFilter ( TorrentFilter::Type  filter)

Definition at line 115 of file transferlistsortmodel.cpp.

116 {
117  if (m_filter.setType(filter))
118  invalidateFilter();
119 }
bool setType(Type type)

References m_filter, and TorrentFilter::setType().

Referenced by TransferListWidget::applyStatusFilter().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setTagFilter()

void TransferListSortModel::setTagFilter ( const QString &  tag)

Definition at line 133 of file transferlistsortmodel.cpp.

134 {
135  if (m_filter.setTag(tag))
136  invalidateFilter();
137 }

References m_filter, and TorrentFilter::setTag().

Referenced by TransferListWidget::applyTagFilter().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setTrackerFilter()

void TransferListSortModel::setTrackerFilter ( const QSet< BitTorrent::TorrentID > &  torrentIDs)

Definition at line 145 of file transferlistsortmodel.cpp.

146 {
147  if (m_filter.setTorrentIDSet(torrentIDs))
148  invalidateFilter();
149 }

References m_filter, and TorrentFilter::setTorrentIDSet().

Referenced by TransferListWidget::applyTrackerFilter().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sort()

void TransferListSortModel::sort ( int  column,
Qt::SortOrder  order = Qt::AscendingOrder 
)
override

Definition at line 102 of file transferlistsortmodel.cpp.

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 }

References m_lastSortColumn, m_lastSortOrder, m_subSortColumn, and m_subSortOrder.

Member Data Documentation

◆ m_filter

◆ m_lastSortColumn

int TransferListSortModel::m_lastSortColumn = -1
private

Definition at line 70 of file transferlistsortmodel.h.

Referenced by sort().

◆ m_lastSortOrder

int TransferListSortModel::m_lastSortOrder = 0
private

Definition at line 71 of file transferlistsortmodel.h.

Referenced by lessThan(), and sort().

◆ m_naturalCompare

Utils::Compare::NaturalCompare<Qt::CaseInsensitive> TransferListSortModel::m_naturalCompare
private

Definition at line 73 of file transferlistsortmodel.h.

Referenced by compare().

◆ m_subSortColumn

CachedSettingValue<int> TransferListSortModel::m_subSortColumn
private

Definition at line 68 of file transferlistsortmodel.h.

Referenced by lessThan(), and sort().

◆ m_subSortOrder

CachedSettingValue<int> TransferListSortModel::m_subSortOrder
private

Definition at line 69 of file transferlistsortmodel.h.

Referenced by lessThan(), and sort().


The documentation for this class was generated from the following files: