]> granicus.if.org Git - transmission/commitdiff
(qt) better integration of sort & filter modes into the preferences mechanism
authorCharles Kerr <charles@transmissionbt.com>
Sat, 11 Apr 2009 18:25:12 +0000 (18:25 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Sat, 11 Apr 2009 18:25:12 +0000 (18:25 +0000)
12 files changed:
qt/filters.cc [new file with mode: 0644]
qt/filters.h [new file with mode: 0644]
qt/mainwin.cc
qt/mainwin.h
qt/prefs-dialog.cc
qt/prefs.cc
qt/prefs.h
qt/qtransmission.pro
qt/session.cc
qt/torrent-filter.cc
qt/torrent-filter.h
qt/types.h

diff --git a/qt/filters.cc b/qt/filters.cc
new file mode 100644 (file)
index 0000000..5c60775
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
+ *
+ * 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<NUM_MODES; ++i )
+        if( names[i] == name )
+            return i;
+    return FilterMode().mode(); // use the default value
+}
+
+const QString SortMode::names[NUM_MODES] = {
+    "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"
+};
+
+int
+SortMode :: modeFromName( const QString& name )
+{
+    for( int i=0; i<NUM_MODES; ++i )
+        if( names[i] == name )
+            return i;
+    return SortMode().mode(); // use the default value
+}
diff --git a/qt/filters.h b/qt/filters.h
new file mode 100644 (file)
index 0000000..8c650d7
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
+ *
+ * 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 <QMetaType>
+#include <QString>
+#include <QVariant>
+
+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
index 125d711ee82ce1db865e8493d19eb7c947401d13..69ad8c0f19ba544acacded9853ca6c2e836b133f 100644 (file)
 #include <QHBoxLayout>
 #include <QSystemTrayIcon>
 #include <QUrl>
+#include <QSignalMapper>
 
 #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<SortMode>(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<FilterMode>(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 );
index adf6369e5c05eb05da94c697130665db278086da..03f7616f7e4f2c9020ac7e52e0c4dbc4e6b8b163 100644 (file)
@@ -68,7 +68,7 @@ class TrMainWindow: public QMainWindow
         QIcon getStockIcon( const QString&, int fallback=-1 );
 
     private:
-        void setShowMode( TorrentFilter::ShowMode );
+        void setShowMode( int );
         QSet<int> 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( );
index 94fc3accd27361c5da57e03473f8cc409b4648aa..462628dc956f675203b7214bd1059ce3d304f5ca 100644 (file)
@@ -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*
index 0d3be38fbbb4dedcef345bff9e1767c38fe3bc31..31b5b1d93fe51ac759e42df10e176d9fbd563567 100644 (file)
@@ -22,6 +22,7 @@
 #include <libtransmission/json.h>
 #include <libtransmission/utils.h>
 #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<SortMode>().name().toUtf8().constData() );
+                break;
+            case TrTypes::FilterModeType:
+                tr_bencDictAddStr( &top, key, val.value<FilterMode>().name().toUtf8().constData() );
+                break;
             case QVariant::String:
                 tr_bencDictAddStr( &top, key, val.toString().toUtf8().constData() );
                 break;
index 1ab03d63fdffa5d644e4cf5235e11ba90f3c4923..632fce0bbb948c7518d10b6544a6cbbbb07a32c1 100644 (file)
@@ -21,6 +21,8 @@
 #include <libtransmission/transmission.h>
 #include <libtransmission/bencode.h>
 
+#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<typename T> T get( int key ) const {
+            return myValues[key].value<T>();
+        }
+
+        void set( int key, char * value ) { set( key, QString::fromUtf8(value) ); }
+        void set( int key, const char * value ) { set( key, QString::fromUtf8(value) ); }
+
         template<typename T> 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 );
index f3c30f06ff9bcf89187096080c284612414492aa..fcecfa9cc306be19e3a35a42334f0bb726cc377e 100644 (file)
@@ -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 \
index 6fc27b1f7c700daefbb359a85678951b16148a74..34faa7261fab66c35d80fa00cc0e2844de2fda69 100644 (file)
@@ -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 ) )
index e562c26cc3db68a74c55bfb10a86c68d94a07ec3..e75ced7c63a3d3ae1296b26d4049cebae14c32ff 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <iostream>
 
+#include "filters.h"
 #include "prefs.h"
 #include "torrent.h"
 #include "torrent-filter.h"
 
 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<int> 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 Torrent*>();
-    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; i<numRows; ++i )
-            if( name == rows[i].name )
-                return rows[i].num;
-        return rows[0].num; // fallback value
-    }
-
-    const char* getName( const struct NameAndNum * rows, int numRows, int num )
-    {
-        for( int i=0; i<numRows; ++i )
-            if( num == rows[i].num )
-                return rows[i].name;
-        return rows[0].name; // fallback value
-    }
-}
-
-TorrentFilter :: ShowMode
-TorrentFilter :: getShowModeFromName( const QString& key ) const
-{
-    return ShowMode( getNum( showModes, showModeCount, key ) );
-}
-
-const char*
-TorrentFilter :: getShowName( int mode ) const
-{
-    if( mode < 0 ) mode = getShowMode( );
-    return getName( showModes, showModeCount, mode );
-}
-
-TorrentFilter :: SortMode
-TorrentFilter :: getSortModeFromName( const QString& key ) const
-{
-    return SortMode( getNum( sortModes, sortModeCount, key ) );
-}
-
-const char*
-TorrentFilter :: getSortName( int mode ) const
-{
-    if( mode < 0 ) mode = getSortMode( );
-    return getName( sortModes, sortModeCount, mode );
-}
-
-/***
-****
-***/
-
-void
-TorrentFilter :: resort( )
-{
-    invalidate( );
-    sort( 0, myIsAscending ? Qt::AscendingOrder : Qt::DescendingOrder );
-}
-
-void
-TorrentFilter :: setAscending( bool b )
-{
-    if( myIsAscending != b )
-    {
-        myIsAscending = b;
-        resort( );
-    }
-}
-
-void
-TorrentFilter :: setSortMode( int sortMode )
-{
-    if( mySortMode != sortMode )
-    {
-        myPrefs.set( Prefs :: SORT_MODE, getSortName( sortMode ) );
-        mySortMode = SortMode( sortMode );
-        setDynamicSortFilter ( true );
-        resort( );
-    }
-}
-
-void TorrentFilter :: sortByActivity ( ) { setSortMode( SORT_BY_ACTIVITY ); }
-void TorrentFilter :: sortByAge      ( ) { setSortMode( SORT_BY_AGE ); }
-void TorrentFilter :: sortByETA      ( ) { setSortMode( SORT_BY_ETA ); }
-void TorrentFilter :: sortById       ( ) { setSortMode( SORT_BY_ID ); }
-void TorrentFilter :: sortByName     ( ) { setSortMode( SORT_BY_NAME ); }
-void TorrentFilter :: sortByProgress ( ) { setSortMode( SORT_BY_PROGRESS ); }
-void TorrentFilter :: sortByRatio    ( ) { setSortMode( SORT_BY_RATIO ); }
-void TorrentFilter :: sortBySize     ( ) { setSortMode( SORT_BY_SIZE ); }
-void TorrentFilter :: sortByState    ( ) { setSortMode( SORT_BY_STATE ); }
-void TorrentFilter :: sortByTracker  ( ) { setSortMode( SORT_BY_TRACKER ); }
-
 bool
 TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) const
 {
@@ -241,36 +90,36 @@ TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) c
     const Torrent * b = sourceModel()->data( right, TorrentModel::TorrentRole ).value<const Torrent*>();
     bool less;
 
-    switch( getSortMode( ) )
+    switch( myPrefs.get<SortMode>(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 Torrent*>();
+    const tr_torrent_activity activity = tor->getActivity( );
+    bool accepts;
+
+    switch( myPrefs.get<FilterMode>(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( );
 }
-
index 9754098216ecc2a93542d8428a0f0a973dcd3474..767297121eaeb545f325a7cff9f80327f18b8c89 100644 (file)
@@ -14,6 +14,9 @@
 #define QTR_TORRENT_FILTER_H
 
 #include <QSortFilterProxyModel>
+#include <QMetaType>
+#include <QVariant>
+
 
 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;
 };
 
index 9c52fbe2f0bb23722526211b606ecee8bdb7ff8e..5efa56344ced39d38ffb254b410734edba98446e 100644 (file)
@@ -22,7 +22,9 @@ class TrTypes
         enum
         {
             PeerList = QVariant::UserType,
-            FileList
+            FileList,
+            FilterModeType,
+            SortModeType
         };
 };