Navigation Menu

Skip to content

Commit

Permalink
[FEATURE] forward port r9689 to model based attribute table
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@13547 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed May 22, 2010
1 parent 331dee9 commit fbaf8cc
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/app/attributetable/qgsattributetablememorymodel.cpp
Expand Up @@ -60,6 +60,7 @@ bool QgsAttributeTableMemoryModel::featureAtId( int fid )
}
}

#if 0
void QgsAttributeTableMemoryModel::featureDeleted( int fid )
{
QgsDebugMsg( "entered." );
Expand All @@ -75,6 +76,7 @@ void QgsAttributeTableMemoryModel::featureAdded( int fid )
mFeatureMap.insert( fid, f );
QgsAttributeTableModel::featureAdded( fid );
}
#endif

void QgsAttributeTableMemoryModel::layerDeleted()
{
Expand Down
2 changes: 2 additions & 0 deletions src/app/attributetable/qgsattributetablememorymodel.h
Expand Up @@ -40,6 +40,7 @@ class QgsAttributeTableMemoryModel : public QgsAttributeTableModel
QgsAttributeTableMemoryModel( QgsVectorLayer *theLayer );

protected slots:
#if 0
/**
* Launched when a feature has been deleted
* @param fid feature id
Expand All @@ -50,6 +51,7 @@ class QgsAttributeTableMemoryModel : public QgsAttributeTableModel
* @param fid feature id
*/
virtual void featureAdded( int fid );
#endif
/**
* Launched when layer has been deleted
*/
Expand Down
62 changes: 41 additions & 21 deletions src/app/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgslogger.h"
#include "qgisapp.h"
#include "qgsattributeaction.h"
#include "qgsmapcanvas.h"

#include <QtGui>
#include <QVariant>
Expand All @@ -35,10 +36,8 @@ QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayer *theLayer, QObjec
{
mFeat.setFeatureId( std::numeric_limits<int>::min() );
mLayer = theLayer;
mFeatureCount = mLayer->pendingFeatureCount();
loadAttributes();


connect( mLayer, SIGNAL( layerModified( bool ) ), this, SLOT( layerModified( bool ) ) );
//connect(mLayer, SIGNAL(attributeValueChanged(int, int, const QVariant&)), this, SLOT( attributeValueChanged(int, int, const QVariant&)));
//connect(mLayer, SIGNAL(featureDeleted(int)), this, SLOT( featureDeleted(int)));
Expand All @@ -52,6 +51,7 @@ bool QgsAttributeTableModel::featureAtId( int fid ) const
return mLayer->featureAtId( fid, mFeat, false, true );
}

#if 0
void QgsAttributeTableModel::featureDeleted( int fid )
{
QgsDebugMsg( "entered." );
Expand Down Expand Up @@ -83,6 +83,7 @@ void QgsAttributeTableModel::featureAdded( int fid )
QgsDebugMsg( QString( "map sizes:%1, %2" ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ) );
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
}
#endif

void QgsAttributeTableModel::attributeAdded( int idx )
{
Expand Down Expand Up @@ -189,37 +190,56 @@ void QgsAttributeTableModel::loadLayer()
QgsFeature f;
bool ins = false, rm = false;

int previousSize = mRowIdMap.size();

mRowIdMap.clear();
mIdRowMap.clear();

int pendingFeatureCount = mLayer->pendingFeatureCount();
if ( mFeatureCount < pendingFeatureCount )
QSettings settings;
int behaviour = settings.value( "/qgis/attributeTableBehaviour", 0 ).toInt();

if ( behaviour == 1 )
{
QgsDebugMsg( "ins" );
ins = true;
beginInsertRows( QModelIndex(), mFeatureCount, pendingFeatureCount - 1 );
const QgsFeatureList &features = mLayer->selectedFeatures();

for ( int i = 0; i < features.size(); ++i )
{
mRowIdMap.insert( i, features[i].id() );
mIdRowMap.insert( features[i].id(), i );
}
}
else if ( mFeatureCount > pendingFeatureCount )
else
{
QgsDebugMsg( "rm" );
rm = true;
beginRemoveRows( QModelIndex(), pendingFeatureCount, mFeatureCount - 1 );
}
QgsRectangle rect;
if ( behaviour == 2 )
{
// current canvas only
rect = QgisApp::instance()->mapCanvas()->extent();
}

mLayer->select( mAttributes, QgsRectangle(), false );
mLayer->select( mAttributes, rect, false );

// preallocate data before inserting
mRowIdMap.reserve( pendingFeatureCount + 50 );
mIdRowMap.reserve( pendingFeatureCount + 50 );
for ( int i = 0; mLayer->nextFeature( f ); ++i )
{
mRowIdMap.insert( i, f.id() );
mIdRowMap.insert( f.id(), i );
}
}

for ( int i = 0; mLayer->nextFeature( f ); ++i )
if ( previousSize < mRowIdMap.size() )
{
mRowIdMap.insert( i, f.id() );
mIdRowMap.insert( f.id(), i );
QgsDebugMsg( "ins" );
ins = true;
beginInsertRows( QModelIndex(), previousSize, mRowIdMap.size() - 1 );
}
else if ( previousSize > mRowIdMap.size() )
{
QgsDebugMsg( "rm" );
rm = true;
beginRemoveRows( QModelIndex(), mRowIdMap.size(), previousSize - 1 );
}

// not needed when we have featureAdded signal
mFeatureCount = mLayer->pendingFeatureCount();
mFieldCount = mAttributes.size();

if ( ins )
Expand Down Expand Up @@ -287,7 +307,7 @@ int QgsAttributeTableModel::fieldIdx( int col ) const

int QgsAttributeTableModel::rowCount( const QModelIndex &parent ) const
{
return mFeatureCount;
return mRowIdMap.size();
}

int QgsAttributeTableModel::columnCount( const QModelIndex &parent ) const
Expand Down
3 changes: 2 additions & 1 deletion src/app/attributetable/qgsattributetablemodel.h
Expand Up @@ -166,6 +166,7 @@ class QgsAttributeTableModel: public QAbstractTableModel
virtual void layerModified( bool onlyGeometry );

protected slots:
#if 0
/**
* Launched when a feature has been deleted
* @param fid feature id
Expand All @@ -176,14 +177,14 @@ class QgsAttributeTableModel: public QAbstractTableModel
* @param fid feature id
*/
virtual void featureAdded( int fid );
#endif
/**
* Launched when layer has been deleted
*/
virtual void layerDeleted();

protected:
QgsVectorLayer *mLayer;
int mFeatureCount;
int mFieldCount;

mutable QgsFeature mFeat;
Expand Down
8 changes: 8 additions & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -142,6 +142,13 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
// set the current theme
cmbTheme->setItemText( cmbTheme->currentIndex(), settings.value( "/Themes" ).toString() );

// set the attribute table behaviour
cmbAttrTableBehaviour->clear();
cmbAttrTableBehaviour->addItem( tr( "Show all features" ) );
cmbAttrTableBehaviour->addItem( tr( "Show selected features" ) );
cmbAttrTableBehaviour->addItem( tr( "Show features in current canvas" ) );
cmbAttrTableBehaviour->setCurrentIndex( settings.value( "/qgis/attributeTableBehaviour", 0 ).toInt() );

// set the display update threshold
spinBoxUpdateThreshold->setValue( settings.value( "/Map/updateThreshold" ).toInt() );
//set the default projection behaviour radio buttongs
Expand Down Expand Up @@ -478,6 +485,7 @@ void QgsOptions::saveOptions()
settings.setValue( "/qgis/showLegendClassifiers", cbxLegendClassifiers->isChecked() );
settings.setValue( "/qgis/hideSplash", cbxHideSplash->isChecked() );
settings.setValue( "/qgis/dockAttributeTable", cbxAttributeTableDocked->isChecked() );
settings.setValue( "/qgis/attributeTableBehaviour", cmbAttrTableBehaviour->currentIndex() );
settings.setValue( "/qgis/dockIdentifyResults", cbxIdentifyResultsDocked->isChecked() );
settings.setValue( "/qgis/addPostgisDC", cbxAddPostgisDC->isChecked() );
settings.setValue( "/qgis/addNewLayersToCurrentGroup", cbxAddNewLayersToCurrentGroup->isChecked() );
Expand Down
42 changes: 42 additions & 0 deletions src/ui/qgsoptionsbase.ui
Expand Up @@ -414,6 +414,48 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="textLabel1_7">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Attribute table behaviour</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QComboBox" name="cmbAttrTableBehaviour">
<property name="duplicatesEnabled">
<bool>false</bool>
</property>
<item>
<property name="text">
<string/>
</property>
</item>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit fbaf8cc

Please sign in to comment.