Skip to content

Commit

Permalink
remove redundancies in QgsAttributeTableMemoryModel and QgsAttributeT…
Browse files Browse the repository at this point in the history
…ableModel

git-svn-id: http://svn.osgeo.org/qgis/trunk@11761 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Oct 6, 2009
1 parent b5bcbac commit 3d802f8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 138 deletions.
2 changes: 1 addition & 1 deletion src/app/attributetable/qgsattributetabledelegate.h
Expand Up @@ -17,7 +17,7 @@
#define QGSATTRIBUTETABLEDELEGATE_H

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

class QPainter;
class QgsVectorLayer;

Expand Down
2 changes: 1 addition & 1 deletion src/app/attributetable/qgsattributetabledialog.cpp
Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions src/app/attributetable/qgsattributetabledialog.h
Expand Up @@ -24,9 +24,6 @@

#include "ui_qgsattributetabledialog.h"

class QgsMapLayer;
class QgsVectorLayer;

#include "qgsvectorlayer.h" //QgsFeatureIds

class QDialogButtonBox;
Expand Down
8 changes: 4 additions & 4 deletions src/app/attributetable/qgsattributetablefiltermodel.h
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -55,6 +54,7 @@ class QgsAttributeTableFilterModel: public QSortFilterProxyModel
bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const;
private:
QgsVectorLayer* mLayer;
bool mHideUnselected;
};

#endif
102 changes: 6 additions & 96 deletions src/app/attributetable/qgsattributetablememorymodel.cpp
Expand Up @@ -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 )
Expand All @@ -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." );
Expand Down
32 changes: 5 additions & 27 deletions src/app/attributetable/qgsattributetablememorymodel.h
Expand Up @@ -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
Expand All @@ -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
*/
Expand Down
17 changes: 14 additions & 3 deletions src/app/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -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." );
Expand Down Expand Up @@ -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();
Expand All @@ -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" );
Expand Down Expand Up @@ -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();
Expand Down
13 changes: 10 additions & 3 deletions src/app/attributetable/qgsattributetablemodel.h
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
};


Expand Down

0 comments on commit 3d802f8

Please sign in to comment.