Skip to content

Commit 9048107

Browse files
committed
[FEATURE] add row cache for attribute table
1 parent 1717d3c commit 9048107

File tree

6 files changed

+250
-149
lines changed

6 files changed

+250
-149
lines changed

images/images.qrc

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
<file>themes/default/propertyicons/rendering.png</file>
204204
<file>themes/default/propertyicons/symbology.png</file>
205205
<file>themes/default/propertyicons/transparency.png</file>
206+
<file>themes/default/propertyicons/gdal.png</file>
206207
<file>themes/default/qgis.xpm</file>
207208
<file>themes/default/rendererCategorizedSymbol.png</file>
208209
<file>themes/default/rendererGraduatedSymbol.png</file>

src/app/qgsoptions.cpp

100644100755
+4-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
6767
cmbSize->addItem( "32" );
6868

6969
QStringList styles = QStyleFactory::keys();
70-
foreach(QString style, styles )
70+
foreach( QString style, styles )
7171
{
7272
cmbStyle->addItem( style );
7373
}
@@ -182,6 +182,8 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
182182
cmbAttrTableBehaviour->addItem( tr( "Show features in current canvas" ) );
183183
cmbAttrTableBehaviour->setCurrentIndex( settings.value( "/qgis/attributeTableBehaviour", 0 ).toInt() );
184184

185+
spinBoxAttrTableRowCache->setValue( settings.value( "/qgis/attributeTableRowCache", 10000 ).toInt() );
186+
185187
// set the display update threshold
186188
spinBoxUpdateThreshold->setValue( settings.value( "/Map/updateThreshold" ).toInt() );
187189
//set the default projection behaviour radio buttongs
@@ -584,6 +586,7 @@ void QgsOptions::saveOptions()
584586
settings.setValue( "/qgis/showTips", cbxShowTips->isChecked() );
585587
settings.setValue( "/qgis/dockAttributeTable", cbxAttributeTableDocked->isChecked() );
586588
settings.setValue( "/qgis/attributeTableBehaviour", cmbAttrTableBehaviour->currentIndex() );
589+
settings.setValue( "/qgis/attributeTableRowCache", spinBoxAttrTableRowCache->value() );
587590
settings.setValue( "/qgis/dockIdentifyResults", cbxIdentifyResultsDocked->isChecked() );
588591
settings.setValue( "/qgis/dockSnapping", cbxSnappingOptionsDocked->isChecked() );
589592
settings.setValue( "/qgis/addPostgisDC", cbxAddPostgisDC->isChecked() );

src/gui/attributetable/qgsattributetablememorymodel.h

-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ class QgsAttributeTableMemoryModel : public QgsAttributeTableModel
8181
* Loads the layer into the model
8282
*/
8383
virtual void loadLayer();
84-
85-
QHash<QgsFeatureId, QgsFeature> mFeatureMap;
8684
};
8785

8886
#endif //QGSATTRIBUTETABLEMEMORYMODEL_H

src/gui/attributetable/qgsattributetablemodel.cpp

100644100755
+32-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayer *theLayer, QObjec
3333
: QAbstractTableModel( parent )
3434
{
3535
mFeat.setFeatureId( std::numeric_limits<int>::min() );
36+
mFeatureMap.clear();
37+
mFeatureQueue.clear();
38+
3639
mLayer = theLayer;
3740
loadAttributes();
3841

@@ -50,9 +53,33 @@ bool QgsAttributeTableModel::featureAtId( QgsFeatureId fid ) const
5053
QgsDebugMsgLevel( QString( "loading feature %1" ).arg( fid ), 3 );
5154

5255
if ( fid == std::numeric_limits<int>::min() )
56+
{
5357
return false;
58+
}
59+
else if ( mFeatureMap.contains( fid ) )
60+
{
61+
mFeat = mFeatureMap[ fid ];
62+
return true;
63+
}
64+
else if ( mLayer->featureAtId( fid, mFeat, false, true ) )
65+
{
66+
QSettings settings;
67+
int cacheSize = settings.value( "/qgis/attributeTableRowCache", "10000" ).toInt();
68+
69+
if ( mFeatureQueue.size() == cacheSize )
70+
{
71+
mFeatureMap.remove( mFeatureQueue.dequeue() );
72+
}
73+
74+
mFeatureQueue.enqueue( fid );
75+
mFeatureMap.insert( fid, mFeat );
76+
77+
return true;
78+
}
5479
else
55-
return mLayer->featureAtId( fid, mFeat, false, true );
80+
{
81+
return false;
82+
}
5683
}
5784

5885
void QgsAttributeTableModel::featureDeleted( QgsFeatureId fid )
@@ -152,6 +179,10 @@ void QgsAttributeTableModel::layerDeleted()
152179

153180
void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value )
154181
{
182+
if ( mFeatureMap.contains( fid ) )
183+
{
184+
mFeatureMap[ fid ].changeAttribute( fieldCol( idx ), value );
185+
}
155186
setData( index( idToRow( fid ), fieldCol( idx ) ), value, Qt::EditRole );
156187
}
157188

src/gui/attributetable/qgsattributetablemodel.h

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <QModelIndex>
2222
#include <QObject>
2323
#include <QHash>
24+
#include <QQueue>
2425

2526
#include "qgsfeature.h" // QgsAttributeMap
2627
#include "qgsvectorlayer.h" // QgsAttributeList
@@ -196,6 +197,7 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
196197
int mFieldCount;
197198

198199
mutable QgsFeature mFeat;
200+
mutable QHash<QgsFeatureId, QgsFeature> mFeatureMap;
199201

200202
QgsAttributeList mAttributes;
201203
QMap< int, const QMap<QString, QVariant> * > mValueMaps;
@@ -228,6 +230,10 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
228230
* @return feature exists
229231
*/
230232
virtual bool featureAtId( QgsFeatureId fid ) const;
233+
234+
private:
235+
mutable QQueue<QgsFeatureId> mFeatureQueue;
236+
231237
};
232238

233239

0 commit comments

Comments
 (0)