Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / gui / torrentcontentfiltermodel.cpp
blob59e2e47717cf24cf3b2471c5d5fc4bf35aa65101
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2006-2012 Christophe Dumez <chris@qbittorrent.org>
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 "torrentcontentfiltermodel.h"
31 #include "torrentcontentmodel.h"
33 TorrentContentFilterModel::TorrentContentFilterModel(QObject *parent)
34 : QSortFilterProxyModel(parent)
35 , m_model(new TorrentContentModel(this))
37 connect(m_model, &TorrentContentModel::filteredFilesChanged, this, &TorrentContentFilterModel::filteredFilesChanged);
38 setSourceModel(m_model);
39 // Filter settings
40 setFilterKeyColumn(TorrentContentModelItem::COL_NAME);
41 setFilterRole(TorrentContentModel::UnderlyingDataRole);
42 setDynamicSortFilter(true);
43 setSortCaseSensitivity(Qt::CaseInsensitive);
44 setSortRole(TorrentContentModel::UnderlyingDataRole);
47 TorrentContentModel *TorrentContentFilterModel::model() const
49 return m_model;
52 TorrentContentModelItem::ItemType TorrentContentFilterModel::itemType(const QModelIndex &index) const
54 return m_model->itemType(mapToSource(index));
57 int TorrentContentFilterModel::getFileIndex(const QModelIndex &index) const
59 return m_model->getFileIndex(mapToSource(index));
62 QModelIndex TorrentContentFilterModel::parent(const QModelIndex &child) const
64 if (!child.isValid()) return {};
66 QModelIndex sourceParent = m_model->parent(mapToSource(child));
67 if (!sourceParent.isValid()) return {};
69 return mapFromSource(sourceParent);
72 bool TorrentContentFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
74 if (m_model->itemType(m_model->index(sourceRow, 0, sourceParent)) == TorrentContentModelItem::FolderType)
76 // accept folders if they have at least one filtered item
77 return hasFiltered(m_model->index(sourceRow, 0, sourceParent));
80 return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
83 bool TorrentContentFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
85 switch (sortColumn())
87 case TorrentContentModelItem::COL_NAME:
89 const TorrentContentModelItem::ItemType leftType = m_model->itemType(m_model->index(left.row(), 0, left.parent()));
90 const TorrentContentModelItem::ItemType rightType = m_model->itemType(m_model->index(right.row(), 0, right.parent()));
92 if (leftType == rightType)
94 const QString strL = left.data().toString();
95 const QString strR = right.data().toString();
96 return m_naturalLessThan(strL, strR);
98 if ((leftType == TorrentContentModelItem::FolderType) && (sortOrder() == Qt::AscendingOrder))
100 return true;
103 return false;
105 default:
106 return QSortFilterProxyModel::lessThan(left, right);
110 void TorrentContentFilterModel::selectAll()
112 for (int i = 0; i < rowCount(); ++i)
113 setData(index(i, 0), Qt::Checked, Qt::CheckStateRole);
115 emit dataChanged(index(0, 0), index((rowCount() - 1), (columnCount() - 1)));
118 void TorrentContentFilterModel::selectNone()
120 for (int i = 0; i < rowCount(); ++i)
121 setData(index(i, 0), Qt::Unchecked, Qt::CheckStateRole);
123 emit dataChanged(index(0, 0), index((rowCount() - 1), (columnCount() - 1)));
126 bool TorrentContentFilterModel::hasFiltered(const QModelIndex &folder) const
128 // this should be called only with folders
129 // check if the folder name itself matches the filter string
130 QString name = folder.data().toString();
131 if (name.contains(filterRegularExpression()))
132 return true;
133 for (int child = 0; child < m_model->rowCount(folder); ++child)
135 QModelIndex childIndex = m_model->index(child, 0, folder);
136 if (m_model->hasChildren(childIndex))
138 if (hasFiltered(childIndex))
139 return true;
140 continue;
142 name = childIndex.data().toString();
143 if (name.contains(filterRegularExpression()))
144 return true;
147 return false;