qBittorrent
searchsortmodel.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2013 sledgehammer999 <hammered999@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 "searchsortmodel.h"
30 
31 #include "base/global.h"
32 
34  : base(parent)
35  , m_isNameFilterEnabled(false)
36  , m_minSeeds(0)
37  , m_maxSeeds(-1)
38  , m_minLeeches(0)
39  , m_maxLeeches(-1)
40  , m_minSize(0)
41  , m_maxSize(-1)
42 {
43  setSortRole(UnderlyingDataRole);
44  setFilterRole(UnderlyingDataRole);
45 }
46 
47 void SearchSortModel::enableNameFilter(const bool enabled)
48 {
49  m_isNameFilterEnabled = enabled;
50 }
51 
52 void SearchSortModel::setNameFilter(const QString &searchTerm)
53 {
55  if ((searchTerm.length() > 2)
56  && searchTerm.startsWith(QLatin1Char('"')) && searchTerm.endsWith(QLatin1Char('"')))
57  {
58  m_searchTermWords = QStringList(m_searchTerm.mid(1, m_searchTerm.length() - 2));
59  }
60  else
61  {
62  m_searchTermWords = searchTerm.split(QLatin1Char(' '), Qt::SkipEmptyParts);
63  }
64 }
65 
66 void SearchSortModel::setSizeFilter(const qint64 minSize, const qint64 maxSize)
67 {
68  m_minSize = std::max(static_cast<qint64>(0), minSize);
69  m_maxSize = std::max(static_cast<qint64>(-1), maxSize);
70 }
71 
72 void SearchSortModel::setSeedsFilter(const int minSeeds, const int maxSeeds)
73 {
74  m_minSeeds = std::max(0, minSeeds);
75  m_maxSeeds = std::max(-1, maxSeeds);
76 }
77 
78 void SearchSortModel::setLeechesFilter(const int minLeeches, const int maxLeeches)
79 {
80  m_minLeeches = std::max(0, minLeeches);
81  m_maxLeeches = std::max(-1, maxLeeches);
82 }
83 
85 {
86  return m_isNameFilterEnabled;
87 }
88 
90 {
91  return m_searchTerm;
92 }
93 
95 {
96  return m_minSeeds;
97 }
98 
100 {
101  return m_maxSeeds;
102 }
103 
105 {
106  return m_minSize;
107 }
108 
110 {
111  return m_maxSize;
112 }
113 
114 bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
115 {
116  switch (sortColumn())
117  {
118  case NAME:
119  case ENGINE_URL:
120  {
121  const QString strL = left.data().toString();
122  const QString strR = right.data().toString();
123  return m_naturalLessThan(strL, strR);
124  }
125  break;
126  default:
127  return base::lessThan(left, right);
128  };
129 }
130 
131 bool SearchSortModel::filterAcceptsRow(const int sourceRow, const QModelIndex &sourceParent) const
132 {
133  const QAbstractItemModel *const sourceModel = this->sourceModel();
134 
135  if (m_isNameFilterEnabled && !m_searchTerm.isEmpty())
136  {
137  const QString name = sourceModel->data(sourceModel->index(sourceRow, NAME, sourceParent), UnderlyingDataRole).toString();
138  for (const QString &word : asConst(m_searchTermWords))
139  {
140  if (!name.contains(word, Qt::CaseInsensitive))
141  return false;
142  }
143  }
144 
145  if ((m_minSize > 0) || (m_maxSize >= 0))
146  {
147  const qlonglong size = sourceModel->data(sourceModel->index(sourceRow, SIZE, sourceParent), UnderlyingDataRole).toLongLong();
148  if (((m_minSize > 0) && (size < m_minSize))
149  || ((m_maxSize > 0) && (size > m_maxSize)))
150  return false;
151  }
152 
153  if ((m_minSeeds > 0) || (m_maxSeeds >= 0))
154  {
155  const int seeds = sourceModel->data(sourceModel->index(sourceRow, SEEDS, sourceParent), UnderlyingDataRole).toInt();
156  if (((m_minSeeds > 0) && (seeds < m_minSeeds))
157  || ((m_maxSeeds > 0) && (seeds > m_maxSeeds)))
158  return false;
159  }
160 
161  if ((m_minLeeches > 0) || (m_maxLeeches >= 0))
162  {
163  const int leeches = sourceModel->data(sourceModel->index(sourceRow, LEECHES, sourceParent), UnderlyingDataRole).toInt();
164  if (((m_minLeeches > 0) && (leeches < m_minLeeches))
165  || ((m_maxLeeches > 0) && (leeches > m_maxLeeches)))
166  return false;
167  }
168 
169  return base::filterAcceptsRow(sourceRow, sourceParent);
170 }
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
QString searchTerm() const
Utils::Compare::NaturalLessThan< Qt::CaseInsensitive > m_naturalLessThan
int maxSeeds() const
void setLeechesFilter(int minLeeches, int maxLeeches)
Sets parameters for filtering by leeches number.
QSortFilterProxyModel base
qint64 minSize() const
SearchSortModel(QObject *parent=nullptr)
void setNameFilter(const QString &searchTerm={})
int minSeeds() const
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
void enableNameFilter(bool enabled)
qint64 maxSize() const
void setSeedsFilter(int minSeeds, int maxSeeds)
Sets parameters for filtering by seeds number.
void setSizeFilter(qint64 minSize, qint64 maxSize)
Sets parameters for filtering by size.
bool isNameFilterEnabled() const
QStringList m_searchTermWords
constexpr std::add_const_t< T > & asConst(T &t) noexcept
Definition: global.h:42