2009-03-20 18:24:19 +00:00
|
|
|
/***************************************************************************
|
2009-04-13 09:44:20 +00:00
|
|
|
QgsAttributeTableModel.cpp
|
2009-03-20 18:24:19 +00:00
|
|
|
--------------------------------------
|
2009-03-27 17:37:59 +00:00
|
|
|
Date : Feb 2009
|
2009-03-20 18:24:19 +00:00
|
|
|
Copyright : (C) 2009 Vita Cizek
|
|
|
|
Email : weetya (at) gmail.com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
#include "qgsattributetablemodel.h"
|
2009-05-04 10:19:25 +00:00
|
|
|
#include "qgsattributetablefiltermodel.h"
|
2009-03-20 18:24:19 +00:00
|
|
|
|
|
|
|
#include "qgsfield.h"
|
|
|
|
#include "qgsvectorlayer.h"
|
2009-05-04 10:19:25 +00:00
|
|
|
#include "qgslogger.h"
|
2009-12-28 21:32:09 +00:00
|
|
|
#include "qgsattributeaction.h"
|
2010-05-22 15:45:49 +00:00
|
|
|
#include "qgsmapcanvas.h"
|
2012-10-26 18:29:00 +02:00
|
|
|
#include "qgsrendererv2.h"
|
2013-02-10 23:08:40 +01:00
|
|
|
#include "qgsmaplayerregistry.h"
|
|
|
|
#include "qgsexpression.h"
|
2009-03-20 18:24:19 +00:00
|
|
|
|
|
|
|
#include <QtGui>
|
|
|
|
#include <QVariant>
|
2011-05-13 22:00:46 +02:00
|
|
|
|
2012-01-07 23:47:36 +01:00
|
|
|
#include <limits>
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2013-02-20 12:14:02 +01:00
|
|
|
QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent )
|
2013-04-02 17:51:39 +02:00
|
|
|
: QAbstractTableModel( parent )
|
|
|
|
, mLayerCache( layerCache )
|
2009-03-27 17:37:59 +00:00
|
|
|
{
|
2011-11-30 01:01:22 +01:00
|
|
|
QgsDebugMsg( "entered." );
|
|
|
|
|
2013-04-02 18:25:39 +02:00
|
|
|
if ( layerCache->layer()->geometryType() == QGis::NoGeometry )
|
|
|
|
{
|
|
|
|
mFeatureRequest.setFlags( QgsFeatureRequest::NoGeometry );
|
|
|
|
}
|
|
|
|
|
2010-02-04 09:31:40 +00:00
|
|
|
mFeat.setFeatureId( std::numeric_limits<int>::min() );
|
2011-11-24 14:24:11 +01:00
|
|
|
|
2013-04-02 17:51:39 +02:00
|
|
|
if ( !layer()->hasGeometryType() )
|
|
|
|
mFeatureRequest.setFlags( QgsFeatureRequest::NoGeometry );
|
|
|
|
|
2009-10-28 13:27:15 +00:00
|
|
|
loadAttributes();
|
2009-09-19 22:56:53 +00:00
|
|
|
|
2013-02-20 12:14:02 +01:00
|
|
|
connect( layer(), SIGNAL( attributeValueChanged( QgsFeatureId, int, const QVariant& ) ), this, SLOT( attributeValueChanged( QgsFeatureId, int, const QVariant& ) ) );
|
|
|
|
connect( layer(), SIGNAL( featureAdded( QgsFeatureId ) ), this, SLOT( featureAdded( QgsFeatureId ) ) );
|
|
|
|
connect( layer(), SIGNAL( featureDeleted( QgsFeatureId ) ), this, SLOT( featureDeleted( QgsFeatureId ) ) );
|
|
|
|
connect( layer(), SIGNAL( attributeAdded( int ) ), this, SLOT( attributeAdded( int ) ) );
|
|
|
|
connect( layer(), SIGNAL( attributeDeleted( int ) ), this, SLOT( attributeDeleted( int ) ) );
|
2013-03-27 13:44:00 +01:00
|
|
|
connect( mLayerCache, SIGNAL( cachedLayerDeleted() ), this, SLOT( layerDeleted() ) );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2013-02-10 23:08:40 +01:00
|
|
|
QgsAttributeTableModel::~QgsAttributeTableModel()
|
|
|
|
{
|
2013-03-27 13:44:00 +01:00
|
|
|
if ( layer() )
|
2013-02-10 23:08:40 +01:00
|
|
|
{
|
2013-03-27 13:44:00 +01:00
|
|
|
const QgsFields& fields = layer()->pendingFields();
|
|
|
|
for ( int idx = 0; idx < fields.count(); ++idx )
|
|
|
|
{
|
|
|
|
if ( layer()->editType( idx ) != QgsVectorLayer::ValueRelation )
|
|
|
|
continue;
|
2013-02-10 23:08:40 +01:00
|
|
|
|
2013-03-27 13:44:00 +01:00
|
|
|
delete mValueMaps.take( idx );
|
|
|
|
}
|
2013-02-10 23:08:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 12:14:02 +01:00
|
|
|
bool QgsAttributeTableModel::loadFeatureAtId( QgsFeatureId fid ) const
|
2009-10-06 21:43:33 +00:00
|
|
|
{
|
2010-11-22 18:53:46 +00:00
|
|
|
QgsDebugMsgLevel( QString( "loading feature %1" ).arg( fid ), 3 );
|
2010-11-21 20:09:36 +00:00
|
|
|
|
|
|
|
if ( fid == std::numeric_limits<int>::min() )
|
2011-11-24 14:24:11 +01:00
|
|
|
{
|
2010-11-21 20:09:36 +00:00
|
|
|
return false;
|
2011-11-24 14:24:11 +01:00
|
|
|
}
|
|
|
|
|
2013-02-20 12:14:02 +01:00
|
|
|
return mLayerCache->featureAtId( fid, mFeat );
|
2009-10-06 21:43:33 +00:00
|
|
|
}
|
|
|
|
|
2011-06-16 02:24:26 +02:00
|
|
|
void QgsAttributeTableModel::featureDeleted( QgsFeatureId fid )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2010-11-22 18:53:46 +00:00
|
|
|
QgsDebugMsgLevel( QString( "deleted fid=%1 => row=%2" ).arg( fid ).arg( idToRow( fid ) ), 3 );
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2010-11-21 20:09:36 +00:00
|
|
|
int row = idToRow( fid );
|
|
|
|
|
|
|
|
beginRemoveRows( QModelIndex(), row, row );
|
|
|
|
removeRow( row );
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QgsAttributeTableModel::removeRows( int row, int count, const QModelIndex &parent )
|
|
|
|
{
|
2011-06-16 02:24:26 +02:00
|
|
|
Q_UNUSED( parent );
|
2010-11-23 23:02:56 +00:00
|
|
|
QgsDebugMsgLevel( QString( "remove %2 rows at %1 (rows %3, ids %4)" ).arg( row ).arg( count ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ), 3 );
|
2009-03-27 17:37:59 +00:00
|
|
|
|
2010-11-21 20:09:36 +00:00
|
|
|
// clean old references
|
|
|
|
for ( int i = row; i < row + count; i++ )
|
|
|
|
{
|
2010-11-23 23:02:56 +00:00
|
|
|
mIdRowMap.remove( mRowIdMap[ i ] );
|
|
|
|
mRowIdMap.remove( i );
|
2010-11-21 20:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update maps
|
|
|
|
int n = mRowIdMap.size() + count;
|
|
|
|
for ( int i = row + count; i < n; i++ )
|
|
|
|
{
|
2011-06-16 02:24:26 +02:00
|
|
|
QgsFeatureId id = mRowIdMap[i];
|
2010-11-21 20:09:36 +00:00
|
|
|
mIdRowMap[ id ] -= count;
|
|
|
|
mRowIdMap[ i-count ] = id;
|
|
|
|
mRowIdMap.remove( i );
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef QGISDEBUG
|
2010-11-23 23:02:56 +00:00
|
|
|
QgsDebugMsgLevel( QString( "after removal rows %1, ids %2" ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ), 4 );
|
|
|
|
QgsDebugMsgLevel( "id->row", 4 );
|
2011-06-16 02:24:26 +02:00
|
|
|
for ( QHash<QgsFeatureId, int>::iterator it = mIdRowMap.begin(); it != mIdRowMap.end(); ++it )
|
|
|
|
QgsDebugMsgLevel( QString( "%1->%2" ).arg( FID_TO_STRING( it.key() ) ).arg( *it ), 4 );
|
|
|
|
|
|
|
|
QHash<QgsFeatureId, int>::iterator idit;
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2010-11-22 18:53:46 +00:00
|
|
|
QgsDebugMsgLevel( "row->id", 4 );
|
2011-06-16 02:24:26 +02:00
|
|
|
for ( QHash<int, QgsFeatureId>::iterator it = mRowIdMap.begin(); it != mRowIdMap.end(); ++it )
|
|
|
|
QgsDebugMsgLevel( QString( "%1->%2" ).arg( it.key() ).arg( FID_TO_STRING( *it ) ), 4 );
|
2010-11-21 20:09:36 +00:00
|
|
|
#endif
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2010-11-23 23:02:56 +00:00
|
|
|
Q_ASSERT( mRowIdMap.size() == mIdRowMap.size() );
|
|
|
|
|
2010-11-21 20:09:36 +00:00
|
|
|
return true;
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
2009-03-27 17:37:59 +00:00
|
|
|
|
2011-06-16 02:24:26 +02:00
|
|
|
void QgsAttributeTableModel::featureAdded( QgsFeatureId fid, bool newOperation )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2013-01-09 16:46:30 +01:00
|
|
|
QgsDebugMsgLevel( QString( "feature %1 added (%2, rows %3, ids %4)" ).arg( fid ).arg( newOperation ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ), 4 );
|
2010-11-21 20:09:36 +00:00
|
|
|
|
|
|
|
int n = mRowIdMap.size();
|
|
|
|
if ( newOperation )
|
|
|
|
beginInsertRows( QModelIndex(), n, n );
|
|
|
|
|
|
|
|
mIdRowMap.insert( fid, n );
|
|
|
|
mRowIdMap.insert( n, fid );
|
|
|
|
|
|
|
|
if ( newOperation )
|
|
|
|
endInsertRows();
|
|
|
|
|
|
|
|
reload( index( rowCount() - 1, 0 ), index( rowCount() - 1, columnCount() ) );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
2009-03-27 17:37:59 +00:00
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::attributeAdded( int idx )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2011-06-16 02:24:26 +02:00
|
|
|
Q_UNUSED( idx );
|
2010-11-21 20:09:36 +00:00
|
|
|
QgsDebugMsg( "entered." );
|
2010-11-29 20:40:58 +00:00
|
|
|
loadAttributes();
|
|
|
|
loadLayer();
|
2009-03-20 18:24:19 +00:00
|
|
|
emit modelChanged();
|
|
|
|
}
|
2009-03-27 17:37:59 +00:00
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::attributeDeleted( int idx )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2011-06-16 02:24:26 +02:00
|
|
|
Q_UNUSED( idx );
|
2010-11-21 20:09:36 +00:00
|
|
|
QgsDebugMsg( "entered." );
|
2010-11-29 20:40:58 +00:00
|
|
|
loadAttributes();
|
2009-03-20 18:24:19 +00:00
|
|
|
emit modelChanged();
|
|
|
|
}
|
2009-03-27 17:37:59 +00:00
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::layerDeleted()
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( "entered." );
|
|
|
|
|
2010-11-21 20:09:36 +00:00
|
|
|
beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
|
|
|
|
removeRows( 0, rowCount() );
|
|
|
|
endRemoveRows();
|
2013-03-27 13:44:00 +01:00
|
|
|
|
|
|
|
const QgsFields& fields = layer()->pendingFields();
|
|
|
|
for ( int idx = 0; idx < fields.count(); ++idx )
|
|
|
|
{
|
|
|
|
if ( layer()->editType( idx ) != QgsVectorLayer::ValueRelation )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
delete mValueMaps.take( idx );
|
|
|
|
}
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2011-06-16 02:24:26 +02:00
|
|
|
void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2013-04-08 15:38:08 +02:00
|
|
|
if ( fid == mFeat.id() )
|
|
|
|
{
|
|
|
|
mFeat.setValid( false );
|
|
|
|
}
|
2011-02-16 23:11:04 +00:00
|
|
|
setData( index( idToRow( fid ), fieldCol( idx ) ), value, Qt::EditRole );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2009-10-28 13:27:15 +00:00
|
|
|
void QgsAttributeTableModel::loadAttributes()
|
|
|
|
{
|
2013-02-20 12:14:02 +01:00
|
|
|
if ( !layer() )
|
2009-10-28 13:27:15 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ins = false, rm = false;
|
|
|
|
|
2010-04-14 16:47:16 +00:00
|
|
|
QgsAttributeList attributes;
|
2013-02-20 12:14:02 +01:00
|
|
|
const QgsFields& fields = layer()->pendingFields();
|
2012-10-20 22:19:55 +02:00
|
|
|
for ( int idx = 0; idx < fields.count(); ++idx )
|
2009-10-28 13:27:15 +00:00
|
|
|
{
|
2013-02-20 12:14:02 +01:00
|
|
|
switch ( layer()->editType( idx ) )
|
2009-10-28 13:27:15 +00:00
|
|
|
{
|
|
|
|
case QgsVectorLayer::Hidden:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case QgsVectorLayer::ValueMap:
|
2013-02-20 12:14:02 +01:00
|
|
|
mValueMaps.insert( idx, &layer()->valueMap( idx ) );
|
2009-10-28 13:27:15 +00:00
|
|
|
break;
|
|
|
|
|
2013-02-10 23:08:40 +01:00
|
|
|
case QgsVectorLayer::ValueRelation:
|
|
|
|
{
|
2013-02-20 12:14:02 +01:00
|
|
|
const QgsVectorLayer::ValueRelationData &data = layer()->valueRelation( idx );
|
2013-02-10 23:08:40 +01:00
|
|
|
|
|
|
|
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( data.mLayer ) );
|
|
|
|
if ( !layer )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int ki = layer->fieldNameIndex( data.mKey );
|
|
|
|
int vi = layer->fieldNameIndex( data.mValue );
|
|
|
|
|
|
|
|
QgsExpression *e = 0;
|
|
|
|
if ( !data.mFilterExpression.isEmpty() )
|
|
|
|
{
|
|
|
|
e = new QgsExpression( data.mFilterExpression );
|
|
|
|
if ( e->hasParserError() || !e->prepare( layer->pendingFields() ) )
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ki >= 0 && vi >= 0 )
|
|
|
|
{
|
|
|
|
QSet<int> attributes;
|
|
|
|
attributes << ki << vi;
|
|
|
|
|
|
|
|
QgsFeatureRequest::Flag flags = QgsFeatureRequest::NoGeometry;
|
|
|
|
|
|
|
|
if ( e )
|
|
|
|
{
|
|
|
|
if ( e->needsGeometry() )
|
|
|
|
flags = QgsFeatureRequest::NoFlags;
|
|
|
|
|
|
|
|
foreach ( const QString &field, e->referencedColumns() )
|
|
|
|
{
|
|
|
|
int idx = layer->fieldNameIndex( field );
|
|
|
|
if ( idx < 0 )
|
|
|
|
continue;
|
|
|
|
attributes << idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QMap< QString, QVariant > *map = new QMap< QString, QVariant >();
|
|
|
|
|
|
|
|
QgsFeatureIterator fit = layer->getFeatures( QgsFeatureRequest().setFlags( flags ).setSubsetOfAttributes( attributes.toList() ) );
|
|
|
|
QgsFeature f;
|
|
|
|
while ( fit.nextFeature( f ) )
|
|
|
|
{
|
|
|
|
if ( e && !e->evaluate( &f ).toBool() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
map->insert( f.attribute( vi ).toString(), f.attribute( ki ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
mValueMaps.insert( idx, map );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-10-28 13:27:15 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-10-20 22:19:55 +02:00
|
|
|
attributes << idx;
|
2009-10-28 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-12 02:21:17 +01:00
|
|
|
if ( columnCount() < attributes.size() )
|
2010-04-14 16:47:16 +00:00
|
|
|
{
|
|
|
|
ins = true;
|
2012-01-12 02:21:17 +01:00
|
|
|
beginInsertColumns( QModelIndex(), columnCount(), attributes.size() - 1 );
|
2010-04-14 16:47:16 +00:00
|
|
|
}
|
2012-01-12 02:21:17 +01:00
|
|
|
else if ( attributes.size() < columnCount() )
|
2010-04-14 16:47:16 +00:00
|
|
|
{
|
|
|
|
rm = true;
|
2012-01-12 02:21:17 +01:00
|
|
|
beginRemoveColumns( QModelIndex(), attributes.size(), columnCount() - 1 );
|
2010-04-14 16:47:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mFieldCount = attributes.size();
|
|
|
|
mAttributes = attributes;
|
2013-04-11 08:52:10 -03:00
|
|
|
|
2009-10-28 13:27:15 +00:00
|
|
|
if ( ins )
|
|
|
|
{
|
|
|
|
endInsertColumns();
|
|
|
|
}
|
|
|
|
else if ( rm )
|
|
|
|
{
|
|
|
|
endRemoveColumns();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::loadLayer()
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( "entered." );
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2010-11-21 20:09:36 +00:00
|
|
|
beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
|
|
|
|
removeRows( 0, rowCount() );
|
|
|
|
endRemoveRows();
|
2010-05-22 15:45:49 +00:00
|
|
|
|
2013-04-02 18:25:39 +02:00
|
|
|
QgsFeatureIterator features = mLayerCache->getFeatures( mFeatureRequest );
|
2013-02-20 12:14:02 +01:00
|
|
|
|
2012-01-07 23:47:36 +01:00
|
|
|
int i = 0;
|
2010-05-22 15:45:49 +00:00
|
|
|
|
2012-01-17 14:50:14 +01:00
|
|
|
QTime t;
|
|
|
|
t.start();
|
|
|
|
|
2013-02-20 12:14:02 +01:00
|
|
|
QgsFeature feat;
|
|
|
|
while ( features.nextFeature( feat ) )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2013-02-20 12:14:02 +01:00
|
|
|
++i;
|
2012-10-26 18:29:00 +02:00
|
|
|
|
2013-02-20 12:14:02 +01:00
|
|
|
if ( t.elapsed() > 1000 )
|
2010-05-22 15:45:49 +00:00
|
|
|
{
|
2013-02-20 12:14:02 +01:00
|
|
|
bool cancel = false;
|
|
|
|
emit( progress( i, cancel ) );
|
|
|
|
if ( cancel )
|
|
|
|
break;
|
2012-11-01 22:50:40 +01:00
|
|
|
|
2013-02-20 12:14:02 +01:00
|
|
|
t.restart();
|
2012-11-01 22:50:40 +01:00
|
|
|
}
|
2013-02-20 12:14:02 +01:00
|
|
|
featureAdded( feat.id() );
|
2010-05-22 15:45:49 +00:00
|
|
|
}
|
2009-07-21 15:28:02 +00:00
|
|
|
|
2013-02-20 12:14:02 +01:00
|
|
|
emit finished();
|
|
|
|
|
2009-09-19 22:56:53 +00:00
|
|
|
mFieldCount = mAttributes.size();
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2011-06-16 02:24:26 +02:00
|
|
|
void QgsAttributeTableModel::swapRows( QgsFeatureId a, QgsFeatureId b )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( a == b )
|
2009-03-20 18:24:19 +00:00
|
|
|
return;
|
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
int rowA = idToRow( a );
|
|
|
|
int rowB = idToRow( b );
|
2009-03-20 18:24:19 +00:00
|
|
|
|
|
|
|
//emit layoutAboutToBeChanged();
|
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
mRowIdMap.remove( rowA );
|
|
|
|
mRowIdMap.remove( rowB );
|
|
|
|
mRowIdMap.insert( rowA, b );
|
|
|
|
mRowIdMap.insert( rowB, a );
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
mIdRowMap.remove( a );
|
|
|
|
mIdRowMap.remove( b );
|
|
|
|
mIdRowMap.insert( a, rowB );
|
|
|
|
mIdRowMap.insert( b, rowA );
|
2009-03-20 18:24:19 +00:00
|
|
|
|
|
|
|
//emit layoutChanged();
|
|
|
|
}
|
|
|
|
|
2011-06-16 02:24:26 +02:00
|
|
|
int QgsAttributeTableModel::idToRow( QgsFeatureId id ) const
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( !mIdRowMap.contains( id ) )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-07-21 13:19:10 +00:00
|
|
|
QgsDebugMsg( QString( "idToRow: id %1 not in the map" ).arg( id ) );
|
2009-03-20 18:24:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mIdRowMap[id];
|
|
|
|
}
|
|
|
|
|
2013-02-20 12:14:02 +01:00
|
|
|
QModelIndex QgsAttributeTableModel::idToIndex( QgsFeatureId id ) const
|
|
|
|
{
|
|
|
|
return index( idToRow( id ), 0 );
|
|
|
|
}
|
|
|
|
|
2011-06-16 02:24:26 +02:00
|
|
|
QgsFeatureId QgsAttributeTableModel::rowToId( const int row ) const
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2011-06-16 02:24:26 +02:00
|
|
|
if ( !mRowIdMap.contains( row ) )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2011-06-16 02:24:26 +02:00
|
|
|
QgsDebugMsg( QString( "rowToId: row %1 not in the map" ).arg( row ) );
|
2009-07-21 15:28:02 +00:00
|
|
|
// return negative infinite (to avoid collision with newly added features)
|
2010-02-04 09:31:40 +00:00
|
|
|
return std::numeric_limits<int>::min();
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2011-06-16 02:24:26 +02:00
|
|
|
return mRowIdMap[row];
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2009-08-15 22:28:06 +00:00
|
|
|
int QgsAttributeTableModel::fieldIdx( int col ) const
|
|
|
|
{
|
|
|
|
return mAttributes[ col ];
|
|
|
|
}
|
|
|
|
|
2011-02-16 23:11:04 +00:00
|
|
|
int QgsAttributeTableModel::fieldCol( int idx ) const
|
|
|
|
{
|
|
|
|
return mAttributes.indexOf( idx );
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
int QgsAttributeTableModel::rowCount( const QModelIndex &parent ) const
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2011-06-16 02:24:26 +02:00
|
|
|
Q_UNUSED( parent );
|
2010-05-22 15:45:49 +00:00
|
|
|
return mRowIdMap.size();
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
int QgsAttributeTableModel::columnCount( const QModelIndex &parent ) const
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2011-06-16 02:24:26 +02:00
|
|
|
Q_UNUSED( parent );
|
2012-01-12 02:21:17 +01:00
|
|
|
return qMax( 1, mFieldCount ); // if there are zero columns all model indices will be considered invalid
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orientation, int role ) const
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2013-03-27 13:44:00 +01:00
|
|
|
if ( !layer() )
|
|
|
|
return QVariant();
|
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( role == Qt::DisplayRole )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( orientation == Qt::Vertical ) //row
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
return QVariant( section );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
2012-06-29 21:33:52 +02:00
|
|
|
else if ( section >= 0 && section < mFieldCount )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2013-02-20 12:14:02 +01:00
|
|
|
QString attributeName = layer()->attributeAlias( mAttributes[section] );
|
2009-07-01 22:38:49 +00:00
|
|
|
if ( attributeName.isEmpty() )
|
2009-07-01 07:29:08 +00:00
|
|
|
{
|
2013-02-20 12:14:02 +01:00
|
|
|
QgsField field = layer()->pendingFields()[ mAttributes[section] ];
|
2009-07-01 07:29:08 +00:00
|
|
|
attributeName = field.name();
|
|
|
|
}
|
2009-07-01 22:38:49 +00:00
|
|
|
return QVariant( attributeName );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
2012-01-12 02:21:17 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return tr( "feature id" );
|
|
|
|
}
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
2009-12-28 21:32:09 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return QVariant();
|
|
|
|
}
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) const
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2013-03-27 16:41:16 +01:00
|
|
|
if ( !index.isValid() || ( role != Qt::TextAlignmentRole && role != Qt::DisplayRole && role != Qt::EditRole && role != SortRole ) )
|
2009-03-20 18:24:19 +00:00
|
|
|
return QVariant();
|
|
|
|
|
2012-01-12 02:21:17 +01:00
|
|
|
QgsFeatureId rowId = rowToId( index.row() );
|
|
|
|
|
|
|
|
if ( index.column() >= mFieldCount )
|
|
|
|
return role == Qt::DisplayRole ? rowId : QVariant();
|
|
|
|
|
2010-02-04 00:18:15 +00:00
|
|
|
int fieldId = mAttributes[ index.column()];
|
2013-03-26 11:43:43 +01:00
|
|
|
const QgsField& field = layer()->pendingFields()[ fieldId ];
|
2010-02-04 00:18:15 +00:00
|
|
|
|
2013-03-16 16:52:11 +01:00
|
|
|
QVariant::Type fldType = field.type();
|
2009-03-27 17:37:59 +00:00
|
|
|
bool fldNumeric = ( fldType == QVariant::Int || fldType == QVariant::Double );
|
|
|
|
|
|
|
|
if ( role == Qt::TextAlignmentRole )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( fldNumeric )
|
|
|
|
return QVariant( Qt::AlignRight );
|
2009-03-20 18:24:19 +00:00
|
|
|
else
|
2009-03-27 17:37:59 +00:00
|
|
|
return QVariant( Qt::AlignLeft );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
2009-03-27 17:37:59 +00:00
|
|
|
|
2009-03-20 18:24:19 +00:00
|
|
|
// if we don't have the row in current cache, load it from layer first
|
2013-04-08 15:38:08 +02:00
|
|
|
if ( mFeat.id() != rowId || !mFeat.isValid() )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2013-02-20 12:14:02 +01:00
|
|
|
if ( !loadFeatureAtId( rowId ) )
|
2009-03-27 17:37:59 +00:00
|
|
|
return QVariant( "ERROR" );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
2009-03-27 17:37:59 +00:00
|
|
|
|
2010-02-04 00:18:15 +00:00
|
|
|
if ( mFeat.id() != rowId )
|
2009-03-28 02:02:00 +00:00
|
|
|
return QVariant( "ERROR" );
|
|
|
|
|
2012-10-19 00:31:03 +02:00
|
|
|
const QVariant &val = mFeat.attribute( fieldId );
|
2009-03-27 17:37:59 +00:00
|
|
|
|
|
|
|
if ( val.isNull() )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
|
|
|
// if the value is NULL, show that in table, but don't show "NULL" text in editor
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( role == Qt::EditRole )
|
2009-08-15 22:28:06 +00:00
|
|
|
{
|
|
|
|
return QVariant( fldType );
|
|
|
|
}
|
2009-03-20 18:24:19 +00:00
|
|
|
else
|
2009-08-15 22:28:06 +00:00
|
|
|
{
|
2011-01-17 21:51:58 +00:00
|
|
|
QSettings settings;
|
|
|
|
return settings.value( "qgis/nullValue", "NULL" );
|
2009-08-15 22:28:06 +00:00
|
|
|
}
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
2009-03-27 17:37:59 +00:00
|
|
|
|
2013-03-13 23:30:14 +01:00
|
|
|
if ( role == Qt::DisplayRole )
|
2009-09-19 22:56:53 +00:00
|
|
|
{
|
2013-03-13 23:30:14 +01:00
|
|
|
if ( mValueMaps.contains( fieldId ) )
|
|
|
|
{
|
|
|
|
return mValueMaps[ fieldId ]->key( val.toString(), QString( "(%1)" ).arg( val.toString() ) );
|
|
|
|
}
|
|
|
|
|
2013-03-26 11:43:43 +01:00
|
|
|
if ( layer()->editType( fieldId ) == QgsVectorLayer::Calendar && val.canConvert( QVariant::Date ) )
|
2013-03-13 23:30:14 +01:00
|
|
|
{
|
2013-03-26 11:43:43 +01:00
|
|
|
return val.toDate().toString( layer()->dateFormat( fieldId ) );
|
2013-03-13 23:30:14 +01:00
|
|
|
}
|
2009-09-19 22:56:53 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 16:41:16 +01:00
|
|
|
if ( role == SortRole )
|
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2013-03-16 16:52:11 +01:00
|
|
|
return field.displayString( val );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
bool QgsAttributeTableModel::setData( const QModelIndex &index, const QVariant &value, int role )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2013-02-20 12:14:02 +01:00
|
|
|
Q_UNUSED( value )
|
2012-01-02 18:30:09 +01:00
|
|
|
|
2013-02-20 12:14:02 +01:00
|
|
|
if ( !index.isValid() || index.column() >= mFieldCount || role != Qt::EditRole || !layer()->isEditable() )
|
|
|
|
return false;
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2013-02-20 12:14:02 +01:00
|
|
|
if ( !layer()->isModified() )
|
2009-03-20 18:24:19 +00:00
|
|
|
return false;
|
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
emit dataChanged( index, index );
|
2010-02-04 00:18:15 +00:00
|
|
|
|
2009-03-20 18:24:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
Qt::ItemFlags QgsAttributeTableModel::flags( const QModelIndex &index ) const
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( !index.isValid() )
|
2009-03-20 18:24:19 +00:00
|
|
|
return Qt::ItemIsEnabled;
|
|
|
|
|
2012-01-12 02:21:17 +01:00
|
|
|
if ( index.column() >= mFieldCount )
|
|
|
|
return Qt::NoItemFlags;
|
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
Qt::ItemFlags flags = QAbstractItemModel::flags( index );
|
|
|
|
|
2013-02-20 12:14:02 +01:00
|
|
|
if ( layer()->isEditable() &&
|
|
|
|
layer()->editType( mAttributes[ index.column()] ) != QgsVectorLayer::Immutable )
|
2009-03-20 18:24:19 +00:00
|
|
|
flags |= Qt::ItemIsEditable;
|
2009-03-27 17:37:59 +00:00
|
|
|
|
2009-03-20 18:24:19 +00:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::reload( const QModelIndex &index1, const QModelIndex &index2 )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2012-01-04 15:54:27 +01:00
|
|
|
for ( int row = index1.row(); row <= index2.row(); row++ )
|
2012-01-02 18:30:09 +01:00
|
|
|
{
|
|
|
|
QgsFeatureId fid = rowToId( row );
|
2013-02-20 12:14:02 +01:00
|
|
|
mLayerCache->removeCachedFeature( fid );
|
2012-01-02 18:30:09 +01:00
|
|
|
}
|
|
|
|
|
2010-10-18 00:40:19 +00:00
|
|
|
mFeat.setFeatureId( std::numeric_limits<int>::min() );
|
2009-03-27 17:37:59 +00:00
|
|
|
emit dataChanged( index1, index2 );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::resetModel()
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2009-12-28 21:32:09 +00:00
|
|
|
void QgsAttributeTableModel::executeAction( int action, const QModelIndex &idx ) const
|
|
|
|
{
|
2012-01-11 15:27:15 +01:00
|
|
|
QgsFeature f = feature( idx );
|
2013-02-20 12:14:02 +01:00
|
|
|
layer()->actions()->doAction( action, f, fieldIdx( idx.column() ) );
|
2009-12-28 21:32:09 +00:00
|
|
|
}
|
2010-11-21 20:09:36 +00:00
|
|
|
|
2012-01-11 15:27:15 +01:00
|
|
|
QgsFeature QgsAttributeTableModel::feature( const QModelIndex &idx ) const
|
2010-11-21 20:09:36 +00:00
|
|
|
{
|
|
|
|
QgsFeature f;
|
2013-01-23 01:11:14 +01:00
|
|
|
f.initAttributes( mAttributes.size() );
|
2011-01-17 21:51:58 +00:00
|
|
|
f.setFeatureId( rowToId( idx.row() ) );
|
2010-11-21 20:09:36 +00:00
|
|
|
for ( int i = 0; i < mAttributes.size(); i++ )
|
|
|
|
{
|
2012-10-19 00:31:03 +02:00
|
|
|
f.setAttribute( mAttributes[i], data( index( idx.row(), i ), Qt::EditRole ) );
|
2010-11-21 20:09:36 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 22:00:46 +02:00
|
|
|
return f;
|
2010-11-21 20:09:36 +00:00
|
|
|
}
|