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
)
36 setSortRole(UnderlyingDataRole
);
37 setFilterRole(UnderlyingDataRole
);
40 void SearchSortModel::enableNameFilter(const bool enabled
)
42 m_isNameFilterEnabled
= enabled
;
45 void SearchSortModel::setNameFilter(const QString
&searchTerm
)
47 m_searchTerm
= searchTerm
;
48 if ((searchTerm
.length() > 2) && searchTerm
.startsWith(u
'"') && searchTerm
.endsWith(u
'"'))
49 m_searchTermWords
= QStringList(m_searchTerm
.mid(1, m_searchTerm
.length() - 2));
51 m_searchTermWords
= searchTerm
.split(u
' ', Qt::SkipEmptyParts
);
54 void SearchSortModel::setSizeFilter(const qint64 minSize
, const qint64 maxSize
)
56 m_minSize
= std::max(static_cast<qint64
>(0), minSize
);
57 m_maxSize
= std::max(static_cast<qint64
>(-1), maxSize
);
60 void SearchSortModel::setSeedsFilter(const int minSeeds
, const int maxSeeds
)
62 m_minSeeds
= std::max(0, minSeeds
);
63 m_maxSeeds
= std::max(-1, maxSeeds
);
66 void SearchSortModel::setLeechesFilter(const int minLeeches
, const int maxLeeches
)
68 m_minLeeches
= std::max(0, minLeeches
);
69 m_maxLeeches
= std::max(-1, maxLeeches
);
72 bool SearchSortModel::isNameFilterEnabled() const
74 return m_isNameFilterEnabled
;
77 QString
SearchSortModel::searchTerm() const
82 int SearchSortModel::minSeeds() const
87 int SearchSortModel::maxSeeds() const
92 qint64
SearchSortModel::minSize() const
97 qint64
SearchSortModel::maxSize() const
102 bool SearchSortModel::lessThan(const QModelIndex
&left
, const QModelIndex
&right
) const
104 switch (sortColumn())
109 const QString strL
= left
.data().toString();
110 const QString strR
= right
.data().toString();
111 return m_naturalLessThan(strL
, strR
);
115 return base::lessThan(left
, right
);
119 bool SearchSortModel::filterAcceptsRow(const int sourceRow
, const QModelIndex
&sourceParent
) const
121 const QAbstractItemModel
*const sourceModel
= this->sourceModel();
123 if (m_isNameFilterEnabled
&& !m_searchTerm
.isEmpty())
125 const QString name
= sourceModel
->data(sourceModel
->index(sourceRow
, NAME
, sourceParent
), UnderlyingDataRole
).toString();
126 for (const QString
&word
: asConst(m_searchTermWords
))
128 if (!name
.contains(word
, Qt::CaseInsensitive
))
133 if ((m_minSize
> 0) || (m_maxSize
>= 0))
135 const qlonglong size
= sourceModel
->data(sourceModel
->index(sourceRow
, SIZE
, sourceParent
), UnderlyingDataRole
).toLongLong();
136 if (((m_minSize
> 0) && (size
< m_minSize
))
137 || ((m_maxSize
> 0) && (size
> m_maxSize
)))
141 if ((m_minSeeds
> 0) || (m_maxSeeds
>= 0))
143 const int seeds
= sourceModel
->data(sourceModel
->index(sourceRow
, SEEDS
, sourceParent
), UnderlyingDataRole
).toInt();
144 if (((m_minSeeds
> 0) && (seeds
< m_minSeeds
))
145 || ((m_maxSeeds
> 0) && (seeds
> m_maxSeeds
)))
149 if ((m_minLeeches
> 0) || (m_maxLeeches
>= 0))
151 const int leeches
= sourceModel
->data(sourceModel
->index(sourceRow
, LEECHES
, sourceParent
), UnderlyingDataRole
).toInt();
152 if (((m_minLeeches
> 0) && (leeches
< m_minLeeches
))
153 || ((m_maxLeeches
> 0) && (leeches
> m_maxLeeches
)))
157 return base::filterAcceptsRow(sourceRow
, sourceParent
);