From e46700022b300766466be53d73f53b82cc2bd454 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 11 Apr 2009 18:25:12 +0000 Subject: [PATCH] (qt) better integration of sort & filter modes into the preferences mechanism --- qt/filters.cc | 52 ++++++++ qt/filters.h | 55 +++++++++ qt/mainwin.cc | 129 ++++++++++---------- qt/mainwin.h | 16 ++- qt/prefs-dialog.cc | 2 +- qt/prefs.cc | 19 ++- qt/prefs.h | 15 ++- qt/qtransmission.pro | 2 + qt/session.cc | 2 + qt/torrent-filter.cc | 276 ++++++++++++++----------------------------- qt/torrent-filter.h | 39 +----- qt/types.h | 4 +- 12 files changed, 314 insertions(+), 297 deletions(-) create mode 100644 qt/filters.cc create mode 100644 qt/filters.h diff --git a/qt/filters.cc b/qt/filters.cc new file mode 100644 index 000000000..5c607752a --- /dev/null +++ b/qt/filters.cc @@ -0,0 +1,52 @@ +/* + * This file Copyright (C) 2009 Charles Kerr + * + * This file is licensed by the GPL version 2. Works owned by the + * Transmission project are granted a special exemption to clause 2(b) + * so that the bulk of its code can remain under the MIT license. + * This exemption does not extend to derived works not owned by + * the Transmission project. + * + * $Id:$ + */ + +#include "filters.h" + +const QString FilterMode::names[NUM_MODES] = { + "show-all", + "show-active", + "show-downloading", + "show-seeding", + "show-paused" +}; + +int +FilterMode :: modeFromName( const QString& name ) +{ + for( int i=0; i + * + * This file is licensed by the GPL version 2. Works owned by the + * Transmission project are granted a special exemption to clause 2(b) + * so that the bulk of its code can remain under the MIT license. + * This exemption does not extend to derived works not owned by + * the Transmission project. + * + * $Id:$ + */ + +#ifndef QTR_FILTERS_H +#define QTR_FILTERS_H + +#include +#include +#include + +class FilterMode +{ + private: + int myMode; + public: + FilterMode( int mode=SHOW_ALL ): myMode(mode) { } + FilterMode( const QString& name ): myMode(modeFromName(name)) { } + static const QString names[]; + enum { SHOW_ALL, SHOW_ACTIVE, SHOW_DOWNLOADING, SHOW_SEEDING, SHOW_PAUSED, NUM_MODES }; + static int modeFromName( const QString& name ); + static const QString& nameFromMode( int mode ) { return names[mode]; } + int mode() const { return myMode; } + const QString& name() const { return names[myMode]; } +}; + +class SortMode +{ + private: + int myMode; + public: + SortMode( int mode=SORT_BY_ID ): myMode(mode) { } + SortMode( const QString& name ): myMode(modeFromName(name)) { } + static const QString names[]; + enum { SORT_BY_ACTIVITY, SORT_BY_AGE, SORT_BY_ETA, SORT_BY_NAME, + SORT_BY_PROGRESS, SORT_BY_RATIO, SORT_BY_SIZE, + SORT_BY_STATE, SORT_BY_TRACKER, SORT_BY_ID, NUM_MODES }; + static int modeFromName( const QString& name ); + static const QString& nameFromMode( int mode ); + int mode() const { return myMode; } + const QString& name() const { return names[myMode]; } +}; + +Q_DECLARE_METATYPE(FilterMode) +Q_DECLARE_METATYPE(SortMode) + +#endif diff --git a/qt/mainwin.cc b/qt/mainwin.cc index 125d711ee..69ad8c0f1 100644 --- a/qt/mainwin.cc +++ b/qt/mainwin.cc @@ -21,9 +21,11 @@ #include #include #include +#include #include "about.h" #include "details.h" +#include "filters.h" #include "mainwin.h" #include "make-dialog.h" #include "options.h" @@ -139,22 +141,21 @@ TrMainWindow :: TrMainWindow( Session& session, Prefs& prefs, TorrentModel& mode ui.filterEntryClearButton->setIcon( getStockIcon( "edit-clear", QStyle::SP_DialogCloseButton ) ); // ui signals - // ccc connect( ui.action_Toolbar, SIGNAL(toggled(bool)), this, SLOT(setToolbarVisible(bool))); connect( ui.action_TrayIcon, SIGNAL(toggled(bool)), this, SLOT(setTrayIconVisible(bool))); connect( ui.action_Filterbar, SIGNAL(toggled(bool)), this, SLOT(setFilterbarVisible(bool))); connect( ui.action_Statusbar, SIGNAL(toggled(bool)), this, SLOT(setStatusbarVisible(bool))); connect( ui.action_MinimalView, SIGNAL(toggled(bool)), this, SLOT(setMinimalView(bool))); - connect( ui.action_SortByActivity, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByActivity()) ); - connect( ui.action_SortByAge, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByAge()) ); - connect( ui.action_SortByETA, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByETA())); - connect( ui.action_SortByName, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByName())); - connect( ui.action_SortByProgress, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByProgress())); - connect( ui.action_SortByRatio, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByRatio())); - connect( ui.action_SortBySize, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortBySize())); - connect( ui.action_SortByState, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByState())); - connect( ui.action_SortByTracker, SIGNAL(toggled(bool)), &myFilterModel, SLOT(sortByTracker())); - connect( ui.action_ReverseSortOrder, SIGNAL(toggled(bool)), &myFilterModel, SLOT(setAscending(bool))); + connect( ui.action_SortByActivity, SIGNAL(toggled(bool)), this, SLOT(onSortByActivityToggled(bool))); + connect( ui.action_SortByAge, SIGNAL(toggled(bool)), this, SLOT(onSortByAgeToggled(bool))); + connect( ui.action_SortByETA, SIGNAL(toggled(bool)), this, SLOT(onSortByETAToggled(bool))); + connect( ui.action_SortByName, SIGNAL(toggled(bool)), this, SLOT(onSortByNameToggled(bool))); + connect( ui.action_SortByProgress, SIGNAL(toggled(bool)), this, SLOT(onSortByProgressToggled(bool))); + connect( ui.action_SortByRatio, SIGNAL(toggled(bool)), this, SLOT(onSortByRatioToggled(bool))); + connect( ui.action_SortBySize, SIGNAL(toggled(bool)), this, SLOT(onSortBySizeToggled(bool))); + connect( ui.action_SortByState, SIGNAL(toggled(bool)), this, SLOT(onSortByStateToggled(bool))); + connect( ui.action_SortByTracker, SIGNAL(toggled(bool)), this, SLOT(onSortByTrackerToggled(bool))); + connect( ui.action_ReverseSortOrder, SIGNAL(toggled(bool)), this, SLOT(setSortAscendingPref(bool))); connect( ui.action_Start, SIGNAL(triggered()), this, SLOT(startSelected())); connect( ui.action_Pause, SIGNAL(triggered()), this, SLOT(pauseSelected())); connect( ui.action_Remove, SIGNAL(triggered()), this, SLOT(removeSelected())); @@ -204,7 +205,6 @@ TrMainWindow :: TrMainWindow( Session& session, Prefs& prefs, TorrentModel& mode setTextButtonSizeHint( ui.filterDownloading ); setTextButtonSizeHint( ui.filterSeeding ); setTextButtonSizeHint( ui.filterPaused ); - setShowMode( myFilterModel.getShowMode( ) ); connect( &myFilterModel, SIGNAL(rowsInserted(const QModelIndex&,int,int)), this, SLOT(refreshVisibleCount())); connect( &myFilterModel, SIGNAL(rowsRemoved(const QModelIndex&,int,int)), this, SLOT(refreshVisibleCount())); @@ -319,6 +319,31 @@ TrMainWindow :: ~TrMainWindow( ) ***** ****/ +void +TrMainWindow :: setSortPref( int i ) +{ + myPrefs.set( Prefs::SORT_MODE, SortMode( i ) ); +} +void TrMainWindow :: onSortByActivityToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_ACTIVITY ); } +void TrMainWindow :: onSortByAgeToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_AGE ); } +void TrMainWindow :: onSortByETAToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_ETA ); } +void TrMainWindow :: onSortByNameToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_NAME ); } +void TrMainWindow :: onSortByProgressToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_PROGRESS ); } +void TrMainWindow :: onSortByRatioToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_RATIO ); } +void TrMainWindow :: onSortBySizeToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_SIZE ); } +void TrMainWindow :: onSortByStateToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_STATE ); } +void TrMainWindow :: onSortByTrackerToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_TRACKER ); } + +void +TrMainWindow :: setSortAscendingPref( bool b ) +{ + myPrefs.set( Prefs::SORT_REVERSED, b ); +} + +/**** +***** +****/ + void TrMainWindow :: openProperties( ) { @@ -511,48 +536,21 @@ TrMainWindow :: reannounceSelected( ) *** **/ -void -TrMainWindow :: setShowMode( TorrentFilter :: ShowMode mode ) -{ - ui.filterAll->setChecked ( mode == TorrentFilter :: SHOW_ALL ); - ui.filterActive->setChecked ( mode == TorrentFilter :: SHOW_ACTIVE ); - ui.filterDownloading->setChecked ( mode == TorrentFilter :: SHOW_DOWNLOADING ); - ui.filterSeeding->setChecked ( mode == TorrentFilter :: SHOW_SEEDING ); - ui.filterPaused->setChecked ( mode == TorrentFilter :: SHOW_PAUSED ); - - myFilterModel.setShowMode( mode ); -} - -void TrMainWindow :: showAll ( ) { setShowMode( TorrentFilter :: SHOW_ALL ); } -void TrMainWindow :: showActive ( ) { setShowMode( TorrentFilter :: SHOW_ACTIVE ); } -void TrMainWindow :: showDownloading ( ) { setShowMode( TorrentFilter :: SHOW_DOWNLOADING ); } -void TrMainWindow :: showSeeding ( ) { setShowMode( TorrentFilter :: SHOW_SEEDING ); } -void TrMainWindow :: showPaused ( ) { setShowMode( TorrentFilter :: SHOW_PAUSED ); } +void TrMainWindow :: setShowMode ( int i ) { myPrefs.set( Prefs::FILTER_MODE, FilterMode( i ) ); } +void TrMainWindow :: showAll ( ) { setShowMode( FilterMode :: SHOW_ALL ); } +void TrMainWindow :: showActive ( ) { setShowMode( FilterMode :: SHOW_ACTIVE ); } +void TrMainWindow :: showDownloading ( ) { setShowMode( FilterMode :: SHOW_DOWNLOADING ); } +void TrMainWindow :: showSeeding ( ) { setShowMode( FilterMode :: SHOW_SEEDING ); } +void TrMainWindow :: showPaused ( ) { setShowMode( FilterMode :: SHOW_PAUSED ); } void TrMainWindow :: filterByName ( ) { myFilterModel.setTextMode( TorrentFilter :: FILTER_BY_NAME ); } void TrMainWindow :: filterByTracker ( ) { myFilterModel.setTextMode( TorrentFilter :: FILTER_BY_TRACKER ); } void TrMainWindow :: filterByFiles ( ) { myFilterModel.setTextMode( TorrentFilter :: FILTER_BY_FILES ); } -void -TrMainWindow :: showTotalRatio( ) -{ - myPrefs.set( Prefs::STATUSBAR_STATS, "total-ratio" ); -} -void -TrMainWindow :: showTotalTransfer( ) -{ - myPrefs.set( Prefs::STATUSBAR_STATS, "total-transfer" ); -} -void -TrMainWindow :: showSessionRatio( ) -{ - myPrefs.set( Prefs::STATUSBAR_STATS, "session-ratio" ); -} -void -TrMainWindow :: showSessionTransfer( ) -{ - myPrefs.set( Prefs::STATUSBAR_STATS, "session-transfer" ); -} +void TrMainWindow :: showTotalRatio ( ) { myPrefs.set( Prefs::STATUSBAR_STATS, "total-ratio"); } +void TrMainWindow :: showTotalTransfer ( ) { myPrefs.set( Prefs::STATUSBAR_STATS, "total-transfer"); } +void TrMainWindow :: showSessionRatio ( ) { myPrefs.set( Prefs::STATUSBAR_STATS, "session-ratio"); } +void TrMainWindow :: showSessionTransfer ( ) { myPrefs.set( Prefs::STATUSBAR_STATS, "session-transfer"); } /** *** @@ -630,28 +628,27 @@ TrMainWindow :: refreshPref( int key ) break; case Prefs::SORT_MODE: - i = myFilterModel.getSortModeFromName( myPrefs.getString( key ) ); - ui.action_SortByActivity->setChecked ( i == TorrentFilter::SORT_BY_ACTIVITY ); - ui.action_SortByAge->setChecked ( i == TorrentFilter::SORT_BY_AGE ); - ui.action_SortByETA->setChecked ( i == TorrentFilter::SORT_BY_ETA ); - ui.action_SortByName->setChecked ( i == TorrentFilter::SORT_BY_NAME ); - ui.action_SortByProgress->setChecked ( i == TorrentFilter::SORT_BY_PROGRESS ); - ui.action_SortByRatio->setChecked ( i == TorrentFilter::SORT_BY_RATIO ); - ui.action_SortBySize->setChecked ( i == TorrentFilter::SORT_BY_SIZE ); - ui.action_SortByState->setChecked ( i == TorrentFilter::SORT_BY_STATE ); - ui.action_SortByTracker->setChecked ( i == TorrentFilter::SORT_BY_TRACKER ); + i = myPrefs.get(key).mode( ); + ui.action_SortByActivity->setChecked ( i == SortMode::SORT_BY_ACTIVITY ); + ui.action_SortByAge->setChecked ( i == SortMode::SORT_BY_AGE ); + ui.action_SortByETA->setChecked ( i == SortMode::SORT_BY_ETA ); + ui.action_SortByName->setChecked ( i == SortMode::SORT_BY_NAME ); + ui.action_SortByProgress->setChecked ( i == SortMode::SORT_BY_PROGRESS ); + ui.action_SortByRatio->setChecked ( i == SortMode::SORT_BY_RATIO ); + ui.action_SortBySize->setChecked ( i == SortMode::SORT_BY_SIZE ); + ui.action_SortByState->setChecked ( i == SortMode::SORT_BY_STATE ); + ui.action_SortByTracker->setChecked ( i == SortMode::SORT_BY_TRACKER ); break; case Prefs::FILTER_MODE: - i = myFilterModel.getShowModeFromName( myPrefs.getString( key ) ); - ui.filterAll->setChecked ( i == TorrentFilter::SHOW_ALL ); - ui.filterActive->setChecked ( i == TorrentFilter::SHOW_ACTIVE ); - ui.filterDownloading->setChecked ( i == TorrentFilter::SHOW_DOWNLOADING ); - ui.filterSeeding->setChecked ( i == TorrentFilter::SHOW_SEEDING ); - ui.filterPaused->setChecked ( i == TorrentFilter::SHOW_PAUSED ); + i = myPrefs.get(key).mode( ); + ui.filterAll->setChecked ( i == FilterMode::SHOW_ALL ); + ui.filterActive->setChecked ( i == FilterMode::SHOW_ACTIVE ); + ui.filterDownloading->setChecked ( i == FilterMode::SHOW_DOWNLOADING ); + ui.filterSeeding->setChecked ( i == FilterMode::SHOW_SEEDING ); + ui.filterPaused->setChecked ( i == FilterMode::SHOW_PAUSED ); break; - case Prefs::FILTERBAR: b = myPrefs.getBool( key ); ui.filterbar->setVisible( b ); diff --git a/qt/mainwin.h b/qt/mainwin.h index adf6369e5..03f7616f7 100644 --- a/qt/mainwin.h +++ b/qt/mainwin.h @@ -68,7 +68,7 @@ class TrMainWindow: public QMainWindow QIcon getStockIcon( const QString&, int fallback=-1 ); private: - void setShowMode( TorrentFilter::ShowMode ); + void setShowMode( int ); QSet getSelectedTorrents( ) const; void updateNetworkIcon( ); QWidgetList myHidden; @@ -101,6 +101,20 @@ class TrMainWindow: public QMainWindow void dataSendProgress( ); void toggleWindows( ); + private slots: + void setSortPref( int ); + void setSortAscendingPref( bool ); + void onSortByActivityToggled ( bool b ); + void onSortByAgeToggled ( bool b ); + void onSortByETAToggled ( bool b ); + void onSortByNameToggled ( bool b ); + void onSortByProgressToggled ( bool b ); + void onSortByRatioToggled ( bool b ); + void onSortBySizeToggled ( bool b ); + void onSortByStateToggled ( bool b ); + void onSortByTrackerToggled ( bool b ); + + public slots: void startAll( ); void startSelected( ); diff --git a/qt/prefs-dialog.cc b/qt/prefs-dialog.cc index 94fc3accd..462628dc9 100644 --- a/qt/prefs-dialog.cc +++ b/qt/prefs-dialog.cc @@ -171,7 +171,7 @@ void PrefsDialog :: textChanged( const QString& text ) { const int key( sender()->property( PREF_KEY ).toInt( ) ); - myPrefs.set( key, qPrintable(text) ); + myPrefs.set( key, text ); } QLineEdit* diff --git a/qt/prefs.cc b/qt/prefs.cc index 0d3be38fb..31b5b1d93 100644 --- a/qt/prefs.cc +++ b/qt/prefs.cc @@ -22,6 +22,7 @@ #include #include #include "prefs.h" +#include "types.h" /*** **** @@ -40,7 +41,7 @@ Prefs::PrefItem Prefs::myItems[] = { START, "start-added-torrents", QVariant::Bool }, { TRASH_ORIGINAL, "trash-original-torrent-files", QVariant::Bool }, { ASKQUIT, "prompt-before-exit", QVariant::Bool }, - { SORT_MODE, "sort-mode", QVariant::String }, + { SORT_MODE, "sort-mode", TrTypes::SortModeType }, { SORT_REVERSED, "sort-reversed", QVariant::Bool }, { MINIMAL_VIEW, "minimal-view", QVariant::Bool }, { FILTERBAR, "show-filterbar", QVariant::Bool }, @@ -54,7 +55,7 @@ Prefs::PrefItem Prefs::myItems[] = { MAIN_WINDOW_WIDTH, "main-window-width", QVariant::Int }, { MAIN_WINDOW_X, "main-window-x", QVariant::Int }, { MAIN_WINDOW_Y, "main-window-y", QVariant::Int }, - { FILTER_MODE, "filter-mode", QVariant::String }, + { FILTER_MODE, "filter-mode", TrTypes::FilterModeType }, /* libtransmission settings */ { ALT_SPEED_LIMIT_UP, TR_PREFS_KEY_ALT_SPEED_UP, QVariant::Int }, @@ -134,6 +135,14 @@ Prefs :: Prefs( const char * configDir ): if( tr_bencGetInt( b, &intVal ) ) myValues[i].setValue( qlonglong(intVal) ); break; + case TrTypes::SortModeType: + if( tr_bencGetStr( b, &str ) ) + myValues[i] = QVariant::fromValue( SortMode( str ) ); + break; + case TrTypes::FilterModeType: + if( tr_bencGetStr( b, &str ) ) + myValues[i] = QVariant::fromValue( FilterMode( str ) ); + break; case QVariant::String: if( tr_bencGetStr( b, &str ) ) myValues[i].setValue( QString::fromUtf8(str) ); @@ -179,6 +188,12 @@ Prefs :: ~Prefs( ) case QVariant::Int: tr_bencDictAddInt( &top, key, val.toInt() ); break; + case TrTypes::SortModeType: + tr_bencDictAddStr( &top, key, val.value().name().toUtf8().constData() ); + break; + case TrTypes::FilterModeType: + tr_bencDictAddStr( &top, key, val.value().name().toUtf8().constData() ); + break; case QVariant::String: tr_bencDictAddStr( &top, key, val.toString().toUtf8().constData() ); break; diff --git a/qt/prefs.h b/qt/prefs.h index 1ab03d63f..632fce0bb 100644 --- a/qt/prefs.h +++ b/qt/prefs.h @@ -21,6 +21,8 @@ #include #include +#include "filters.h" + class Prefs: public QObject { Q_OBJECT; @@ -114,7 +116,7 @@ class Prefs: public QObject struct PrefItem { int id; const char * key; - QVariant::Type type; + int type; }; static PrefItem myItems[]; @@ -128,7 +130,7 @@ class Prefs: public QObject bool isCore( int key ) const { return FIRST_CORE_PREF<=key && key<=LAST_CORE_PREF; } bool isClient( int key ) const { return !isCore( key ); } const char * keyStr( int i ) const { return myItems[i].key; } - QVariant::Type type( int i ) const { return myItems[i].type; } + int type( int i ) const { return myItems[i].type; } const QVariant& variant( int i ) const { return myValues[i]; } Prefs( const char * configDir ); @@ -140,9 +142,16 @@ class Prefs: public QObject double getDouble( int key) const; QDateTime getDateTime( int key ) const; + template T get( int key ) const { + return myValues[key].value(); + } + + void set( int key, char * value ) { set( key, QString::fromUtf8(value) ); } + void set( int key, const char * value ) { set( key, QString::fromUtf8(value) ); } + template void set( int key, const T& value ) { QVariant& v( myValues[key] ); - const QVariant tmp( value ); + const QVariant tmp = QVariant::fromValue(value); if( v.isNull() || (v!=tmp) ) { v = tmp; emit changed( key ); diff --git a/qt/qtransmission.pro b/qt/qtransmission.pro index f3c30f06f..fcecfa9cc 100644 --- a/qt/qtransmission.pro +++ b/qt/qtransmission.pro @@ -20,6 +20,7 @@ HEADERS += about.h \ app.h \ details.h \ file-tree.h \ + filters.h \ hig.h \ mainwin.h \ make-dialog.h \ @@ -44,6 +45,7 @@ SOURCES += about.cc \ app.cc \ details.cc \ file-tree.cc \ + filters.cc \ hig.cc \ mainwin.cc \ make-dialog.cc \ diff --git a/qt/session.cc b/qt/session.cc index 6fc27b1f7..34faa7261 100644 --- a/qt/session.cc +++ b/qt/session.cc @@ -657,6 +657,8 @@ Session :: updateInfo( tr_benc * d ) myPrefs.set( i, (bool)val ); break; } + case TrTypes :: FilterModeType: + case TrTypes :: SortModeType: case QVariant :: String: { const char * val; if( tr_bencGetStr( b, &val ) ) diff --git a/qt/torrent-filter.cc b/qt/torrent-filter.cc index e562c26cc..e75ced7c6 100644 --- a/qt/torrent-filter.cc +++ b/qt/torrent-filter.cc @@ -12,6 +12,7 @@ #include +#include "filters.h" #include "prefs.h" #include "torrent.h" #include "torrent-filter.h" @@ -19,39 +20,49 @@ TorrentFilter :: TorrentFilter( Prefs& prefs ): myPrefs( prefs ), - myShowMode( getShowModeFromName( prefs.getString( Prefs::FILTER_MODE ) ) ), - myTextMode( FILTER_BY_NAME ), - mySortMode( getSortModeFromName( prefs.getString( Prefs::SORT_MODE ) ) ), - myIsAscending( prefs.getBool( Prefs::SORT_REVERSED ) ) + myTextMode( FILTER_BY_NAME ) { - resort( ); + // listen for changes to the preferences to know when to refilter / resort + connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(refreshPref(int))); + + // initialize our state from the current prefs + QList initKeys; + initKeys << Prefs :: SORT_MODE + << Prefs :: FILTER_MODE; + foreach( int key, initKeys ) + refreshPref( key ); } TorrentFilter :: ~TorrentFilter( ) { } -/*** -**** -***/ - void -TorrentFilter :: setShowMode( int showMode ) +TorrentFilter :: refreshPref( int key ) { - if( myShowMode != showMode ) + switch( key ) { - myPrefs.set( Prefs :: FILTER_MODE, getShowName( showMode ) ); - myShowMode = ShowMode( showMode ); - invalidateFilter( ); + case Prefs :: SORT_MODE: + case Prefs :: SORT_REVERSED: + sort( 0, myPrefs.getBool(Prefs::SORT_REVERSED) ? Qt::AscendingOrder : Qt::DescendingOrder ); + invalidate( ); + break; + case Prefs :: FILTER_MODE: + invalidateFilter( ); + break; } } +/*** +**** +***/ + void -TorrentFilter :: setTextMode( int textMode ) +TorrentFilter :: setTextMode( int i ) { - if( myTextMode != textMode ) + if( myTextMode != i ) { - myTextMode = TextMode( textMode ); + myTextMode = TextMode( i ); invalidateFilter( ); } } @@ -68,172 +79,10 @@ TorrentFilter :: setText( QString text ) } } -bool -TorrentFilter :: filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const -{ - QModelIndex childIndex = sourceModel()->index( sourceRow, 0, sourceParent ); - const Torrent * tor = childIndex.model()->data( childIndex, TorrentModel::TorrentRole ).value(); - const tr_torrent_activity activity = tor->getActivity( ); - bool accepts; - - switch( myShowMode ) - { - case SHOW_ALL: - accepts = true; - break; - case SHOW_ACTIVE: - accepts = tor->peersWeAreUploadingTo( ) > 0 || tor->peersWeAreDownloadingFrom( ) > 0 || tor->isVerifying( ); - break; - case SHOW_DOWNLOADING: - accepts = activity == TR_STATUS_DOWNLOAD; - break; - case SHOW_SEEDING: - accepts = activity == TR_STATUS_SEED; - break; - case SHOW_PAUSED: - accepts = activity == TR_STATUS_STOPPED; - break; - } - - if( accepts && !myText.isEmpty( ) ) switch( myTextMode ) - { - case FILTER_BY_NAME: - accepts = tor->name().contains( myText, Qt::CaseInsensitive ); - break; - case FILTER_BY_FILES: - accepts = tor->hasFileSubstring( myText ); - break; - case FILTER_BY_TRACKER: - accepts = tor->hasTrackerSubstring( myText ); - break; - } - - return accepts; -} - /*** **** ***/ -namespace -{ - struct NameAndNum - { - const char * name; - int num; - }; - - const struct NameAndNum showModes[] = { - { "show-all", TorrentFilter::SHOW_ALL }, - { "show-active", TorrentFilter::SHOW_ACTIVE }, - { "show-downloading", TorrentFilter::SHOW_DOWNLOADING }, - { "show-seeding", TorrentFilter::SHOW_SEEDING }, - { "show-paused", TorrentFilter::SHOW_PAUSED } - }; - - const int showModeCount = sizeof(showModes) / sizeof(showModes[0]); - - const struct NameAndNum sortModes[] = { - { "sort-by-name", TorrentFilter::SORT_BY_NAME }, - { "sort-by-activity", TorrentFilter::SORT_BY_ACTIVITY }, - { "sort-by-age", TorrentFilter::SORT_BY_AGE }, - { "sort-by-eta", TorrentFilter::SORT_BY_ETA }, - { "sort-by-progress", TorrentFilter::SORT_BY_PROGRESS }, - { "sort-by-ratio", TorrentFilter::SORT_BY_RATIO }, - { "sort-by-size", TorrentFilter::SORT_BY_SIZE }, - { "sort-by-state", TorrentFilter::SORT_BY_STATE }, - { "sort-by-tracker", TorrentFilter::SORT_BY_TRACKER } - }; - - const int sortModeCount = sizeof(sortModes) / sizeof(sortModes[0]); - - int getNum( const struct NameAndNum * rows, int numRows, const QString& name ) - { - for( int i=0; idata( right, TorrentModel::TorrentRole ).value(); bool less; - switch( getSortMode( ) ) + switch( myPrefs.get(Prefs::SORT_MODE).mode() ) { - case SORT_BY_SIZE: + case SortMode :: SORT_BY_SIZE: less = a->sizeWhenDone() < b->sizeWhenDone(); break; - case SORT_BY_ACTIVITY: + case SortMode :: SORT_BY_ACTIVITY: less = a->downloadSpeed() + a->uploadSpeed() < b->downloadSpeed() + b->uploadSpeed(); break; - case SORT_BY_AGE: + case SortMode :: SORT_BY_AGE: less = a->dateAdded() < b->dateAdded(); break; - case SORT_BY_ID: + case SortMode :: SORT_BY_ID: less = a->id() < b->id(); break; - case SORT_BY_RATIO: + case SortMode :: SORT_BY_RATIO: less = a->compareRatio( *b ) < 0; break; - case SORT_BY_PROGRESS: + case SortMode :: SORT_BY_PROGRESS: less = a->percentDone() < b->percentDone(); break; - case SORT_BY_ETA: + case SortMode :: SORT_BY_ETA: less = a->compareETA( *b ) < 0; break; - case SORT_BY_STATE: + case SortMode :: SORT_BY_STATE: if( a->hasError() != b->hasError() ) less = a->hasError(); else less = a->getActivity() < b->getActivity(); break; - case SORT_BY_TRACKER: + case SortMode :: SORT_BY_TRACKER: less = a->compareTracker( *b ) < 0; break; default: @@ -281,9 +130,56 @@ TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) c return less; } + +/*** +**** +***/ + +bool +TorrentFilter :: filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const +{ + QModelIndex childIndex = sourceModel()->index( sourceRow, 0, sourceParent ); + const Torrent * tor = childIndex.model()->data( childIndex, TorrentModel::TorrentRole ).value(); + const tr_torrent_activity activity = tor->getActivity( ); + bool accepts; + + switch( myPrefs.get(Prefs::FILTER_MODE).mode() ) + { + case FilterMode::SHOW_ALL: + accepts = true; + break; + case FilterMode::SHOW_ACTIVE: + accepts = tor->peersWeAreUploadingTo( ) > 0 || tor->peersWeAreDownloadingFrom( ) > 0 || tor->isVerifying( ); + break; + case FilterMode::SHOW_DOWNLOADING: + accepts = activity == TR_STATUS_DOWNLOAD; + break; + case FilterMode::SHOW_SEEDING: + accepts = activity == TR_STATUS_SEED; + break; + case FilterMode::SHOW_PAUSED: + accepts = activity == TR_STATUS_STOPPED; + break; + } + + if( accepts && !myText.isEmpty( ) ) switch( myTextMode ) + { + case FILTER_BY_NAME: + accepts = tor->name().contains( myText, Qt::CaseInsensitive ); + break; + case FILTER_BY_FILES: + accepts = tor->hasFileSubstring( myText ); + break; + case FILTER_BY_TRACKER: + accepts = tor->hasTrackerSubstring( myText ); + break; + } + + return accepts; +} + int TorrentFilter :: hiddenRowCount( ) const { return sourceModel()->rowCount( ) - rowCount( ); } - diff --git a/qt/torrent-filter.h b/qt/torrent-filter.h index 975409821..767297121 100644 --- a/qt/torrent-filter.h +++ b/qt/torrent-filter.h @@ -14,6 +14,9 @@ #define QTR_TORRENT_FILTER_H #include +#include +#include + struct Prefs; struct QString; @@ -27,43 +30,16 @@ class TorrentFilter: public QSortFilterProxyModel virtual ~TorrentFilter( ); public: - enum ShowMode { SHOW_ALL, SHOW_ACTIVE, SHOW_DOWNLOADING, SHOW_SEEDING, SHOW_PAUSED }; - ShowMode getShowMode( ) const { return myShowMode; } - ShowMode getShowModeFromName( const QString& name ) const; - const char * getShowName( int mode=-1 ) const; - enum TextMode { FILTER_BY_NAME, FILTER_BY_FILES, FILTER_BY_TRACKER }; TextMode getTextMode( ) const { return myTextMode; } - - enum SortMode{ SORT_BY_ACTIVITY, SORT_BY_AGE, SORT_BY_ETA, SORT_BY_NAME, - SORT_BY_PROGRESS, SORT_BY_RATIO, SORT_BY_SIZE, - SORT_BY_STATE, SORT_BY_TRACKER, SORT_BY_ID }; - SortMode getSortMode( ) const { return mySortMode; } - SortMode getSortModeFromName( const QString& name) const; - const char * getSortName( int mode=-1 ) const; - - bool isAscending( ) const { return myIsAscending; } - int hiddenRowCount( ) const; - public slots: - void setShowMode( int showMode ); void setTextMode( int textMode ); - void setSortMode( int sortMode ); void setText( QString ); - void sortByActivity( ); - void sortByAge( ); - void sortByETA( ); - void sortById( ); - void sortByName( ); - void sortByProgress( ); - void sortByRatio( ); - void sortBySize( ); - void sortByState( ); - void sortByTracker( ); - void setAscending( bool ); - void resort( ); + + private slots: + void refreshPref( int key ); protected: virtual bool filterAcceptsRow( int, const QModelIndex& ) const; @@ -71,10 +47,7 @@ class TorrentFilter: public QSortFilterProxyModel private: Prefs& myPrefs; - ShowMode myShowMode; TextMode myTextMode; - SortMode mySortMode; - bool myIsAscending; QString myText; }; diff --git a/qt/types.h b/qt/types.h index 9c52fbe2f..5efa56344 100644 --- a/qt/types.h +++ b/qt/types.h @@ -22,7 +22,9 @@ class TrTypes enum { PeerList = QVariant::UserType, - FileList + FileList, + FilterModeType, + SortModeType }; }; -- 2.40.0