--- /dev/null
+/*
+ * 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
+}
--- /dev/null
+/*
+ * 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
#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"
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()));
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()));
*****
****/
+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( )
{
***
**/
-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"); }
/**
***
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 );
QIcon getStockIcon( const QString&, int fallback=-1 );
private:
- void setShowMode( TorrentFilter::ShowMode );
+ void setShowMode( int );
QSet<int> getSelectedTorrents( ) const;
void updateNetworkIcon( );
QWidgetList myHidden;
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( );
PrefsDialog :: textChanged( const QString& text )
{
const int key( sender()->property( PREF_KEY ).toInt( ) );
- myPrefs.set( key, qPrintable(text) );
+ myPrefs.set( key, text );
}
QLineEdit*
#include <libtransmission/json.h>
#include <libtransmission/utils.h>
#include "prefs.h"
+#include "types.h"
/***
****
{ 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 },
{ 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 },
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) );
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;
#include <libtransmission/transmission.h>
#include <libtransmission/bencode.h>
+#include "filters.h"
+
class Prefs: public QObject
{
Q_OBJECT;
struct PrefItem {
int id;
const char * key;
- QVariant::Type type;
+ int type;
};
static PrefItem myItems[];
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 );
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 );
app.h \
details.h \
file-tree.h \
+ filters.h \
hig.h \
mainwin.h \
make-dialog.h \
app.cc \
details.cc \
file-tree.cc \
+ filters.cc \
hig.cc \
mainwin.cc \
make-dialog.cc \
myPrefs.set( i, (bool)val );
break;
}
+ case TrTypes :: FilterModeType:
+ case TrTypes :: SortModeType:
case QVariant :: String: {
const char * val;
if( tr_bencGetStr( b, &val ) )
#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( );
}
}
}
}
-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
{
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:
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( );
}
-
#define QTR_TORRENT_FILTER_H
#include <QSortFilterProxyModel>
+#include <QMetaType>
+#include <QVariant>
+
struct Prefs;
struct QString;
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;
private:
Prefs& myPrefs;
- ShowMode myShowMode;
TextMode myTextMode;
- SortMode mySortMode;
- bool myIsAscending;
QString myText;
};
enum
{
PeerList = QVariant::UserType,
- FileList
+ FileList,
+ FilterModeType,
+ SortModeType
};
};