source: trunk/qt/torrent-filter.cc @ 8216

Last change on this file since 8216 was 8216, checked in by charles, 13 years ago

(qt) better integration of sort & filter modes into the preferences mechanism

File size: 4.9 KB
Line 
1/*
2 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id:$
11 */
12
13#include <iostream>
14
15#include "filters.h"
16#include "prefs.h"
17#include "torrent.h"
18#include "torrent-filter.h"
19#include "torrent-model.h"
20
21TorrentFilter :: TorrentFilter( Prefs& prefs ):
22    myPrefs( prefs ),
23    myTextMode( FILTER_BY_NAME )
24{
25    // listen for changes to the preferences to know when to refilter / resort
26    connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(refreshPref(int)));
27
28    // initialize our state from the current prefs
29    QList<int> initKeys;
30    initKeys << Prefs :: SORT_MODE
31             << Prefs :: FILTER_MODE;
32    foreach( int key, initKeys )
33        refreshPref( key );
34}
35
36TorrentFilter :: ~TorrentFilter( )
37{
38}
39
40void
41TorrentFilter :: refreshPref( int key )
42{
43    switch( key )
44    {
45        case Prefs :: SORT_MODE:
46        case Prefs :: SORT_REVERSED:
47            sort( 0, myPrefs.getBool(Prefs::SORT_REVERSED) ? Qt::AscendingOrder : Qt::DescendingOrder );
48            invalidate( );
49            break;
50        case Prefs :: FILTER_MODE:
51            invalidateFilter( );
52            break;
53    }
54}
55
56/***
57****
58***/
59
60void
61TorrentFilter :: setTextMode( int i )
62{
63    if( myTextMode != i )
64    {
65        myTextMode = TextMode( i );
66        invalidateFilter( );
67    }
68}
69
70void
71TorrentFilter :: setText( QString text )
72{
73    QString trimmed = text.trimmed( );
74
75    if( myText != trimmed )
76    {
77        myText = trimmed;
78        invalidateFilter( );
79    }
80}
81
82/***
83****
84***/
85
86bool
87TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) const
88{
89    const Torrent * a = sourceModel()->data( left, TorrentModel::TorrentRole ).value<const Torrent*>();
90    const Torrent * b = sourceModel()->data( right, TorrentModel::TorrentRole ).value<const Torrent*>();
91    bool less;
92
93    switch( myPrefs.get<SortMode>(Prefs::SORT_MODE).mode() )
94    {
95        case SortMode :: SORT_BY_SIZE:
96            less = a->sizeWhenDone() < b->sizeWhenDone();
97            break;
98        case SortMode :: SORT_BY_ACTIVITY:
99            less = a->downloadSpeed() + a->uploadSpeed() < b->downloadSpeed() + b->uploadSpeed();
100            break;
101        case SortMode :: SORT_BY_AGE:
102            less = a->dateAdded() < b->dateAdded();
103            break;
104        case SortMode :: SORT_BY_ID:
105            less = a->id() < b->id();
106            break;
107        case SortMode :: SORT_BY_RATIO:
108            less = a->compareRatio( *b ) < 0;
109            break;
110        case SortMode :: SORT_BY_PROGRESS:
111            less = a->percentDone() < b->percentDone();
112            break;
113        case SortMode :: SORT_BY_ETA:
114            less = a->compareETA( *b ) < 0;
115            break;
116        case SortMode :: SORT_BY_STATE:
117            if( a->hasError() != b->hasError() )
118                less = a->hasError();
119            else
120                less = a->getActivity() < b->getActivity();
121            break;
122        case SortMode :: SORT_BY_TRACKER:
123            less = a->compareTracker( *b ) < 0;
124            break;
125        default:
126            less = a->name().compare( b->name(), Qt::CaseInsensitive ) > 0;
127            break;
128    }
129
130    return less;
131}
132
133
134/***
135****
136***/
137
138bool
139TorrentFilter :: filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
140{
141    QModelIndex childIndex = sourceModel()->index( sourceRow, 0, sourceParent );
142    const Torrent * tor = childIndex.model()->data( childIndex, TorrentModel::TorrentRole ).value<const Torrent*>();
143    const tr_torrent_activity activity = tor->getActivity( );
144    bool accepts;
145
146    switch( myPrefs.get<FilterMode>(Prefs::FILTER_MODE).mode() )
147    {
148        case FilterMode::SHOW_ALL:
149            accepts = true;
150            break;
151        case FilterMode::SHOW_ACTIVE:
152            accepts = tor->peersWeAreUploadingTo( ) > 0 || tor->peersWeAreDownloadingFrom( ) > 0 || tor->isVerifying( );
153            break;
154        case FilterMode::SHOW_DOWNLOADING:
155            accepts = activity == TR_STATUS_DOWNLOAD;
156            break;
157        case FilterMode::SHOW_SEEDING:
158            accepts = activity == TR_STATUS_SEED;
159            break;
160        case FilterMode::SHOW_PAUSED:
161            accepts = activity == TR_STATUS_STOPPED;
162            break;
163    }
164
165    if( accepts && !myText.isEmpty( ) ) switch( myTextMode )
166    {
167        case FILTER_BY_NAME:
168            accepts = tor->name().contains( myText, Qt::CaseInsensitive );
169            break;
170        case FILTER_BY_FILES:
171            accepts = tor->hasFileSubstring( myText );
172            break;
173        case FILTER_BY_TRACKER:
174            accepts = tor->hasTrackerSubstring( myText );
175            break;
176    }
177
178    return accepts;
179}
180
181int
182TorrentFilter :: hiddenRowCount( ) const
183{
184    return sourceModel()->rowCount( ) - rowCount( );
185}
Note: See TracBrowser for help on using the repository browser.