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-03-20 18:24:19 +00:00
|
|
|
|
|
|
|
#include <QtGui>
|
|
|
|
#include <QVariant>
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
////////////////////////////
|
|
|
|
// QgsAttributeTableModel //
|
|
|
|
////////////////////////////
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayer *theLayer, QObject *parent )
|
2009-03-27 17:37:59 +00:00
|
|
|
: QAbstractTableModel( parent )
|
|
|
|
{
|
2009-03-20 18:24:19 +00:00
|
|
|
mLastRowId = -1;
|
|
|
|
mLastRow = NULL;
|
|
|
|
mLayer = theLayer;
|
|
|
|
mFeatureCount = mLayer->pendingFeatureCount();
|
2009-10-28 13:27:15 +00:00
|
|
|
loadAttributes();
|
2009-09-19 22:56:53 +00:00
|
|
|
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
connect( mLayer, SIGNAL( layerModified( bool ) ), this, SLOT( layerModified( bool ) ) );
|
2009-03-20 18:24:19 +00:00
|
|
|
//connect(mLayer, SIGNAL(attributeAdded(int)), this, SLOT( attributeAdded(int)));
|
|
|
|
//connect(mLayer, SIGNAL(attributeDeleted(int)), this, SLOT( attributeDeleted(int)));
|
|
|
|
//connect(mLayer, SIGNAL(attributeValueChanged(int, int, const QVariant&)), this, SLOT( attributeValueChanged(int, int, const QVariant&)));
|
|
|
|
//connect(mLayer, SIGNAL(featureDeleted(int)), this, SLOT( featureDeleted(int)));
|
|
|
|
//connect(mLayer, SIGNAL(featureAdded(int)), this, SLOT( featureAdded(int)));
|
2009-03-27 17:37:59 +00:00
|
|
|
|
2009-03-20 18:24:19 +00:00
|
|
|
loadLayer();
|
|
|
|
}
|
|
|
|
|
2009-10-06 21:43:33 +00:00
|
|
|
bool QgsAttributeTableModel::featureAtId( int fid )
|
|
|
|
{
|
|
|
|
return mLayer->featureAtId( fid, mFeat, false, true );
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::featureDeleted( int fid )
|
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
|
|
|
|
2009-03-28 02:02:00 +00:00
|
|
|
#ifdef QGISDEBUG
|
2009-03-20 18:24:19 +00:00
|
|
|
int idx = mIdRowMap[fid];
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( idx );
|
|
|
|
QgsDebugMsg( fid );
|
2009-03-28 02:02:00 +00:00
|
|
|
#endif
|
2009-03-27 17:37:59 +00:00
|
|
|
|
|
|
|
QgsDebugMsg( "id->row" );
|
2009-07-21 15:28:02 +00:00
|
|
|
QHash<int, int>::iterator it;
|
2009-03-27 17:37:59 +00:00
|
|
|
for ( it = mIdRowMap.begin(); it != mIdRowMap.end(); ++it )
|
|
|
|
QgsDebugMsg( QString( "%1->%2" ).arg( it.key() ).arg( *it ) );
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( "row->id" );
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
for ( it = mRowIdMap.begin(); it != mRowIdMap.end(); ++it )
|
|
|
|
QgsDebugMsg( QString( "%1->%2" ).arg( it.key() ).arg( *it ) );
|
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::featureAdded( int fid )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( "BM feature added" );
|
2009-03-20 18:24:19 +00:00
|
|
|
++mFeatureCount;
|
2009-03-27 17:37:59 +00:00
|
|
|
mIdRowMap.insert( fid, mFeatureCount - 1 );
|
|
|
|
mRowIdMap.insert( mFeatureCount - 1, fid );
|
|
|
|
QgsDebugMsg( QString( "map sizes:%1, %2" ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ) );
|
|
|
|
reload( index( 0, 0 ), index( rowCount(), 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
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( "BM attribute added" );
|
2009-03-20 18:24:19 +00:00
|
|
|
loadLayer();
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( QString( "map sizes:%1, %2" ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ) );
|
|
|
|
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
|
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
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( "BM attribute deleted" );
|
2009-03-20 18:24:19 +00:00
|
|
|
loadLayer();
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( QString( "map sizes:%1, %2" ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ) );
|
|
|
|
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
|
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." );
|
2009-03-20 18:24:19 +00:00
|
|
|
mIdRowMap.clear();
|
|
|
|
mRowIdMap.clear();
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( QString( "map sizes:%1, %2" ).arg( mRowIdMap.size() ).arg( mIdRowMap.size() ) );
|
|
|
|
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
2009-03-27 17:37:59 +00:00
|
|
|
|
2009-03-20 18:24:19 +00:00
|
|
|
//TODO: check whether caching in data()/setData() doesn't cache old value
|
2009-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::attributeValueChanged( int fid, int idx, const QVariant &value )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( "entered." );
|
|
|
|
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::layerModified( bool onlyGeometry )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( onlyGeometry )
|
2009-03-20 18:24:19 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-28 13:27:15 +00:00
|
|
|
loadAttributes();
|
2009-03-20 18:24:19 +00:00
|
|
|
loadLayer();
|
|
|
|
emit modelChanged();
|
2009-07-23 11:11:41 +00:00
|
|
|
emit headerDataChanged( Qt::Horizontal, 0, columnCount() - 1 );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2009-10-28 13:27:15 +00:00
|
|
|
void QgsAttributeTableModel::loadAttributes()
|
|
|
|
{
|
|
|
|
if ( !mLayer )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ins = false, rm = false;
|
|
|
|
int pendingFieldCount = mLayer->pendingFields().size();
|
|
|
|
|
|
|
|
if ( mFieldCount < pendingFieldCount )
|
|
|
|
{
|
|
|
|
ins = true;
|
|
|
|
beginInsertColumns( QModelIndex(), mFieldCount, pendingFieldCount - 1 );
|
|
|
|
}
|
|
|
|
else if ( pendingFieldCount < mFieldCount )
|
|
|
|
{
|
|
|
|
rm = true;
|
|
|
|
beginRemoveColumns( QModelIndex(), pendingFieldCount, mFieldCount - 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
mFieldCount = 0;
|
|
|
|
mAttributes.clear();
|
|
|
|
mValueMaps.clear();
|
|
|
|
|
|
|
|
for ( QgsFieldMap::const_iterator it = mLayer->pendingFields().constBegin(); it != mLayer->pendingFields().end(); it++ )
|
|
|
|
{
|
|
|
|
switch ( mLayer->editType( it.key() ) )
|
|
|
|
{
|
|
|
|
case QgsVectorLayer::Hidden:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case QgsVectorLayer::ValueMap:
|
|
|
|
mValueMaps.insert( it.key(), &mLayer->valueMap( it.key() ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFieldCount++;
|
|
|
|
mAttributes << it.key();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
QgsFeature f;
|
|
|
|
bool ins = false, rm = false;
|
|
|
|
|
|
|
|
mRowIdMap.clear();
|
|
|
|
mIdRowMap.clear();
|
|
|
|
|
2009-07-21 13:19:10 +00:00
|
|
|
int pendingFeatureCount = mLayer->pendingFeatureCount();
|
2009-07-23 11:11:41 +00:00
|
|
|
if ( mFeatureCount < pendingFeatureCount )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( "ins" );
|
|
|
|
ins = true;
|
2009-07-21 13:19:10 +00:00
|
|
|
beginInsertRows( QModelIndex(), mFeatureCount, pendingFeatureCount - 1 );
|
2009-03-27 17:37:59 +00:00
|
|
|
// QgsDebugMsg(QString("%1, %2").arg(mFeatureCount).arg(mLayer->pendingFeatureCount() - 1));
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
2009-07-21 13:19:10 +00:00
|
|
|
else if ( mFeatureCount > pendingFeatureCount )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( "rm" );
|
|
|
|
rm = true;
|
2009-07-21 13:19:10 +00:00
|
|
|
beginRemoveRows( QModelIndex(), pendingFeatureCount, mFeatureCount - 1 );
|
2009-03-27 17:37:59 +00:00
|
|
|
// QgsDebugMsg(QString("%1, %2").arg(mFeatureCount).arg(mLayer->pendingFeatureCount() -1));
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2009-09-19 22:56:53 +00:00
|
|
|
mLayer->select( mAttributes, QgsRectangle(), false );
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2009-07-21 15:28:02 +00:00
|
|
|
// preallocate data before inserting
|
2009-07-23 11:11:41 +00:00
|
|
|
mRowIdMap.reserve( pendingFeatureCount + 50 );
|
|
|
|
mIdRowMap.reserve( pendingFeatureCount + 50 );
|
2009-07-21 15:28:02 +00:00
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
for ( int i = 0; mLayer->nextFeature( f ); ++i )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
mRowIdMap.insert( i, f.id() );
|
|
|
|
mIdRowMap.insert( f.id(), i );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// not needed when we have featureAdded signal
|
2009-07-21 13:19:10 +00:00
|
|
|
mFeatureCount = pendingFeatureCount;
|
2009-09-19 22:56:53 +00:00
|
|
|
mFieldCount = mAttributes.size();
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( ins )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
|
|
|
endInsertRows();
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( "end ins" );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
2009-03-27 17:37:59 +00:00
|
|
|
else if ( rm )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
|
|
|
endRemoveRows();
|
2009-03-27 17:37:59 +00:00
|
|
|
QgsDebugMsg( "end rm" );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::swapRows( int a, int 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();
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
int QgsAttributeTableModel::idToRow( const int 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];
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
int QgsAttributeTableModel::rowToId( const int id ) const
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( !mRowIdMap.contains( id ) )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-07-21 13:19:10 +00:00
|
|
|
QgsDebugMsg( QString( "rowToId: row %1 not in the map" ).arg( id ) );
|
2009-07-21 15:28:02 +00:00
|
|
|
// return negative infinite (to avoid collision with newly added features)
|
|
|
|
return -999999;
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mRowIdMap[id];
|
|
|
|
}
|
|
|
|
|
2009-08-15 22:28:06 +00:00
|
|
|
int QgsAttributeTableModel::fieldIdx( int col ) const
|
|
|
|
{
|
|
|
|
return mAttributes[ col ];
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
int QgsAttributeTableModel::rowCount( const QModelIndex &parent ) const
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
|
|
|
return mFeatureCount;
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
int QgsAttributeTableModel::columnCount( const QModelIndex &parent ) const
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
|
|
|
return mFieldCount;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-01 07:29:08 +00:00
|
|
|
QString attributeName = mLayer->attributeAlias( mAttributes[section] );
|
2009-07-01 22:38:49 +00:00
|
|
|
if ( attributeName.isEmpty() )
|
2009-07-01 07:29:08 +00:00
|
|
|
{
|
|
|
|
QgsField field = mLayer->pendingFields()[ mAttributes[section] ];
|
|
|
|
attributeName = field.name();
|
|
|
|
}
|
2009-07-01 22:38:49 +00:00
|
|
|
return QVariant( attributeName );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else return QVariant();
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::sort( int column, Qt::SortOrder order )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
|
|
|
QgsAttributeMap row;
|
|
|
|
QgsAttributeList attrs;
|
|
|
|
QgsFeature f;
|
|
|
|
|
2009-04-02 12:52:34 +00:00
|
|
|
attrs.append( mAttributes[column] );
|
2009-03-20 18:24:19 +00:00
|
|
|
|
|
|
|
emit layoutAboutToBeChanged();
|
2009-03-27 17:37:59 +00:00
|
|
|
// QgsDebugMsg("SORTing");
|
2009-03-20 18:24:19 +00:00
|
|
|
|
|
|
|
mSortList.clear();
|
2009-03-27 17:37:59 +00:00
|
|
|
mLayer->select( attrs, QgsRectangle(), false );
|
|
|
|
while ( mLayer->nextFeature( f ) )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
|
|
|
row = f.attributeMap();
|
2009-07-23 11:11:41 +00:00
|
|
|
mSortList.append( QgsAttributeTableIdColumnPair( f.id(), row[ mAttributes[column] ] ) );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( order == Qt::AscendingOrder )
|
|
|
|
qStableSort( mSortList.begin(), mSortList.end() );
|
2009-03-20 18:24:19 +00:00
|
|
|
else
|
2009-05-04 10:19:25 +00:00
|
|
|
qStableSort( mSortList.begin(), mSortList.end(), qGreater<QgsAttributeTableIdColumnPair>() );
|
2009-03-20 18:24:19 +00:00
|
|
|
|
|
|
|
// recalculate id<->row maps
|
|
|
|
mRowIdMap.clear();
|
|
|
|
mIdRowMap.clear();
|
|
|
|
|
|
|
|
int i = 0;
|
2009-05-04 10:19:25 +00:00
|
|
|
QList<QgsAttributeTableIdColumnPair>::Iterator it;
|
2009-03-27 17:37:59 +00:00
|
|
|
for ( it = mSortList.begin(); it != mSortList.end(); ++it, ++i )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-07-23 11:11:41 +00:00
|
|
|
mRowIdMap.insert( i, it->id() );
|
|
|
|
mIdRowMap.insert( it->id(), i );
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// restore selection
|
|
|
|
emit layoutChanged();
|
|
|
|
//reset();
|
|
|
|
emit modelChanged();
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) const
|
2009-10-06 21:43:33 +00:00
|
|
|
{
|
|
|
|
return (( QgsAttributeTableModel * ) this )->data( index, role );
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( !index.isValid() || ( role != Qt::TextAlignmentRole && role != Qt::DisplayRole && role != Qt::EditRole ) )
|
2009-03-20 18:24:19 +00:00
|
|
|
return QVariant();
|
|
|
|
|
2009-03-27 17:42:35 +00:00
|
|
|
QVariant::Type fldType = mLayer->pendingFields()[ mAttributes[index.column()] ].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
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( mLastRowId != rowToId( index.row() ) )
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
2009-10-06 21:43:33 +00:00
|
|
|
bool res = featureAtId( rowToId( index.row() ) );
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( !res )
|
|
|
|
return QVariant( "ERROR" );
|
|
|
|
|
|
|
|
mLastRowId = rowToId( index.row() );
|
2009-03-27 17:42:35 +00:00
|
|
|
mLastRow = ( QgsAttributeMap * ) & mFeat.attributeMap();
|
2009-03-20 18:24:19 +00:00
|
|
|
}
|
2009-03-27 17:37:59 +00:00
|
|
|
|
2009-04-03 06:54:21 +00:00
|
|
|
if ( !mLastRow )
|
2009-03-28 02:02:00 +00:00
|
|
|
return QVariant( "ERROR" );
|
|
|
|
|
2009-09-19 22:56:53 +00:00
|
|
|
const QVariant &val = ( *mLastRow )[ mAttributes[index.column()] ];
|
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
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
return QVariant( "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
|
|
|
|
2009-09-19 22:56:53 +00:00
|
|
|
if ( role == Qt::DisplayRole && mValueMaps.contains( index.column() ) )
|
|
|
|
{
|
|
|
|
return mValueMaps[ index.column()]->key( val.toString(), QString( "(%1)" ).arg( val.toString() ) );
|
|
|
|
}
|
|
|
|
|
2009-03-20 18:24:19 +00:00
|
|
|
return val.toString();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( !index.isValid() || role != Qt::EditRole )
|
2009-03-20 18:24:19 +00:00
|
|
|
return false;
|
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( !mLayer->isEditable() )
|
2009-03-20 18:24:19 +00:00
|
|
|
return false;
|
|
|
|
|
2009-10-06 21:43:33 +00:00
|
|
|
bool res = featureAtId( rowToId( index.row() ) );
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( res )
|
|
|
|
{
|
|
|
|
mLastRowId = rowToId( index.row() );
|
2009-10-06 21:43:33 +00:00
|
|
|
mLastRow = ( QgsAttributeMap * ) & mFeat.attributeMap();
|
|
|
|
|
2009-12-20 23:20:25 +00:00
|
|
|
disconnect( mLayer, SIGNAL( layerModified( bool ) ), this, SLOT( layerModified( bool ) ) );
|
|
|
|
|
2009-06-26 15:29:46 +00:00
|
|
|
mLayer->beginEditCommand( tr( "Attribute changed" ) );
|
2009-06-02 14:40:04 +00:00
|
|
|
mLayer->changeAttributeValue( rowToId( index.row() ), mAttributes[ index.column()], value, true );
|
2009-06-14 14:21:44 +00:00
|
|
|
mLayer->endEditCommand();
|
2009-12-20 23:20:25 +00:00
|
|
|
|
|
|
|
( *mLastRow )[ mAttributes[index.column()] ] = value;
|
|
|
|
|
|
|
|
connect( mLayer, SIGNAL( layerModified( bool ) ), this, SLOT( layerModified( bool ) ) );
|
2009-03-27 17:37:59 +00:00
|
|
|
}
|
2009-03-20 18:24:19 +00:00
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
if ( !mLayer->isModified() )
|
2009-03-20 18:24:19 +00:00
|
|
|
return false;
|
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
emit dataChanged( index, index );
|
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;
|
|
|
|
|
2009-03-27 17:37:59 +00:00
|
|
|
Qt::ItemFlags flags = QAbstractItemModel::flags( index );
|
|
|
|
|
|
|
|
if ( mLayer->isEditable() )
|
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
|
|
|
{
|
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-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::changeLayout()
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
|
|
|
emit layoutChanged();
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:44:20 +00:00
|
|
|
void QgsAttributeTableModel::incomingChangeLayout()
|
2009-03-20 18:24:19 +00:00
|
|
|
{
|
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
}
|
|
|
|
|