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
)
35 , m_isNameFilterEnabled(false)
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));
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
94 int SearchSortModel::minSeeds() const
99 int SearchSortModel::maxSeeds() const
104 qint64
SearchSortModel::minSize() const
109 qint64
SearchSortModel::maxSize() const
114 bool SearchSortModel::lessThan(const QModelIndex
&left
, const QModelIndex
&right
) const
116 switch (sortColumn())
121 const QString strL
= left
.data().toString();
122 const QString strR
= right
.data().toString();
123 return m_naturalLessThan(strL
, strR
);
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
))
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
)))
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
)))
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
)))
169 return base::filterAcceptsRow(sourceRow
, sourceParent
);