hgcacheproxymodel/tsrc/unit/dataproviderhelper.cpp
changeset 1 e48454f237ca
child 3 c863538fcbb6
equal deleted inserted replaced
0:89c329efa980 1:e48454f237ca
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 *  Version     : %version: 3 %
       
    17 */
       
    18 #include "dataproviderhelper.h"
       
    19 #include <QtDebug>
       
    20 
       
    21 const int KRole1 = Qt::UserRole+1;
       
    22 const int KRole2 = Qt::UserRole+2;
       
    23 const int KRole3 = Qt::UserRole+3;
       
    24 
       
    25 DataProviderHelper::DataProviderHelper(int size, QObject *parent):
       
    26 HgDataProviderModel(parent)
       
    27 {
       
    28     mDefaultIcon = QString("DefaultIcon");
       
    29     
       
    30     for ( int i(0);i<size;i++){
       
    31         QList< QPair< QVariant, int > > list;
       
    32         int a = i;
       
    33         int b = -i;
       
    34         int c = a/4 + b/10 + i%30 + i/2;
       
    35         QString s = QString("ITEM%1 %2 %3").arg(c).arg(a).arg(b);
       
    36         if (i%2){
       
    37             s = s.toLower();
       
    38         }
       
    39         list.append( QPair< QVariant, int >(s, Qt::DisplayRole) );
       
    40         list.append( QPair< QVariant, int >(QVariant(a), KRole1) );
       
    41         list.append( QPair< QVariant, int >(QVariant(b), KRole2) );
       
    42         list.append( QPair< QVariant, int >(QVariant(c), KRole3) );
       
    43         newItem(&list, true);
       
    44     }
       
    45     
       
    46     connect(this, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
       
    47             this, SLOT(slotDataUpdated(QModelIndex,QModelIndex)));
       
    48     
       
    49     connect(this, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
       
    50             this, SLOT(slotRowsAboutToBeInserted(QModelIndex,int,int)));
       
    51 
       
    52     connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)),
       
    53             this, SLOT(slotRowsInserted(QModelIndex,int,int)));    
       
    54     
       
    55     connect(this, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
       
    56             this, SLOT(slotRowsAboutToBeRemoved(QModelIndex,int,int)));
       
    57 
       
    58     connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)),
       
    59             this, SLOT(slotRowsRemoved(QModelIndex,int,int)));    
       
    60     
       
    61     connect(this, SIGNAL(modelAboutToBeReset()), 
       
    62             this, SLOT(slotModelAboutToBeReset()));
       
    63     
       
    64     connect(this, SIGNAL(modelReset()), 
       
    65             this, SLOT(slotModelReset()));
       
    66 }
       
    67 
       
    68 DataProviderHelper::~DataProviderHelper()
       
    69 {
       
    70     
       
    71 }
       
    72 
       
    73 void DataProviderHelper::doReleaseData(QList<int> list, bool silent)
       
    74 {
       
    75     Q_UNUSED(silent);    
       
    76     mLastRelease = QList<int>(list);
       
    77 }
       
    78 
       
    79 void DataProviderHelper::doRequestData(QList<int> list, bool silent)
       
    80 {
       
    81     Q_UNUSED(silent);
       
    82     mLastRequest = QList<int>(list);
       
    83     for ( int i=0; i<list.size(); i++){
       
    84         updateIcon(list[i], QString("MyNiceIcon %1").arg(list[i]) );
       
    85     }
       
    86 }
       
    87 
       
    88 QVariant DataProviderHelper::defaultIcon() const
       
    89 {
       
    90     return mDefaultIcon;
       
    91 }
       
    92 
       
    93 void DataProviderHelper::testClearCache()
       
    94 {
       
    95     clearCache();
       
    96 }
       
    97 
       
    98 void DataProviderHelper::testNewItem(QList< QPair< QVariant, int > >* list, bool silent)
       
    99 {
       
   100     newItem(list, silent);
       
   101 }
       
   102 
       
   103 void DataProviderHelper::testNewItem(QPair< QVariant, int > item, bool silent)
       
   104 {
       
   105     newItem(item, silent);
       
   106 }
       
   107 
       
   108 bool DataProviderHelper::testUpdate(int pos, QList< QPair< QVariant, int > >* list, bool silent)
       
   109 {
       
   110     return update(pos, list, silent);
       
   111 }
       
   112 
       
   113 bool DataProviderHelper::testUpdate(int pos, QVariant obj, int role, bool silent )
       
   114 {
       
   115     return update(pos, obj, role, silent);
       
   116 }
       
   117 
       
   118 void DataProviderHelper::testRemoveItems(int pos, int size)
       
   119 {
       
   120     removeItems(pos, size);
       
   121 }
       
   122 
       
   123 void DataProviderHelper::testRemoveItem(int pos)
       
   124 {
       
   125     removeItem(pos);
       
   126 }
       
   127 
       
   128 void DataProviderHelper::testInsertItem(int pos, QPair< QVariant, int > item, bool silent)
       
   129 {
       
   130     insertItem(pos, item, silent);
       
   131 }
       
   132 
       
   133 void DataProviderHelper::doResetModel()
       
   134 {
       
   135     clearCache();
       
   136     for ( int i(0);i<1000;i++){
       
   137         QList< QPair< QVariant, int > > list;
       
   138         int a = i;
       
   139         int b = -i;
       
   140         int c = a/4 + b/10 + i%30 + i/2;
       
   141         QString s = QString("NEW ITEM %1 %2 %3").arg(c).arg(a).arg(b);
       
   142         if (i%2){
       
   143             s = s.toLower();
       
   144         }
       
   145         list.append( QPair< QVariant, int >(s, Qt::DisplayRole) );
       
   146         list.append( QPair< QVariant, int >(QVariant(a), KRole1) );
       
   147         list.append( QPair< QVariant, int >(QVariant(b), KRole2) );
       
   148         list.append( QPair< QVariant, int >(QVariant(c), KRole3) );
       
   149         newItem(&list, true);
       
   150     }
       
   151 }
       
   152 
       
   153 bool DataProviderHelper::setData(const QModelIndex &index, const QVariant &value, int role)
       
   154 {
       
   155     bool res = false;
       
   156     if ( index.row()>=0 && index.row() < count() ){
       
   157 //        qWarning()<<index.row();
       
   158         res = update(index.row(), value, role, false);
       
   159     }
       
   160     return res;
       
   161 }
       
   162 bool DataProviderHelper::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
       
   163 {
       
   164     bool res = false;
       
   165     if ( index.row()>=0 && index.row() < count() ){
       
   166         QMapIterator<int, QVariant> i(roles);
       
   167         while (i.hasNext()){
       
   168             i.next();
       
   169             res = update(index.row(), i.value(), i.key(), true) | res;
       
   170         }
       
   171     }
       
   172     return res;
       
   173 }
       
   174 
       
   175 QList< QPair< int, int > > DataProviderHelper::getObserverDataUpdated()
       
   176 {
       
   177     QList< QPair< int, int > > res = mObserverDataUpdated;
       
   178     mObserverDataUpdated.clear();
       
   179     return res;
       
   180 }
       
   181 
       
   182 QList< QPair< int, int > > DataProviderHelper::getSignalDataUpdated()
       
   183 {
       
   184     QList< QPair< int, int > > res = mSignalDataUpdated;
       
   185     mSignalDataUpdated.clear();
       
   186     return res;
       
   187 }
       
   188 
       
   189 QList< QPair< int, int > > DataProviderHelper::getSignalRowsAboutToBeInserted()
       
   190 {
       
   191     QList< QPair< int, int > > res = mSignalRowsAboutToBeInserted;
       
   192     mSignalRowsAboutToBeInserted.clear();
       
   193     return res;
       
   194 }
       
   195 
       
   196 QList< QPair< int, int > > DataProviderHelper::getSignalRowsInserted()
       
   197 {
       
   198     QList< QPair< int, int > > res = mSignalRowsInserted;
       
   199     mSignalRowsInserted.clear();
       
   200     return res;
       
   201 }
       
   202 
       
   203 QList< QPair< int, int > > DataProviderHelper::getSignalRowsAboutToBeRemoved()
       
   204 {
       
   205     QList< QPair< int, int > > res = mSignalRowsAboutToBeRemoved;
       
   206     mSignalRowsAboutToBeRemoved.clear();
       
   207     return res;
       
   208 }
       
   209 
       
   210 QList< QPair< int, int > > DataProviderHelper::getSignalRowsRemoved()
       
   211 {
       
   212     QList< QPair< int, int > > res = mSignalRowsRemoved;
       
   213     mSignalRowsRemoved.clear();
       
   214     return res;
       
   215 }
       
   216 
       
   217 bool DataProviderHelper::getSignalAboutToBeReset()
       
   218 {
       
   219     bool res = mSignalAboutToBeReset;
       
   220     mSignalAboutToBeReset = false;
       
   221     return res;
       
   222 }
       
   223 
       
   224 bool DataProviderHelper::getSignalReset()
       
   225 {
       
   226     bool res = mSignalReset;
       
   227     mSignalReset = false;
       
   228     return res;
       
   229 }
       
   230 
       
   231 QList<int> DataProviderHelper::getLastRelease()
       
   232 {
       
   233     QList<int> res = mLastRelease;
       
   234     mLastRelease.clear();
       
   235     return res;
       
   236 }
       
   237 
       
   238 QList<int> DataProviderHelper::getLastRequest()
       
   239 {
       
   240     QList<int> res = mLastRequest;
       
   241     mLastRequest.clear();
       
   242     return res;
       
   243 }
       
   244 
       
   245 void DataProviderHelper::testReleasePixmap(int idx)
       
   246 {
       
   247     releasePixmap(idx);
       
   248 }
       
   249 
       
   250 QVariant DataProviderHelper::testCreateIcon(int index, QPixmap aPixmap)
       
   251 {
       
   252     return createIcon(index, aPixmap);
       
   253 }
       
   254 
       
   255 void DataProviderHelper::testEmitDataChanged(QModelIndex from,QModelIndex to)
       
   256 {
       
   257     emit dataChanged(from,to);
       
   258 }
       
   259 
       
   260 void DataProviderHelper::testEmitHeaderDataChanged(Qt::Orientation orientation, int first, int last)
       
   261 {
       
   262     emit headerDataChanged(orientation, first, last);
       
   263 }
       
   264 
       
   265 void DataProviderHelper::testEmitRowsInsert(QModelIndex parent,int from,int to)
       
   266 {
       
   267     beginInsertRows(parent, from, to);
       
   268     endInsertRows();
       
   269 }
       
   270 
       
   271 
       
   272 void DataProviderHelper::testEmitColumnsInsert(QModelIndex parent,int from,int to)
       
   273 {
       
   274     beginInsertColumns(parent, from, to);
       
   275     endInsertColumns();
       
   276 }
       
   277 
       
   278 
       
   279 void DataProviderHelper::testEmitRowsRemove(QModelIndex parent,int from,int to)
       
   280 {
       
   281     beginRemoveRows(parent, from, to);
       
   282     endRemoveRows();
       
   283 }
       
   284 
       
   285 void DataProviderHelper::testEmitColumnsRemove(QModelIndex parent,int from,int to)
       
   286 {
       
   287     beginRemoveColumns(parent, from, to);
       
   288     endRemoveColumns();
       
   289 }
       
   290 
       
   291 void DataProviderHelper::testEmitLayoutAboutToBeChanged()
       
   292 {
       
   293     emit layoutAboutToBeChanged();
       
   294 }
       
   295 
       
   296 void DataProviderHelper::testEmitLayoutChanged()
       
   297 {
       
   298     emit layoutChanged();
       
   299 }
       
   300 
       
   301 void DataProviderHelper::testEmitModelAboutToBeReset()
       
   302 {
       
   303     beginResetModel();
       
   304 }
       
   305 
       
   306 void DataProviderHelper::testEmitModelReset()
       
   307 {
       
   308     endResetModel();
       
   309 }
       
   310 
       
   311 void DataProviderHelper::dataUpdated(int from, int to)
       
   312 {
       
   313     QPair< int, int > p;
       
   314     p.first = from;
       
   315     p.second = to;    
       
   316     mObserverDataUpdated.append(p);
       
   317 }
       
   318 
       
   319 void DataProviderHelper::slotDataUpdated(QModelIndex from,QModelIndex to)
       
   320 {
       
   321     QPair< int, int > p;
       
   322     p.first = from.row();
       
   323     p.second = to.row();    
       
   324     mSignalDataUpdated.append(p);
       
   325 }
       
   326 
       
   327 void DataProviderHelper::slotRowsAboutToBeInserted(QModelIndex parent, int from, int to)
       
   328 {
       
   329     Q_UNUSED(parent);
       
   330     QPair< int, int > p;
       
   331     p.first = from;
       
   332     p.second = to;    
       
   333     mSignalRowsAboutToBeInserted.append(p);
       
   334 }
       
   335 
       
   336 void DataProviderHelper::slotRowsInserted(QModelIndex parent, int from, int to)
       
   337 {
       
   338     Q_UNUSED(parent);
       
   339     QPair< int, int > p;
       
   340     p.first = from;
       
   341     p.second = to;    
       
   342     mSignalRowsInserted.append(p);
       
   343 }
       
   344 
       
   345 void DataProviderHelper::slotRowsAboutToBeRemoved(QModelIndex parent, int from, int to)
       
   346 {
       
   347     Q_UNUSED(parent);
       
   348     QPair< int, int > p;
       
   349     p.first = from;
       
   350     p.second = to;    
       
   351     mSignalRowsAboutToBeRemoved.append(p);
       
   352 }
       
   353 
       
   354 void DataProviderHelper::slotRowsRemoved(QModelIndex parent, int from, int to)
       
   355 {
       
   356     Q_UNUSED(parent);    
       
   357     Q_UNUSED(parent);
       
   358     QPair< int, int > p;
       
   359     p.first = from;
       
   360     p.second = to;    
       
   361     mSignalRowsRemoved.append(p);
       
   362 }
       
   363 
       
   364 void DataProviderHelper::slotModelAboutToBeReset()
       
   365 {
       
   366     mSignalAboutToBeReset = true;
       
   367 }
       
   368 
       
   369 void DataProviderHelper::slotModelReset()
       
   370 {
       
   371     mSignalReset = true;
       
   372 }
       
   373 
       
   374 
       
   375 
       
   376 
       
   377 
       
   378