mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2024-11-28 21:38:51 +03:00
Merge label filter into TransferListSortModel
This also fixes a bug that when label filter contains special symbols from regex, the label filter may match torrents with multiple different labels.
This commit is contained in:
parent
8bafc5e216
commit
f235c0ae6c
4 changed files with 42 additions and 17 deletions
|
@ -45,6 +45,22 @@ void TransferListSortModel::setStatusFilter(const TorrentFilter::TorrentFilter &
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TransferListSortModel::setLabelFilter(QString const& label) {
|
||||||
|
if (!labelFilterEnabled || labelFilter != label) {
|
||||||
|
labelFilterEnabled = true;
|
||||||
|
labelFilter = label;
|
||||||
|
invalidateFilter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TransferListSortModel::disableLabelFilter() {
|
||||||
|
if (labelFilterEnabled) {
|
||||||
|
labelFilterEnabled = false;
|
||||||
|
labelFilter = QString();
|
||||||
|
invalidateFilter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const {
|
bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const {
|
||||||
const int column = sortColumn();
|
const int column = sortColumn();
|
||||||
|
|
||||||
|
@ -180,6 +196,7 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex
|
||||||
|
|
||||||
bool TransferListSortModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
|
bool TransferListSortModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
|
||||||
return matchStatusFilter(sourceRow, sourceParent)
|
return matchStatusFilter(sourceRow, sourceParent)
|
||||||
|
&& matchLabelFilter(sourceRow, sourceParent)
|
||||||
&& QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
|
&& QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,3 +242,14 @@ bool TransferListSortModel::matchStatusFilter(int sourceRow, const QModelIndex &
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool TransferListSortModel::matchLabelFilter(int sourceRow, const QModelIndex &sourceParent) const {
|
||||||
|
if (!labelFilterEnabled)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
QAbstractItemModel *model = sourceModel();
|
||||||
|
if (!model)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return model->index(sourceRow, TorrentModelItem::TR_LABEL, sourceParent).data().toString() == labelFilter;
|
||||||
|
}
|
||||||
|
|
|
@ -41,15 +41,21 @@ public:
|
||||||
TransferListSortModel(QObject *parent = 0);
|
TransferListSortModel(QObject *parent = 0);
|
||||||
|
|
||||||
void setStatusFilter(const TorrentFilter::TorrentFilter &filter);
|
void setStatusFilter(const TorrentFilter::TorrentFilter &filter);
|
||||||
|
void setLabelFilter(QString const& label);
|
||||||
|
void disableLabelFilter();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
|
bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
|
||||||
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
|
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
|
||||||
|
|
||||||
bool matchStatusFilter(int sourceRow, const QModelIndex &sourceParent) const;
|
bool matchStatusFilter(int sourceRow, const QModelIndex &sourceParent) const;
|
||||||
|
bool matchLabelFilter(int sourceRow, const QModelIndex &sourceParent) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TorrentFilter::TorrentFilter filter0;
|
TorrentFilter::TorrentFilter filter0;
|
||||||
|
|
||||||
|
bool labelFilterEnabled;
|
||||||
|
QString labelFilter;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // TRANSFERLISTSORTMODEL_H
|
#endif // TRANSFERLISTSORTMODEL_H
|
||||||
|
|
|
@ -81,16 +81,9 @@ TransferListWidget::TransferListWidget(QWidget *parent, MainWindow *main_window,
|
||||||
// Create transfer list model
|
// Create transfer list model
|
||||||
listModel = new TorrentModel(this);
|
listModel = new TorrentModel(this);
|
||||||
|
|
||||||
// Set Sort/Filter proxy
|
|
||||||
labelFilterModel = new QSortFilterProxyModel();
|
|
||||||
labelFilterModel->setDynamicSortFilter(true);
|
|
||||||
labelFilterModel->setSourceModel(listModel);
|
|
||||||
labelFilterModel->setFilterKeyColumn(TorrentModelItem::TR_LABEL);
|
|
||||||
labelFilterModel->setFilterRole(Qt::DisplayRole);
|
|
||||||
|
|
||||||
nameFilterModel = new TransferListSortModel();
|
nameFilterModel = new TransferListSortModel();
|
||||||
nameFilterModel->setDynamicSortFilter(true);
|
nameFilterModel->setDynamicSortFilter(true);
|
||||||
nameFilterModel->setSourceModel(labelFilterModel);
|
nameFilterModel->setSourceModel(listModel);
|
||||||
nameFilterModel->setFilterKeyColumn(TorrentModelItem::TR_NAME);
|
nameFilterModel->setFilterKeyColumn(TorrentModelItem::TR_NAME);
|
||||||
nameFilterModel->setFilterRole(Qt::DisplayRole);
|
nameFilterModel->setFilterRole(Qt::DisplayRole);
|
||||||
nameFilterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
|
nameFilterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
|
||||||
|
@ -165,7 +158,6 @@ TransferListWidget::~TransferListWidget() {
|
||||||
// Save settings
|
// Save settings
|
||||||
saveSettings();
|
saveSettings();
|
||||||
// Clean up
|
// Clean up
|
||||||
delete labelFilterModel;
|
|
||||||
delete nameFilterModel;
|
delete nameFilterModel;
|
||||||
delete listModel;
|
delete listModel;
|
||||||
delete listDelegate;
|
delete listDelegate;
|
||||||
|
@ -198,14 +190,14 @@ inline QString TransferListWidget::getHashFromRow(int row) const {
|
||||||
inline QModelIndex TransferListWidget::mapToSource(const QModelIndex &index) const {
|
inline QModelIndex TransferListWidget::mapToSource(const QModelIndex &index) const {
|
||||||
Q_ASSERT(index.isValid());
|
Q_ASSERT(index.isValid());
|
||||||
if (index.model() == nameFilterModel)
|
if (index.model() == nameFilterModel)
|
||||||
return labelFilterModel->mapToSource(nameFilterModel->mapToSource(index));
|
return nameFilterModel->mapToSource(index);
|
||||||
return labelFilterModel->mapToSource(index);
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline QModelIndex TransferListWidget::mapFromSource(const QModelIndex &index) const {
|
inline QModelIndex TransferListWidget::mapFromSource(const QModelIndex &index) const {
|
||||||
Q_ASSERT(index.isValid());
|
Q_ASSERT(index.isValid());
|
||||||
Q_ASSERT(index.model() == labelFilterModel);
|
Q_ASSERT(index.model() == nameFilterModel);
|
||||||
return nameFilterModel->mapFromSource(labelFilterModel->mapFromSource(index));
|
return nameFilterModel->mapFromSource(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TransferListWidget::torrentDoubleClicked(const QModelIndex& index) {
|
void TransferListWidget::torrentDoubleClicked(const QModelIndex& index) {
|
||||||
|
@ -895,15 +887,15 @@ void TransferListWidget::currentChanged(const QModelIndex& current, const QModel
|
||||||
|
|
||||||
void TransferListWidget::applyLabelFilter(QString label) {
|
void TransferListWidget::applyLabelFilter(QString label) {
|
||||||
if (label == "all") {
|
if (label == "all") {
|
||||||
labelFilterModel->setFilterRegExp(QRegExp());
|
nameFilterModel->disableLabelFilter();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (label == "none") {
|
if (label == "none") {
|
||||||
labelFilterModel->setFilterRegExp(QRegExp("^$"));
|
nameFilterModel->setLabelFilter(QString());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
qDebug("Applying Label filter: %s", qPrintable(label));
|
qDebug("Applying Label filter: %s", qPrintable(label));
|
||||||
labelFilterModel->setFilterRegExp(QRegExp("^" + QRegExp::escape(label) + "$", Qt::CaseSensitive));
|
nameFilterModel->setLabelFilter(label);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TransferListWidget::applyNameFilter(const QString& name) {
|
void TransferListWidget::applyNameFilter(const QString& name) {
|
||||||
|
|
|
@ -113,7 +113,6 @@ private:
|
||||||
TransferListDelegate *listDelegate;
|
TransferListDelegate *listDelegate;
|
||||||
TorrentModel *listModel;
|
TorrentModel *listModel;
|
||||||
TransferListSortModel *nameFilterModel;
|
TransferListSortModel *nameFilterModel;
|
||||||
QSortFilterProxyModel *labelFilterModel;
|
|
||||||
QBtSession* BTSession;
|
QBtSession* BTSession;
|
||||||
MainWindow *main_window;
|
MainWindow *main_window;
|
||||||
QShortcut *editHotkey;
|
QShortcut *editHotkey;
|
||||||
|
|
Loading…
Reference in a new issue