Skip to content

Commit

Permalink
attribute editor fixes:
Browse files Browse the repository at this point in the history
- fix column misindexing in attribute table, if the geometry column is not last
- fix NULL handling (fixes #1876)



git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11392 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Aug 15, 2009
1 parent 751f38f commit 2db782f
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 50 deletions.
27 changes: 25 additions & 2 deletions src/app/attributetable/qgsattributetabledelegate.cpp
Expand Up @@ -39,6 +39,20 @@ QgsVectorLayer *QgsAttributeTableDelegate::layer( const QAbstractItemModel *mode
return NULL;
}

int QgsAttributeTableDelegate::fieldIdx( const QModelIndex &index ) const
{
const QgsAttributeTableModel *tm = dynamic_cast<const QgsAttributeTableModel*>( index.model() );
if ( tm )
return tm->fieldIdx( index.column() );

const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel*>( index.model() );
if ( fm )
return fm->tableModel()->fieldIdx( index.column() );

return -1;
}


QWidget *QgsAttributeTableDelegate::createEditor(
QWidget *parent,
const QStyleOptionViewItem &option,
Expand All @@ -48,7 +62,7 @@ QWidget *QgsAttributeTableDelegate::createEditor(
if ( vl == NULL )
return NULL;

QWidget *widget = QgsAttributeEditor::createAttributeEditor( parent, vl, index.column(), index.model()->data( index ) );
QWidget *widget = QgsAttributeEditor::createAttributeEditor( parent, vl, fieldIdx( index ), index.model()->data( index, Qt::EditRole ) );
widget->adjustSize();

QgsAttributeTableView *tv = dynamic_cast<QgsAttributeTableView *>( parent->parentWidget() );
Expand All @@ -65,12 +79,21 @@ void QgsAttributeTableDelegate::setModelData( QWidget *editor, QAbstractItemMode
return;

QVariant value;
if ( !QgsAttributeEditor::retrieveValue( editor, vl, index.column(), value ) )
if ( !QgsAttributeEditor::retrieveValue( editor, vl, fieldIdx( index ), value ) )
return;

model->setData( index, value );
}

void QgsAttributeTableDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
{
QgsVectorLayer *vl = layer( index.model() );
if ( vl == NULL )
return;

QgsAttributeEditor::setValue( editor, vl, fieldIdx( index ), index.model()->data( index, Qt::EditRole ) );
}

void QgsAttributeTableDelegate::paint( QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const
Expand Down
10 changes: 9 additions & 1 deletion src/app/attributetable/qgsattributetabledelegate.h
Expand Up @@ -31,6 +31,7 @@ class QgsAttributeTableDelegate : public QItemDelegate
Q_OBJECT;

QgsVectorLayer *layer( const QAbstractItemModel *model ) const;
int fieldIdx( const QModelIndex &index ) const;

public:
/** Constructor
Expand All @@ -52,13 +53,20 @@ class QgsAttributeTableDelegate : public QItemDelegate
const QModelIndex & index ) const;

/**
* Sets data from editor backk to model. Overloads default metod
* Sets data from editor back to model. Overloads default method
* @param editor editor which was created by create editor function in this class
* @param model model where data should be updated
* @param index index of field which is to be modified
*/
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;

/**
* Sets data from model into the editor. Overloads default method
* @param editor editor which was created by create editor function in this class
* @param index index of field which is to be retrieved
*/
void setEditorData( QWidget *editor, const QModelIndex &index ) const;

};

#endif //QGSATTRIBUTETABLEDELEGATE_H
20 changes: 3 additions & 17 deletions src/app/attributetable/qgsattributetablefiltermodel.cpp
Expand Up @@ -23,10 +23,10 @@

void QgsAttributeTableFilterModel::sort( int column, Qt::SortOrder order )
{
(( QgsAttributeTableModel * )sourceModel() )->sort( column, order );
tableModel()->sort( column, order );
}

QgsAttributeTableFilterModel::QgsAttributeTableFilterModel( QgsVectorLayer* theLayer )
QgsAttributeTableFilterModel::QgsAttributeTableFilterModel( QgsVectorLayer *theLayer )
{
mLayer = theLayer;
mHideUnselected = false;
Expand All @@ -36,21 +36,7 @@ QgsAttributeTableFilterModel::QgsAttributeTableFilterModel( QgsVectorLayer* theL
bool QgsAttributeTableFilterModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
{
if ( mHideUnselected )
// unreadable? yes, i agree :-)
return mLayer->selectedFeaturesIds().contains((( QgsAttributeTableModel * )sourceModel() )->rowToId( sourceRow ) );
return mLayer->selectedFeaturesIds().contains( tableModel()->rowToId( sourceRow ) );

return true;
}

/*
QModelIndex QgsAttributeTableFilterModel::mapFromSource ( const QModelIndex& sourceIndex ) const
{
return sourceIndex;
}
QModelIndex QgsAttributeTableFilterModel::mapToSource ( const QModelIndex& filterIndex ) const
{
return filterIndex;
}
*/

3 changes: 3 additions & 0 deletions src/app/attributetable/qgsattributetablefiltermodel.h
Expand Up @@ -23,6 +23,8 @@
//QGIS Includes
#include "qgsvectorlayer.h" //QgsAttributeList

class QgsAttributeTableModel;

class QgsAttributeTableFilterModel: public QSortFilterProxyModel
{
public:
Expand All @@ -42,6 +44,7 @@ class QgsAttributeTableFilterModel: public QSortFilterProxyModel
//QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const;

QgsVectorLayer *layer() const { return mLayer; }
QgsAttributeTableModel *tableModel() const { return reinterpret_cast<QgsAttributeTableModel*>( sourceModel() ); }

protected:
/**
Expand Down
2 changes: 1 addition & 1 deletion src/app/attributetable/qgsattributetablememorymodel.h
Expand Up @@ -28,7 +28,7 @@
#include "qgsattributetablemodel.h"
#include "qgsattributetableidcolumnpair.h"

class QgsAttributeTableMemoryModel: public QgsAttributeTableModel
class QgsAttributeTableMemoryModel : public QgsAttributeTableModel
{
Q_OBJECT;

Expand Down
13 changes: 11 additions & 2 deletions src/app/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -259,6 +259,11 @@ int QgsAttributeTableModel::rowToId( const int id ) const
return mRowIdMap[id];
}

int QgsAttributeTableModel::fieldIdx( int col ) const
{
return mAttributes[ col ];
}

int QgsAttributeTableModel::rowCount( const QModelIndex &parent ) const
{
return mFeatureCount;
Expand Down Expand Up @@ -364,15 +369,19 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
if ( !mLastRow )
return QVariant( "ERROR" );

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

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

// force also numeric data for EditRole to be strings
Expand Down
4 changes: 4 additions & 0 deletions src/app/attributetable/qgsattributetablemodel.h
Expand Up @@ -99,6 +99,10 @@ class QgsAttributeTableModel: public QAbstractTableModel
* @param id feature id
*/
int idToRow( const int id ) const;
/**
* get field index from column
*/
int fieldIdx( int col ) const;
/**
* Maps row to feature id
* @param id row id
Expand Down

0 comments on commit 2db782f

Please sign in to comment.