hgcacheproxymodel/src/hgcacheproxymodel.cpp
changeset 17 a10844a9914d
parent 6 1cdcc61142d2
child 20 a60f8b6b1d32
equal deleted inserted replaced
15:1ef5359bf0f4 17:a10844a9914d
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 *  Version     : %version: 11 %
    16 *  Version     : %version: 14 %
    17 */
    17 */
    18 #include <QList>
    18 #include <QList>
    19 #include <QAbstractItemModel>
    19 #include <QAbstractItemModel>
    20 
    20 
    21 #include <hgwidgets/hgcacheproxymodel.h>
    21 #include <hgwidgets/hgcacheproxymodel.h>
    31     QAbstractItemModel(parent),
    31     QAbstractItemModel(parent),
    32     mBufferManager(0),
    32     mBufferManager(0),
    33     mSortFilterProxyModel(new QSortFilterProxyModel(this)),
    33     mSortFilterProxyModel(new QSortFilterProxyModel(this)),
    34     mDataProviderModel(0),
    34     mDataProviderModel(0),
    35     mSupressBM(false),
    35     mSupressBM(false),
    36     mCurrentPos(0)//,
    36     mCurrentPos(0)
    37 //    mSortParameterChanged(true)
       
    38 {
    37 {
    39     TX_ENTRY
    38     TX_ENTRY
    40     connect(mSortFilterProxyModel, SIGNAL(columnsAboutToBeInserted(const QModelIndex&, int, int)),
    39     connect(mSortFilterProxyModel, SIGNAL(columnsAboutToBeInserted(const QModelIndex&, int, int)),
    41             this, SLOT(sourceColumnsAboutToBeInserted(const QModelIndex&, int, int)));
    40             this, SLOT(sourceColumnsAboutToBeInserted(const QModelIndex&, int, int)));
    42     connect(mSortFilterProxyModel, SIGNAL(columnsAboutToBeMoved(const QModelIndex&, int, int, const QModelIndex&, int)),
    41     connect(mSortFilterProxyModel, SIGNAL(columnsAboutToBeMoved(const QModelIndex&, int, int, const QModelIndex&, int)),
    88 void HgCacheProxyModel::setDataProvider(HgDataProviderModel *dataProvider, int cacheSize, int cacheTreshold)
    87 void HgCacheProxyModel::setDataProvider(HgDataProviderModel *dataProvider, int cacheSize, int cacheTreshold)
    89 {
    88 {
    90     TX_ENTRY
    89     TX_ENTRY
    91     mDataProviderModel = dataProvider;
    90     mDataProviderModel = dataProvider;
    92     mSortFilterProxyModel->setSourceModel(mDataProviderModel);
    91     mSortFilterProxyModel->setSourceModel(mDataProviderModel);
    93     if (mDataProviderModel){
    92     if (mDataProviderModel) {
    94         mDataProviderModel->registerObserver(this);
    93         mDataProviderModel->registerObserver(this);
    95 
    94 
    96         mSupressBM = true;
    95         mSupressBM = true;
    97         resizeCache(cacheSize, cacheTreshold);
    96         resizeCache(cacheSize, cacheTreshold);
    98         mSupressBM = false;
    97         mSupressBM = false;
    99 
    98 
   100         if (mBufferManager == NULL){
    99         if (mBufferManager == NULL) {
   101             mBufferManager = new HgBufferManager(this, cacheSize, cacheTreshold, 0, count() );
   100             mBufferManager = new HgBufferManager(this, cacheSize, cacheTreshold, 0, count() );
   102         } else {
   101         } else {
   103             mBufferManager->resetBuffer(0, count());
   102             mBufferManager->resetBuffer(0, count());
   104         }
   103         }
   105         
   104         
   124 }
   123 }
   125 
   124 
   126 QModelIndex HgCacheProxyModel::index(int row, int column, const QModelIndex &parent) const
   125 QModelIndex HgCacheProxyModel::index(int row, int column, const QModelIndex &parent) const
   127 {
   126 {
   128     Q_UNUSED(parent);
   127     Q_UNUSED(parent);
   129     if (  row >= rowCount() ){
   128     if (row >= rowCount()) {
   130         row = -1;
   129         row = -1;
   131     }
   130     }
   132     if (  column >= columnCount() ){
   131     if (column >= columnCount()) {
   133         column = -1;
   132         column = -1;
   134     }
   133     }
   135     
   134     
   136     return QAbstractItemModel::createIndex(row, column); 
   135     return QAbstractItemModel::createIndex(row, column); 
   137 }
   136 }
   195 }
   194 }
   196 
   195 
   197 QMimeData *HgCacheProxyModel::mimeData(const QModelIndexList &indexes) const
   196 QMimeData *HgCacheProxyModel::mimeData(const QModelIndexList &indexes) const
   198 {
   197 {
   199     QModelIndexList list;
   198     QModelIndexList list;
   200     for ( int i=0; i < indexes.count(); i++){
   199     for (int i=0; i < indexes.count(); i++) {
   201         list.append(mapToSource(indexes[i]));
   200         list.append(mapToSource(indexes[i]));
   202     }
   201     }
   203     return mSortFilterProxyModel->mimeData(list);
   202     return mSortFilterProxyModel->mimeData(list);
   204 }
   203 }
   205 
   204 
   278     return mSortFilterProxyModel->sortCaseSensitivity();
   277     return mSortFilterProxyModel->sortCaseSensitivity();
   279 }
   278 }
   280 
   279 
   281 void HgCacheProxyModel::setSortCaseSensitivity(Qt::CaseSensitivity cs)
   280 void HgCacheProxyModel::setSortCaseSensitivity(Qt::CaseSensitivity cs)
   282 {
   281 {
   283     if (sortCaseSensitivity() != cs){
   282     if (sortCaseSensitivity() != cs) {
   284         mSupressBM = true;
   283         mSupressBM = true;
   285         sourceModelAboutToBeReset();
   284         sourceModelAboutToBeReset();
   286         mSortFilterProxyModel->setSortCaseSensitivity(cs);
   285         mSortFilterProxyModel->setSortCaseSensitivity(cs);
   287         sourceModelReset();  
   286         sourceModelReset();  
   288     }
   287     }
   293     return mSortFilterProxyModel->isSortLocaleAware();
   292     return mSortFilterProxyModel->isSortLocaleAware();
   294 }
   293 }
   295 
   294 
   296 void HgCacheProxyModel::setSortLocaleAware(bool on)
   295 void HgCacheProxyModel::setSortLocaleAware(bool on)
   297 {
   296 {
   298     if (isSortLocaleAware() != on){
   297     if (isSortLocaleAware() != on) {
   299         mSupressBM = true;    
   298         mSupressBM = true;    
   300         sourceModelAboutToBeReset();
   299         sourceModelAboutToBeReset();
   301         mSortFilterProxyModel->setSortLocaleAware(on);
   300         mSortFilterProxyModel->setSortLocaleAware(on);
   302         sourceModelReset();  
   301         sourceModelReset();  
   303     }
   302     }
   318     return mSortFilterProxyModel->dynamicSortFilter();
   317     return mSortFilterProxyModel->dynamicSortFilter();
   319 }
   318 }
   320 
   319 
   321 void HgCacheProxyModel::setDynamicSortFilter(bool enable)
   320 void HgCacheProxyModel::setDynamicSortFilter(bool enable)
   322 {
   321 {
   323     if (dynamicSortFilter() != enable){
   322     if (dynamicSortFilter() != enable) {
   324         mSupressBM = true;   
   323         mSupressBM = true;   
   325         sourceModelAboutToBeReset();
   324         sourceModelAboutToBeReset();
   326         mSortFilterProxyModel->setDynamicSortFilter(enable);
   325         mSortFilterProxyModel->setDynamicSortFilter(enable);
   327         sourceModelReset();  
   326         sourceModelReset();  
   328     }
   327     }
   333     return mSortFilterProxyModel->sortRole();
   332     return mSortFilterProxyModel->sortRole();
   334 }
   333 }
   335 
   334 
   336 void HgCacheProxyModel::setSortRole(int role)
   335 void HgCacheProxyModel::setSortRole(int role)
   337 {
   336 {
   338     if (sortRole() != role){
   337     if (sortRole() != role) {
   339         mSupressBM = true;
   338         mSupressBM = true;
   340         sourceModelAboutToBeReset();
   339         sourceModelAboutToBeReset();
   341         mSortFilterProxyModel->setSortRole(role);
   340         mSortFilterProxyModel->setSortRole(role);
   342         sourceModelReset();    
   341         sourceModelReset();    
   343     }
   342     }
   348     return mSortFilterProxyModel->filterRegExp();
   347     return mSortFilterProxyModel->filterRegExp();
   349 }
   348 }
   350 
   349 
   351 void HgCacheProxyModel::setFilterRegExp(const QRegExp &regExp)
   350 void HgCacheProxyModel::setFilterRegExp(const QRegExp &regExp)
   352 {
   351 {
   353     if (filterRegExp() != regExp){
   352     if (filterRegExp() != regExp) {
   354         mSupressBM = true;
   353         mSupressBM = true;
   355         sourceModelAboutToBeReset();
   354         sourceModelAboutToBeReset();
   356         mSortFilterProxyModel->setFilterRegExp(regExp);
   355         mSortFilterProxyModel->setFilterRegExp(regExp);
   357         sourceModelReset();    
   356         sourceModelReset();    
   358     }
   357     }
   363     return mSortFilterProxyModel->filterKeyColumn();
   362     return mSortFilterProxyModel->filterKeyColumn();
   364 }
   363 }
   365 
   364 
   366 void HgCacheProxyModel::setFilterKeyColumn(int column)
   365 void HgCacheProxyModel::setFilterKeyColumn(int column)
   367 {
   366 {
   368     if (filterKeyColumn() != column){   
   367     if (filterKeyColumn() != column) {
   369         mSupressBM = true;
   368         mSupressBM = true;
   370         sourceModelAboutToBeReset();
   369         sourceModelAboutToBeReset();
   371         mSortFilterProxyModel->setFilterKeyColumn(column);
   370         mSortFilterProxyModel->setFilterKeyColumn(column);
   372         sourceModelReset();    
   371         sourceModelReset();    
   373     }
   372     }
   378     return mSortFilterProxyModel->filterCaseSensitivity();
   377     return mSortFilterProxyModel->filterCaseSensitivity();
   379 }
   378 }
   380 
   379 
   381 void HgCacheProxyModel::setFilterCaseSensitivity(Qt::CaseSensitivity cs)
   380 void HgCacheProxyModel::setFilterCaseSensitivity(Qt::CaseSensitivity cs)
   382 {
   381 {
   383     if ( filterCaseSensitivity() != cs){   
   382     if (filterCaseSensitivity() != cs) {   
   384         mSupressBM = true;
   383         mSupressBM = true;
   385         sourceModelAboutToBeReset();
   384         sourceModelAboutToBeReset();
   386         mSortFilterProxyModel->setFilterCaseSensitivity(cs);
   385         mSortFilterProxyModel->setFilterCaseSensitivity(cs);
   387         sourceModelReset();    
   386         sourceModelReset();    
   388     }
   387     }
   393     return mSortFilterProxyModel->filterRole();
   392     return mSortFilterProxyModel->filterRole();
   394 }
   393 }
   395 
   394 
   396 void HgCacheProxyModel::setFilterRole(int role)
   395 void HgCacheProxyModel::setFilterRole(int role)
   397 {
   396 {
   398     if ( filterRole() != role ){   
   397     if (filterRole() != role) {
   399         mSupressBM = true;
   398         mSupressBM = true;
   400         sourceModelAboutToBeReset();
   399         sourceModelAboutToBeReset();
   401         mSortFilterProxyModel->setFilterRole(role);
   400         mSortFilterProxyModel->setFilterRole(role);
   402         sourceModelReset();    
   401         sourceModelReset();    
   403     }
   402     }
   416 void HgCacheProxyModel::release(int start, int end)
   415 void HgCacheProxyModel::release(int start, int end)
   417 {
   416 {
   418     TX_ENTRY_ARGS( QString("%0-%1").arg(start).arg(end));   
   417     TX_ENTRY_ARGS( QString("%0-%1").arg(start).arg(end));   
   419     QList<int> list;
   418     QList<int> list;
   420     int idx = 0;
   419     int idx = 0;
   421     if ( start > end){
   420     if (start > end) {
   422         idx = end;
   421         idx = end;
   423         end = start;
   422         end = start;
   424         start = idx;
   423         start = idx;
   425         idx = 0;
   424         idx = 0;
   426     }
   425     }
   427     for ( int i=start; i <=end; i++){
   426     for (int i=start; i <=end; i++) {
   428         idx = mapToDataProviderIndex(i);
   427         idx = mapToDataProviderIndex(i);
   429         if ( idx >=0)
   428         if (idx >=0)
   430             list.append(idx);
   429             list.append(idx);
   431     }
   430     }
   432     if (mDataProviderModel)
   431     if (mDataProviderModel) {
   433         mDataProviderModel->release(list, true);
   432         mSupressBM = true;
       
   433         mDataProviderModel->release(list, false);
       
   434         mSupressBM = false;
       
   435     }
   434     TX_EXIT
   436     TX_EXIT
   435 }
   437 }
   436 
   438 
   437 void HgCacheProxyModel::request(int start, int end, HgRequestOrder order)
   439 void HgCacheProxyModel::request(int start, int end, HgRequestOrder order)
   438 {
   440 {
   439     TX_ENTRY_ARGS( QString("%0-%1").arg(start).arg(end));   
   441     TX_ENTRY_ARGS( QString("%0-%1").arg(start).arg(end));   
   440     QList<int> list;
   442     QList<int> list;
   441     int idx;
   443     int idx;
   442     if (order == HgRequestOrderAscending){
   444     if (order == HgRequestOrderAscending) {
   443         for ( int i=start; i <=end; i++){
   445         for (int i=start; i <=end; i++) {
   444             idx = mapToDataProviderIndex(i);
   446             idx = mapToDataProviderIndex(i);
   445             if ( idx >=0)
   447             if (idx >=0)
   446                 list.append(idx);
   448                 list.append(idx);
   447         }
   449         }
   448     } else {
   450     } else {
   449         for ( int i=end; i >=start; i--){
   451         for (int i=end; i >=start; i--) {
   450             idx = mapToDataProviderIndex(i);
   452             idx = mapToDataProviderIndex(i);
   451             if ( idx >=0)
   453             if (idx >=0)
   452                 list.append(idx);
   454                 list.append(idx);
   453         }
   455         }
   454     }
   456     }
   455     if (mDataProviderModel && list.count()>0)
   457     if (mDataProviderModel && list.count()>0)
   456         mDataProviderModel->request(list, mSupressBM);
   458         mDataProviderModel->request(list, mSupressBM);
   482     return mSortFilterProxyModel->mapFromSource( mDataProviderModel->index(myIndex, 0) ).row();
   484     return mSortFilterProxyModel->mapFromSource( mDataProviderModel->index(myIndex, 0) ).row();
   483 }
   485 }
   484 
   486 
   485 void HgCacheProxyModel::releaseAll()
   487 void HgCacheProxyModel::releaseAll()
   486 {
   488 {
   487     if ( mDataProviderModel ){
   489     if (mDataProviderModel) {
   488         QList<int> list;
   490         QList<int> list;
   489         for ( int i=0; i<mDataProviderModel->rowCount(); i++){
   491         for (int i=0; i<mDataProviderModel->rowCount(); i++) {
   490             list.append(i);
   492             list.append(i);
   491         }
   493         }
   492         mDataProviderModel->release(list, true);
   494         mDataProviderModel->release(list, true);
   493     }
   495     }
   494 }
   496 }
   495 
   497 
   496 void HgCacheProxyModel::setBufferPosition(int pos) const
   498 void HgCacheProxyModel::setBufferPosition(int pos) const
   497 {
   499 {
   498     if (mBufferManager && !mSupressBM){
   500     if (mBufferManager && !mSupressBM) {
   499         if (mCurrentPos!=pos){
   501         if (mCurrentPos!=pos) {
   500 //            TX_LOG_ARGS(QString("pos:%1 ").arg(pos) );
   502 //            TX_LOG_ARGS(QString("pos:%1 ").arg(pos) );
   501             mCurrentPos = pos;
   503             mCurrentPos = pos;
   502             mBufferManager->setPosition(mCurrentPos);
   504             mBufferManager->setPosition(mCurrentPos);
   503         }
   505         }
   504     }
   506     }
   560 }
   562 }
   561 
   563 
   562 void HgCacheProxyModel::sourceDataChanged( const QModelIndex & topLeft, const QModelIndex & bottomRight )
   564 void HgCacheProxyModel::sourceDataChanged( const QModelIndex & topLeft, const QModelIndex & bottomRight )
   563 {
   565 {
   564     TX_ENTRY_ARGS(QString("from:%1 to:%2").arg( topLeft.row() ).arg( bottomRight.row() ) );
   566     TX_ENTRY_ARGS(QString("from:%1 to:%2").arg( topLeft.row() ).arg( bottomRight.row() ) );
   565     QModelIndex begin = index( topLeft.row(), topLeft.column() );
   567     QModelIndex begin = index(topLeft.row(), topLeft.column());
   566     QModelIndex end = index( bottomRight.row(), bottomRight.column() );
   568     QModelIndex end = index(bottomRight.row(), bottomRight.column());
   567     
   569     
   568     if (begin.isValid() && end.isValid() && !mSupressBM){
   570     if (begin.isValid() && end.isValid() && !mSupressBM) {
   569         emit dataChanged(begin, end);
   571         emit dataChanged(begin, end);
   570     }
   572     }
   571     TX_EXIT
   573     TX_EXIT
   572 }
   574 }
   573 
   575 
   574 void HgCacheProxyModel::sourceHeaderDataChanged( Qt::Orientation orientation, int first, int last )
   576 void HgCacheProxyModel::sourceHeaderDataChanged( Qt::Orientation orientation, int first, int last )
   575 {
   577 {
   576     TX_ENTRY
   578     TX_ENTRY
   577     emit headerDataChanged( orientation, first, last );
   579     emit headerDataChanged(orientation, first, last);
   578     TX_EXIT
   580     TX_EXIT
   579 }
   581 }
   580 
   582 
   581 void HgCacheProxyModel::sourceLayoutAboutToBeChanged()
   583 void HgCacheProxyModel::sourceLayoutAboutToBeChanged()
   582 {
   584 {
   603 
   605 
   604 void HgCacheProxyModel::sourceModelReset()
   606 void HgCacheProxyModel::sourceModelReset()
   605 {
   607 {
   606     TX_ENTRY
   608     TX_ENTRY
   607     mSupressBM = true;
   609     mSupressBM = true;
   608     if (mBufferManager){
   610     if (mBufferManager) {
   609         mCurrentPos = 0;
   611         mCurrentPos = 0;
   610         mBufferManager->resetBuffer( mCurrentPos, count() );
   612         mBufferManager->resetBuffer(mCurrentPos, count());
   611     }
   613     }
   612     mSupressBM = false;
   614     mSupressBM = false;
   613     endResetModel();
   615     endResetModel();
   614     TX_EXIT
   616     TX_EXIT
   615 }
   617 }
   616 
   618 
   617 void HgCacheProxyModel::sourceRowsAboutToBeInserted( const QModelIndex & parent, int start, int end )
   619 void HgCacheProxyModel::sourceRowsAboutToBeInserted( const QModelIndex & parent, int start, int end )
   618 {
   620 {
   619     TX_ENTRY
   621     TX_ENTRY
   620     if (mBufferManager && !mSupressBM){
   622     if (mBufferManager && !mSupressBM) {
   621         beginInsertRows(parent, start, end);
   623         beginInsertRows(parent, start, end);
   622         for ( int i=start; i <=end; i++){
   624         for (int i=start; i <=end; i++) {
   623             mBufferManager->aboutToInsertItem(i);
   625             mBufferManager->aboutToInsertItem(i);
   624         }        
   626         }        
   625     }
   627     }
   626     TX_EXIT
   628     TX_EXIT
   627 }
   629 }
   635 }
   637 }
   636 
   638 
   637 void HgCacheProxyModel::sourceRowsAboutToBeRemoved( const QModelIndex & parent, int start, int end )
   639 void HgCacheProxyModel::sourceRowsAboutToBeRemoved( const QModelIndex & parent, int start, int end )
   638 {
   640 {
   639     TX_ENTRY
   641     TX_ENTRY
   640     if (mBufferManager && !mSupressBM){
   642     if (mBufferManager && !mSupressBM) {
   641         beginRemoveRows(parent, start, end);
   643         beginRemoveRows(parent, start, end);
   642         for ( int i=start; i <=end; i++){
   644         for (int i=start; i <=end; i++) {
   643             mBufferManager->aboutToRemoveItem(i);
   645             mBufferManager->aboutToRemoveItem(i);
   644         }
   646         }
   645     }
   647     }
   646     TX_EXIT
   648     TX_EXIT
   647 }
   649 }
   649 void HgCacheProxyModel::sourceRowsInserted( const QModelIndex & parent, int start, int end )
   651 void HgCacheProxyModel::sourceRowsInserted( const QModelIndex & parent, int start, int end )
   650 {
   652 {
   651     TX_ENTRY
   653     TX_ENTRY
   652     Q_UNUSED(parent);
   654     Q_UNUSED(parent);
   653     Q_UNUSED(end);
   655     Q_UNUSED(end);
   654     if (mBufferManager && !mSupressBM){
   656     if (mBufferManager && !mSupressBM) {
   655         for ( int i=start; i <=end; i++){
   657         for (int i=start; i <=end; i++) {
   656             mBufferManager->insertedItem(i);
   658             mBufferManager->insertedItem(i);
   657         }
   659         }
   658         endInsertRows();
   660         endInsertRows();
   659     }
   661     }
   660     TX_EXIT    
   662     TX_EXIT    
   676 void HgCacheProxyModel::sourceRowsRemoved( const QModelIndex & parent, int start, int end )
   678 void HgCacheProxyModel::sourceRowsRemoved( const QModelIndex & parent, int start, int end )
   677 {
   679 {
   678     TX_ENTRY
   680     TX_ENTRY
   679     Q_UNUSED(parent);
   681     Q_UNUSED(parent);
   680     Q_UNUSED(end);
   682     Q_UNUSED(end);
   681     if (mBufferManager && !mSupressBM){
   683     if (mBufferManager && !mSupressBM) {
   682         for ( int i=start; i <=end; i++){
   684         for (int i=start; i <=end; i++) {
   683             mBufferManager->removedItem(i);
   685             mBufferManager->removedItem(i);
   684         }
   686         }
   685         endRemoveRows();
   687         endRemoveRows();
   686     }
   688     }
   687     TX_EXIT
   689     TX_EXIT
   692     TX_LOG_ARGS(QString("from:%1 to:%2").arg(from).arg(to));
   694     TX_LOG_ARGS(QString("from:%1 to:%2").arg(from).arg(to));
   693     QModelIndex begin;
   695     QModelIndex begin;
   694     QModelIndex end;
   696     QModelIndex end;
   695     begin = index(mapFromDataProviderIndex(from),0);
   697     begin = index(mapFromDataProviderIndex(from),0);
   696     
   698     
   697     if ( from == to ){
   699     if (from == to) {
   698         end = begin;
   700         end = begin;
   699     } else {
   701     } else {
   700         end = index(mapFromDataProviderIndex(to),0);
   702         end = index(mapFromDataProviderIndex(to),0);
   701     }
   703     }
   702     
   704     
   703     if (begin.isValid() && end.isValid() && !mSupressBM){
   705     if (begin.isValid() && end.isValid() && !mSupressBM) {
   704         emit dataChanged(begin, end );
   706         emit dataChanged(begin, end );
   705     }
   707     }
   706 }
   708 }
   707 
   709 
   708 //eof
   710 //eof