~hendrik-grewe/transmission/private-patch

« back to all changes in this revision

Viewing changes to qt/torrent-filter.cc

  • Committer: charles
  • Date: 2009-04-09 17:55:47 UTC
  • Revision ID: svn-v4:f4695dd4-2c0a-0410-b89c-da849a56a58e:trunk:8188
(trunk) add the Qt beta into svn 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "prefs.h"
 
16
#include "torrent.h"
 
17
#include "torrent-filter.h"
 
18
#include "torrent-model.h"
 
19
 
 
20
TorrentFilter :: TorrentFilter( Prefs& prefs ):
 
21
    myPrefs( prefs ),
 
22
    myShowMode( SHOW_ALL ),
 
23
    myTextMode( FILTER_BY_NAME ),
 
24
    mySortMode( SORT_BY_ID ),
 
25
    myIsAscending( FALSE )
 
26
{
 
27
}
 
28
 
 
29
TorrentFilter :: ~TorrentFilter( )
 
30
{
 
31
}
 
32
 
 
33
/***
 
34
****
 
35
***/
 
36
 
 
37
void
 
38
TorrentFilter :: setShowMode( int showMode )
 
39
{
 
40
    if( myShowMode != showMode )
 
41
    {
 
42
        myShowMode = ShowMode( showMode );
 
43
        invalidateFilter( );
 
44
    }
 
45
}
 
46
 
 
47
void
 
48
TorrentFilter :: setTextMode( int textMode )
 
49
{
 
50
    if( myTextMode != textMode )
 
51
    {
 
52
        myTextMode = TextMode( textMode );
 
53
        invalidateFilter( );
 
54
    }
 
55
}
 
56
 
 
57
void
 
58
TorrentFilter :: setText( QString text )
 
59
{
 
60
    QString trimmed = text.trimmed( );
 
61
 
 
62
    if( myText != trimmed )
 
63
    {
 
64
        myText = trimmed;
 
65
        invalidateFilter( );
 
66
    }
 
67
}
 
68
 
 
69
bool
 
70
TorrentFilter :: filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
 
71
{
 
72
    QModelIndex childIndex = sourceModel()->index( sourceRow, 0, sourceParent );
 
73
    const Torrent * tor = childIndex.model()->data( childIndex, TorrentModel::TorrentRole ).value<const Torrent*>();
 
74
    const tr_torrent_activity activity = tor->getActivity( );
 
75
    bool accepts;
 
76
 
 
77
    switch( myShowMode )
 
78
    {
 
79
        case SHOW_ALL:
 
80
            accepts = true;
 
81
            break;
 
82
        case SHOW_ACTIVE:
 
83
            accepts = tor->peersWeAreUploadingTo( ) > 0 || tor->peersWeAreDownloadingFrom( ) > 0;
 
84
            break;
 
85
        case SHOW_DOWNLOADING:
 
86
            accepts = activity == TR_STATUS_DOWNLOAD;
 
87
            break;
 
88
        case SHOW_SEEDING:
 
89
            accepts = activity == TR_STATUS_SEED;
 
90
            break;
 
91
        case SHOW_PAUSED:
 
92
            accepts = activity == TR_STATUS_STOPPED;
 
93
            break;
 
94
    }
 
95
 
 
96
    if( accepts && !myText.isEmpty( ) ) switch( myTextMode )
 
97
    {
 
98
        case FILTER_BY_NAME:
 
99
            accepts = tor->name().contains( myText, Qt::CaseInsensitive );
 
100
            break;
 
101
        case FILTER_BY_FILES:
 
102
            accepts = tor->hasFileSubstring( myText );
 
103
            break;
 
104
        case FILTER_BY_TRACKER:
 
105
            accepts = tor->hasTrackerSubstring( myText );
 
106
            break;
 
107
    }
 
108
 
 
109
    return accepts;
 
110
}
 
111
 
 
112
/***
 
113
****
 
114
***/
 
115
 
 
116
const char*
 
117
TorrentFilter :: getSortKey( int modeIn )
 
118
{
 
119
    switch( modeIn < 0 ? getSortMode( ) : SortMode( modeIn ) )
 
120
    {
 
121
        case SORT_BY_ACTIVITY: return "sort-by-activity";
 
122
        case SORT_BY_AGE:      return "sort-by-age";
 
123
        case SORT_BY_ETA:      return "sort-by-eta";
 
124
        case SORT_BY_PROGRESS: return "sort-by-progress";
 
125
        case SORT_BY_RATIO:    return "sort-by-ratio";
 
126
        case SORT_BY_SIZE:     return "sort-by-size";
 
127
        case SORT_BY_STATE:    return "sort-by-state";
 
128
        case SORT_BY_TRACKER:  return "sort-by-tracker";
 
129
        default:               return "sort-by-name";
 
130
    }
 
131
}
 
132
 
 
133
void
 
134
TorrentFilter :: resort( )
 
135
{
 
136
    invalidate( );
 
137
    sort( 0, myIsAscending ? Qt::AscendingOrder : Qt::DescendingOrder );
 
138
}
 
139
 
 
140
void
 
141
TorrentFilter :: setAscending( bool b )
 
142
{
 
143
    if( myIsAscending != b )
 
144
    {
 
145
        myIsAscending = b;
 
146
        resort( );
 
147
    }
 
148
}
 
149
 
 
150
void
 
151
TorrentFilter :: setSortMode( int sortMode )
 
152
{
 
153
    if( mySortMode != sortMode )
 
154
    {
 
155
        myPrefs.set( Prefs :: SORT_MODE, getSortKey( sortMode ) );
 
156
        mySortMode = SortMode( sortMode );
 
157
        setDynamicSortFilter ( true );
 
158
        resort( );
 
159
    }
 
160
}
 
161
 
 
162
void TorrentFilter :: sortByActivity ( ) { setSortMode( SORT_BY_ACTIVITY ); }
 
163
void TorrentFilter :: sortByAge      ( ) { setSortMode( SORT_BY_AGE ); }
 
164
void TorrentFilter :: sortByETA      ( ) { setSortMode( SORT_BY_ETA ); }
 
165
void TorrentFilter :: sortById       ( ) { setSortMode( SORT_BY_ID ); }
 
166
void TorrentFilter :: sortByName     ( ) { setSortMode( SORT_BY_NAME ); }
 
167
void TorrentFilter :: sortByProgress ( ) { setSortMode( SORT_BY_PROGRESS ); }
 
168
void TorrentFilter :: sortByRatio    ( ) { setSortMode( SORT_BY_RATIO ); }
 
169
void TorrentFilter :: sortBySize     ( ) { setSortMode( SORT_BY_SIZE ); }
 
170
void TorrentFilter :: sortByState    ( ) { setSortMode( SORT_BY_STATE ); }
 
171
void TorrentFilter :: sortByTracker  ( ) { setSortMode( SORT_BY_TRACKER ); }
 
172
 
 
173
bool
 
174
TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) const
 
175
{
 
176
    const Torrent * a = sourceModel()->data( left, TorrentModel::TorrentRole ).value<const Torrent*>();
 
177
    const Torrent * b = sourceModel()->data( right, TorrentModel::TorrentRole ).value<const Torrent*>();
 
178
    bool less;
 
179
 
 
180
    switch( getSortMode( ) )
 
181
    {
 
182
        case SORT_BY_SIZE:
 
183
            less = a->sizeWhenDone() < b->sizeWhenDone();
 
184
            break;
 
185
        case SORT_BY_ACTIVITY:
 
186
            less = a->downloadSpeed() + a->uploadSpeed() < b->downloadSpeed() + b->uploadSpeed();
 
187
            break;
 
188
        case SORT_BY_AGE:
 
189
            less = a->dateAdded() < b->dateAdded();
 
190
            break;
 
191
        case SORT_BY_ID:
 
192
            less = a->id() < b->id();
 
193
            break;
 
194
        case SORT_BY_RATIO:
 
195
            less = a->compareRatio( *b ) < 0;
 
196
            break;
 
197
        case SORT_BY_PROGRESS:
 
198
            less = a->percentDone() < b->percentDone();
 
199
            break;
 
200
        case SORT_BY_ETA:
 
201
            less = a->compareETA( *b ) < 0;
 
202
            break;
 
203
        case SORT_BY_STATE:
 
204
            if( a->hasError() != b->hasError() )
 
205
                less = a->hasError();
 
206
            else
 
207
                less = a->getActivity() < b->getActivity();
 
208
            break;
 
209
        case SORT_BY_TRACKER:
 
210
            less = a->compareTracker( *b ) < 0;
 
211
            break;
 
212
        default:
 
213
            less = a->name().compare( b->name(), Qt::CaseInsensitive ) > 0;
 
214
            break;
 
215
    }
 
216
 
 
217
    return less;
 
218
}
 
219
 
 
220
int
 
221
TorrentFilter :: hiddenRowCount( ) const
 
222
{
 
223
    return sourceModel()->rowCount( ) - rowCount( );
 
224
}
 
225