Changeset 8216


Ignore:
Timestamp:
Apr 11, 2009, 6:25:12 PM (14 years ago)
Author:
charles
Message:

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

Location:
trunk/qt
Files:
2 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/mainwin.cc

    r8194 r8216  
    2222#include <QSystemTrayIcon>
    2323#include <QUrl>
     24#include <QSignalMapper>
    2425
    2526#include "about.h"
    2627#include "details.h"
     28#include "filters.h"
    2729#include "mainwin.h"
    2830#include "make-dialog.h"
     
    140142
    141143    // ui signals
    142     // ccc
    143144    connect( ui.action_Toolbar, SIGNAL(toggled(bool)), this, SLOT(setToolbarVisible(bool)));
    144145    connect( ui.action_TrayIcon, SIGNAL(toggled(bool)), this, SLOT(setTrayIconVisible(bool)));
     
    146147    connect( ui.action_Statusbar, SIGNAL(toggled(bool)), this, SLOT(setStatusbarVisible(bool)));
    147148    connect( ui.action_MinimalView, SIGNAL(toggled(bool)), this, SLOT(setMinimalView(bool)));
    148     connect( ui.action_SortByActivity, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByActivity()) );
    149     connect( ui.action_SortByAge, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByAge()) );
    150     connect( ui.action_SortByETA, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByETA()));
    151     connect( ui.action_SortByName, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByName()));
    152     connect( ui.action_SortByProgress, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByProgress()));
    153     connect( ui.action_SortByRatio, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByRatio()));
    154     connect( ui.action_SortBySize, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortBySize()));
    155     connect( ui.action_SortByState, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByState()));
    156     connect( ui.action_SortByTracker, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByTracker()));
    157     connect( ui.action_ReverseSortOrder, SIGNAL(toggled(bool)), &myFilterModel, SLOT(setAscending(bool)));
     149    connect( ui.action_SortByActivity, SIGNAL(toggled(bool)), this, SLOT(onSortByActivityToggled(bool)));
     150    connect( ui.action_SortByAge,      SIGNAL(toggled(bool)), this, SLOT(onSortByAgeToggled(bool)));
     151    connect( ui.action_SortByETA,      SIGNAL(toggled(bool)), this, SLOT(onSortByETAToggled(bool)));
     152    connect( ui.action_SortByName,     SIGNAL(toggled(bool)), this, SLOT(onSortByNameToggled(bool)));
     153    connect( ui.action_SortByProgress, SIGNAL(toggled(bool)), this, SLOT(onSortByProgressToggled(bool)));
     154    connect( ui.action_SortByRatio,    SIGNAL(toggled(bool)), this, SLOT(onSortByRatioToggled(bool)));
     155    connect( ui.action_SortBySize,     SIGNAL(toggled(bool)), this, SLOT(onSortBySizeToggled(bool)));
     156    connect( ui.action_SortByState,    SIGNAL(toggled(bool)), this, SLOT(onSortByStateToggled(bool)));
     157    connect( ui.action_SortByTracker,  SIGNAL(toggled(bool)), this, SLOT(onSortByTrackerToggled(bool)));
     158    connect( ui.action_ReverseSortOrder, SIGNAL(toggled(bool)), this, SLOT(setSortAscendingPref(bool)));
    158159    connect( ui.action_Start, SIGNAL(triggered()), this, SLOT(startSelected()));
    159160    connect( ui.action_Pause, SIGNAL(triggered()), this, SLOT(pauseSelected()));
     
    205206    setTextButtonSizeHint( ui.filterSeeding );
    206207    setTextButtonSizeHint( ui.filterPaused );
    207     setShowMode( myFilterModel.getShowMode( ) );
    208208
    209209    connect( &myFilterModel, SIGNAL(rowsInserted(const QModelIndex&,int,int)), this, SLOT(refreshVisibleCount()));
     
    321321
    322322void
     323TrMainWindow :: setSortPref( int i )
     324{
     325    myPrefs.set( Prefs::SORT_MODE, SortMode( i ) );
     326}
     327void TrMainWindow :: onSortByActivityToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_ACTIVITY ); }
     328void TrMainWindow :: onSortByAgeToggled      ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_AGE );      }
     329void TrMainWindow :: onSortByETAToggled      ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_ETA );      }
     330void TrMainWindow :: onSortByNameToggled     ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_NAME );     }
     331void TrMainWindow :: onSortByProgressToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_PROGRESS ); }
     332void TrMainWindow :: onSortByRatioToggled    ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_RATIO );    }
     333void TrMainWindow :: onSortBySizeToggled     ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_SIZE );     }
     334void TrMainWindow :: onSortByStateToggled    ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_STATE );    }
     335void TrMainWindow :: onSortByTrackerToggled  ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_TRACKER );  }
     336
     337void
     338TrMainWindow :: setSortAscendingPref( bool b )
     339{
     340    myPrefs.set( Prefs::SORT_REVERSED, b );
     341}
     342
     343/****
     344*****
     345****/
     346
     347void
    323348TrMainWindow :: openProperties( )
    324349{
     
    512537**/
    513538
    514 void
    515 TrMainWindow :: setShowMode( TorrentFilter :: ShowMode mode )
    516 {
    517     ui.filterAll->setChecked         ( mode == TorrentFilter :: SHOW_ALL );
    518     ui.filterActive->setChecked      ( mode == TorrentFilter :: SHOW_ACTIVE );
    519     ui.filterDownloading->setChecked ( mode == TorrentFilter :: SHOW_DOWNLOADING );
    520     ui.filterSeeding->setChecked     ( mode == TorrentFilter :: SHOW_SEEDING );
    521     ui.filterPaused->setChecked      ( mode == TorrentFilter :: SHOW_PAUSED );
    522 
    523     myFilterModel.setShowMode( mode );
    524 }
    525 
    526 void TrMainWindow :: showAll         ( ) { setShowMode( TorrentFilter :: SHOW_ALL ); }
    527 void TrMainWindow :: showActive      ( ) { setShowMode( TorrentFilter :: SHOW_ACTIVE ); }
    528 void TrMainWindow :: showDownloading ( ) { setShowMode( TorrentFilter :: SHOW_DOWNLOADING ); }
    529 void TrMainWindow :: showSeeding     ( ) { setShowMode( TorrentFilter :: SHOW_SEEDING ); }
    530 void TrMainWindow :: showPaused      ( ) { setShowMode( TorrentFilter :: SHOW_PAUSED ); }
     539void TrMainWindow :: setShowMode     ( int i ) { myPrefs.set( Prefs::FILTER_MODE, FilterMode( i ) ); }
     540void TrMainWindow :: showAll         ( ) { setShowMode( FilterMode :: SHOW_ALL ); }
     541void TrMainWindow :: showActive      ( ) { setShowMode( FilterMode :: SHOW_ACTIVE ); }
     542void TrMainWindow :: showDownloading ( ) { setShowMode( FilterMode :: SHOW_DOWNLOADING ); }
     543void TrMainWindow :: showSeeding     ( ) { setShowMode( FilterMode :: SHOW_SEEDING ); }
     544void TrMainWindow :: showPaused      ( ) { setShowMode( FilterMode :: SHOW_PAUSED ); }
    531545
    532546void TrMainWindow :: filterByName    ( ) { myFilterModel.setTextMode( TorrentFilter :: FILTER_BY_NAME ); }
     
    534548void TrMainWindow :: filterByFiles   ( ) { myFilterModel.setTextMode( TorrentFilter :: FILTER_BY_FILES ); }
    535549
    536 void
    537 TrMainWindow :: showTotalRatio( )
    538 {
    539     myPrefs.set( Prefs::STATUSBAR_STATS, "total-ratio" );
    540 }
    541 void
    542 TrMainWindow :: showTotalTransfer( )
    543 {
    544     myPrefs.set( Prefs::STATUSBAR_STATS, "total-transfer" );
    545 }
    546 void
    547 TrMainWindow :: showSessionRatio( )
    548 {
    549     myPrefs.set( Prefs::STATUSBAR_STATS, "session-ratio" );
    550 }
    551 void
    552 TrMainWindow :: showSessionTransfer( )
    553 {
    554     myPrefs.set( Prefs::STATUSBAR_STATS, "session-transfer" );
    555 }
     550void TrMainWindow :: showTotalRatio      ( ) { myPrefs.set( Prefs::STATUSBAR_STATS, "total-ratio"); }
     551void TrMainWindow :: showTotalTransfer   ( ) { myPrefs.set( Prefs::STATUSBAR_STATS, "total-transfer"); }
     552void TrMainWindow :: showSessionRatio    ( ) { myPrefs.set( Prefs::STATUSBAR_STATS, "session-ratio"); }
     553void TrMainWindow :: showSessionTransfer ( ) { myPrefs.set( Prefs::STATUSBAR_STATS, "session-transfer"); }
    556554
    557555/**
     
    631629
    632630        case Prefs::SORT_MODE:
    633             i = myFilterModel.getSortModeFromName( myPrefs.getString( key ) );
    634             ui.action_SortByActivity->setChecked ( i == TorrentFilter::SORT_BY_ACTIVITY );
    635             ui.action_SortByAge->setChecked      ( i == TorrentFilter::SORT_BY_AGE );
    636             ui.action_SortByETA->setChecked      ( i == TorrentFilter::SORT_BY_ETA );
    637             ui.action_SortByName->setChecked     ( i == TorrentFilter::SORT_BY_NAME );
    638             ui.action_SortByProgress->setChecked ( i == TorrentFilter::SORT_BY_PROGRESS );
    639             ui.action_SortByRatio->setChecked    ( i == TorrentFilter::SORT_BY_RATIO );
    640             ui.action_SortBySize->setChecked     ( i == TorrentFilter::SORT_BY_SIZE );
    641             ui.action_SortByState->setChecked    ( i == TorrentFilter::SORT_BY_STATE );
    642             ui.action_SortByTracker->setChecked  ( i == TorrentFilter::SORT_BY_TRACKER );
     631            i = myPrefs.get<SortMode>(key).mode( );
     632            ui.action_SortByActivity->setChecked ( i == SortMode::SORT_BY_ACTIVITY );
     633            ui.action_SortByAge->setChecked      ( i == SortMode::SORT_BY_AGE );
     634            ui.action_SortByETA->setChecked      ( i == SortMode::SORT_BY_ETA );
     635            ui.action_SortByName->setChecked     ( i == SortMode::SORT_BY_NAME );
     636            ui.action_SortByProgress->setChecked ( i == SortMode::SORT_BY_PROGRESS );
     637            ui.action_SortByRatio->setChecked    ( i == SortMode::SORT_BY_RATIO );
     638            ui.action_SortBySize->setChecked     ( i == SortMode::SORT_BY_SIZE );
     639            ui.action_SortByState->setChecked    ( i == SortMode::SORT_BY_STATE );
     640            ui.action_SortByTracker->setChecked  ( i == SortMode::SORT_BY_TRACKER );
    643641            break;
    644642
    645643        case Prefs::FILTER_MODE:
    646             i = myFilterModel.getShowModeFromName( myPrefs.getString( key ) );
    647             ui.filterAll->setChecked         ( i == TorrentFilter::SHOW_ALL );
    648             ui.filterActive->setChecked      ( i == TorrentFilter::SHOW_ACTIVE );
    649             ui.filterDownloading->setChecked ( i == TorrentFilter::SHOW_DOWNLOADING );
    650             ui.filterSeeding->setChecked     ( i == TorrentFilter::SHOW_SEEDING );
    651             ui.filterPaused->setChecked      ( i == TorrentFilter::SHOW_PAUSED );
    652             break;
    653 
     644            i = myPrefs.get<FilterMode>(key).mode( );
     645            ui.filterAll->setChecked         ( i == FilterMode::SHOW_ALL );
     646            ui.filterActive->setChecked      ( i == FilterMode::SHOW_ACTIVE );
     647            ui.filterDownloading->setChecked ( i == FilterMode::SHOW_DOWNLOADING );
     648            ui.filterSeeding->setChecked     ( i == FilterMode::SHOW_SEEDING );
     649            ui.filterPaused->setChecked      ( i == FilterMode::SHOW_PAUSED );
     650            break;
    654651
    655652        case Prefs::FILTERBAR:
  • trunk/qt/mainwin.h

    r8188 r8216  
    6969
    7070    private:
    71         void setShowMode( TorrentFilter::ShowMode );
     71        void setShowMode( int );
    7272        QSet<int> getSelectedTorrents( ) const;
    7373        void updateNetworkIcon( );
     
    102102        void toggleWindows( );
    103103
     104     private slots:
     105        void setSortPref( int );
     106        void setSortAscendingPref( bool );
     107        void onSortByActivityToggled ( bool b );
     108        void onSortByAgeToggled      ( bool b );
     109        void onSortByETAToggled      ( bool b );
     110        void onSortByNameToggled     ( bool b );
     111        void onSortByProgressToggled ( bool b );
     112        void onSortByRatioToggled    ( bool b );
     113        void onSortBySizeToggled     ( bool b );
     114        void onSortByStateToggled    ( bool b );
     115        void onSortByTrackerToggled  ( bool b );
     116
     117
    104118     public slots:
    105119        void startAll( );
  • trunk/qt/prefs-dialog.cc

    r8211 r8216  
    172172{
    173173    const int key( sender()->property( PREF_KEY ).toInt( ) );
    174     myPrefs.set( key, qPrintable(text) );
     174    myPrefs.set( key, text );
    175175}
    176176
  • trunk/qt/prefs.cc

    r8197 r8216  
    2323#include <libtransmission/utils.h>
    2424#include "prefs.h"
     25#include "types.h"
    2526
    2627/***
     
    4142    { TRASH_ORIGINAL, "trash-original-torrent-files", QVariant::Bool },
    4243    { ASKQUIT, "prompt-before-exit", QVariant::Bool },
    43     { SORT_MODE, "sort-mode", QVariant::String },
     44    { SORT_MODE, "sort-mode", TrTypes::SortModeType },
    4445    { SORT_REVERSED, "sort-reversed", QVariant::Bool },
    4546    { MINIMAL_VIEW, "minimal-view", QVariant::Bool },
     
    5556    { MAIN_WINDOW_X, "main-window-x", QVariant::Int },
    5657    { MAIN_WINDOW_Y, "main-window-y", QVariant::Int },
    57     { FILTER_MODE, "filter-mode", QVariant::String },
     58    { FILTER_MODE, "filter-mode", TrTypes::FilterModeType },
    5859
    5960    /* libtransmission settings */
     
    135136                    myValues[i].setValue( qlonglong(intVal) );
    136137                break;
     138            case TrTypes::SortModeType:
     139                if( tr_bencGetStr( b, &str ) )
     140                    myValues[i] = QVariant::fromValue( SortMode( str ) );
     141                break;
     142            case TrTypes::FilterModeType:
     143                if( tr_bencGetStr( b, &str ) )
     144                    myValues[i] = QVariant::fromValue( FilterMode( str ) );
     145                break;
    137146            case QVariant::String:
    138147                if( tr_bencGetStr( b, &str ) )
     
    179188            case QVariant::Int:
    180189                tr_bencDictAddInt( &top, key, val.toInt() );
     190                break;
     191            case TrTypes::SortModeType:
     192                tr_bencDictAddStr( &top, key, val.value<SortMode>().name().toUtf8().constData() );
     193                break;
     194            case TrTypes::FilterModeType:
     195                tr_bencDictAddStr( &top, key, val.value<FilterMode>().name().toUtf8().constData() );
    181196                break;
    182197            case QVariant::String:
  • trunk/qt/prefs.h

    r8194 r8216  
    2121#include <libtransmission/transmission.h>
    2222#include <libtransmission/bencode.h>
     23
     24#include "filters.h"
    2325
    2426class Prefs: public QObject
     
    115117            int id;
    116118            const char * key;
    117             QVariant::Type type;
     119            int type;
    118120        };
    119121
     
    129131        bool isClient( int key ) const { return !isCore( key ); }
    130132        const char * keyStr( int i ) const { return myItems[i].key; }
    131         QVariant::Type type( int i ) const { return myItems[i].type; }
     133        int type( int i ) const { return myItems[i].type; }
    132134        const QVariant& variant( int i ) const { return myValues[i]; }
    133135
     
    141143        QDateTime getDateTime( int key ) const;
    142144
     145        template<typename T> T get( int key ) const {
     146            return myValues[key].value<T>();
     147        }
     148
     149        void set( int key, char * value ) { set( key, QString::fromUtf8(value) ); }
     150        void set( int key, const char * value ) { set( key, QString::fromUtf8(value) ); }
     151
    143152        template<typename T> void set( int key, const T& value ) {
    144153            QVariant& v( myValues[key] );
    145             const QVariant tmp( value );
     154            const QVariant tmp = QVariant::fromValue(value);
    146155            if( v.isNull() || (v!=tmp) ) {
    147156                v = tmp;
  • trunk/qt/qtransmission.pro

    r8188 r8216  
    2121           details.h \
    2222           file-tree.h \
     23           filters.h \
    2324           hig.h \
    2425           mainwin.h \
     
    4546           details.cc \
    4647           file-tree.cc \
     48           filters.cc \
    4749           hig.cc \
    4850           mainwin.cc \
  • trunk/qt/session.cc

    r8196 r8216  
    658658                break;
    659659            }
     660            case TrTypes :: FilterModeType:
     661            case TrTypes :: SortModeType:
    660662            case QVariant :: String: {
    661663                const char * val;
  • trunk/qt/torrent-filter.cc

    r8213 r8216  
    1313#include <iostream>
    1414
     15#include "filters.h"
    1516#include "prefs.h"
    1617#include "torrent.h"
     
    2021TorrentFilter :: TorrentFilter( Prefs& prefs ):
    2122    myPrefs( prefs ),
    22     myShowMode( getShowModeFromName( prefs.getString( Prefs::FILTER_MODE ) ) ),
    23     myTextMode( FILTER_BY_NAME ),
    24     mySortMode( getSortModeFromName( prefs.getString( Prefs::SORT_MODE ) ) ),
    25     myIsAscending( prefs.getBool( Prefs::SORT_REVERSED ) )
     23    myTextMode( FILTER_BY_NAME )
    2624{
    27     resort( );
     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 );
    2834}
    2935
    3036TorrentFilter :: ~TorrentFilter( )
    3137{
     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    }
    3254}
    3355
     
    3759
    3860void
    39 TorrentFilter :: setShowMode( int showMode )
     61TorrentFilter :: setTextMode( int i )
    4062{
    41     if( myShowMode != showMode )
     63    if( myTextMode != i )
    4264    {
    43         myPrefs.set( Prefs :: FILTER_MODE, getShowName( showMode ) );
    44         myShowMode = ShowMode( showMode );
    45         invalidateFilter( );
    46     }
    47 }
    48 
    49 void
    50 TorrentFilter :: setTextMode( int textMode )
    51 {
    52     if( myTextMode != textMode )
    53     {
    54         myTextMode = TextMode( textMode );
     65        myTextMode = TextMode( i );
    5566        invalidateFilter( );
    5667    }
     
    6980}
    7081
     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
    71138bool
    72139TorrentFilter :: filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
     
    77144    bool accepts;
    78145
    79     switch( myShowMode )
     146    switch( myPrefs.get<FilterMode>(Prefs::FILTER_MODE).mode() )
    80147    {
    81         case SHOW_ALL:
     148        case FilterMode::SHOW_ALL:
    82149            accepts = true;
    83150            break;
    84         case SHOW_ACTIVE:
     151        case FilterMode::SHOW_ACTIVE:
    85152            accepts = tor->peersWeAreUploadingTo( ) > 0 || tor->peersWeAreDownloadingFrom( ) > 0 || tor->isVerifying( );
    86153            break;
    87         case SHOW_DOWNLOADING:
     154        case FilterMode::SHOW_DOWNLOADING:
    88155            accepts = activity == TR_STATUS_DOWNLOAD;
    89156            break;
    90         case SHOW_SEEDING:
     157        case FilterMode::SHOW_SEEDING:
    91158            accepts = activity == TR_STATUS_SEED;
    92159            break;
    93         case SHOW_PAUSED:
     160        case FilterMode::SHOW_PAUSED:
    94161            accepts = activity == TR_STATUS_STOPPED;
    95162            break;
     
    112179}
    113180
    114 /***
    115 ****
    116 ***/
    117 
    118 namespace
    119 {
    120     struct NameAndNum
    121     {
    122         const char * name;
    123         int num;
    124     };
    125 
    126     const struct NameAndNum showModes[] = {
    127         { "show-all",         TorrentFilter::SHOW_ALL },
    128         { "show-active",      TorrentFilter::SHOW_ACTIVE },
    129         { "show-downloading", TorrentFilter::SHOW_DOWNLOADING },
    130         { "show-seeding",     TorrentFilter::SHOW_SEEDING },
    131         { "show-paused",      TorrentFilter::SHOW_PAUSED }
    132     };
    133 
    134     const int showModeCount = sizeof(showModes) / sizeof(showModes[0]);
    135 
    136     const struct NameAndNum sortModes[] = {
    137         { "sort-by-name",     TorrentFilter::SORT_BY_NAME },
    138         { "sort-by-activity", TorrentFilter::SORT_BY_ACTIVITY },
    139         { "sort-by-age",      TorrentFilter::SORT_BY_AGE },
    140         { "sort-by-eta",      TorrentFilter::SORT_BY_ETA },
    141         { "sort-by-progress", TorrentFilter::SORT_BY_PROGRESS },
    142         { "sort-by-ratio",    TorrentFilter::SORT_BY_RATIO },
    143         { "sort-by-size",     TorrentFilter::SORT_BY_SIZE },
    144         { "sort-by-state",    TorrentFilter::SORT_BY_STATE },
    145         { "sort-by-tracker",  TorrentFilter::SORT_BY_TRACKER }
    146     };
    147 
    148     const int sortModeCount = sizeof(sortModes) / sizeof(sortModes[0]);
    149 
    150     int getNum( const struct NameAndNum * rows, int numRows, const QString& name )
    151     {
    152         for( int i=0; i<numRows; ++i )
    153             if( name == rows[i].name )
    154                 return rows[i].num;
    155         return rows[0].num; // fallback value
    156     }
    157 
    158     const char* getName( const struct NameAndNum * rows, int numRows, int num )
    159     {
    160         for( int i=0; i<numRows; ++i )
    161             if( num == rows[i].num )
    162                 return rows[i].name;
    163         return rows[0].name; // fallback value
    164     }
    165 }
    166 
    167 TorrentFilter :: ShowMode
    168 TorrentFilter :: getShowModeFromName( const QString& key ) const
    169 {
    170     return ShowMode( getNum( showModes, showModeCount, key ) );
    171 }
    172 
    173 const char*
    174 TorrentFilter :: getShowName( int mode ) const
    175 {
    176     if( mode < 0 ) mode = getShowMode( );
    177     return getName( showModes, showModeCount, mode );
    178 }
    179 
    180 TorrentFilter :: SortMode
    181 TorrentFilter :: getSortModeFromName( const QString& key ) const
    182 {
    183     return SortMode( getNum( sortModes, sortModeCount, key ) );
    184 }
    185 
    186 const char*
    187 TorrentFilter :: getSortName( int mode ) const
    188 {
    189     if( mode < 0 ) mode = getSortMode( );
    190     return getName( sortModes, sortModeCount, mode );
    191 }
    192 
    193 /***
    194 ****
    195 ***/
    196 
    197 void
    198 TorrentFilter :: resort( )
    199 {
    200     invalidate( );
    201     sort( 0, myIsAscending ? Qt::AscendingOrder : Qt::DescendingOrder );
    202 }
    203 
    204 void
    205 TorrentFilter :: setAscending( bool b )
    206 {
    207     if( myIsAscending != b )
    208     {
    209         myIsAscending = b;
    210         resort( );
    211     }
    212 }
    213 
    214 void
    215 TorrentFilter :: setSortMode( int sortMode )
    216 {
    217     if( mySortMode != sortMode )
    218     {
    219         myPrefs.set( Prefs :: SORT_MODE, getSortName( sortMode ) );
    220         mySortMode = SortMode( sortMode );
    221         setDynamicSortFilter ( true );
    222         resort( );
    223     }
    224 }
    225 
    226 void TorrentFilter :: sortByActivity ( ) { setSortMode( SORT_BY_ACTIVITY ); }
    227 void TorrentFilter :: sortByAge      ( ) { setSortMode( SORT_BY_AGE ); }
    228 void TorrentFilter :: sortByETA      ( ) { setSortMode( SORT_BY_ETA ); }
    229 void TorrentFilter :: sortById       ( ) { setSortMode( SORT_BY_ID ); }
    230 void TorrentFilter :: sortByName     ( ) { setSortMode( SORT_BY_NAME ); }
    231 void TorrentFilter :: sortByProgress ( ) { setSortMode( SORT_BY_PROGRESS ); }
    232 void TorrentFilter :: sortByRatio    ( ) { setSortMode( SORT_BY_RATIO ); }
    233 void TorrentFilter :: sortBySize     ( ) { setSortMode( SORT_BY_SIZE ); }
    234 void TorrentFilter :: sortByState    ( ) { setSortMode( SORT_BY_STATE ); }
    235 void TorrentFilter :: sortByTracker  ( ) { setSortMode( SORT_BY_TRACKER ); }
    236 
    237 bool
    238 TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) const
    239 {
    240     const Torrent * a = sourceModel()->data( left, TorrentModel::TorrentRole ).value<const Torrent*>();
    241     const Torrent * b = sourceModel()->data( right, TorrentModel::TorrentRole ).value<const Torrent*>();
    242     bool less;
    243 
    244     switch( getSortMode( ) )
    245     {
    246         case SORT_BY_SIZE:
    247             less = a->sizeWhenDone() < b->sizeWhenDone();
    248             break;
    249         case SORT_BY_ACTIVITY:
    250             less = a->downloadSpeed() + a->uploadSpeed() < b->downloadSpeed() + b->uploadSpeed();
    251             break;
    252         case SORT_BY_AGE:
    253             less = a->dateAdded() < b->dateAdded();
    254             break;
    255         case SORT_BY_ID:
    256             less = a->id() < b->id();
    257             break;
    258         case SORT_BY_RATIO:
    259             less = a->compareRatio( *b ) < 0;
    260             break;
    261         case SORT_BY_PROGRESS:
    262             less = a->percentDone() < b->percentDone();
    263             break;
    264         case SORT_BY_ETA:
    265             less = a->compareETA( *b ) < 0;
    266             break;
    267         case SORT_BY_STATE:
    268             if( a->hasError() != b->hasError() )
    269                 less = a->hasError();
    270             else
    271                 less = a->getActivity() < b->getActivity();
    272             break;
    273         case SORT_BY_TRACKER:
    274             less = a->compareTracker( *b ) < 0;
    275             break;
    276         default:
    277             less = a->name().compare( b->name(), Qt::CaseInsensitive ) > 0;
    278             break;
    279     }
    280 
    281     return less;
    282 }
    283 
    284181int
    285182TorrentFilter :: hiddenRowCount( ) const
     
    287184    return sourceModel()->rowCount( ) - rowCount( );
    288185}
    289 
  • trunk/qt/torrent-filter.h

    r8194 r8216  
    1515
    1616#include <QSortFilterProxyModel>
     17#include <QMetaType>
     18#include <QVariant>
     19
    1720
    1821struct Prefs;
     
    2831
    2932    public:
    30         enum ShowMode { SHOW_ALL, SHOW_ACTIVE, SHOW_DOWNLOADING, SHOW_SEEDING, SHOW_PAUSED };
    31         ShowMode getShowMode( ) const { return myShowMode; }
    32         ShowMode getShowModeFromName( const QString& name ) const;
    33         const char * getShowName( int mode=-1 ) const;
    34 
    3533        enum TextMode { FILTER_BY_NAME, FILTER_BY_FILES, FILTER_BY_TRACKER };
    3634        TextMode getTextMode( ) const { return myTextMode; }
    37 
    38         enum SortMode{ SORT_BY_ACTIVITY, SORT_BY_AGE, SORT_BY_ETA, SORT_BY_NAME,
    39                        SORT_BY_PROGRESS, SORT_BY_RATIO, SORT_BY_SIZE,
    40                        SORT_BY_STATE, SORT_BY_TRACKER, SORT_BY_ID };
    41         SortMode getSortMode( ) const { return mySortMode; }
    42         SortMode getSortModeFromName( const QString& name) const;
    43         const char * getSortName( int mode=-1 ) const;
    44 
    45         bool isAscending( ) const { return myIsAscending; }
    46 
    4735        int hiddenRowCount( ) const;
    4836
     37    public slots:
     38        void setTextMode( int textMode );
     39        void setText( QString );
    4940
    50     public slots:
    51         void setShowMode( int showMode );
    52         void setTextMode( int textMode );
    53         void setSortMode( int sortMode );
    54         void setText( QString );
    55         void sortByActivity( );
    56         void sortByAge( );
    57         void sortByETA( );
    58         void sortById( );
    59         void sortByName( );
    60         void sortByProgress( );
    61         void sortByRatio( );
    62         void sortBySize( );
    63         void sortByState( );
    64         void sortByTracker( );
    65         void setAscending( bool );
    66         void resort( );
     41    private slots:
     42        void refreshPref( int key );
    6743
    6844    protected:
     
    7248    private:
    7349        Prefs& myPrefs;
    74         ShowMode myShowMode;
    7550        TextMode myTextMode;
    76         SortMode mySortMode;
    77         bool myIsAscending;
    7851        QString myText;
    7952};
  • trunk/qt/types.h

    r8188 r8216  
    2323        {
    2424            PeerList = QVariant::UserType,
    25             FileList
     25            FileList,
     26            FilterModeType,
     27            SortModeType
    2628        };
    2729};
Note: See TracChangeset for help on using the changeset viewer.