(trunk qt) "Qt client's Files list should be sortable" -- fixed

This commit is contained in:
Charles Kerr 2010-07-28 18:53:25 +00:00
parent 969c72b927
commit cee744fcdd
2 changed files with 62 additions and 11 deletions

View File

@ -17,6 +17,7 @@
#include <QHeaderView> #include <QHeaderView>
#include <QPainter> #include <QPainter>
#include <QResizeEvent> #include <QResizeEvent>
#include <QSortFilterProxyModel>
#include <QStringList> #include <QStringList>
#include <libtransmission/transmission.h> // priorities #include <libtransmission/transmission.h> // priorities
@ -505,6 +506,39 @@ FileTreeModel :: clicked( const QModelIndex& index )
***** *****
****/ ****/
QSize
FileTreeDelegate :: sizeHint( const QStyleOptionViewItem& item, const QModelIndex& index ) const
{
QSize size;
switch( index.column( ) )
{
case COL_NAME: {
const QFontMetrics fm( item.font );
const QString text = index.model()->data(index).toString();
const int iconSize = QApplication::style()->pixelMetric( QStyle::PM_SmallIconSize );
size.rwidth() = HIG::PAD_SMALL + iconSize;
size.rheight() = std::max( iconSize, fm.height( ) );
break;
}
case COL_PROGRESS:
case COL_WANTED:
size = QSize( 20, 1 );
break;
default: {
const QFontMetrics fm( item.font );
const QString text = index.model()->data(index).toString();
size = fm.size( 0, text );
break;
}
}
size.rheight() += 8; // make the spacing a little nicer
return size;
}
void void
FileTreeDelegate :: paint( QPainter * painter, FileTreeDelegate :: paint( QPainter * painter,
const QStyleOptionViewItem & option, const QStyleOptionViewItem & option,
@ -512,7 +546,6 @@ FileTreeDelegate :: paint( QPainter * painter,
{ {
const int column( index.column( ) ); const int column( index.column( ) );
if( ( column != COL_PROGRESS ) && ( column != COL_WANTED ) && ( column != COL_NAME ) ) if( ( column != COL_PROGRESS ) && ( column != COL_WANTED ) && ( column != COL_NAME ) )
{ {
QItemDelegate::paint(painter, option, index); QItemDelegate::paint(painter, option, index);
@ -594,21 +627,25 @@ FileTreeDelegate :: paint( QPainter * painter,
FileTreeView :: FileTreeView( QWidget * parent ): FileTreeView :: FileTreeView( QWidget * parent ):
QTreeView( parent ), QTreeView( parent ),
myModel( this ), myModel( this ),
myProxy( new QSortFilterProxyModel( ) ),
myDelegate( this ) myDelegate( this )
{ {
setSortingEnabled( true );
setAlternatingRowColors( true ); setAlternatingRowColors( true );
setSelectionBehavior( QAbstractItemView::SelectRows ); setSelectionBehavior( QAbstractItemView::SelectRows );
setSelectionMode( QAbstractItemView::ExtendedSelection ); setSelectionMode( QAbstractItemView::ExtendedSelection );
setModel( &myModel ); myProxy->setSourceModel( &myModel );
setModel( myProxy );
setItemDelegate( &myDelegate ); setItemDelegate( &myDelegate );
setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
sortByColumn( COL_NAME, Qt::AscendingOrder );
installEventFilter( this ); installEventFilter( this );
for( int i=0; i<=NUM_COLUMNS; ++i ) for( int i=0; i<=NUM_COLUMNS; ++i )
header()->setResizeMode( i, QHeaderView::Fixed ); header()->setResizeMode( i, QHeaderView::Fixed );
connect( this, SIGNAL(clicked(const QModelIndex&)), connect( this, SIGNAL(clicked(const QModelIndex&)),
&myModel, SLOT(clicked(const QModelIndex&))); this, SLOT(onClicked(const QModelIndex&)) );
connect( &myModel, SIGNAL(priorityChanged(const QSet<int>&, int)), connect( &myModel, SIGNAL(priorityChanged(const QSet<int>&, int)),
this, SIGNAL(priorityChanged(const QSet<int>&, int))); this, SIGNAL(priorityChanged(const QSet<int>&, int)));
@ -617,6 +654,13 @@ FileTreeView :: FileTreeView( QWidget * parent ):
this, SIGNAL(wantedChanged(const QSet<int>&, bool))); this, SIGNAL(wantedChanged(const QSet<int>&, bool)));
} }
void
FileTreeView :: onClicked( const QModelIndex& proxyIndex )
{
const QModelIndex modelIndex = myProxy->mapToSource( proxyIndex );
myModel.clicked( modelIndex );
}
bool bool
FileTreeView :: eventFilter( QObject * o, QEvent * event ) FileTreeView :: eventFilter( QObject * o, QEvent * event )
{ {
@ -662,7 +706,7 @@ FileTreeView :: update( const FileList& files, bool torrentChanged )
QList<QModelIndex> added; QList<QModelIndex> added;
myModel.addFile( file.index, file.filename, file.wanted, file.priority, file.size, file.have, added, torrentChanged ); myModel.addFile( file.index, file.filename, file.wanted, file.priority, file.size, file.have, added, torrentChanged );
foreach( QModelIndex i, added ) foreach( QModelIndex i, added )
expand( i ); expand( myProxy->mapFromSource( i ) );
} }
} }

View File

@ -15,12 +15,16 @@
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QObject> #include <QObject>
#include <QItemDelegate>
#include <QList> #include <QList>
#include <QString>
#include <QSet> #include <QSet>
#include <QSize>
#include <QString>
#include <QTreeView> #include <QTreeView>
#include <QVariant> #include <QVariant>
#include <QItemDelegate>
class QSortFilterProxyModel;
class QStyle;
#include "torrent.h" // FileList #include "torrent.h" // FileList
@ -123,13 +127,12 @@ class FileTreeDelegate: public QItemDelegate
Q_OBJECT Q_OBJECT
public: public:
FileTreeDelegate( QObject * parent=0 ): QItemDelegate( parent ) { } FileTreeDelegate( QObject * parent=0 ): QItemDelegate( parent ) { }
virtual ~FileTreeDelegate( ) { } virtual ~FileTreeDelegate( ) { }
void paint( QPainter * painter, public:
const QStyleOptionViewItem & option, virtual QSize sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const;
const QModelIndex & index ) const; virtual void paint(QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const;
}; };
class FileTreeView: public QTreeView class FileTreeView: public QTreeView
@ -152,7 +155,11 @@ class FileTreeView: public QTreeView
private: private:
FileTreeModel myModel; FileTreeModel myModel;
QSortFilterProxyModel * myProxy;
FileTreeDelegate myDelegate; FileTreeDelegate myDelegate;
public slots:
void onClicked ( const QModelIndex & index );
}; };
#endif #endif