Skip to content
Permalink
Browse files
remove redundancies in QgsAttributeTableMemoryModel and QgsAttributeT…
…ableModel

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11761 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Oct 6, 2009
1 parent b431337 commit 250fafbd2b62c68b081fc454f4d4f45c3110fa92
@@ -17,7 +17,7 @@
#define QGSATTRIBUTETABLEDELEGATE_H

#include <QItemDelegate>
#include "qgsvectorlayer.h"

class QPainter;
class QgsVectorLayer;

@@ -218,7 +218,7 @@ void QgsAttributeTableDialog::on_mRemoveSelectionButton_clicked()

void QgsAttributeTableDialog::on_cbxShowSelectedOnly_toggled( bool theFlag )
{
mFilterModel->mHideUnselected = theFlag;
mFilterModel->setHideUnselected( theFlag );
mFilterModel->invalidate();
//TODO: weird
//mModel->changeLayout();
@@ -24,9 +24,6 @@

#include "ui_qgsattributetabledialog.h"

class QgsMapLayer;
class QgsVectorLayer;

#include "qgsvectorlayer.h" //QgsFeatureIds

class QDialogButtonBox;
@@ -20,15 +20,12 @@
#include <QSortFilterProxyModel>
#include <QModelIndex>

//QGIS Includes
#include "qgsvectorlayer.h" //QgsAttributeList

class QgsAttributeTableModel;
class QgsVectorLayer;

class QgsAttributeTableFilterModel: public QSortFilterProxyModel
{
public:
bool mHideUnselected;
/**
* Constructor
* @param theLayer initializing layer pointer
@@ -46,6 +43,8 @@ class QgsAttributeTableFilterModel: public QSortFilterProxyModel
QgsVectorLayer *layer() const { return mLayer; }
QgsAttributeTableModel *tableModel() const { return reinterpret_cast<QgsAttributeTableModel*>( sourceModel() ); }

void setHideUnselected( bool theFlag ) { mHideUnselected = theFlag; }

protected:
/**
* Returns true if the source row will be accepted
@@ -55,6 +54,7 @@ class QgsAttributeTableFilterModel: public QSortFilterProxyModel
bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const;
private:
QgsVectorLayer* mLayer;
bool mHideUnselected;
};

#endif
@@ -46,92 +46,18 @@ QgsAttributeTableMemoryModel::QgsAttributeTableMemoryModel
loadLayer();
}

QVariant QgsAttributeTableMemoryModel::data( const QModelIndex &index, int role ) const
bool QgsAttributeTableMemoryModel::featureAtId( int fid )
{
if ( !index.isValid() || ( role != Qt::TextAlignmentRole && role != Qt::DisplayRole && role != Qt::EditRole ) )
return QVariant();

QVariant::Type fldType = mLayer->pendingFields()[ mAttributes[index.column()] ].type();
bool fldNumeric = ( fldType == QVariant::Int || fldType == QVariant::Double );

if ( role == Qt::TextAlignmentRole )
{
if ( fldNumeric )
return QVariant( Qt::AlignRight );
else
return QVariant( Qt::AlignLeft );
}

// if we don't have the row in current cache, load it from layer first
if ( mLastRowId != rowToId( index.row() ) )
if ( mFeatureMap.contains( fid ) )
{
//bool res = mLayer->featureAtId(rowToId(index.row()), mFeat, false, true);
bool res = mFeatureMap.contains( rowToId( index.row() ) );

if ( !res )
return QVariant( "ERROR" );

mLastRowId = rowToId( index.row() );
mFeat = mFeatureMap[rowToId( index.row() )];
mLastRow = ( QgsAttributeMap * ) & mFeat.attributeMap();
mFeat = mFeatureMap[ fid ];
return true;
}

if ( !mLastRow )
return QVariant( "ERROR" );

QVariant &val = ( *mLastRow )[ mAttributes[index.column()] ];

if ( val.isNull() )
else
{
// if the value is NULL, show that in table, but don't show "NULL" text in editor
if ( role == Qt::EditRole )
return QVariant();
else
return QVariant( "NULL" );
}

// force also numeric data for EditRole to be strings
// otherwise it creates spinboxes instead of line edits
// (probably not what we do want)
if ( fldNumeric && role == Qt::EditRole )
return val.toString();

// convert to QString from some other representation
// this prevents displaying greater numbers in exponential format
return val.toString();
}

bool QgsAttributeTableMemoryModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
if ( !index.isValid() || role != Qt::EditRole )
return false;

if ( !mLayer->isEditable() )
QgsDebugMsg( QString( "feature %1 not loaded" ).arg( fid ) );
return false;

//bool res = mLayer->featureAtId(rowToId(index.row()), mFeat, false, true);
bool res = mFeatureMap.contains( rowToId( index.row() ) );

if ( res )
{
mLastRowId = rowToId( index.row() );
mFeat = mFeatureMap[rowToId( index.row() )];
mLastRow = ( QgsAttributeMap * ) & mFeat.attributeMap();


// QgsDebugMsg(mFeatureMap[rowToId(index.row())].id());
mFeatureMap[rowToId( index.row() )].changeAttribute( mAttributes[ index.column()], value );
// propagate back to the layer
mLayer->beginEditCommand( tr( "Attribute changed" ) );
mLayer->changeAttributeValue( rowToId( index.row() ), mAttributes[ index.column()], value, true );
mLayer->endEditCommand();
}

if ( !mLayer->isModified() )
return false;

emit dataChanged( index, index );
return true;
}

void QgsAttributeTableMemoryModel::featureDeleted( int fid )
@@ -150,22 +76,6 @@ void QgsAttributeTableMemoryModel::featureAdded( int fid )
QgsAttributeTableModel::featureAdded( fid );
}

#if 0
void QgsAttributeTableMemoryModel::attributeAdded( int idx )
{
QgsDebugMsg( "entered." );
loadLayer();
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
}

void QgsAttributeTableMemoryModel::attributeDeleted( int idx )
{
QgsDebugMsg( "entered." );
loadLayer();
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
}
#endif

void QgsAttributeTableMemoryModel::layerDeleted()
{
QgsDebugMsg( "entered." );
@@ -56,22 +56,6 @@ class QgsAttributeTableMemoryModel : public QgsAttributeTableModel
virtual void layerDeleted();

private slots:
/**
* Launched when attribute has been added
* @param idx attribute index
*/
//virtual void attributeAdded (int idx);
/**
* Launched when attribute has been deleted
* @param idx attribute index
*/
//virtual void attributeDeleted (int idx);
/**
* Launched when layer has been modified
* Rebuilds the model
* @param onlyGeometry true if only geometry has changed
*/
//virtual void layerModified(bool onlyGeometry);
/**
* Launched when attribute value has been changed
* @param fid feature id
@@ -82,18 +66,12 @@ class QgsAttributeTableMemoryModel : public QgsAttributeTableModel

private:
/**
* Returns data on the given index
* @param index model index
* @param role data role
*/
virtual QVariant data( const QModelIndex &index, int role ) const;
/**
* Updates data on given index
* @param index model index
* @param value new data value
* @param role data role
* load feature fid into mFeat
* @param fid feature id
* @return feature exists
*/
virtual bool setData( const QModelIndex &index, const QVariant &value, int role );
virtual bool featureAtId( int fid );

/**
* Loads the layer into the model
*/
@@ -68,6 +68,11 @@ QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayer *theLayer, QObjec
loadLayer();
}

bool QgsAttributeTableModel::featureAtId( int fid )
{
return mLayer->featureAtId( fid, mFeat, false, true );
}

void QgsAttributeTableModel::featureDeleted( int fid )
{
QgsDebugMsg( "entered." );
@@ -360,6 +365,11 @@ void QgsAttributeTableModel::sort( int column, Qt::SortOrder order )
}

QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) const
{
return (( QgsAttributeTableModel * ) this )->data( index, role );
}

QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role )
{
if ( !index.isValid() || ( role != Qt::TextAlignmentRole && role != Qt::DisplayRole && role != Qt::EditRole ) )
return QVariant();
@@ -378,7 +388,7 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
// if we don't have the row in current cache, load it from layer first
if ( mLastRowId != rowToId( index.row() ) )
{
bool res = mLayer->featureAtId( rowToId( index.row() ), mFeat, false, true );
bool res = featureAtId( rowToId( index.row() ) );

if ( !res )
return QVariant( "ERROR" );
@@ -429,12 +439,13 @@ bool QgsAttributeTableModel::setData( const QModelIndex &index, const QVariant &
if ( !mLayer->isEditable() )
return false;

bool res = mLayer->featureAtId( rowToId( index.row() ), mFeat, false, true );
bool res = featureAtId( rowToId( index.row() ) );

if ( res )
{
mLastRowId = rowToId( index.row() );
mLastRow = ( QgsAttributeMap * )( &( mFeat.attributeMap() ) );
mLastRow = ( QgsAttributeMap * ) & mFeat.attributeMap();

mLayer->beginEditCommand( tr( "Attribute changed" ) );
mLayer->changeAttributeValue( rowToId( index.row() ), mAttributes[ index.column()], value, true );
mLayer->endEditCommand();
@@ -22,9 +22,8 @@
#include <QObject>
#include <QHash>

//QGIS Includes
#include "qgsfeature.h" //QgsAttributeMap
#include "qgsvectorlayer.h" //QgsAttributeList
#include "qgsfeature.h" // QgsAttributeMap
#include "qgsvectorlayer.h" // QgsAttributeList
#include "qgsattributetableidcolumnpair.h"

class QgsAttributeTableModel: public QAbstractTableModel
@@ -203,6 +202,14 @@ class QgsAttributeTableModel: public QAbstractTableModel
*/
virtual void loadLayer();

/**
* load feature fid into mFeat
* @param fid feature id
* @return feature exists
*/
virtual bool featureAtId( int fid );

QVariant data( const QModelIndex &index, int role );
};


0 comments on commit 250fafb

Please sign in to comment.