Browse Source

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.
adaptive-webui-19844
Ivan Sorokin 10 years ago
parent
commit
f235c0ae6c
  1. 28
      src/transferlistsortmodel.cpp
  2. 6
      src/transferlistsortmodel.h
  3. 24
      src/transferlistwidget.cpp
  4. 1
      src/transferlistwidget.h

28
src/transferlistsortmodel.cpp

@ -45,6 +45,22 @@ void TransferListSortModel::setStatusFilter(const TorrentFilter::TorrentFilter & @@ -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 @@ -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 & @@ -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;
}

6
src/transferlistsortmodel.h

@ -41,15 +41,21 @@ public: @@ -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

24
src/transferlistwidget.cpp

@ -81,16 +81,9 @@ TransferListWidget::TransferListWidget(QWidget *parent, MainWindow *main_window, @@ -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() { @@ -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 { @@ -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 @@ -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) {

1
src/transferlistwidget.h

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

Loading…
Cancel
Save