From f63d475d1335496a85b1e2b93152f9df6ec6c320 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 30 Jul 2010 22:24:48 +0000 Subject: [PATCH] (trunk qt) minor code simplification --- qt/file-tree.cc | 10 ++-- qt/torrent-delegate.cc | 4 +- qt/torrent-filter.cc | 106 +++++++++++++++++++++++++------------ qt/tracker-model-filter.cc | 2 +- 4 files changed, 80 insertions(+), 42 deletions(-) diff --git a/qt/file-tree.cc b/qt/file-tree.cc index 65ff59371..5a1a9516f 100644 --- a/qt/file-tree.cc +++ b/qt/file-tree.cc @@ -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; diff --git a/qt/torrent-delegate.cc b/qt/torrent-delegate.cc index 5b42fca10..75acc9563 100644 --- a/qt/torrent-delegate.cc +++ b/qt/torrent-delegate.cc @@ -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 * tor( index.data( TorrentModel::TorrentRole ).value() ); 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 * tor( index.data( TorrentModel::TorrentRole ).value() ); painter->save( ); painter->setClipRect( option.rect ); drawBackground( painter, option, index ); diff --git a/qt/torrent-filter.cc b/qt/torrent-filter.cc index 88f3bba1c..3185038cc 100644 --- a/qt/torrent-filter.cc +++ b/qt/torrent-filter.cc @@ -13,14 +13,15 @@ #include #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 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 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 tr_torrent_activity activity = tor->getActivity( ); bool accepts; + const tr_torrent_activity activity = tor->getActivity( ); - switch( myPrefs.get(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(); + bool accepts = true; + + if( accepts ) { + const FilterMode m = myPrefs.get(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(); + if( activityFilterAcceptsTorrent( tor, mode ) ) + ++count; + } + + return count; +} diff --git a/qt/tracker-model-filter.cc b/qt/tracker-model-filter.cc index df509cad2..77d7eee89 100644 --- a/qt/tracker-model-filter.cc +++ b/qt/tracker-model-filter.cc @@ -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(); + const TrackerInfo trackerInfo = index.data( TrackerModel::TrackerRole ).value(); return myShowBackups || !trackerInfo.st.isBackup; } -- 2.40.0