WebUI: Minor optimizations to the login page
[qBittorrent.git] / src / gui / torrentcontentfiltermodel.cpp
blob357ba217b2345888a1e4ed5683ec0c5c81b92350
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006-2012 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "torrentcontentfiltermodel.h"
32 #include "torrentcontentmodel.h"
34 TorrentContentFilterModel::TorrentContentFilterModel(QObject *parent)
35 : QSortFilterProxyModel(parent)
37 // Filter settings
38 setFilterKeyColumn(TorrentContentModelItem::COL_NAME);
39 setFilterRole(TorrentContentModel::UnderlyingDataRole);
40 setDynamicSortFilter(true);
41 setSortCaseSensitivity(Qt::CaseInsensitive);
42 setSortRole(TorrentContentModel::UnderlyingDataRole);
45 void TorrentContentFilterModel::setSourceModel(TorrentContentModel *model)
47 m_model = model;
48 QSortFilterProxyModel::setSourceModel(m_model);
51 TorrentContentModelItem::ItemType TorrentContentFilterModel::itemType(const QModelIndex &index) const
53 return m_model->itemType(mapToSource(index));
56 int TorrentContentFilterModel::getFileIndex(const QModelIndex &index) const
58 return m_model->getFileIndex(mapToSource(index));
61 QModelIndex TorrentContentFilterModel::parent(const QModelIndex &child) const
63 if (!child.isValid())
64 return {};
66 QModelIndex sourceParent = m_model->parent(mapToSource(child));
67 if (!sourceParent.isValid())
68 return {};
70 return mapFromSource(sourceParent);
73 bool TorrentContentFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
75 if (m_model->itemType(m_model->index(sourceRow, 0, sourceParent)) == TorrentContentModelItem::FolderType)
77 // accept folders if they have at least one filtered item
78 return hasFiltered(m_model->index(sourceRow, 0, sourceParent));
81 return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
84 bool TorrentContentFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
86 switch (sortColumn())
88 case TorrentContentModelItem::COL_NAME:
90 const TorrentContentModelItem::ItemType leftType = m_model->itemType(m_model->index(left.row(), 0, left.parent()));
91 const TorrentContentModelItem::ItemType rightType = m_model->itemType(m_model->index(right.row(), 0, right.parent()));
93 if (leftType == rightType)
95 const QString strL = left.data().toString();
96 const QString strR = right.data().toString();
97 return m_naturalLessThan(strL, strR);
100 if ((leftType == TorrentContentModelItem::FolderType) && (sortOrder() == Qt::AscendingOrder))
102 return true;
105 return false;
108 default:
109 return QSortFilterProxyModel::lessThan(left, right);
113 bool TorrentContentFilterModel::hasFiltered(const QModelIndex &folder) const
115 // this should be called only with folders
116 // check if the folder name itself matches the filter string
117 QString name = folder.data().toString();
118 if (name.contains(filterRegularExpression()))
119 return true;
121 for (int child = 0; child < m_model->rowCount(folder); ++child)
123 QModelIndex childIndex = m_model->index(child, 0, folder);
124 if (m_model->hasChildren(childIndex))
126 if (hasFiltered(childIndex))
127 return true;
129 continue;
132 name = childIndex.data().toString();
133 if (name.contains(filterRegularExpression()))
134 return true;
137 return false;