From cee744fcddbcae69cc1a28077f4b0ae6468c3c11 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 28 Jul 2010 18:53:25 +0000 Subject: [PATCH] (trunk qt) "Qt client's Files list should be sortable" -- fixed --- qt/file-tree.cc | 54 ++++++++++++++++++++++++++++++++++++++++++++----- qt/file-tree.h | 19 +++++++++++------ 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/qt/file-tree.cc b/qt/file-tree.cc index 672287ac9..cf2682a7e 100644 --- a/qt/file-tree.cc +++ b/qt/file-tree.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include // 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 FileTreeDelegate :: paint( QPainter * painter, const QStyleOptionViewItem & option, @@ -512,7 +546,6 @@ FileTreeDelegate :: paint( QPainter * painter, { const int column( index.column( ) ); - if( ( column != COL_PROGRESS ) && ( column != COL_WANTED ) && ( column != COL_NAME ) ) { QItemDelegate::paint(painter, option, index); @@ -594,21 +627,25 @@ FileTreeDelegate :: paint( QPainter * painter, FileTreeView :: FileTreeView( QWidget * parent ): QTreeView( parent ), myModel( this ), + myProxy( new QSortFilterProxyModel( ) ), myDelegate( this ) { + setSortingEnabled( true ); setAlternatingRowColors( true ); setSelectionBehavior( QAbstractItemView::SelectRows ); setSelectionMode( QAbstractItemView::ExtendedSelection ); - setModel( &myModel ); + myProxy->setSourceModel( &myModel ); + setModel( myProxy ); setItemDelegate( &myDelegate ); setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); + sortByColumn( COL_NAME, Qt::AscendingOrder ); installEventFilter( this ); for( int i=0; i<=NUM_COLUMNS; ++i ) header()->setResizeMode( i, QHeaderView::Fixed ); - connect( this, SIGNAL(clicked(const QModelIndex&)), - &myModel, SLOT(clicked(const QModelIndex&))); + connect( this, SIGNAL(clicked(const QModelIndex&)), + this, SLOT(onClicked(const QModelIndex&)) ); connect( &myModel, SIGNAL(priorityChanged(const QSet&, int)), this, SIGNAL(priorityChanged(const QSet&, int))); @@ -617,6 +654,13 @@ FileTreeView :: FileTreeView( QWidget * parent ): this, SIGNAL(wantedChanged(const QSet&, bool))); } +void +FileTreeView :: onClicked( const QModelIndex& proxyIndex ) +{ + const QModelIndex modelIndex = myProxy->mapToSource( proxyIndex ); + myModel.clicked( modelIndex ); +} + bool FileTreeView :: eventFilter( QObject * o, QEvent * event ) { @@ -662,7 +706,7 @@ FileTreeView :: update( const FileList& files, bool torrentChanged ) QList added; myModel.addFile( file.index, file.filename, file.wanted, file.priority, file.size, file.have, added, torrentChanged ); foreach( QModelIndex i, added ) - expand( i ); + expand( myProxy->mapFromSource( i ) ); } } diff --git a/qt/file-tree.h b/qt/file-tree.h index 11c07ea41..2cf89a125 100644 --- a/qt/file-tree.h +++ b/qt/file-tree.h @@ -15,12 +15,16 @@ #include #include +#include #include -#include #include +#include +#include #include #include -#include + +class QSortFilterProxyModel; +class QStyle; #include "torrent.h" // FileList @@ -123,13 +127,12 @@ class FileTreeDelegate: public QItemDelegate Q_OBJECT public: - FileTreeDelegate( QObject * parent=0 ): QItemDelegate( parent ) { } virtual ~FileTreeDelegate( ) { } - void paint( QPainter * painter, - const QStyleOptionViewItem & option, - const QModelIndex & index ) const; + public: + virtual QSize sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const; + virtual void paint(QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const; }; class FileTreeView: public QTreeView @@ -152,7 +155,11 @@ class FileTreeView: public QTreeView private: FileTreeModel myModel; + QSortFilterProxyModel * myProxy; FileTreeDelegate myDelegate; + + public slots: + void onClicked ( const QModelIndex & index ); }; #endif