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:
Ivan Sorokin 2014-11-09 02:49:21 +03:00
parent 8bafc5e216
commit f235c0ae6c
4 changed files with 42 additions and 17 deletions

View file

@ -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 {
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 {
return matchStatusFilter(sourceRow, sourceParent)
&& matchLabelFilter(sourceRow, sourceParent)
&& QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
@ -225,3 +242,14 @@ bool TransferListSortModel::matchStatusFilter(int sourceRow, const QModelIndex &
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;
}

View file

@ -41,15 +41,21 @@ public:
TransferListSortModel(QObject *parent = 0);
void setStatusFilter(const TorrentFilter::TorrentFilter &filter);
void setLabelFilter(QString const& label);
void disableLabelFilter();
private:
bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
bool matchStatusFilter(int sourceRow, const QModelIndex &sourceParent) const;
bool matchLabelFilter(int sourceRow, const QModelIndex &sourceParent) const;
private:
TorrentFilter::TorrentFilter filter0;
bool labelFilterEnabled;
QString labelFilter;
};
#endif // TRANSFERLISTSORTMODEL_H

View file

@ -81,16 +81,9 @@ TransferListWidget::TransferListWidget(QWidget *parent, MainWindow *main_window,
// Create transfer list model
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->setDynamicSortFilter(true);
nameFilterModel->setSourceModel(labelFilterModel);
nameFilterModel->setSourceModel(listModel);
nameFilterModel->setFilterKeyColumn(TorrentModelItem::TR_NAME);
nameFilterModel->setFilterRole(Qt::DisplayRole);
nameFilterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
@ -165,7 +158,6 @@ TransferListWidget::~TransferListWidget() {
// Save settings
saveSettings();
// Clean up
delete labelFilterModel;
delete nameFilterModel;
delete listModel;
delete listDelegate;
@ -198,14 +190,14 @@ inline QString TransferListWidget::getHashFromRow(int row) const {
inline QModelIndex TransferListWidget::mapToSource(const QModelIndex &index) const {
Q_ASSERT(index.isValid());
if (index.model() == nameFilterModel)
return labelFilterModel->mapToSource(nameFilterModel->mapToSource(index));
return labelFilterModel->mapToSource(index);
return nameFilterModel->mapToSource(index);
return index;
}
inline QModelIndex TransferListWidget::mapFromSource(const QModelIndex &index) const {
Q_ASSERT(index.isValid());
Q_ASSERT(index.model() == labelFilterModel);
return nameFilterModel->mapFromSource(labelFilterModel->mapFromSource(index));
Q_ASSERT(index.model() == nameFilterModel);
return nameFilterModel->mapFromSource(index);
}
void TransferListWidget::torrentDoubleClicked(const QModelIndex& index) {
@ -895,15 +887,15 @@ void TransferListWidget::currentChanged(const QModelIndex& current, const QModel
void TransferListWidget::applyLabelFilter(QString label) {
if (label == "all") {
labelFilterModel->setFilterRegExp(QRegExp());
nameFilterModel->disableLabelFilter();
return;
}
if (label == "none") {
labelFilterModel->setFilterRegExp(QRegExp("^$"));
nameFilterModel->setLabelFilter(QString());
return;
}
qDebug("Applying Label filter: %s", qPrintable(label));
labelFilterModel->setFilterRegExp(QRegExp("^" + QRegExp::escape(label) + "$", Qt::CaseSensitive));
nameFilterModel->setLabelFilter(label);
}
void TransferListWidget::applyNameFilter(const QString& name) {

View file

@ -113,7 +113,6 @@ private:
TransferListDelegate *listDelegate;
TorrentModel *listModel;
TransferListSortModel *nameFilterModel;
QSortFilterProxyModel *labelFilterModel;
QBtSession* BTSession;
MainWindow *main_window;
QShortcut *editHotkey;