QList< File* > BaseModel::cleanSelection( QModelIndexList &selection ) const
{
/* We want to filter the index list here, since we cannot allow a selection to contain both
root items and children of those root items. Here we traverse the list, filtering out any
children who's parents are also in the list */
QList< File* > files;
while( it.hasNext() ){
BaseType *base = static_cast< BaseType* >( idx.internalPointer() );
if( base->type() == "File" ){
File *file = dynamic_cast< File* >( base );
if( file ){
//If the current files parent is also in the list, then we don't want to process it
if( selection.indexOf( index( downloadqueue->indexOf( file->parent() ), 0 ) ) != -1 ){
files.append( file );
it.remove();
}
}
}
}
return files;
}
QList< File* > BaseModel::cleanSelection( QModelIndexList &selection ) const
{
/* We want to filter the index list here, since we cannot allow a selection to contain both
root items and children of those root items. Here we traverse the list, filtering out any
children who's parents are also in the list */
QMutableListIterator< QModelIndex > it( selection );
QList< File* > files;
while( it.hasNext() ){
QModelIndex idx = it.next();
BaseType *base = static_cast< BaseType* >( idx.internalPointer() );
if( base->type() == "File" ){
File *file = dynamic_cast< File* >( base );
if( file ){
//If the current files parent is also in the list, then we don't want to process it
if( selection.indexOf( index( downloadqueue->indexOf( file->parent() ), 0 ) ) != -1 ){
files.append( file );
it.remove();
}
}
}
}
return files;
}
To copy to clipboard, switch view to plain text mode
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();
}
void KNewzModel::moveToTop()
{
QModelIndexList selection = view->selectionModel()->selectedRows();
if( selection.size() < 1 )
return;
QList< File* > files = cleanSelection( selection );
QMutexLocker lock( &downloadqueue->mutex() );
QModelIndex rootIdx = index( 0, 0 );
QModelIndex childIdx = index( 0, 0, rootIdx );
foreach( const QModelIndex &idx, 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() );
QModelIndex idx = index( row, 0 );
bool expanded = view->isExpanded( idx );
beginRemoveRows( QModelIndex(), row, row );
nzbFile = downloadqueue->takeAt( row );
int rows = nzbFile->size();
endRemoveRows();
row = rootIdx.row();
beginInsertRows( QModelIndex(), row, row );
downloadqueue->insert( row, nzbFile );
endInsertRows();
QModelIndex newIdx = index( row, 0 );
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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks