2
* This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
4
* This file is licensed by the GPL version 2. Works owned by the
5
* Transmission project are granted a special exemption to clause 2(b)
6
* so that the bulk of its code can remain under the MIT license.
7
* This exemption does not extend to derived works not owned by
8
* the Transmission project.
16
#include <QApplication>
18
#include <QResizeEvent>
19
#include <QHeaderView>
20
#include <QStringList>
22
#include <libtransmission/transmission.h> // priorities
24
#include "file-tree.h"
26
#include "torrent.h" // FileList
27
#include "utils.h" // mime icons
42
FileTreeItem :: ~FileTreeItem( )
44
assert( myChildren.isEmpty( ) );
47
if( !myParent->myChildren.removeOne( this ) )
48
assert( 0 && "failed to remove" );
52
FileTreeItem :: appendChild( FileTreeItem * child )
54
child->myParent = this;
55
myChildren.append( child );
59
FileTreeItem :: child( const QString& filename )
61
foreach( FileTreeItem * c, myChildren )
62
if( c->name() == filename )
69
FileTreeItem :: row( ) const
74
i = myParent->myChildren.indexOf( const_cast<FileTreeItem*>(this) );
80
FileTreeItem :: data( int column ) const
85
case COL_NAME: value.setValue( name() ); break;
86
case COL_PROGRESS: value.setValue( progress( ) ); break;
87
case COL_WANTED: value.setValue( isSubtreeWanted( ) ); break;
88
case COL_PRIORITY: value.setValue( priorityString( ) ); break;
95
FileTreeItem :: getSubtreeSize( uint64_t& have, uint64_t& total ) const
100
foreach( const FileTreeItem * i, myChildren )
101
i->getSubtreeSize( have, total );
105
FileTreeItem :: progress( ) const
108
uint64_t have(0), total(0);
109
getSubtreeSize( have, total );
111
d = have / (double)total;
116
FileTreeItem :: update( int index, bool wanted, int priority, uint64_t totalSize, uint64_t haveSize )
118
bool changed = false;
120
if( myIndex != index )
125
if( myIsWanted != wanted )
130
if( myPriority != priority )
132
myPriority = priority;
135
if( myTotalSize != totalSize )
137
myTotalSize = totalSize;
140
if( myHaveSize != haveSize )
142
myHaveSize = haveSize;
150
FileTreeItem :: priorityString( ) const
152
const int i( priority( ) );
153
if( i == LOW ) return tr( "Low" );
154
if( i == HIGH ) return tr( "High" );
155
if( i == NORMAL ) return tr( "Normal" );
156
return tr( "Mixed" );
160
FileTreeItem :: priority( ) const
164
if( myChildren.isEmpty( ) ) switch( myPriority ) {
165
case TR_PRI_LOW: i |= LOW; break;
166
case TR_PRI_HIGH: i |= HIGH; break;
167
default: i |= NORMAL; break;
170
foreach( const FileTreeItem * child, myChildren )
171
i |= child->priority( );
177
FileTreeItem :: setSubtreePriority( int i, QSet<int>& ids )
179
if( myPriority != i ) {
182
ids.insert( myIndex );
185
foreach( FileTreeItem * child, myChildren )
186
child->setSubtreePriority( i, ids );
190
FileTreeItem :: twiddlePriority( QSet<int>& ids, int& p )
192
const int old( priority( ) );
194
if ( old & LOW ) p = TR_PRI_NORMAL;
195
else if( old & NORMAL ) p = TR_PRI_HIGH;
198
setSubtreePriority( p, ids );
202
FileTreeItem :: isSubtreeWanted( ) const
204
if( myChildren.isEmpty( ) )
205
return myIsWanted ? Qt::Checked : Qt::Unchecked;
208
foreach( const FileTreeItem * child, myChildren ) {
209
const int childWanted = child->isSubtreeWanted( );
211
wanted = childWanted;
212
if( wanted != childWanted )
213
wanted = Qt::PartiallyChecked;
214
if( wanted == Qt::PartiallyChecked )
222
FileTreeItem :: setSubtreeWanted( bool b, QSet<int>& ids )
224
if( myIsWanted != b ) {
227
ids.insert( myIndex );
230
foreach( FileTreeItem * child, myChildren )
231
child->setSubtreeWanted( b, ids );
235
FileTreeItem :: twiddleWanted( QSet<int>& ids, bool& wanted )
237
wanted = isSubtreeWanted( ) != Qt::Checked;
238
setSubtreeWanted( wanted, ids );
246
FileTreeModel :: FileTreeModel( QObject *parent ):
247
QAbstractItemModel(parent)
249
rootItem = new FileTreeItem( -1 );
252
FileTreeModel :: ~FileTreeModel( )
260
FileTreeModel :: data( const QModelIndex &index, int role ) const
264
if( index.isValid() && role==Qt::DisplayRole )
266
FileTreeItem *item = static_cast<FileTreeItem*>(index.internalPointer());
267
value = item->data( index.column( ) );
274
FileTreeModel :: flags( const QModelIndex& index ) const
276
int i( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
278
if( index.column( ) == COL_WANTED )
279
i |= Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
281
return (Qt::ItemFlags)i;
285
FileTreeModel :: headerData( int column, Qt::Orientation orientation, int role ) const
289
if( orientation==Qt::Horizontal && role==Qt::DisplayRole ) {
291
case COL_NAME: data.setValue( tr( "File" ) ); break;
292
case COL_PROGRESS: data.setValue( tr( "Progress" ) ); break;
293
case COL_WANTED: data.setValue( tr( "Download" ) ); break;
294
case COL_PRIORITY: data.setValue( tr( "Priority" ) ); break;
303
FileTreeModel :: index( int row, int column, const QModelIndex& parent ) const
307
if( !hasIndex( row, column, parent ) )
309
std::cerr << " I don't have this index " << std::endl;
313
FileTreeItem * parentItem;
315
if( !parent.isValid( ) )
316
parentItem = rootItem;
318
parentItem = static_cast<FileTreeItem*>(parent.internalPointer());
320
FileTreeItem * childItem = parentItem->child( row );
323
i = createIndex( row, column, childItem );
325
//std::cerr << "FileTreeModel::index(row("<<row<<"),col("<<column<<"),parent("<<qPrintable(parentItem->name())<<")) is returning " << qPrintable(childItem->name()) << ": internalPointer " << i.internalPointer() << " row " << i.row() << " col " << i.column() << std::endl;
332
FileTreeModel :: parent( const QModelIndex& child ) const
334
return parent( child, 0 ); // QAbstractItemModel::parent() wants col 0
338
FileTreeModel :: parent( const QModelIndex& child, int column ) const
340
if( !child.isValid( ) )
341
return QModelIndex( );
343
FileTreeItem * childItem = static_cast<FileTreeItem*>(child.internalPointer());
345
return indexOf( childItem->parent( ), column );
349
FileTreeModel :: rowCount( const QModelIndex& parent ) const
351
FileTreeItem * parentItem;
353
if( !parent.isValid( ) )
354
parentItem = rootItem;
356
parentItem = static_cast<FileTreeItem*>(parent.internalPointer());
358
return parentItem->childCount();
362
FileTreeModel :: columnCount( const QModelIndex &parent ) const
370
FileTreeModel :: indexOf( FileTreeItem * item, int column ) const
372
if( !item || item==rootItem )
373
return QModelIndex( );
375
return createIndex( item->row( ), column, item );
379
FileTreeModel :: clearSubtree( const QModelIndex& top )
381
while( hasChildren( top ) )
382
clearSubtree( index( 0, 0, top ) );
384
delete static_cast<FileTreeItem*>(top.internalPointer());
388
FileTreeModel :: clear( )
390
clearSubtree( QModelIndex( ) );
396
FileTreeModel :: addFile( int index,
397
const QString & filename,
402
QList<QModelIndex> & rowsAdded )
404
FileTreeItem * i( rootItem );
406
foreach( QString token, filename.split( "/" ) )
408
FileTreeItem * child( i->child( token ) );
411
QModelIndex parentIndex( indexOf( i, 0 ) );
412
const int n( i->childCount( ) );
413
beginInsertRows( parentIndex, n, n );
414
i->appendChild(( child = new FileTreeItem( -1, token )));
416
rowsAdded.append( indexOf( child, 0 ) );
422
if( i->update( index, wanted, priority, size, have ) )
423
dataChanged( indexOf( i, 0 ), indexOf( i, NUM_COLUMNS-1 ) );
427
FileTreeModel :: parentsChanged( const QModelIndex& index, int column )
429
QModelIndex walk = index;
432
walk = parent( walk, column );
433
if( !walk.isValid( ) )
435
dataChanged( walk, walk );
440
FileTreeModel :: subtreeChanged( const QModelIndex& index, int column )
442
const int childCount = rowCount( index );
446
// tell everyone that this tier changed
447
dataChanged( index.child(0,column), index.child(childCount-1,column) );
450
for( int i=0; i<childCount; ++i )
451
subtreeChanged( index.child(i,column), column );
455
FileTreeModel :: clicked( const QModelIndex& index )
457
const int column( index.column( ) );
459
if( !index.isValid( ) )
462
if( column == COL_WANTED )
464
FileTreeItem * item( static_cast<FileTreeItem*>(index.internalPointer()));
467
item->twiddleWanted( fileIds, want );
468
emit wantedChanged( fileIds, want );
470
dataChanged( index, index );
471
parentsChanged( index, column );
472
subtreeChanged( index, column );
474
else if( column == COL_PRIORITY )
476
FileTreeItem * item( static_cast<FileTreeItem*>(index.internalPointer()));
479
item->twiddlePriority( fileIds, priority );
480
emit priorityChanged( fileIds, priority );
482
dataChanged( index, index );
483
parentsChanged( index, column );
484
subtreeChanged( index, column );
493
FileTreeDelegate :: paint( QPainter * painter,
494
const QStyleOptionViewItem & option,
495
const QModelIndex & index ) const
497
const int column( index.column( ) );
500
if( ( column != COL_PROGRESS ) && ( column != COL_WANTED ) && ( column != COL_NAME ) )
502
QItemDelegate::paint(painter, option, index);
506
QStyle * style( QApplication :: style( ) );
507
if( option.state & QStyle::State_Selected )
508
painter->fillRect( option.rect, option.palette.highlight( ) );
510
if( option.state & QStyle::State_Selected )
511
painter->setBrush(option.palette.highlightedText());
513
if( column == COL_NAME )
515
// draw the file icon
516
static const int iconSize( style->pixelMetric( QStyle :: PM_SmallIconSize ) );
517
const QRect iconArea( option.rect.x(),
518
option.rect.y() + (option.rect.height()-iconSize)/2,
519
iconSize, iconSize );
521
if( index.model()->hasChildren( index ) )
522
icon = style->standardIcon( QStyle::StandardPixmap( QStyle::SP_DirOpenIcon ) );
524
icon = Utils :: guessMimeIcon( index.model()->data(index).toString( ) );
525
icon.paint( painter, iconArea, Qt::AlignCenter, QIcon::Normal, QIcon::On );
528
QStyleOptionViewItem tmp( option );
529
tmp.rect.setWidth( option.rect.width( ) - iconArea.width( ) - HIG::PAD_SMALL );
530
tmp.rect.moveRight( option.rect.right( ) );
531
QItemDelegate::paint( painter, tmp, index );
533
else if( column == COL_PROGRESS )
535
QStyleOptionProgressBar p;
536
p.state = QStyle::State_Enabled;
537
p.direction = QApplication::layoutDirection();
538
p.rect = option.rect;
539
p.rect.setSize( QSize( option.rect.width()-2, option.rect.height()-2 ) );
540
p.rect.moveCenter( option.rect.center( ) );
541
p.fontMetrics = QApplication::fontMetrics();
544
p.textAlignment = Qt::AlignCenter;
545
p.textVisible = true;
546
p.progress = (int)(100.0*index.model()->data(index).toDouble());
547
p.text = QString( ).sprintf( "%d%%", p.progress );
548
style->drawControl( QStyle::CE_ProgressBar, &p, painter );
550
else if( column == COL_WANTED )
552
QStyleOptionButton o;
553
o.state = QStyle::State_Enabled;
554
o.direction = QApplication::layoutDirection();
555
o.rect.setSize( QSize( 20, option.rect.height( ) ) );
556
o.rect.moveCenter( option.rect.center( ) );
557
o.fontMetrics = QApplication::fontMetrics();
558
switch( index.model()->data(index).toInt() ) {
559
case Qt::Unchecked: o.state |= QStyle::State_Off; break;
560
case Qt::Checked: o.state |= QStyle::State_On; break;
561
default: o.state |= QStyle::State_NoChange;break;
563
style->drawControl( QStyle::CE_CheckBox, &o, painter );
575
FileTreeView :: FileTreeView( QWidget * parent ):
580
setAlternatingRowColors( true );
581
setSelectionBehavior( QAbstractItemView::SelectRows );
582
setSelectionMode( QAbstractItemView::ExtendedSelection );
583
setModel( &myModel );
584
setItemDelegate( &myDelegate );
585
setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
586
installEventFilter( this );
588
for( int i=0; i<=NUM_COLUMNS; ++i )
589
header()->setResizeMode( i, QHeaderView::Fixed );
591
connect( this, SIGNAL(clicked(const QModelIndex&)),
592
&myModel, SLOT(clicked(const QModelIndex&)));
594
connect( &myModel, SIGNAL(priorityChanged(const QSet<int>&, int)),
595
this, SIGNAL(priorityChanged(const QSet<int>&, int)));
597
connect( &myModel, SIGNAL(wantedChanged(const QSet<int>&, bool)),
598
this, SIGNAL(wantedChanged(const QSet<int>&, bool)));
602
FileTreeView :: eventFilter( QObject * o, QEvent * event )
607
// this is kind of a hack to get the last three columns be the
608
// right size, and to have the filename column use whatever
609
// space is left over...
610
if( event->type() == QEvent::Resize )
612
QResizeEvent * r = dynamic_cast<QResizeEvent*>(event);
613
int left = r->size().width();
614
const QFontMetrics fontMetrics( font( ) );
615
for( int column=0; column<NUM_COLUMNS; ++column ) {
616
if( column == COL_NAME )
618
if( isColumnHidden( column ) )
620
const QString header = myModel.headerData( column, Qt::Horizontal ).toString( ) + " ";
621
const int width = fontMetrics.size( 0, header ).width( );
622
setColumnWidth( column, width );
625
left -= 20; // not sure why this is necessary. it works in different themes + font sizes though...
626
setColumnWidth( COL_NAME, std::max(left,0) );
634
FileTreeView :: update( const FileList& files )
636
foreach( const TrFile file, files ) {
637
QList<QModelIndex> added;
638
myModel.addFile( file.index, file.filename, file.wanted, file.priority, file.size, file.have, added );
639
foreach( QModelIndex i, added )
645
FileTreeView :: clear( )