コード例 #1
0
ファイル: MojDbSearchCursor.cpp プロジェクト: pombredanne/db8
MojErr MojDbSearchCursor::loadIds(ObjectSet& idsOut)
{
    LOG_TRACE("Entering function %s", __FUNCTION__);

    MojUInt32 groupNum = 0;
    bool found = false;
    MojSharedPtr<ObjectSet> group;
    GroupMap groupMap;

    for(;;) {
        // get current id
        MojObject id;
        MojUInt32 idGroupNum = 0;
        MojErr err = m_storageQuery->getId(id, idGroupNum, found);
        MojErrCheck(err);
        if (!found)
            break;

        // if it is in a new group, create a new set
        if (!group.get() || idGroupNum != groupNum) {
            // find/create new group
            GroupMap::Iterator iter;
            err = groupMap.find(idGroupNum, iter);
            MojErrCheck(err);
            if (iter != groupMap.end()) {
                group = iter.value();
            } else {
                err = group.resetChecked(new ObjectSet);
                MojErrCheck(err);
                err = groupMap.put(idGroupNum, group);
                MojErrCheck(err);
            }
            groupNum = idGroupNum;
        }
        // add id to current set
        err = group->put(id);
        MojErrCheck(err);
    }

    // no matches unless all groups are accounted for
    MojUInt32 groupCount = m_storageQuery->groupCount();
    for (MojUInt32 i = 0; i < groupCount; ++i) {
        if (!groupMap.contains(i))
            return MojErrNone;
    }

    // find intersection of all groups
    GroupMap::ConstIterator begin = groupMap.begin();
    for (GroupMap::ConstIterator i = begin; i != groupMap.end(); ++i) {
        if (i == begin) {
            // special handling for first group
            idsOut = *(i.value());
        } else {
            MojErr err = idsOut.intersect(*(i.value()));
            MojErrCheck(err);
        }
    }
    return MojErrNone;
}
コード例 #2
0
void ModelGrouper::group( GroupMap & grouped )
{
	QList<QPersistentModelIndex> persistentGroupIndexes;
	
	// If we are already grouped, we need to insert items into existing groups before creating new ones
	if( mIsGrouped ) {
		// Get persistent indexes for each group item, because regular ones may be invalidated by 
		// the move call in the loop
		for( ModelIter it(model()); it.isValid(); ++it )
			if( model()->translator(*it) == groupedItemTranslator() )
				persistentGroupIndexes.append( *it );
		foreach( QPersistentModelIndex idx, persistentGroupIndexes ) {
			bool isEmptyGroup = model()->rowCount(idx) == 0;
			QString groupVal = idx.sibling( idx.row(), mGroupColumn ).data( Qt::DisplayRole ).toString();
			GroupMap::Iterator mapIt = grouped.find( groupVal );
			if( mapIt != grouped.end() ) {
				QModelIndexList toMove(fromPersist(mapIt.value()));
				//LOG_5( QString("Moving indexes %1 to existing group item at index %2").arg(indexListToStr(toMove)).arg(indexToStr(idx)) );
				model()->move( toMove, idx );
				if( isEmptyGroup )
					emit groupPopulated( idx );
				if( mUpdateScheduled ) {
					if( !mGroupItemsToUpdate.contains( idx ) )
						mGroupItemsToUpdate.append(idx);
				} else
					// Tell the group item to update itself based on the added children
					model()->setData( idx, QVariant(), GroupingUpdate );
				grouped.erase( mapIt );
			}
		}
		// Deal with any now-empty groups
		for( QList<QPersistentModelIndex>::Iterator it = persistentGroupIndexes.begin(); it != persistentGroupIndexes.end(); )
			if( model()->translator(*it) == groupedItemTranslator() && model()->rowCount(*it) == 0 ) {
				emit groupEmptied(*it);
				++it;
			} else
				it = persistentGroupIndexes.erase( it );
		
		if( emptyGroupPolicy() == RemoveEmptyGroups )
			model()->remove( fromPersist( persistentGroupIndexes ) );
	}