]> granicus.if.org Git - transmission/commitdiff
(trunk qt) minor code simplification
authorCharles Kerr <charles@transmissionbt.com>
Fri, 30 Jul 2010 22:24:48 +0000 (22:24 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Fri, 30 Jul 2010 22:24:48 +0000 (22:24 +0000)
qt/file-tree.cc
qt/torrent-delegate.cc
qt/torrent-filter.cc
qt/tracker-model-filter.cc

index 65ff59371b2756c0586c5e929772019c99fa94e1..5a1a9516f0f7e5e10cef29201f85001630a6a742 100644 (file)
@@ -515,7 +515,7 @@ FileTreeDelegate :: sizeHint( const QStyleOptionViewItem& item, const QModelInde
     {
         case COL_NAME: {
             const QFontMetrics fm( item.font );
-            const QString text = index.model()->data(index).toString();
+            const QString text = index.data().toString();
             const int iconSize = QApplication::style()->pixelMetric( QStyle::PM_SmallIconSize );
             size.rwidth() = HIG::PAD_SMALL + iconSize;
             size.rheight() = std::max( iconSize, fm.height( ) );
@@ -529,7 +529,7 @@ FileTreeDelegate :: sizeHint( const QStyleOptionViewItem& item, const QModelInde
 
         default: {
             const QFontMetrics fm( item.font );
-            const QString text = index.model()->data(index).toString();
+            const QString text = index.data().toString();
             size = fm.size( 0, text );
             break;
         }
@@ -571,7 +571,7 @@ FileTreeDelegate :: paint( QPainter                    * painter,
             icon = style->standardIcon( QStyle::StandardPixmap( QStyle::SP_DirOpenIcon ) );
         else
         {
-            QString name = index.model()->data(index).toString();
+            QString name = index.data().toString();
             icon = Utils :: guessMimeIcon( name.left( name.lastIndexOf( " (" ) ) );
         }
         icon.paint( painter, iconArea, Qt::AlignCenter, QIcon::Normal, QIcon::On );
@@ -595,7 +595,7 @@ FileTreeDelegate :: paint( QPainter                    * painter,
         p.maximum = 100;
         p.textAlignment = Qt::AlignCenter;
         p.textVisible = true;
-        p.progress = (int)(100.0*index.model()->data(index).toDouble());
+        p.progress = (int)(100.0*index.data().toDouble());
         p.text = QString( ).sprintf( "%d%%", p.progress );
         style->drawControl( QStyle::CE_ProgressBar, &p, painter );
     }
@@ -607,7 +607,7 @@ FileTreeDelegate :: paint( QPainter                    * painter,
         o.rect.setSize( QSize( 20, option.rect.height( ) ) );
         o.rect.moveCenter( option.rect.center( ) );
         o.fontMetrics = QApplication::fontMetrics();
-        switch( index.model()->data(index).toInt() ) {
+        switch( index.data().toInt() ) {
             case Qt::Unchecked: o.state |= QStyle::State_Off; break;
             case Qt::Checked:   o.state |= QStyle::State_On; break;
             default:            o.state |= QStyle::State_NoChange;break;
index 5b42fca10b0c05b12efd9a37005e8b08f2e90386..75acc9563f06abd774d565d3c95d7709fd05552a 100644 (file)
@@ -298,7 +298,7 @@ QSize
 TorrentDelegate :: sizeHint( const QStyleOptionViewItem  & option,
                              const QModelIndex           & index ) const
 {
-    const Torrent * tor( index.model()->data( index, TorrentModel::TorrentRole ).value<const Torrent*>() );
+    const Torrent * tor( index.data( TorrentModel::TorrentRole ).value<const Torrent*>() );
     return sizeHint( option, *tor );
 }
 
@@ -307,7 +307,7 @@ TorrentDelegate :: paint( QPainter                    * painter,
                           const QStyleOptionViewItem  & option,
                           const QModelIndex           & index) const
 {
-    const Torrent * tor( index.model()->data( index, TorrentModel::TorrentRole ).value<const Torrent*>() );
+    const Torrent * tor( index.data( TorrentModel::TorrentRole ).value<const Torrent*>() );
     painter->save( );
     painter->setClipRect( option.rect );
     drawBackground( painter, option, index );
index 88f3bba1c4e09ad66e890d33b3807d29d2ff96be..3185038ccd215f0f97c3e206e84517cf71382efa 100644 (file)
 #include <iostream>
 
 #include "filters.h"
+#include "hig.h"
 #include "prefs.h"
 #include "torrent.h"
 #include "torrent-filter.h"
 #include "torrent-model.h"
+#include "utils.h"
 
 TorrentFilter :: TorrentFilter( Prefs& prefs ):
-    myPrefs( prefs ),
-    myTextMode( FILTER_BY_NAME )
+    myPrefs( prefs )
 {
     // listen for changes to the preferences to know when to refilter / resort
     connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(refreshPref(int)));
@@ -30,7 +31,9 @@ TorrentFilter :: TorrentFilter( Prefs& prefs ):
     // initialize our state from the current prefs
     QList<int> initKeys;
     initKeys << Prefs :: SORT_MODE
-             << Prefs :: FILTER_MODE;
+             << Prefs :: FILTER_MODE
+             << Prefs :: FILTER_TRACKERS
+             << Prefs :: FILTER_TEXT;
     foreach( int key, initKeys )
         refreshPref( key );
 }
@@ -44,10 +47,13 @@ TorrentFilter :: refreshPref( int key )
 {
     switch( key )
     {
+        case Prefs :: FILTER_TEXT:
         case Prefs :: FILTER_MODE:
+        case Prefs :: FILTER_TRACKERS:
             invalidateFilter( );
             /* force a re-sort */
             sort( 0, !myPrefs.getBool(Prefs::SORT_REVERSED) ? Qt::AscendingOrder : Qt::DescendingOrder );
+
         case Prefs :: SORT_MODE:
         case Prefs :: SORT_REVERSED:
             sort( 0, myPrefs.getBool(Prefs::SORT_REVERSED) ? Qt::AscendingOrder : Qt::DescendingOrder );
@@ -60,32 +66,6 @@ TorrentFilter :: refreshPref( int key )
 ****
 ***/
 
-void
-TorrentFilter :: setTextMode( int i )
-{
-    if( myTextMode != i )
-    {
-        myTextMode = TextMode( i );
-        invalidateFilter( );
-    }
-}
-
-void
-TorrentFilter :: setText( QString text )
-{
-    QString trimmed = text.trimmed( );
-
-    if( myText != trimmed )
-    {
-        myText = trimmed;
-        invalidateFilter( );
-    }
-}
-
-/***
-****
-***/
-
 namespace
 {
     template <typename T> int compare( const T a, const T b )
@@ -155,14 +135,18 @@ TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) c
 ***/
 
 bool
-TorrentFilter :: filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
+TorrentFilter :: trackerFilterAcceptsTorrent( const Torrent * tor, const QString& tracker ) const
+{
+    return tracker.isEmpty() || tor->hasTrackerSubstring( tracker );
+}
+
+bool
+TorrentFilter :: activityFilterAcceptsTorrent( const Torrent * tor, const FilterMode& m ) 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;
+    const tr_torrent_activity activity = tor->getActivity( );
 
-    switch( myPrefs.get<FilterMode>(Prefs::FILTER_MODE).mode() )
+    switch( m.mode( ) )
     {
         case FilterMode::SHOW_ALL:
             accepts = true;
@@ -179,8 +163,44 @@ TorrentFilter :: filterAcceptsRow( int sourceRow, const QModelIndex& sourceParen
         case FilterMode::SHOW_PAUSED:
             accepts = activity == TR_STATUS_STOPPED;
             break;
+        case FilterMode::SHOW_QUEUED:
+            accepts = activity == TR_STATUS_CHECK_WAIT;
+            break;
+        case FilterMode::SHOW_VERIFYING:
+            accepts = activity == TR_STATUS_CHECK;
+            break;
+        case FilterMode::SHOW_ERROR:
+            accepts = tor->hasError( );
+            break;
+    }
+
+    return accepts;
+}
+
+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*>();
+    bool accepts = true;
+
+    if( accepts ) {
+        const FilterMode m = myPrefs.get<FilterMode>(Prefs::FILTER_MODE);
+        accepts = activityFilterAcceptsTorrent( tor, m );
+    }
+
+    if( accepts ) {
+        const QString trackers = myPrefs.getString(Prefs::FILTER_TRACKERS);
+        accepts = trackerFilterAcceptsTorrent( tor, trackers );
     }
 
+    if( accepts ) {
+        const QString text = myPrefs.getString( Prefs::FILTER_TEXT );
+        if( !text.isEmpty( ) )
+            accepts = tor->name().contains( text, Qt::CaseInsensitive );
+    }
+
+#if 0
     if( accepts && !myText.isEmpty( ) ) switch( myTextMode )
     {
         case FILTER_BY_NAME:
@@ -193,6 +213,7 @@ TorrentFilter :: filterAcceptsRow( int sourceRow, const QModelIndex& sourceParen
             accepts = tor->hasTrackerSubstring( myText );
             break;
     }
+#endif
 
     return accepts;
 }
@@ -202,3 +223,20 @@ TorrentFilter :: hiddenRowCount( ) const
 {
     return sourceModel()->rowCount( ) - rowCount( );
 }
+
+int
+TorrentFilter :: count( const FilterMode& mode ) const
+{
+    int count = 0;
+
+    for( int row=0; ; ++row ) {
+        QModelIndex index = sourceModel()->index( row, 0 );
+        if( !index.isValid( ) )
+            break;
+        const Torrent * tor = index.data( TorrentModel::TorrentRole ).value<const Torrent*>();
+        if( activityFilterAcceptsTorrent( tor, mode ) )
+            ++count;
+    }
+
+    return count;
+}
index df509cad22f0bcde49542d235e0f831aa423dac3..77d7eee89fe71f8405afccb9b96210ef5fb9d765 100644 (file)
@@ -31,6 +31,6 @@ TrackerModelFilter :: filterAcceptsRow( int                 sourceRow,
                                         const QModelIndex & sourceParent ) const
 {
     QModelIndex index = sourceModel()->index( sourceRow, 0, sourceParent );
-    const TrackerInfo trackerInfo = index.model()->data( index, TrackerModel::TrackerRole ).value<TrackerInfo>();
+    const TrackerInfo trackerInfo = index.data( TrackerModel::TrackerRole ).value<TrackerInfo>();
     return myShowBackups || !trackerInfo.st.isBackup;
 }