enum
{
- COL_NAME,
- COL_PROGRESS,
- COL_WANTED,
- COL_PRIORITY,
- NUM_COLUMNS
+ COL_NAME,
+ COL_PROGRESS,
+ COL_WANTED,
+ COL_PRIORITY,
+ NUM_COLUMNS
};
/****
QHash<QString,int>&
FileTreeItem :: getMyChildRows()
{
- const size_t n = childCount( );
+ const size_t n = childCount();
- // ensure that all the rows are hashed
- while( myFirstUnhashedRow < n )
+ // ensure that all the rows are hashed
+ while (myFirstUnhashedRow < n)
{
myChildRows.insert (myChildren[myFirstUnhashedRow]->name(), myFirstUnhashedRow);
myFirstUnhashedRow++;
}
- return myChildRows;
+ return myChildRows;
}
-FileTreeItem :: ~FileTreeItem( )
+FileTreeItem :: ~FileTreeItem()
{
- assert( myChildren.isEmpty( ) );
+ assert(myChildren.isEmpty());
- if( myParent != 0 )
+ if (myParent != 0)
{
- const int pos = row( );
- assert ((pos>=0) && "couldn't find child in parent's lookup");
- myParent->myChildren.removeAt( pos );
- myParent->myChildRows.remove( name() );
- myParent->myFirstUnhashedRow = pos;
+ const int pos = row();
+ assert ((pos>=0) && "couldn't find child in parent's lookup");
+ myParent->myChildren.removeAt(pos);
+ myParent->myChildRows.remove(name());
+ myParent->myFirstUnhashedRow = pos;
}
}
void
-FileTreeItem :: appendChild( FileTreeItem * child )
+FileTreeItem :: appendChild (FileTreeItem * child)
{
- const size_t n = childCount();
- child->myParent = this;
- myChildren.append (child);
- myFirstUnhashedRow = n;
+ const size_t n = childCount();
+ child->myParent = this;
+ myChildren.append (child);
+ myFirstUnhashedRow = n;
}
FileTreeItem *
-FileTreeItem :: child (const QString& filename )
+FileTreeItem :: child (const QString& filename)
{
FileTreeItem * item(0);
}
int
-FileTreeItem :: row( ) const
+FileTreeItem :: row() const
{
- int i(-1);
+ int i(-1);
- if( myParent )
+ if(myParent)
{
- i = myParent->getMyChildRows().value (name(), -1);
- assert (this == myParent->myChildren[i]);
+ i = myParent->getMyChildRows().value (name(), -1);
+ assert (this == myParent->myChildren[i]);
}
- return i;
+ return i;
}
QVariant
}
else if (role == Qt::DisplayRole)
{
- switch( column )
+ switch(column)
{
case COL_NAME:
value.setValue (fileSizeName());
}
void
-FileTreeItem :: getSubtreeSize( uint64_t& have, uint64_t& total ) const
+FileTreeItem :: getSubtreeSize (uint64_t& have, uint64_t& total) const
{
- have += myHaveSize;
- total += myTotalSize;
+ have += myHaveSize;
+ total += myTotalSize;
- foreach( const FileTreeItem * i, myChildren )
- i->getSubtreeSize( have, total );
+ foreach(const FileTreeItem * i, myChildren)
+ i->getSubtreeSize(have, total);
}
double
-FileTreeItem :: progress( ) const
+FileTreeItem :: progress () const
{
- double d(0);
- uint64_t have(0), total(0);
- getSubtreeSize( have, total );
- if( total )
- d = have / (double)total;
- return d;
+ double d(0);
+ uint64_t have(0), total(0);
+
+ getSubtreeSize(have, total);
+ if (total)
+ d = have / (double)total;
+
+ return d;
}
QString
-FileTreeItem :: fileSizeName( ) const
+FileTreeItem :: fileSizeName () const
{
- uint64_t have(0), total(0);
- QString str;
- getSubtreeSize( have, total );
- str = QString( name() + " (%1)" ).arg( Formatter::sizeToString( total ) );
- return str;
+ uint64_t have(0), total(0);
+ QString str;
+ getSubtreeSize(have, total);
+ str = QString(name() + " (%1)").arg(Formatter::sizeToString(total));
+ return str;
}
bool
-FileTreeItem :: update( int index, bool wanted, int priority, uint64_t totalSize, uint64_t haveSize, bool torrentChanged )
+FileTreeItem :: update (int index,
+ bool wanted,
+ int priority,
+ uint64_t totalSize,
+ uint64_t haveSize,
+ bool torrentChanged)
{
- bool changed = false;
+ bool changed = false;
- if( myIndex != index )
+ if (myIndex != index)
{
- myIndex = index;
- changed = true;
+ myIndex = index;
+ changed = true;
}
- if( torrentChanged && myIsWanted != wanted )
+
+ if (torrentChanged && myIsWanted != wanted)
{
- myIsWanted = wanted;
- changed = true;
+ myIsWanted = wanted;
+ changed = true;
}
- if( torrentChanged && myPriority != priority )
+
+ if (torrentChanged && myPriority != priority)
{
- myPriority = priority;
- changed = true;
+ myPriority = priority;
+ changed = true;
}
- if( myTotalSize != totalSize )
+
+ if (myTotalSize != totalSize)
{
- myTotalSize = totalSize;
- changed = true;
+ myTotalSize = totalSize;
+ changed = true;
}
- if( myHaveSize != haveSize )
+
+ if (myHaveSize != haveSize)
{
- myHaveSize = haveSize;
- changed = true;
+ myHaveSize = haveSize;
+ changed = true;
}
- return changed;
+ return changed;
}
QString
-FileTreeItem :: priorityString( ) const
+FileTreeItem :: priorityString () const
{
- const int i( priority( ) );
- if( i == LOW ) return tr( "Low" );
- if( i == HIGH ) return tr( "High" );
- if( i == NORMAL ) return tr( "Normal" );
- return tr( "Mixed" );
+ const int i(priority());
+ if(i == LOW) return tr("Low");
+ if(i == HIGH) return tr("High");
+ if(i == NORMAL) return tr("Normal");
+ return tr("Mixed");
}
int
-FileTreeItem :: priority( ) const
+FileTreeItem :: priority () const
{
- int i( 0 );
+ int i(0);
+
+ if(myChildren.isEmpty()) switch(myPriority)
+ {
+ case TR_PRI_LOW:
+ i |= LOW;
+ break;
+
+ case TR_PRI_HIGH:
+ i |= HIGH;
+ break;
- if( myChildren.isEmpty( ) ) switch( myPriority ) {
- case TR_PRI_LOW: i |= LOW; break;
- case TR_PRI_HIGH: i |= HIGH; break;
- default: i |= NORMAL; break;
+ default:
+ i |= NORMAL;
+ break;
}
- foreach( const FileTreeItem * child, myChildren )
- i |= child->priority( );
+ foreach(const FileTreeItem * child, myChildren)
+ i |= child->priority();
- return i;
+ return i;
}
void
-FileTreeItem :: setSubtreePriority( int i, QSet<int>& ids )
+FileTreeItem :: setSubtreePriority (int i, QSet<int>& ids)
{
- if( myPriority != i ) {
- myPriority = i;
- if( myIndex >= 0 )
- ids.insert( myIndex );
+ if (myPriority != i)
+ {
+ myPriority = i;
+
+ if (myIndex >= 0)
+ ids.insert (myIndex);
}
- foreach( FileTreeItem * child, myChildren )
- child->setSubtreePriority( i, ids );
+ foreach (FileTreeItem * child, myChildren)
+ child->setSubtreePriority (i, ids);
}
void
-FileTreeItem :: twiddlePriority( QSet<int>& ids, int& p )
+FileTreeItem :: twiddlePriority (QSet<int>& ids, int& p)
{
- const int old( priority( ) );
+ const int old(priority());
- if ( old & LOW ) p = TR_PRI_NORMAL;
- else if( old & NORMAL ) p = TR_PRI_HIGH;
- else p = TR_PRI_LOW;
+ if (old & LOW)
+ p = TR_PRI_NORMAL;
+ else if (old & NORMAL)
+ p = TR_PRI_HIGH;
+ else
+ p = TR_PRI_LOW;
- setSubtreePriority( p, ids );
+ setSubtreePriority (p, ids);
}
int
-FileTreeItem :: isSubtreeWanted( ) const
+FileTreeItem :: isSubtreeWanted () const
{
- if( myChildren.isEmpty( ) )
- return myIsWanted ? Qt::Checked : Qt::Unchecked;
+ if(myChildren.isEmpty())
+ return myIsWanted ? Qt::Checked : Qt::Unchecked;
+
+ int wanted(-1);
+ foreach (const FileTreeItem * child, myChildren)
+ {
+ const int childWanted = child->isSubtreeWanted();
+
+ if(wanted == -1)
+ wanted = childWanted;
- int wanted( -1 );
- foreach( const FileTreeItem * child, myChildren ) {
- const int childWanted = child->isSubtreeWanted( );
- if( wanted == -1 )
- wanted = childWanted;
- if( wanted != childWanted )
- wanted = Qt::PartiallyChecked;
- if( wanted == Qt::PartiallyChecked )
- return wanted;
+ if(wanted != childWanted)
+ wanted = Qt::PartiallyChecked;
+
+ if(wanted == Qt::PartiallyChecked)
+ return wanted;
}
- return wanted;
+ return wanted;
}
void
-FileTreeItem :: setSubtreeWanted( bool b, QSet<int>& ids )
+FileTreeItem :: setSubtreeWanted (bool b, QSet<int>& ids)
{
- if( myIsWanted != b ) {
- myIsWanted = b;
- if( myIndex >= 0 )
- ids.insert( myIndex );
+ if(myIsWanted != b)
+ {
+ myIsWanted = b;
+
+ if (myIndex >= 0)
+ ids.insert(myIndex);
}
- foreach( FileTreeItem * child, myChildren )
- child->setSubtreeWanted( b, ids );
+ foreach (FileTreeItem * child, myChildren)
+ child->setSubtreeWanted (b, ids);
}
void
-FileTreeItem :: twiddleWanted( QSet<int>& ids, bool& wanted )
+FileTreeItem :: twiddleWanted (QSet<int>& ids, bool& wanted)
{
- wanted = isSubtreeWanted( ) != Qt::Checked;
- setSubtreeWanted( wanted, ids );
+ wanted = isSubtreeWanted() != Qt::Checked;
+ setSubtreeWanted (wanted, ids);
}
/***
****
***/
-FileTreeModel :: FileTreeModel( QObject *parent ):
- QAbstractItemModel(parent)
+FileTreeModel :: FileTreeModel (QObject *parent):
+ QAbstractItemModel(parent)
{
- rootItem = new FileTreeItem( -1 );
+ rootItem = new FileTreeItem(-1);
}
-FileTreeModel :: ~FileTreeModel( )
+FileTreeModel :: ~FileTreeModel()
{
- clear( );
+ clear();
- delete rootItem;
+ delete rootItem;
}
QVariant
-FileTreeModel :: data( const QModelIndex &index, int role ) const
+FileTreeModel :: data (const QModelIndex &index, int role) const
{
- QVariant value;
+ QVariant value;
- if (index.isValid())
- {
- FileTreeItem * i = static_cast<FileTreeItem*>(index.internalPointer());
- value = i->data (index.column(), role);
- }
+ if (index.isValid())
+ {
+ FileTreeItem * i = static_cast<FileTreeItem*>(index.internalPointer());
+ value = i->data (index.column(), role);
+ }
- return value;
+ return value;
}
Qt::ItemFlags
-FileTreeModel :: flags( const QModelIndex& index ) const
+FileTreeModel :: flags (const QModelIndex& index) const
{
- int i( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
+ int i(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
- if( index.column( ) == COL_NAME )
- i |= Qt::ItemIsEditable;
+ if(index.column() == COL_NAME)
+ i |= Qt::ItemIsEditable;
- if( index.column( ) == COL_WANTED )
- i |= Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
+ if(index.column() == COL_WANTED)
+ i |= Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
- return (Qt::ItemFlags)i;
+ return (Qt::ItemFlags)i;
}
bool
}
QVariant
-FileTreeModel :: headerData( int column, Qt::Orientation orientation, int role ) const
+FileTreeModel :: headerData(int column, Qt::Orientation orientation, int role) const
{
- QVariant data;
+ QVariant data;
+
+ if(orientation==Qt::Horizontal && role==Qt::DisplayRole)
+ {
+ switch (column)
+ {
+ case COL_NAME:
+ data.setValue (tr("File"));
+ break;
+
+ case COL_PROGRESS:
+ data.setValue (tr("Progress"));
+ break;
+
+ case COL_WANTED:
+ data.setValue (tr("Download"));
+ break;
+
+ case COL_PRIORITY:
+ data.setValue (tr("Priority"));
+ break;
- if( orientation==Qt::Horizontal && role==Qt::DisplayRole ) {
- switch( column ) {
- case COL_NAME: data.setValue( tr( "File" ) ); break;
- case COL_PROGRESS: data.setValue( tr( "Progress" ) ); break;
- case COL_WANTED: data.setValue( tr( "Download" ) ); break;
- case COL_PRIORITY: data.setValue( tr( "Priority" ) ); break;
- default: break;
+ default:
+ break;
}
}
- return data;
+ return data;
}
QModelIndex
-FileTreeModel :: index( int row, int column, const QModelIndex& parent ) const
+FileTreeModel :: index (int row, int column, const QModelIndex& parent) const
{
- QModelIndex i;
+ QModelIndex i;
- if( !hasIndex( row, column, parent ) )
+ if (!hasIndex (row, column, parent))
{
- std::cerr << " I don't have this index " << std::endl;
+ std::cerr << " I don't have this index " << std::endl;
}
- else
+ else
{
- FileTreeItem * parentItem;
+ FileTreeItem * parentItem;
- if( !parent.isValid( ) )
- parentItem = rootItem;
- else
- parentItem = static_cast<FileTreeItem*>(parent.internalPointer());
-
- FileTreeItem * childItem = parentItem->child( row );
+ if(!parent.isValid())
+ parentItem = rootItem;
+ else
+ parentItem = static_cast<FileTreeItem*>(parent.internalPointer());
- if( childItem )
- i = createIndex( row, column, childItem );
+ FileTreeItem * childItem = parentItem->child(row);
-//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;
+ if (childItem)
+ i = createIndex(row, column, childItem);
}
return i;
}
QModelIndex
-FileTreeModel :: parent( const QModelIndex& child ) const
+FileTreeModel :: parent (const QModelIndex& child) const
{
- return parent( child, 0 ); // QAbstractItemModel::parent() wants col 0
+ return parent (child, 0); // QAbstractItemModel::parent() wants col 0
}
QModelIndex
-FileTreeModel :: parent( const QModelIndex& child, int column ) const
+FileTreeModel :: parent (const QModelIndex& child, int column) const
{
- if( !child.isValid( ) )
- return QModelIndex( );
+ if (!child.isValid())
+ return QModelIndex();
- FileTreeItem * childItem = static_cast<FileTreeItem*>(child.internalPointer());
+ FileTreeItem * childItem = static_cast<FileTreeItem*>(child.internalPointer());
- return indexOf( childItem->parent( ), column );
+ return indexOf (childItem->parent(), column);
}
int
-FileTreeModel :: rowCount( const QModelIndex& parent ) const
+FileTreeModel :: rowCount (const QModelIndex& parent) const
{
- FileTreeItem * parentItem;
+ FileTreeItem * parentItem;
- if( !parent.isValid( ) )
- parentItem = rootItem;
- else
- parentItem = static_cast<FileTreeItem*>(parent.internalPointer());
+ if (!parent.isValid())
+ parentItem = rootItem;
+ else
+ parentItem = static_cast<FileTreeItem*>(parent.internalPointer());
- return parentItem->childCount();
+ return parentItem->childCount();
}
int
-FileTreeModel :: columnCount( const QModelIndex &parent ) const
+FileTreeModel :: columnCount (const QModelIndex &parent) const
{
- Q_UNUSED( parent );
+ Q_UNUSED(parent);
- return 4;
+ return 4;
}
QModelIndex
-FileTreeModel :: indexOf( FileTreeItem * item, int column ) const
+FileTreeModel :: indexOf (FileTreeItem * item, int column) const
{
- if( !item || item==rootItem )
- return QModelIndex( );
+ if (!item || item==rootItem)
+ return QModelIndex();
- return createIndex( item->row( ), column, item );
+ return createIndex(item->row(), column, item);
}
void
-FileTreeModel :: clearSubtree( const QModelIndex& top )
+FileTreeModel :: clearSubtree (const QModelIndex& top)
{
- size_t i = rowCount( top );
+ size_t i = rowCount (top);
- while( i > 0 )
- clearSubtree( index( --i, 0, top ) );
+ while (i > 0)
+ clearSubtree(index(--i, 0, top));
delete static_cast<FileTreeItem*>(top.internalPointer());
}
void
-FileTreeModel :: clear( )
+FileTreeModel :: clear ()
{
- clearSubtree( QModelIndex( ) );
+ clearSubtree (QModelIndex());
- reset( );
+ reset ();
}
void
-FileTreeModel :: addFile( int index,
+FileTreeModel :: addFile (int index,
const QString & filename,
bool wanted,
int priority,
uint64_t size,
uint64_t have,
QList<QModelIndex> & rowsAdded,
- bool torrentChanged )
+ bool torrentChanged)
{
- FileTreeItem * i( rootItem );
+ FileTreeItem * i(rootItem);
- foreach( QString token, filename.split( QChar::fromAscii('/') ) )
+ foreach (QString token, filename.split(QChar::fromAscii('/')))
{
- FileTreeItem * child( i->child( token ) );
- if( !child )
+ FileTreeItem * child(i->child(token));
+ if (!child)
{
- QModelIndex parentIndex( indexOf( i, 0 ) );
- const int n( i->childCount( ) );
- beginInsertRows( parentIndex, n, n );
- i->appendChild(( child = new FileTreeItem( -1, token )));
- endInsertRows( );
- rowsAdded.append( indexOf( child, 0 ) );
+ QModelIndex parentIndex (indexOf(i, 0));
+ const int n (i->childCount());
+ beginInsertRows (parentIndex, n, n);
+ i->appendChild ((child = new FileTreeItem(-1, token)));
+ endInsertRows ();
+ rowsAdded.append (indexOf(child, 0));
}
- i = child;
+ i = child;
}
- if( i != rootItem )
- if( i->update( index, wanted, priority, size, have, torrentChanged ) )
- dataChanged( indexOf( i, 0 ), indexOf( i, NUM_COLUMNS-1 ) );
+ if (i != rootItem)
+ if (i->update (index, wanted, priority, size, have, torrentChanged))
+ dataChanged (indexOf(i, 0), indexOf(i, NUM_COLUMNS-1));
}
void
-FileTreeModel :: parentsChanged( const QModelIndex& index, int column )
+FileTreeModel :: parentsChanged (const QModelIndex& index, int column)
{
- QModelIndex walk = index;
+ QModelIndex walk = index;
- for( ;; ) {
- walk = parent( walk, column );
- if( !walk.isValid( ) )
- break;
- dataChanged( walk, walk );
+ for (;;)
+ {
+ walk = parent(walk, column);
+ if(!walk.isValid())
+ break;
+
+ dataChanged(walk, walk);
}
}
void
-FileTreeModel :: subtreeChanged( const QModelIndex& index, int column )
+FileTreeModel :: subtreeChanged (const QModelIndex& index, int column)
{
- const int childCount = rowCount( index );
- if( !childCount )
- return;
+ const int childCount = rowCount (index);
+ if (!childCount)
+ return;
- // tell everyone that this tier changed
- dataChanged( index.child(0,column), index.child(childCount-1,column) );
+ // tell everyone that this tier changed
+ dataChanged (index.child(0,column), index.child(childCount-1,column));
- // walk the subtiers
- for( int i=0; i<childCount; ++i )
- subtreeChanged( index.child(i,column), column );
+ // walk the subtiers
+ for (int i=0; i<childCount; ++i)
+ subtreeChanged (index.child(i,column), column);
}
void
-FileTreeModel :: clicked( const QModelIndex& index )
+FileTreeModel :: clicked (const QModelIndex& index)
{
- const int column( index.column( ) );
+ const int column (index.column());
- if( !index.isValid( ) )
- return;
+ if (!index.isValid())
+ return;
- if( column == COL_WANTED )
+ if (column == COL_WANTED)
{
- FileTreeItem * item( static_cast<FileTreeItem*>(index.internalPointer()));
- bool want;
- QSet<int> fileIds;
- item->twiddleWanted( fileIds, want );
- emit wantedChanged( fileIds, want );
+ bool want;
+ QSet<int> file_ids;
+ FileTreeItem * item;
- dataChanged( index, index );
- parentsChanged( index, column );
- subtreeChanged( index, column );
+ item = static_cast<FileTreeItem*>(index.internalPointer());
+ item->twiddleWanted (file_ids, want);
+ emit wantedChanged (file_ids, want);
+
+ dataChanged (index, index);
+ parentsChanged (index, column);
+ subtreeChanged (index, column);
}
- else if( column == COL_PRIORITY )
+ else if (column == COL_PRIORITY)
{
- FileTreeItem * item( static_cast<FileTreeItem*>(index.internalPointer()));
- int priority;
- QSet<int>fileIds;
- item->twiddlePriority( fileIds, priority );
- emit priorityChanged( fileIds, priority );
+ int priority;
+ QSet<int> file_ids;
+ FileTreeItem * item;
+
+ item = static_cast<FileTreeItem*>(index.internalPointer());
+ item->twiddlePriority (file_ids, priority);
+ emit priorityChanged (file_ids, priority);
- dataChanged( index, index );
- parentsChanged( index, column );
- subtreeChanged( index, column );
+ dataChanged(index, index);
+ parentsChanged(index, column);
+ subtreeChanged(index, column);
}
}
****/
QSize
-FileTreeDelegate :: sizeHint( const QStyleOptionViewItem& item, const QModelIndex& index ) const
+FileTreeDelegate :: sizeHint(const QStyleOptionViewItem& item, const QModelIndex& index) const
{
- QSize size;
+ QSize size;
- switch( index.column( ) )
+ switch(index.column())
{
- case COL_NAME: {
- const QFontMetrics fm( item.font );
- 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( ) );
- break;
+ case COL_NAME:
+ {
+ const QFontMetrics fm(item.font);
+ 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());
+ break;
}
- case COL_PROGRESS:
- case COL_WANTED:
- size = QSize( 20, 1 );
- break;
+ case COL_PROGRESS:
+ case COL_WANTED:
+ size = QSize(20, 1);
+ break;
- default: {
- const QFontMetrics fm( item.font );
- const QString text = index.data().toString();
- size = fm.size( 0, text );
- break;
+ default:
+ {
+ const QFontMetrics fm(item.font);
+ const QString text = index.data().toString();
+ size = fm.size(0, text);
+ break;
}
}
- size.rheight() += 8; // make the spacing a little nicer
- return size;
+ size.rheight() += 8; // make the spacing a little nicer
+ return size;
}
void
-FileTreeDelegate :: paint( QPainter * painter,
+FileTreeDelegate :: paint (QPainter * painter,
const QStyleOptionViewItem & option,
- const QModelIndex & index ) const
+ const QModelIndex & index) const
{
- 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);
- return;
+ QItemDelegate::paint(painter, option, index);
+ return;
}
- QStyle * style( QApplication :: style( ) );
- if( option.state & QStyle::State_Selected )
- painter->fillRect( option.rect, option.palette.highlight( ) );
- painter->save();
- if( option.state & QStyle::State_Selected )
- painter->setBrush(option.palette.highlightedText());
+ QStyle * style(QApplication :: style());
+ if (option.state & QStyle::State_Selected)
+ painter->fillRect(option.rect, option.palette.highlight());
+ painter->save();
+ if (option.state & QStyle::State_Selected)
+ painter->setBrush(option.palette.highlightedText());
- if( column == COL_NAME )
+ if (column == COL_NAME)
{
- // draw the file icon
- static const int iconSize( style->pixelMetric( QStyle :: PM_SmallIconSize ) );
- const QRect iconArea( option.rect.x(),
- option.rect.y() + (option.rect.height()-iconSize)/2,
- iconSize, iconSize );
- QIcon icon;
- if( index.model()->hasChildren( index ) )
- icon = style->standardIcon( QStyle::StandardPixmap( QStyle::SP_DirOpenIcon ) );
- else
+ // draw the file icon
+ static const int iconSize(style->pixelMetric(QStyle :: PM_SmallIconSize));
+ const QRect iconArea(option.rect.x(),
+ option.rect.y() + (option.rect.height()-iconSize)/2,
+ iconSize, iconSize);
+ QIcon icon;
+ if (index.model()->hasChildren(index))
+ {
+ icon = style->standardIcon(QStyle::StandardPixmap(QStyle::SP_DirOpenIcon));
+ }
+ else
{
- QString name = index.data().toString();
- icon = Utils :: guessMimeIcon( name.left( name.lastIndexOf( " (" ) ) );
+ QString name = index.data().toString();
+ icon = Utils :: guessMimeIcon (name.left(name.lastIndexOf(" (")));
}
- icon.paint( painter, iconArea, Qt::AlignCenter, QIcon::Normal, QIcon::On );
-
- // draw the name
- QStyleOptionViewItem tmp( option );
- tmp.rect.setWidth( option.rect.width( ) - iconArea.width( ) - HIG::PAD_SMALL );
- tmp.rect.moveRight( option.rect.right( ) );
- QItemDelegate::paint( painter, tmp, index );
- }
- else if( column == COL_PROGRESS )
- {
- QStyleOptionProgressBar p;
- p.state = option.state | QStyle::State_Small;
- p.direction = QApplication::layoutDirection();
- p.rect = option.rect;
- p.rect.setSize( QSize( option.rect.width()-2, option.rect.height()-8 ) );
- p.rect.moveCenter( option.rect.center( ) );
- p.fontMetrics = QApplication::fontMetrics();
- p.minimum = 0;
- p.maximum = 100;
- p.textAlignment = Qt::AlignCenter;
- p.textVisible = true;
- p.progress = (int)(100.0*index.data().toDouble());
- p.text = QString( ).sprintf( "%d%%", p.progress );
- style->drawControl( QStyle::CE_ProgressBar, &p, painter );
- }
- else if( column == COL_WANTED )
- {
- QStyleOptionButton o;
- o.state = option.state;
- o.direction = QApplication::layoutDirection();
- o.rect.setSize( QSize( 20, option.rect.height( ) ) );
- o.rect.moveCenter( option.rect.center( ) );
- o.fontMetrics = QApplication::fontMetrics();
- 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;
+ icon.paint(painter, iconArea, Qt::AlignCenter, QIcon::Normal, QIcon::On);
+
+ // draw the name
+ QStyleOptionViewItem tmp(option);
+ tmp.rect.setWidth(option.rect.width() - iconArea.width() - HIG::PAD_SMALL);
+ tmp.rect.moveRight(option.rect.right());
+ QItemDelegate::paint(painter, tmp, index);
+ }
+ else if(column == COL_PROGRESS)
+ {
+ QStyleOptionProgressBar p;
+ p.state = option.state | QStyle::State_Small;
+ p.direction = QApplication::layoutDirection();
+ p.rect = option.rect;
+ p.rect.setSize(QSize(option.rect.width()-2, option.rect.height()-8));
+ p.rect.moveCenter(option.rect.center());
+ p.fontMetrics = QApplication::fontMetrics();
+ p.minimum = 0;
+ p.maximum = 100;
+ p.textAlignment = Qt::AlignCenter;
+ p.textVisible = true;
+ p.progress = (int)(100.0*index.data().toDouble());
+ p.text = QString().sprintf("%d%%", p.progress);
+ style->drawControl(QStyle::CE_ProgressBar, &p, painter);
+ }
+ else if(column == COL_WANTED)
+ {
+ QStyleOptionButton o;
+ o.state = option.state;
+ o.direction = QApplication::layoutDirection();
+ o.rect.setSize(QSize(20, option.rect.height()));
+ o.rect.moveCenter(option.rect.center());
+ o.fontMetrics = QApplication::fontMetrics();
+ 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;
}
- style->drawControl( QStyle::CE_CheckBox, &o, painter );
+ style->drawControl(QStyle::CE_CheckBox, &o, painter);
}
- painter->restore( );
+ painter->restore();
}
/****
*****
****/
-FileTreeView :: FileTreeView( QWidget * parent ):
- QTreeView( parent ),
- myModel( this ),
- myProxy( new QSortFilterProxyModel( ) ),
- myDelegate( this )
+FileTreeView :: FileTreeView (QWidget * parent):
+ QTreeView (parent),
+ myModel (this),
+ myProxy (new QSortFilterProxyModel()),
+ myDelegate (this)
{
- setSortingEnabled( true );
- setAlternatingRowColors( true );
- setSelectionBehavior( QAbstractItemView::SelectRows );
- setSelectionMode( QAbstractItemView::ExtendedSelection );
- myProxy->setSourceModel( &myModel );
- setModel( myProxy );
- setItemDelegate( &myDelegate );
- setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
- sortByColumn( COL_NAME, Qt::AscendingOrder );
- installEventFilter( this );
+ setSortingEnabled (true);
+ setAlternatingRowColors (true);
+ setSelectionBehavior (QAbstractItemView::SelectRows);
+ setSelectionMode (QAbstractItemView::ExtendedSelection);
+ 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::Interactive );
+ for (int i=0; i<NUM_COLUMNS; ++i)
+ header()->setResizeMode(i, QHeaderView::Interactive);
- connect( this, SIGNAL(clicked(const QModelIndex&)),
- this, SLOT(onClicked(const QModelIndex&)) );
+ connect (this, SIGNAL(clicked(const QModelIndex&)),
+ this, SLOT(onClicked(const QModelIndex&)));
- connect( &myModel, SIGNAL(priorityChanged(const QSet<int>&, int)),
- this, SIGNAL(priorityChanged(const QSet<int>&, int)));
+ connect (&myModel, SIGNAL(priorityChanged(const QSet<int>&, int)),
+ this, SIGNAL(priorityChanged(const QSet<int>&, int)));
- connect( &myModel, SIGNAL(wantedChanged(const QSet<int>&, bool)),
- this, SIGNAL(wantedChanged(const QSet<int>&, bool)));
+ connect (&myModel, SIGNAL(wantedChanged(const QSet<int>&, bool)),
+ this, SIGNAL(wantedChanged(const QSet<int>&, bool)));
- connect( &myModel, SIGNAL(pathEdited(const QString&, const QString&)),
- this, SIGNAL(pathEdited(const QString&, const QString&)));
+ connect (&myModel, SIGNAL(pathEdited(const QString&, const QString&)),
+ this, SIGNAL(pathEdited(const QString&, const QString&)));
}
-FileTreeView :: ~FileTreeView( )
+FileTreeView :: ~FileTreeView ()
{
- myProxy->deleteLater();
+ myProxy->deleteLater();
}
void
-FileTreeView :: onClicked( const QModelIndex& proxyIndex )
+FileTreeView :: onClicked (const QModelIndex& proxyIndex)
{
- const QModelIndex modelIndex = myProxy->mapToSource( proxyIndex );
- myModel.clicked( modelIndex );
+ const QModelIndex modelIndex = myProxy->mapToSource(proxyIndex);
+ myModel.clicked(modelIndex);
}
bool
-FileTreeView :: eventFilter( QObject * o, QEvent * event )
-{
- if( o != this )
- return false;
-
- // this is kind of a hack to get the last three columns be the
- // right size, and to have the filename column use whatever
- // space is left over...
- if( event->type() == QEvent::Resize )
- {
- QResizeEvent * r = dynamic_cast<QResizeEvent*>(event);
- int left = r->size().width();
- const QFontMetrics fontMetrics( font( ) );
- for( int column=0; column<NUM_COLUMNS; ++column ) {
- if( column == COL_NAME )
- continue;
- if( isColumnHidden( column ) )
- continue;
- const QString header = myModel.headerData( column, Qt::Horizontal ).toString( ) + " ";
- const int width = fontMetrics.size( 0, header ).width( );
- setColumnWidth( column, width );
+FileTreeView :: eventFilter (QObject * o, QEvent * event)
+{
+ if (o != this)
+ return false;
+
+ // this is kind of a hack to get the last three columns be the
+ // right size, and to have the filename column use whatever
+ // space is left over...
+ if (event->type() == QEvent::Resize)
+ {
+ QResizeEvent * r = dynamic_cast<QResizeEvent*>(event);
+ int left = r->size().width();
+ const QFontMetrics fontMetrics(font());
+ for (int column=0; column<NUM_COLUMNS; ++column)
+ {
+ if (column == COL_NAME)
+ continue;
+ if (isColumnHidden(column))
+ continue;
+ const QString header = myModel.headerData (column, Qt::Horizontal).toString() + " ";
+ const int width = fontMetrics.size (0, header).width();
+ setColumnWidth (column, width);
left -= width;
}
- left -= 20; // not sure why this is necessary. it works in different themes + font sizes though...
- setColumnWidth( COL_NAME, std::max(left,0) );
- return false;
+ left -= 20; // not sure why this is necessary. it works in different themes + font sizes though...
+ setColumnWidth(COL_NAME, std::max(left,0));
+ return false;
}
- // handle using the keyboard to toggle the
- // wanted/unwanted state or the file priority
- else if( event->type() == QEvent::KeyPress )
+ // handle using the keyboard to toggle the
+ // wanted/unwanted state or the file priority
+ else if(event->type() == QEvent::KeyPress)
{
- switch( dynamic_cast<QKeyEvent*>(event)->key() )
+ switch(dynamic_cast<QKeyEvent*>(event)->key())
{
- case Qt::Key_Space:
- foreach( QModelIndex i, selectionModel()->selectedRows(COL_WANTED) )
- clicked( i );
- return false;
-
- case Qt::Key_Enter:
- case Qt::Key_Return:
- foreach( QModelIndex i, selectionModel()->selectedRows(COL_PRIORITY) )
- clicked( i );
- return false;
+ case Qt::Key_Space:
+ foreach (QModelIndex i, selectionModel()->selectedRows(COL_WANTED))
+ clicked (i);
+ return false;
+
+ case Qt::Key_Enter:
+ case Qt::Key_Return:
+ foreach (QModelIndex i, selectionModel()->selectedRows(COL_PRIORITY))
+ clicked (i);
+ return false;
}
}
- return false;
+ return false;
}
void
-FileTreeView :: update( const FileList& files )
+FileTreeView :: update (const FileList& files)
{
- update( files, true );
+ update(files, true);
}
void
-FileTreeView :: update( const FileList& files, bool torrentChanged )
+FileTreeView :: update (const FileList& files, bool torrentChanged)
{
- foreach( const TrFile file, files ) {
- QList<QModelIndex> added;
- myModel.addFile( file.index, file.filename, file.wanted, file.priority, file.size, file.have, added, torrentChanged );
- foreach( QModelIndex i, added )
- expand( myProxy->mapFromSource( i ) );
+ foreach (const TrFile file, files)
+ {
+ QList<QModelIndex> added;
+ myModel.addFile (file.index, file.filename, file.wanted, file.priority, file.size, file.have, added, torrentChanged);
+ foreach (QModelIndex i, added)
+ expand(myProxy->mapFromSource(i));
}
}
void
-FileTreeView :: clear( )
+FileTreeView :: clear ()
{
- myModel.clear( );
+ myModel.clear();
}
class FileTreeItem: public QObject
{
- Q_OBJECT;
-
- enum { LOW=(1<<0), NORMAL=(1<<1), HIGH=(1<<2) };
-
- public:
- virtual ~FileTreeItem( );
- FileTreeItem( int fileIndex, const QString& name="" ):
- myIndex(fileIndex), myParent(0), myName(name),
- myPriority(0), myIsWanted(0),
- myHaveSize(0), myTotalSize(0),
- myFirstUnhashedRow(0) { }
-
- public:
- void appendChild( FileTreeItem *child );
- FileTreeItem * child( const QString& filename );
- FileTreeItem * child( int row ) { return myChildren.at( row ); }
- int childCount( ) const { return myChildren.size( ); }
- FileTreeItem * parent( ) { return myParent; }
- const FileTreeItem * parent( ) const { return myParent; }
- int row( ) const;
- const QString& name( ) const { return myName; }
- QVariant data( int column, int role ) const;
- bool update( int index, bool want, int priority, uint64_t total, uint64_t have, bool torrentChanged );
- void twiddleWanted( QSet<int>& fileIds, bool& );
- void twiddlePriority( QSet<int>& fileIds, int& );
-
- private:
- void setSubtreePriority( int priority, QSet<int>& fileIds );
- void setSubtreeWanted( bool, QSet<int>& fileIds );
- QString priorityString( ) const;
- void getSubtreeSize( uint64_t& have, uint64_t& total ) const;
- QString fileSizeName( ) const;
- double progress( ) const;
- int priority( ) const;
- int isSubtreeWanted( ) const;
-
- int myIndex;
- FileTreeItem * myParent;
- QList<FileTreeItem*> myChildren;
- QHash<QString,int> myChildRows;
- QHash<QString,int>& getMyChildRows();
- const QString myName;
- int myPriority;
- bool myIsWanted;
- uint64_t myHaveSize;
- uint64_t myTotalSize;
- size_t myFirstUnhashedRow;
+ Q_OBJECT;
+
+ enum { LOW=(1<<0), NORMAL=(1<<1), HIGH=(1<<2) };
+
+ public:
+
+ virtual ~FileTreeItem();
+
+ FileTreeItem (int fileIndex, const QString& name=""):
+ myIndex (fileIndex),
+ myParent (0),
+ myName (name),
+ myPriority (0),
+ myIsWanted (0),
+ myHaveSize (0),
+ myTotalSize (0),
+ myFirstUnhashedRow (0) { }
+
+ public:
+ void appendChild (FileTreeItem *child);
+ FileTreeItem * child (const QString& filename);
+ FileTreeItem * child (int row) { return myChildren.at(row); }
+ int childCount () const { return myChildren.size(); }
+ FileTreeItem * parent () { return myParent; }
+ const FileTreeItem * parent () const { return myParent; }
+ int row () const;
+ const QString& name () const { return myName; }
+ QVariant data (int column, int role) const;
+ bool update (int index, bool want, int priority, uint64_t total, uint64_t have, bool torrentChanged);
+ void twiddleWanted (QSet<int>& fileIds, bool&);
+ void twiddlePriority (QSet<int>& fileIds, int&);
+
+ private:
+ void setSubtreePriority (int priority, QSet<int>& fileIds);
+ void setSubtreeWanted (bool, QSet<int>& fileIds);
+ QString priorityString () const;
+ void getSubtreeSize (uint64_t& have, uint64_t& total) const;
+ QString fileSizeName () const;
+ double progress () const;
+ int priority () const;
+ int isSubtreeWanted () const;
+
+ int myIndex;
+ FileTreeItem * myParent;
+ QList<FileTreeItem*> myChildren;
+ QHash<QString,int> myChildRows;
+ QHash<QString,int>& getMyChildRows();
+ const QString myName;
+ int myPriority;
+ bool myIsWanted;
+ uint64_t myHaveSize;
+ uint64_t myTotalSize;
+ size_t myFirstUnhashedRow;
};
class FileTreeModel: public QAbstractItemModel
{
- Q_OBJECT
-
- public:
- FileTreeModel( QObject *parent = 0);
- ~FileTreeModel( );
-
- public:
- QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
- Qt::ItemFlags flags( const QModelIndex& index ) const;
- QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
- QModelIndex index( int row, int column, const QModelIndex& parent = QModelIndex() ) const;
- QModelIndex parent( const QModelIndex& child ) const;
- QModelIndex parent( const QModelIndex& child, int column ) const;
- int rowCount( const QModelIndex& parent = QModelIndex( ) ) const;
- int columnCount( const QModelIndex &parent = QModelIndex( ) ) const;
- virtual bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
-
- signals:
- void priorityChanged( const QSet<int>& fileIndices, int );
- void wantedChanged( const QSet<int>& fileIndices, bool );
- void pathEdited (const QString& oldpath, const QString& newname);
-
- public:
- void clear( );
- void addFile( int index, const QString& filename,
- bool wanted, int priority,
- uint64_t size, uint64_t have,
- QList<QModelIndex>& rowsAdded,
- bool torrentChanged );
-
- private:
- void clearSubtree( const QModelIndex & );
- QModelIndex indexOf( FileTreeItem *, int column ) const;
- void parentsChanged( const QModelIndex &, int column );
- void subtreeChanged( const QModelIndex &, int column );
-
- private:
- FileTreeItem * rootItem;
-
- public slots:
- void clicked ( const QModelIndex & index );
+ Q_OBJECT
+
+ public:
+ FileTreeModel (QObject *parent = 0);
+ ~FileTreeModel ();
+
+ public:
+ QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const;
+ Qt::ItemFlags flags (const QModelIndex& index) const;
+ QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
+ QModelIndex index (int row, int column, const QModelIndex& parent = QModelIndex()) const;
+ QModelIndex parent (const QModelIndex& child) const;
+ QModelIndex parent (const QModelIndex& child, int column) const;
+ int rowCount (const QModelIndex& parent = QModelIndex()) const;
+ int columnCount (const QModelIndex &parent = QModelIndex()) const;
+ virtual bool setData (const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
+
+ signals:
+ void priorityChanged (const QSet<int>& fileIndices, int);
+ void wantedChanged (const QSet<int>& fileIndices, bool);
+ void pathEdited (const QString& oldpath, const QString& newname);
+
+ public:
+ void clear ();
+ void addFile (int index, const QString& filename,
+ bool wanted, int priority,
+ uint64_t size, uint64_t have,
+ QList<QModelIndex>& rowsAdded,
+ bool torrentChanged);
+
+ private:
+ void clearSubtree (const QModelIndex &);
+ QModelIndex indexOf (FileTreeItem *, int column) const;
+ void parentsChanged (const QModelIndex &, int column);
+ void subtreeChanged (const QModelIndex &, int column);
+
+ private:
+ FileTreeItem * rootItem;
+
+ public slots:
+ void clicked (const QModelIndex & index);
};
class FileTreeDelegate: public QItemDelegate
{
- Q_OBJECT
+ Q_OBJECT
- public:
- FileTreeDelegate( QObject * parent=0 ): QItemDelegate( parent ) { }
- virtual ~FileTreeDelegate( ) { }
+ public:
+ FileTreeDelegate (QObject * parent=0): QItemDelegate(parent) { }
+ virtual ~FileTreeDelegate() { }
- public:
- virtual QSize sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const;
- virtual void paint(QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const;
+ public:
+ virtual QSize sizeHint (const QStyleOptionViewItem&, const QModelIndex&) const;
+ virtual void paint (QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const;
};
class FileTreeView: public QTreeView
{
- Q_OBJECT
-
- public:
- FileTreeView( QWidget * parent=0 );
- virtual ~FileTreeView( );
- void clear( );
- void update( const FileList& files );
- void update( const FileList& files, bool torrentChanged );
-
- signals:
- void priorityChanged( const QSet<int>& fileIndices, int );
- void wantedChanged( const QSet<int>& fileIndices, bool );
- void pathEdited (const QString& oldpath, const QString& newname);
-
- protected:
- bool eventFilter( QObject *, QEvent * );
-
- private:
- FileTreeModel myModel;
- QSortFilterProxyModel * myProxy;
- FileTreeDelegate myDelegate;
-
- public slots:
- void onClicked ( const QModelIndex & index );
+ Q_OBJECT
+
+ public:
+ FileTreeView (QWidget * parent=0);
+ virtual ~FileTreeView ();
+ void clear ();
+ void update (const FileList& files);
+ void update (const FileList& files, bool torrentChanged);
+
+ signals:
+ void priorityChanged (const QSet<int>& fileIndices, int);
+ void wantedChanged (const QSet<int>& fileIndices, bool);
+ void pathEdited (const QString& oldpath, const QString& newname);
+
+ protected:
+ bool eventFilter (QObject *, QEvent *);
+
+ private:
+ FileTreeModel myModel;
+ QSortFilterProxyModel * myProxy;
+ FileTreeDelegate myDelegate;
+
+ public slots:
+ void onClicked (const QModelIndex & index);
};
#endif