Skip to content

Commit d0c19c5

Browse files
author
jef
committed
attribute editor fixes:
- 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@11392 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent a66513b commit d0c19c5

9 files changed

+160
-50
lines changed

src/app/attributetable/qgsattributetabledelegate.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ QgsVectorLayer *QgsAttributeTableDelegate::layer( const QAbstractItemModel *mode
3939
return NULL;
4040
}
4141

42+
int QgsAttributeTableDelegate::fieldIdx( const QModelIndex &index ) const
43+
{
44+
const QgsAttributeTableModel *tm = dynamic_cast<const QgsAttributeTableModel*>( index.model() );
45+
if ( tm )
46+
return tm->fieldIdx( index.column() );
47+
48+
const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel*>( index.model() );
49+
if ( fm )
50+
return fm->tableModel()->fieldIdx( index.column() );
51+
52+
return -1;
53+
}
54+
55+
4256
QWidget *QgsAttributeTableDelegate::createEditor(
4357
QWidget *parent,
4458
const QStyleOptionViewItem &option,
@@ -48,7 +62,7 @@ QWidget *QgsAttributeTableDelegate::createEditor(
4862
if ( vl == NULL )
4963
return NULL;
5064

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

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

6781
QVariant value;
68-
if ( !QgsAttributeEditor::retrieveValue( editor, vl, index.column(), value ) )
82+
if ( !QgsAttributeEditor::retrieveValue( editor, vl, fieldIdx( index ), value ) )
6983
return;
7084

7185
model->setData( index, value );
7286
}
7387

88+
void QgsAttributeTableDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
89+
{
90+
QgsVectorLayer *vl = layer( index.model() );
91+
if ( vl == NULL )
92+
return;
93+
94+
QgsAttributeEditor::setValue( editor, vl, fieldIdx( index ), index.model()->data( index, Qt::EditRole ) );
95+
}
96+
7497
void QgsAttributeTableDelegate::paint( QPainter * painter,
7598
const QStyleOptionViewItem & option,
7699
const QModelIndex & index ) const

src/app/attributetable/qgsattributetabledelegate.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class QgsAttributeTableDelegate : public QItemDelegate
3131
Q_OBJECT;
3232

3333
QgsVectorLayer *layer( const QAbstractItemModel *model ) const;
34+
int fieldIdx( const QModelIndex &index ) const;
3435

3536
public:
3637
/** Constructor
@@ -52,13 +53,20 @@ class QgsAttributeTableDelegate : public QItemDelegate
5253
const QModelIndex & index ) const;
5354

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

63+
/**
64+
* Sets data from model into the editor. Overloads default method
65+
* @param editor editor which was created by create editor function in this class
66+
* @param index index of field which is to be retrieved
67+
*/
68+
void setEditorData( QWidget *editor, const QModelIndex &index ) const;
69+
6270
};
6371

6472
#endif //QGSATTRIBUTETABLEDELEGATE_H

src/app/attributetable/qgsattributetablefiltermodel.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323

2424
void QgsAttributeTableFilterModel::sort( int column, Qt::SortOrder order )
2525
{
26-
(( QgsAttributeTableModel * )sourceModel() )->sort( column, order );
26+
tableModel()->sort( column, order );
2727
}
2828

29-
QgsAttributeTableFilterModel::QgsAttributeTableFilterModel( QgsVectorLayer* theLayer )
29+
QgsAttributeTableFilterModel::QgsAttributeTableFilterModel( QgsVectorLayer *theLayer )
3030
{
3131
mLayer = theLayer;
3232
mHideUnselected = false;
@@ -36,21 +36,7 @@ QgsAttributeTableFilterModel::QgsAttributeTableFilterModel( QgsVectorLayer* theL
3636
bool QgsAttributeTableFilterModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
3737
{
3838
if ( mHideUnselected )
39-
// unreadable? yes, i agree :-)
40-
return mLayer->selectedFeaturesIds().contains((( QgsAttributeTableModel * )sourceModel() )->rowToId( sourceRow ) );
39+
return mLayer->selectedFeaturesIds().contains( tableModel()->rowToId( sourceRow ) );
4140

4241
return true;
4342
}
44-
45-
/*
46-
QModelIndex QgsAttributeTableFilterModel::mapFromSource ( const QModelIndex& sourceIndex ) const
47-
{
48-
return sourceIndex;
49-
}
50-
51-
QModelIndex QgsAttributeTableFilterModel::mapToSource ( const QModelIndex& filterIndex ) const
52-
{
53-
return filterIndex;
54-
}
55-
*/
56-

src/app/attributetable/qgsattributetablefiltermodel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
//QGIS Includes
2424
#include "qgsvectorlayer.h" //QgsAttributeList
2525

26+
class QgsAttributeTableModel;
27+
2628
class QgsAttributeTableFilterModel: public QSortFilterProxyModel
2729
{
2830
public:
@@ -42,6 +44,7 @@ class QgsAttributeTableFilterModel: public QSortFilterProxyModel
4244
//QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const;
4345

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

4649
protected:
4750
/**

src/app/attributetable/qgsattributetablememorymodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "qgsattributetablemodel.h"
2929
#include "qgsattributetableidcolumnpair.h"
3030

31-
class QgsAttributeTableMemoryModel: public QgsAttributeTableModel
31+
class QgsAttributeTableMemoryModel : public QgsAttributeTableModel
3232
{
3333
Q_OBJECT;
3434

src/app/attributetable/qgsattributetablemodel.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ int QgsAttributeTableModel::rowToId( const int id ) const
259259
return mRowIdMap[id];
260260
}
261261

262+
int QgsAttributeTableModel::fieldIdx( int col ) const
263+
{
264+
return mAttributes[ col ];
265+
}
266+
262267
int QgsAttributeTableModel::rowCount( const QModelIndex &parent ) const
263268
{
264269
return mFeatureCount;
@@ -364,15 +369,19 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
364369
if ( !mLastRow )
365370
return QVariant( "ERROR" );
366371

367-
QVariant& val = ( *mLastRow )[ mAttributes[index.column()] ];
372+
QVariant &val = ( *mLastRow )[ mAttributes[index.column()] ];
368373

369374
if ( val.isNull() )
370375
{
371376
// if the value is NULL, show that in table, but don't show "NULL" text in editor
372377
if ( role == Qt::EditRole )
373-
return QVariant();
378+
{
379+
return QVariant( fldType );
380+
}
374381
else
382+
{
375383
return QVariant( "NULL" );
384+
}
376385
}
377386

378387
// force also numeric data for EditRole to be strings

src/app/attributetable/qgsattributetablemodel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class QgsAttributeTableModel: public QAbstractTableModel
9999
* @param id feature id
100100
*/
101101
int idToRow( const int id ) const;
102+
/**
103+
* get field index from column
104+
*/
105+
int fieldIdx( int col ) const;
102106
/**
103107
* Maps row to feature id
104108
* @param id row id

0 commit comments

Comments
 (0)