(trunk qt) minor code simplification

This commit is contained in:
Charles Kerr 2010-07-30 22:24:48 +00:00
parent 729bbd292f
commit f63d475d13
4 changed files with 81 additions and 43 deletions

View 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;

View 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 );

View File

@ -13,14 +13,15 @@
#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
{
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;
return tracker.isEmpty() || tor->hasTrackerSubstring( tracker );
}
switch( myPrefs.get<FilterMode>(Prefs::FILTER_MODE).mode() )
bool
TorrentFilter :: activityFilterAcceptsTorrent( const Torrent * tor, const FilterMode& m ) const
{
bool accepts;
const tr_torrent_activity activity = tor->getActivity( );
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;
}

View 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;
}