Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / gui / search / searchsortmodel.cpp
blob6ab34363298c5ed1f6889c23cf2af8f5dac193a5
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2013 sledgehammer999 <hammered999@gmail.com>
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.
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.
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.
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.
29 #include "searchsortmodel.h"
31 #include "base/global.h"
33 SearchSortModel::SearchSortModel(QObject *parent)
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)
43 setSortRole(UnderlyingDataRole);
44 setFilterRole(UnderlyingDataRole);
47 void SearchSortModel::enableNameFilter(const bool enabled)
49 m_isNameFilterEnabled = enabled;
52 void SearchSortModel::setNameFilter(const QString &searchTerm)
54 m_searchTerm = searchTerm;
55 if ((searchTerm.length() > 2)
56 && searchTerm.startsWith(QLatin1Char('"')) && searchTerm.endsWith(QLatin1Char('"')))
58 m_searchTermWords = QStringList(m_searchTerm.mid(1, m_searchTerm.length() - 2));
60 else
62 m_searchTermWords = searchTerm.split(QLatin1Char(' '), Qt::SkipEmptyParts);
66 void SearchSortModel::setSizeFilter(const qint64 minSize, const qint64 maxSize)
68 m_minSize = std::max(static_cast<qint64>(0), minSize);
69 m_maxSize = std::max(static_cast<qint64>(-1), maxSize);
72 void SearchSortModel::setSeedsFilter(const int minSeeds, const int maxSeeds)
74 m_minSeeds = std::max(0, minSeeds);
75 m_maxSeeds = std::max(-1, maxSeeds);
78 void SearchSortModel::setLeechesFilter(const int minLeeches, const int maxLeeches)
80 m_minLeeches = std::max(0, minLeeches);
81 m_maxLeeches = std::max(-1, maxLeeches);
84 bool SearchSortModel::isNameFilterEnabled() const
86 return m_isNameFilterEnabled;
89 QString SearchSortModel::searchTerm() const
91 return m_searchTerm;
94 int SearchSortModel::minSeeds() const
96 return m_minSeeds;
99 int SearchSortModel::maxSeeds() const
101 return m_maxSeeds;
104 qint64 SearchSortModel::minSize() const
106 return m_minSize;
109 qint64 SearchSortModel::maxSize() const
111 return m_maxSize;
114 bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
116 switch (sortColumn())
118 case NAME:
119 case ENGINE_URL:
121 const QString strL = left.data().toString();
122 const QString strR = right.data().toString();
123 return m_naturalLessThan(strL, strR);
125 break;
126 default:
127 return base::lessThan(left, right);
131 bool SearchSortModel::filterAcceptsRow(const int sourceRow, const QModelIndex &sourceParent) const
133 const QAbstractItemModel *const sourceModel = this->sourceModel();
135 if (m_isNameFilterEnabled && !m_searchTerm.isEmpty())
137 const QString name = sourceModel->data(sourceModel->index(sourceRow, NAME, sourceParent), UnderlyingDataRole).toString();
138 for (const QString &word : asConst(m_searchTermWords))
140 if (!name.contains(word, Qt::CaseInsensitive))
141 return false;
145 if ((m_minSize > 0) || (m_maxSize >= 0))
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;
153 if ((m_minSeeds > 0) || (m_maxSeeds >= 0))
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;
161 if ((m_minLeeches > 0) || (m_maxLeeches >= 0))
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;
169 return base::filterAcceptsRow(sourceRow, sourceParent);