/*!
  \reimp
*/
void QStringListModel::sort(int, Qt::SortOrder order)
{
    emit layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint);

    QVector<QPair<QString, int> > list;
    const int lstCount = lst.count();
    list.reserve(lstCount);
    for (int i = 0; i < lstCount; ++i)
        list.append(QPair<QString, int>(lst.at(i), i));

    if (order == Qt::AscendingOrder)
        std::sort(list.begin(), list.end(), ascendingLessThan);
    else
        std::sort(list.begin(), list.end(), decendingLessThan);

    lst.clear();
    QVector<int> forwarding(lstCount);
    for (int i = 0; i < lstCount; ++i) {
        lst.append(list.at(i).first);
        forwarding[list.at(i).second] = i;
    }

    QModelIndexList oldList = persistentIndexList();
    QModelIndexList newList;
    const int numOldIndexes = oldList.count();
    newList.reserve(numOldIndexes);
    for (int i = 0; i < numOldIndexes; ++i)
        newList.append(index(forwarding.at(oldList.at(i).row()), 0));
    changePersistentIndexList(oldList, newList);

    emit layoutChanged(QList<QPersistentModelIndex>(), VerticalSortHint);
}
Ejemplo n.º 2
0
/*!
  \reimp
*/
void QStringListModel::sort(int, Qt::SortOrder order)
{
    emit layoutAboutToBeChanged();

    QList<QPair<QString, int> > list;
    for (int i = 0; i < lst.count(); ++i)
        list.append(QPair<QString, int>(lst.at(i), i));

    if (order == Qt::AscendingOrder)
        qSort(list.begin(), list.end(), ascendingLessThan);
    else
        qSort(list.begin(), list.end(), decendingLessThan);

    lst.clear();
    QVector<int> forwarding(list.count());
    for (int i = 0; i < list.count(); ++i) {
        lst.append(list.at(i).first);
        forwarding[list.at(i).second] = i;
    }

    QModelIndexList oldList = persistentIndexList();
    QModelIndexList newList;
    for (int i = 0; i < oldList.count(); ++i)
        newList.append(index(forwarding.at(oldList.at(i).row()), 0));
    changePersistentIndexList(oldList, newList);

    emit layoutChanged();
}
Ejemplo n.º 3
0
void DeckListModel::sortHelper(InnerDecklistNode *node, Qt::SortOrder order)
{
    // Sort children of node and save the information needed to
    // update the list of persistent indexes.
    QVector<QPair<int, int> > sortResult = node->sort(order);
    
    QModelIndexList from, to;
    int columns = columnCount();
    for (int i = sortResult.size() - 1; i >= 0; --i) {
        const int fromRow = sortResult[i].first;
        const int toRow = sortResult[i].second;
        AbstractDecklistNode *temp = node->at(toRow);
        for (int j = 0; j < columns; ++j) {
            from << createIndex(fromRow, j, temp);
            to << createIndex(toRow, j, temp);
        }
    }
    changePersistentIndexList(from, to);

    // Recursion
    for (int i = node->size() - 1; i >= 0; --i) {
        InnerDecklistNode *subNode = dynamic_cast<InnerDecklistNode *>(node->at(i));
        if (subNode)
            sortHelper(subNode, order);
    }
}
Ejemplo n.º 4
0
void DistrictModel::removeDistricts( const QModelIndexList& _indexes )
{
	if ( _indexes.isEmpty() ) {
		return;
	}
	
	QModelIndexList indexes = _indexes;
	
	qSort( indexes );
	
	emit layoutAboutToBeChanged();
	
	const QModelIndexList oldIndexes = persistentIndexList();
	QModelIndexList newIndexes;
	
	for ( int i = 0; i < oldIndexes.count(); i++ ) {
		newIndexes << QModelIndex();
	}
	
	for ( int i = indexes.count() -1; i >= 0; i-- ) {
		const QModelIndex& index = indexes[ i ];
		
		d->districts.removeAt( index.row() );
	}
	
	changePersistentIndexList( oldIndexes, newIndexes );
	
	emit layoutChanged();
}
Ejemplo n.º 5
0
/*!
    Sorts the model using the given \a method and \a order.

    \sa lessThan()
 */
void IrcUserModel::sort(Irc::SortMethod method, Qt::SortOrder order)
{
    Q_D(IrcUserModel);
    if (method == Irc::SortByHand)
        return;

    emit layoutAboutToBeChanged();

    QList<IrcUser*> persistentUsers;
    QModelIndexList oldPersistentIndexes = persistentIndexList();
    foreach (const QModelIndex& index, oldPersistentIndexes)
        persistentUsers += static_cast<IrcUser*>(index.internalPointer());

    if (order == Qt::AscendingOrder)
        qSort(d->userList.begin(), d->userList.end(), IrcUserLessThan(this, method));
    else
        qSort(d->userList.begin(), d->userList.end(), IrcUserGreaterThan(this, method));

    if (d->updateTitles())
        emit titlesChanged(d->titles);

    QModelIndexList newPersistentIndexes;
    foreach (IrcUser* user, persistentUsers)
        newPersistentIndexes += index(d->userList.indexOf(user));
    changePersistentIndexList(oldPersistentIndexes, newPersistentIndexes);

    emit layoutChanged();
}
Ejemplo n.º 6
0
void ListModel::sort(ListItem* parent, App::SortMode mode)
{
    int count = parent->childCount();

    QVector<QPair<ListItem*, int>> sorting(count);
    for (int i = 0; i < count; ++i) {
        sorting[i].first = parent->child(i);
        sorting[i].second = i;
    }

    if (!parent->sort(mode))
        return;

    QModelIndexList oldIndexes;
    QModelIndexList newIndexes;
    for (QModelIndex idx : persistentIndexList()) {
        ListItem* item = itemFromIndex(idx);
        if (!item)
            continue;
        if (idx.row() != item->row()) {
            oldIndexes.append(idx);
            newIndexes.append(indexFromItem(item));
        }
    }

    qDebug() << __FUNCTION__ << oldIndexes;
    qDebug() << __FUNCTION__ << newIndexes;

    changePersistentIndexList(oldIndexes, newIndexes);
    emit layoutChanged();
}
void FormGenBagModel::setCompareOperator(const Compare &comparison)
{
    emit layoutAboutToBeChanged(QList<QPersistentModelIndex>(), QAbstractItemModel::VerticalSortHint);

    QHash<int, int> persistentRows;
    {
        auto tmp = persistentIndexList();
        for( const auto &idx: tmp )
            persistentRows.insert(idx.row(), -1);
    }

    mItems.setCompareOperatorGetReorderMap(comparison, &persistentRows);

    {
        QModelIndexList oldList, newList;
        oldList.reserve(persistentRows.size());
        newList.reserve(persistentRows.size());
        for( auto it = persistentRows.cbegin(); it != persistentRows.cend(); ++it ) {
            //changePersistentIndex(index(it.key()), index(it.value()));
            oldList.append(index(it.key()));
            newList.append(index(it.value()));
        }
        changePersistentIndexList(oldList, newList);
    }

    emit layoutChanged(QList<QPersistentModelIndex>(), QAbstractItemModel::VerticalSortHint);
}
Ejemplo n.º 8
0
void FeedbackModel::sort( int column, Qt::SortOrder order )
{
	if ( rowCount() == 0 ) {
		return;
	}
	
	emit layoutAboutToBeChanged();
	
	const QModelIndexList oldIndexes = persistentIndexList();
	QModelIndexList newIndexes;
	
	QMap<int, Feedback> oldMapping;
	QMap<Feedback, int> newMapping;
	
	for ( int i = 0; i < d->feedbacks.count(); i++ ) {
		oldMapping[ i ] = d->feedbacks[ i ];
	}
	
	switch ( order ) {
		case Qt::AscendingOrder: {
			if ( column == 0 ) {
				qSort( d->feedbacks.begin(), d->feedbacks.end() );
			}
			else if ( column == 1 ) {
				qSort( d->feedbacks.begin(), d->feedbacks.end(), FeedbackModelPrivate::lessThanMessage );
			}
			
			break;
		}
		
		case Qt::DescendingOrder: {
			if ( column == 0 ) {
				qSort( d->feedbacks.begin(), d->feedbacks.end(), qGreater<Feedback>() );
			}
			else if ( column == 1 ) {
				qSort( d->feedbacks.begin(), d->feedbacks.end(), FeedbackModelPrivate::greaterThanMessage );
			}
			
			break;
		}
	}
	
	for ( int i = 0; i < d->feedbacks.count(); i++ ) {
		newMapping[ d->feedbacks[ i ] ] = i;
	}
	
	for ( int i = 0; i < oldIndexes.count(); i++ ) {
		const QModelIndex& index = oldIndexes[ i ];
		const Feedback& feedback = oldMapping[ index.row() ];
		newIndexes << QAbstractTableModel::index( newMapping[ feedback ], index.column() );
	}
	
	changePersistentIndexList( oldIndexes, newIndexes );
	
	emit layoutChanged();
}
Ejemplo n.º 9
0
void QCompletionModel::resetModel()
{
    if (rowCount() == 0) {
        reset();
        return;
    }

    emit layoutAboutToBeChanged();
    QModelIndexList piList = persistentIndexList();
    QModelIndexList empty;
    for (int i = 0; i < piList.size(); i++)
        empty.append(QModelIndex());
    changePersistentIndexList(piList, empty);
    emit layoutChanged();
}
Ejemplo n.º 10
0
void DistrictModel::sort( int column, Qt::SortOrder order )
{
	Q_UNUSED( column );
	
	if ( rowCount() == 0 ) {
		return;
	}
	
	emit layoutAboutToBeChanged();
	
	const QModelIndexList oldIndexes = persistentIndexList();
	QModelIndexList newIndexes;
	
	QMap<int, District> oldMapping;
	QMap<District, int> newMapping;
	
	for ( int i = 0; i < d->districts.count(); i++ ) {
		oldMapping[ i ] = d->districts[ i ];
	}
	
	switch ( order ) {
		case Qt::AscendingOrder: {
			qSort( d->districts.begin(), d->districts.end() );
			break;
		}
		
		case Qt::DescendingOrder: {
			qSort( d->districts.begin(), d->districts.end(), qGreater<District>() );
			break;
		}
	}
	
	for ( int i = 0; i < d->districts.count(); i++ ) {
		newMapping[ d->districts[ i ] ] = i;
	}
	
	for ( int i = 0; i < oldIndexes.count(); i++ ) {
		const QModelIndex& index = oldIndexes[ i ];
		const District& district = oldMapping[ index.row() ];
		newIndexes << QAbstractTableModel::index( newMapping[ district ], index.column() );
	}
	
	changePersistentIndexList( oldIndexes, newIndexes );
	
	emit layoutChanged();
}
Ejemplo n.º 11
0
void pOpenedFileModel::rebuildMapping( const QList<pAbstractChild*>& oldList, const QList<pAbstractChild*>& newList )
{
    emit layoutAboutToBeChanged();
    const QModelIndexList pOldIndexes = persistentIndexList();
    QModelIndexList pIndexes;
    QMap<int, pAbstractChild*> documentsMapping;
    QMap<int, int> mapping;

    // build old mapping
    for ( int i = 0; i < pOldIndexes.count(); i++ )
    {
        const QModelIndex& index = pOldIndexes.at( i );
        const int row = index.row();
        documentsMapping[ row ] = oldList.at( row );
        mapping[ row ] = row;
    }

    mDocuments = newList;

    // build new mapping
    for ( int i = 0; i < pOldIndexes.count(); i++ )
    {
        const QModelIndex& pIndex = pOldIndexes.at( i );
        const int row = pIndex.row();
        pAbstractChild* document = documentsMapping[ row ];
        const int index = mDocuments.indexOf( document );
        mapping[ row ] = index;
    }

    for ( int i = 0; i < pOldIndexes.count(); i++ )
    {
        const QModelIndex& pIndex = pOldIndexes.at( i );
        const int row = pIndex.row();
        const int index = mapping[ row ];

        if ( pOldIndexes.at( i ).isValid() )
            pIndexes << createIndex( index, pIndex.column(), mDocuments.at( index ) );
        else
            pIndexes << QModelIndex();
    }

    changePersistentIndexList( pOldIndexes, pIndexes );
    emit layoutChanged();
}
Ejemplo n.º 12
0
void MessageModel::replaceMeWithSubtree(const QModelIndex &parent, MessagePart *partToReplace, MessagePart::Ptr tree)
{
    Q_ASSERT(!parent.isValid() || parent.model() == this);
    auto *part = translatePtr(parent);
    Q_ASSERT(part);

#ifdef MIME_TREE_DEBUG
    qDebug() << "Whole tree:\n" << *m_rootPart;
    qDebug() << "Replacing:\n" << (void*)(partToReplace);
    qDebug() << "New items:\n" << *(tree.get());
#endif

    Q_ASSERT(partToReplace);
    Q_ASSERT(tree->row() == partToReplace->row());

    auto it = part->m_children.find(partToReplace->row());
    Q_ASSERT(it != part->m_children.end());
    Q_ASSERT(it->second->row() == partToReplace->row());
    Q_ASSERT(partToReplace->row() == tree->row());

    emit layoutAboutToBeChanged(QList<QPersistentModelIndex>() << parent);

    auto oldIndexes = persistentIndexList();
    auto newIndexes = oldIndexes;
    for (int i = 0; i < oldIndexes.size(); ++i) {
        const auto &index = oldIndexes[i];
        if (index.parent() == parent && index.column() == 0 && index.row() == tree->row()) {
            newIndexes[i] = createIndex(tree->row(), 0, tree.get());
        }
    }
    changePersistentIndexList(oldIndexes, newIndexes);

    tree->m_parent = part;
    MessagePart::Ptr partGoingAway = std::move(it->second);
    it->second = std::move(tree);
    emit layoutChanged(QList<QPersistentModelIndex>() << parent);

    emit dataChanged(parent, parent);

#ifdef MIME_TREE_DEBUG
    qDebug() << "After replacement:\n" << *m_rootPart;
#endif
}
Ejemplo n.º 13
0
void KrVfsModel::sort(int column, Qt::SortOrder order)
{
    _view->sortModeUpdated(column, order);

    if(lastSortOrder() == KrViewProperties::NoColumn)
        return;

    emit layoutAboutToBeChanged();

    QModelIndexList oldPersistentList = persistentIndexList();

    KrSort::Sorter sorter(createSorter());
    sorter.sort();

    _vfiles.clear();
    _vfileNdx.clear();
    _nameNdx.clear();

    bool sortOrderChanged = false;
    QHash<int, int> changeMap;
    for (int i = 0; i < sorter.items().count(); ++i) {
        const KrSort::SortProps *props = sorter.items()[i];
        _vfiles.append(props->vf());
        changeMap[ props->originalIndex() ] = i;
        if (i != props->originalIndex())
            sortOrderChanged = true;
        _vfileNdx[ props->vf()] = index(i, 0);
        _nameNdx[ props->vf()->vfile_getName()] = index(i, 0);
    }

    QModelIndexList newPersistentList;
    foreach(const QModelIndex &mndx, oldPersistentList)
        newPersistentList << index(changeMap[ mndx.row()], mndx.column());

    changePersistentIndexList(oldPersistentList, newPersistentList);

    emit layoutChanged();
    if (sortOrderChanged)
        _view->makeItemVisible(_view->getCurrentKrViewItem());
}
Ejemplo n.º 14
0
void KrVfsModel::clear()
{
    if(!_vfiles.count())
        return;
    emit layoutAboutToBeChanged();
    // clear persistent indexes
    QModelIndexList oldPersistentList = persistentIndexList();
    QModelIndexList newPersistentList;
#if QT_VERSION >= 0x040700
    newPersistentList.reserve(oldPersistentList.size());
#endif
    for (int i=0; i< oldPersistentList.size(); ++i)
        newPersistentList.append(QModelIndex());
    changePersistentIndexList(oldPersistentList, newPersistentList);

    _vfiles.clear();
    _vfileNdx.clear();
    _nameNdx.clear();
    _dummyVfile = 0;

    emit layoutChanged();
}
Ejemplo n.º 15
0
/*
  Return[0]: totalFile
  Return[1]: totalSize
  Return[2]: currentFile
  */
QList<quint64> TransferModel::synchronizeItems(const QList<Ultracopier::ReturnActionOnCopyList>& returnActions)
{
    loop_size=returnActions.size();
    index_for_loop=0;
    quint64 totalFile=0,totalSize=0,currentFile=0;
    totalFile=0;
    totalSize=0;
    currentFile=0;

    emit layoutAboutToBeChanged();
    const QModelIndexList oldIndexes = persistentIndexList();
    QModelIndexList newIndexes = oldIndexes;
    QMap<int, quint64> oldMapping;  // model index row in model before update, item id
    QMap<quint64, int> newMapping;  // item id, model index row in model after update

    for ( int i = 0; i < oldIndexes.count(); i++ ) {
        const QModelIndex& index = oldIndexes[ i ];
        oldMapping[ index.row() ] = index.data( Qt::UserRole ).value<quint64>();
    }

    while(index_for_loop<loop_size)
    {
        const Ultracopier::ReturnActionOnCopyList& action=returnActions.at(index_for_loop);
        switch(action.type)
        {
            case Ultracopier::AddingItem:
            {
                TransfertItem newItem;
                newItem.id=action.addAction.id;
                newItem.source=action.addAction.sourceFullPath;
                newItem.size=facilityEngine->sizeToString(action.addAction.size);
                newItem.destination=action.addAction.destinationFullPath;
                transfertItemList<<newItem;
                totalFile++;
                totalSize+=action.addAction.size;
            }
            break;
            case Ultracopier::MoveItem:
            {
                //bool current_entry=
                if(action.userAction.position<0)
                {
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QString("id: %1, position is wrong: %2").arg(action.addAction.id).arg(action.userAction.position));
                    break;
                }
                if(action.userAction.position>(transfertItemList.size()-1))
                {
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QString("id: %1, position is wrong: %2").arg(action.addAction.id).arg(action.userAction.position));
                    break;
                }
                if(action.userAction.moveAt<0)
                {
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QString("id: %1, position is wrong: %2").arg(action.addAction.id).arg(action.userAction.position));
                    break;
                }
                if(action.userAction.moveAt>(transfertItemList.size()-1))
                {
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QString("id: %1, position is wrong: %2").arg(action.addAction.id).arg(action.userAction.position));
                    break;
                }
                if(action.userAction.position==action.userAction.moveAt)
                {
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QString("id: %1, move at same position: %2").arg(action.addAction.id).arg(action.userAction.position));
                    break;
                }
                transfertItemList.move(action.userAction.position,action.userAction.moveAt);
                //newIndexes.move(action.userAction.position,action.userAction.moveAt);
            }
            break;
            case Ultracopier::RemoveItem:
            {
                if(currentIndexSearch>0 && action.userAction.position<=currentIndexSearch)
                    currentIndexSearch--;
                if(action.userAction.position<0)
                {
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QString("id: %1, position is wrong: %3").arg(action.addAction.id).arg(action.userAction.position));
                    break;
                }
                if(action.userAction.position>(transfertItemList.size()-1))
                {
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QString("id: %1, position is wrong: %3").arg(action.addAction.id).arg(action.userAction.position));
                    break;
                }
                transfertItemList.removeAt(action.userAction.position);
                currentFile++;
                startId.remove(action.addAction.id);
                stopId.remove(action.addAction.id);
                internalRunningOperation.remove(action.addAction.id);
                //newIndexes.remove(action.userAction.moveAt);
            }
            break;
            case Ultracopier::PreOperation:
            {
                ItemOfCopyListWithMoreInformations tempItem;
                tempItem.currentReadProgression=0;
                tempItem.currentWriteProgression=0;
                tempItem.generalData=action.addAction;
                tempItem.actionType=action.type;
                internalRunningOperation[action.addAction.id]=tempItem;
            }
            break;
            case Ultracopier::Transfer:
            {
                if(!startId.contains(action.addAction.id))
                    startId << action.addAction.id;
                stopId.remove(action.addAction.id);
                if(internalRunningOperation.contains(action.addAction.id))
                    internalRunningOperation[action.addAction.id].actionType=action.type;
                else
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QString("unable to found entry for file %1: actionType: Transfer").arg(action.addAction.id));
            }
            break;
            case Ultracopier::PostOperation:
            {
                if(!stopId.contains(action.addAction.id))
                    stopId << action.addAction.id;
                startId.remove(action.addAction.id);
            }
            break;
            case Ultracopier::CustomOperation:
            {
                bool custom_with_progression=(action.addAction.size==1);
                //without progression
                if(custom_with_progression)
                {
                    if(startId.remove(action.addAction.id))
                        if(!stopId.contains(action.addAction.id))
                            stopId << action.addAction.id;
                }
                //with progression
                else
                {
                    stopId.remove(action.addAction.id);
                    if(!startId.contains(action.addAction.id))
                        startId << action.addAction.id;
                }
                if(internalRunningOperation.contains(action.addAction.id))
                {
                    ItemOfCopyListWithMoreInformations &item=internalRunningOperation[action.addAction.id];
                    item.actionType=action.type;
                    item.custom_with_progression=custom_with_progression;
                    item.currentReadProgression=0;
                    item.currentWriteProgression=0;
                }
            }
            break;
            default:
                //unknow code, ignore it
            break;
        }
        index_for_loop++;
    }

    if(!oldIndexes.isEmpty())
    {
        const QSet<quint64> ids = oldMapping.values().toSet();

        for ( int i = 0; i < transfertItemList.count(); i++ ) {
            const TransferModel::TransfertItem& item = transfertItemList[ i ];

            if ( ids.contains( item.id ) ) {
                newMapping[ item.id ] = i;
            }
        }

        for ( int i = 0; i < oldIndexes.count(); i++ ) {
            const QModelIndex& index = oldIndexes[ i ];
            const int newRow = newMapping.value( oldMapping[ index.row() ], -1 );
            newIndexes[ i ] = newRow == -1 ? QModelIndex() : QAbstractTableModel::index( newRow, index.column(), index.parent() );
        }
    }

    changePersistentIndexList( oldIndexes, newIndexes );
    emit layoutChanged();
    return QList<quint64>() << totalFile << totalSize << currentFile;
}