void KNewzModel::moveToTop()
{
QModelIndexList selection = view->selectionModel()->selectedRows();
if( selection.size() < 1 )
return;
QList< File* > files = cleanSelection( selection );
BaseType *base = static_cast< BaseType* >( idx.internalPointer() );
if( base->type() == "NzbFile" ){
NzbFile *nzbFile = dynamic_cast< NzbFile* >( base );
if( !nzbFile )
break;
//No need to move the first item to the top, it's there already
if( nzbFile == downloadqueue->first() )
continue;
int row = downloadqueue->indexOf( nzbFile );
Q_ASSERT( row >= 0 && row < downloadqueue->size() );
bool expanded = view->isExpanded( idx );
nzbFile = downloadqueue->takeAt( row );
int rows = nzbFile->size();
endRemoveRows();
row = rootIdx.row();
downloadqueue->insert( row, nzbFile );
endInsertRows();
emit dataChanged( newIdx, index( rows, columnCount(), newIdx ) );
if( files.size() > 0 ){
File *firstChild = nzbFile->first();
/* Check if any children were selected. We can't just ignore them now,
because this action can legally operate on both children and their top-level
parent simultaneously */
QMutableListIterator< File* > it( files );
while( it.hasNext() ){
File *file = it.next();
if( file->parent() == nzbFile ){
it.remove();
int row = nzbFile->indexOf( file );
if( row >= 0 && row < nzbFile->size() ){
beginRemoveRows( newIdx, row, row );
file = nzbFile->takeAt( row );
endRemoveRows();
row = nzbFile->indexOf( firstChild );
if( row < 0 )
row = 0;
beginInsertRows( newIdx, row, row );
nzbFile->insert( row, file );
endInsertRows();
emit dataChanged( index( row, 0, newIdx ), index( row, columnCount(), newIdx ) );
}
}
}
}
if( expanded )
view->setExpanded( newIdx, true );
}else{
File *file = dynamic_cast< File* >( base );
NzbFile *nzbFile = file->parent();
int row = nzbFile->indexOf( file );
/* We want row > 0 here because we don't need to move the first child
to the top since it's already there */
if( row > 0 && row < nzbFile->size() ){
QModelIndex parentIdx
= index
( downloadqueue
->indexOf
( nzbFile
),
0 );
Q_ASSERT( parentIdx.isValid() );
beginRemoveRows( parentIdx, row, row );
file = nzbFile->takeAt( row );
endRemoveRows();
beginInsertRows( parentIdx, 0, 0 );
nzbFile->prepend( file );
endInsertRows();
emit dataChanged( index( 0, 0, parentIdx ), index( 0, columnCount(), parentIdx ) );
}
}
}
view->selectionModel()->clearSelection();
}