Quantum GIS API Documentation  master-4435c4f
src/gui/attributetable/qgsattributetablememorymodel.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002      QgsAttributeTableMemoryModel.cpp
00003      --------------------------------------
00004     Date                 : Feb 2009
00005     Copyright            : (C) 2009 Vita Cizek
00006     Email                : weetya (at) gmail.com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include "qgsattributetablememorymodel.h"
00017 #include "qgsattributetablefiltermodel.h"
00018 
00019 #include "qgsfield.h"
00020 #include "qgsvectorlayer.h"
00021 #include "qgslogger.h"
00022 
00023 #include <QtGui>
00024 #include <QVariant>
00025 
00027 // In-Memory model //
00029 
00030 QgsAttributeTableMemoryModel::QgsAttributeTableMemoryModel( QgsMapCanvas *theCanvas, QgsVectorLayer *theLayer )
00031     : QgsAttributeTableModel( theCanvas, theLayer )
00032 {
00033   QgsDebugMsg( "entered." );
00034 }
00035 
00036 void QgsAttributeTableMemoryModel::loadLayer()
00037 {
00038   QgsDebugMsg( "entered." );
00039 
00040   QSettings settings;
00041   int behaviour = settings.value( "/qgis/attributeTableBehaviour", 0 ).toInt();
00042 
00043   QgsRectangle rect;
00044   if ( behaviour == 2 )
00045   {
00046     // current canvas only
00047     rect = mCurrentExtent;
00048   }
00049 
00050   QgsFeatureIterator fit = mLayer->getFeatures( QgsFeatureRequest().setFilterRect( rect ).setFlags( QgsFeatureRequest::NoGeometry ) );
00051 
00052   if ( behaviour != 1 )
00053     mFeatureMap.reserve( mLayer->pendingFeatureCount() + 50 );
00054   else
00055     mFeatureMap.reserve( mLayer->selectedFeatureCount() );
00056 
00057   int i = 0;
00058 
00059   QTime t;
00060   t.start();
00061 
00062   QgsFeature f;
00063   while ( fit.nextFeature( f ) )
00064   {
00065     if ( behaviour == 1 && !mLayer->selectedFeaturesIds().contains( f.id() ) )
00066       continue;
00067 
00068     mIdRowMap.insert( f.id(), i );
00069     mRowIdMap.insert( i, f.id() );
00070     mFeatureMap.insert( f.id(), f );
00071 
00072     i++;
00073 
00074     if ( t.elapsed() > 5000 )
00075     {
00076       bool cancel = false;
00077       emit progress( i, cancel );
00078       if ( cancel )
00079         break;
00080 
00081       t.restart();
00082     }
00083   }
00084 
00085   emit finished();
00086 
00087   mFieldCount = mAttributes.size();
00088 }
00089 
00090 int QgsAttributeTableMemoryModel::rowCount( const QModelIndex &parent ) const
00091 {
00092   Q_UNUSED( parent );
00093   return mFeatureMap.size();
00094 }
00095 
00096 bool QgsAttributeTableMemoryModel::featureAtId( QgsFeatureId fid ) const
00097 {
00098   QgsDebugMsg( QString( "entered featureAtId: %1." ).arg( fid ) );
00099   if ( mFeatureMap.contains( fid ) )
00100   {
00101     mFeat = mFeatureMap[ fid ];
00102     return true;
00103   }
00104   else
00105   {
00106     QgsDebugMsg( QString( "feature %1 not loaded" ).arg( fid ) );
00107     return false;
00108   }
00109 }
00110 
00111 void QgsAttributeTableMemoryModel::featureDeleted( QgsFeatureId fid )
00112 {
00113   QgsDebugMsg( "entered." );
00114   mFeatureMap.remove( fid );
00115   QgsAttributeTableModel::featureDeleted( fid );
00116 }
00117 
00118 void QgsAttributeTableMemoryModel::featureAdded( QgsFeatureId fid, bool inOperation )
00119 {
00120   QgsDebugMsg( "entered." );
00121   Q_UNUSED( fid );
00122   Q_UNUSED( inOperation );
00123   loadLayer();
00124 }
00125 
00126 void QgsAttributeTableMemoryModel::layerDeleted()
00127 {
00128   QgsDebugMsg( "entered." );
00129   loadLayer();
00130 }
00131 
00132 void QgsAttributeTableMemoryModel::attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value )
00133 {
00134   QgsDebugMsg( "entered." );
00135   mFeatureMap[fid].setAttribute( idx, value );
00136   QgsAttributeTableModel::attributeValueChanged( fid, idx, value );
00137 }
00138 
00139 bool QgsAttributeTableMemoryModel::removeRows( int row, int count, const QModelIndex &parent )
00140 {
00141   QgsDebugMsg( "entered." );
00142   for ( int i = row; i < row + count; i++ )
00143   {
00144     mFeatureMap.remove( mRowIdMap[ i ] );
00145   }
00146   return QgsAttributeTableModel::removeRows( row, count, parent );
00147 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines