@@ -27,44 +27,37 @@ using namespace std;
27
27
class QgsStandardItem : public QStandardItem
28
28
{
29
29
public:
30
- QgsStandardItem ( QString text ) : QStandardItem( text ) { init (); }
31
- QgsStandardItem ( int value ) : QStandardItem( QString::number( value ) ) { init (); }
32
- QgsStandardItem ( double value ) : QStandardItem( QString::number( value, ' f' , 2 ) ) { init (); }
33
-
34
- private:
35
- void init ()
30
+ QgsStandardItem ( QString text ) : QStandardItem( text )
36
31
{
37
- setTextAlignment ( Qt::AlignCenter );
32
+ // In addition to the DisplayRole, also set the user role, which is used for sorting.
33
+ setData ( QVariant ( text ), Qt::UserRole);
34
+ setTextAlignment ( Qt::AlignRight );
38
35
}
39
- };
40
-
41
- #define QGSSTANDARDITEM (value ) (new QgsStandardItem(value))
42
36
43
- #if 0
44
- template <class T> class QNumericItem : public QStandardItem
45
- {
46
- public:
47
- QNumericItem( T value ) : QStandardItem( QString( "%1" ).arg( value ) ), mValue( value )
37
+ QgsStandardItem ( int value ) : QStandardItem( QString::number( value ) )
48
38
{
39
+ // In addition to the DisplayRole, also set the user role, which is used for sorting.
40
+ // This is needed for numerical sorting to work corretly (otherwise sorting is lexicographic).
41
+ setData ( QVariant ( value ), Qt::UserRole);
42
+ setTextAlignment ( Qt::AlignCenter );
49
43
}
50
-
51
- bool operator < ( const QStandardItem &other ) const
44
+
45
+ QgsStandardItem ( double value ) : QStandardItem( QString::number( value, ' f ' , 2 ) )
52
46
{
53
- const QNumericItem<T> *otherD = dynamic_cast<const QNumericItem<T> *>( &other );
54
- if ( otherD == NULL )
55
- return false ;
56
- return mValue < otherD->mValue ;
47
+ // In addition to the DisplayRole, also set the user role, which is used for sorting.
48
+ // This is needed for numerical sorting to work corretly (otherwise sorting is lexicographic).
49
+ setData ( QVariant ( value ), Qt::UserRole) ;
50
+ setTextAlignment ( Qt::AlignRight ) ;
57
51
}
58
- private:
59
- T mValue;
60
52
};
61
- #endif
62
53
63
54
QgsGCPListModel::QgsGCPListModel ( QObject *parent )
64
55
: QStandardItemModel( parent )
65
56
, mGCPList( 0 )
66
57
, mGeorefTransform( 0 )
67
58
{
59
+ // Use data provided by Qt::UserRole as sorting key (needed for numerical sorting).
60
+ setSortRole ( Qt::UserRole );
68
61
}
69
62
70
63
void QgsGCPListModel::setGCPList ( QgsGCPList *theGCPList )
@@ -82,7 +75,7 @@ void QgsGCPListModel::setGeorefTransform( QgsGeorefTransform *theGeorefTransform
82
75
83
76
void QgsGCPListModel::updateModel ()
84
77
{
85
- clear ();
78
+ // clear();
86
79
if ( !mGCPList )
87
80
return ;
88
81
@@ -92,8 +85,6 @@ void QgsGCPListModel::updateModel()
92
85
vector<QgsPoint> mapCoords, pixelCoords;
93
86
mGCPList ->createGCPVectors ( mapCoords, pixelCoords );
94
87
95
-
96
-
97
88
// // Setup table header
98
89
QStringList itemLabels;
99
90
QString unitType;
@@ -136,11 +127,11 @@ void QgsGCPListModel::updateModel()
136
127
si->setCheckState ( Qt::Unchecked );
137
128
138
129
setItem ( i, j++, si );
139
- setItem ( i, j++, QGSSTANDARDITEM ( i ) /* create_item<int>(i) */ );
140
- setItem ( i, j++, QGSSTANDARDITEM ( p-> pixelCoords (). x () ) /* create_item<double>( p->pixelCoords().x() )*/ );
141
- setItem ( i, j++, QGSSTANDARDITEM ( -p-> pixelCoords (). y () ) /* create_item<double>( -p->pixelCoords().y() )*/ );
142
- setItem ( i, j++, QGSSTANDARDITEM ( p-> mapCoords (). x () ) /* create_item<double>( p->mapCoords().x() )*/ );
143
- setItem ( i, j++, QGSSTANDARDITEM ( p-> mapCoords (). y () ) /* create_item<double>( p->mapCoords().y() )*/ );
130
+ setItem ( i, j++, new QgsStandardItem ( i ) );
131
+ setItem ( i, j++, new QgsStandardItem ( p->pixelCoords ().x () ) );
132
+ setItem ( i, j++, new QgsStandardItem ( -p->pixelCoords ().y () ) );
133
+ setItem ( i, j++, new QgsStandardItem ( p->mapCoords ().x () ) );
134
+ setItem ( i, j++, new QgsStandardItem ( p->mapCoords ().y () ) );
144
135
145
136
double residual;
146
137
double dX = 0 ;
@@ -179,19 +170,17 @@ void QgsGCPListModel::updateModel()
179
170
180
171
if ( residual >= 0 .f )
181
172
{
182
- setItem ( i, j++, QGSSTANDARDITEM ( dX ) /* create_item<double>(dX) */ );
183
- setItem ( i, j++, QGSSTANDARDITEM ( dY ) /* create_item<double>(-dY) */ );
184
- setItem ( i, j++, QGSSTANDARDITEM ( residual ) /* create_item<double>(residual) */ );
173
+ setItem ( i, j++, new QgsStandardItem ( dX ) );
174
+ setItem ( i, j++, new QgsStandardItem ( dY ) );
175
+ setItem ( i, j++, new QgsStandardItem ( residual ) );
185
176
}
186
177
else
187
178
{
188
- setItem ( i, j++, QGSSTANDARDITEM ( " n/a" ) /* create_std_item("n/a") */ );
189
- setItem ( i, j++, QGSSTANDARDITEM ( " n/a" ) /* create_std_item("n/a") */ );
190
- setItem ( i, j++, QGSSTANDARDITEM ( " n/a" ) /* create_std_item("n/a") */ );
179
+ setItem ( i, j++, new QgsStandardItem ( " n/a" ) );
180
+ setItem ( i, j++, new QgsStandardItem ( " n/a" ) );
181
+ setItem ( i, j++, new QgsStandardItem ( " n/a" ) );
191
182
}
192
183
}
193
- // sort(); // Sort data
194
- // reset(); // Signal to views that the model has changed
195
184
}
196
185
197
186
// --------------------------- public slots -------------------------------- //
@@ -207,19 +196,3 @@ void QgsGCPListModel::onGCPListModified()
207
196
void QgsGCPListModel::onTransformationModified ()
208
197
{
209
198
}
210
-
211
- #if 0
212
- template <class T> QNumericItem<T> *create_item( const T value, bool isEditable = true )
213
- {
214
- QNumericItem<T> *item = new QNumericItem<T>( value );
215
- item->setEditable( isEditable );
216
- return item;
217
- }
218
-
219
- QStandardItem *create_std_item( const QString &S, bool isEditable = false )
220
- {
221
- QStandardItem *std_item = new QStandardItem( S );
222
- std_item->setEditable( isEditable );
223
- return std_item;
224
- }
225
- #endif
0 commit comments