/
qgsgcplistmodel.cpp
186 lines (162 loc) · 5.9 KB
/
qgsgcplistmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/***************************************************************************
qgsgcplistmodel.cpp - Model implementation of GCPList Model/View
--------------------------------------
Date : 27-Feb-2009
Copyright : (c) 2009 by Manuel Massing
Email : m.massing at warped-space.de
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */
#include "qgsgcplist.h"
#include "qgsgcplistmodel.h"
#include "qgsgeorefdatapoint.h"
#include "qgsgeoreftransform.h"
#include <cmath>
using namespace std;
class QgsStandardItem : public QStandardItem {
public:
QgsStandardItem(QString text) : QStandardItem(text) { init(); }
QgsStandardItem(int value) : QStandardItem(QString::number(value)) { init(); }
QgsStandardItem(double value) : QStandardItem(QString::number(value, 'f', 2)) { init(); }
private:
void init() {
setTextAlignment(Qt::AlignCenter);
}
};
#define QGSSTANDARDITEM(value) (new QgsStandardItem(value))
#if 0
template <class T> class QNumericItem : public QStandardItem {
public:
QNumericItem(T value) : QStandardItem(QString("%1").arg(value)), mValue(value)
{
}
bool operator < (const QStandardItem &other) const
{
const QNumericItem<T> *otherD = dynamic_cast<const QNumericItem<T> *>(&other);
if (otherD == NULL)
return false;
return mValue < otherD->mValue;
}
private:
T mValue;
};
#endif
QgsGCPListModel::QgsGCPListModel(QObject *parent)
: QStandardItemModel(parent)
, mGCPList(0)
, mGeorefTransform(0)
{
}
void QgsGCPListModel::setGCPList(QgsGCPList *theGCPList)
{
mGCPList = theGCPList;
updateModel();
}
// ------------------------------- public ---------------------------------- //
void QgsGCPListModel::setGeorefTransform(QgsGeorefTransform *theGeorefTransform)
{
mGeorefTransform = theGeorefTransform;
updateModel();
}
void QgsGCPListModel::updateModel()
{
clear();
if (!mGCPList)
return;
// // Setup table header
QStringList itemLabels;
// // Set column headers
itemLabels<< "on/off" << "id" << "srcX" << "srcY" << "dstX" << "dstY" << "dX" << "dY" << "residual";
// setColumnCount(itemLabels.size());
setHorizontalHeaderLabels(itemLabels);
setRowCount(mGCPList->size());
bool bTransformUpdated = false;
if (mGeorefTransform)
{
vector<QgsPoint> mapCoords, pixelCoords;
mGCPList->createGCPVectors(mapCoords, pixelCoords);
// TODO: the parameters should probable be updated externally (by user interaction)
bTransformUpdated = mGeorefTransform->updateParametersFromGCPs(mapCoords, pixelCoords);
}
for (int i = 0; i < mGCPList->sizeAll(); ++i)
{
int j = 0;
QgsGeorefDataPoint *p = mGCPList->at(i);
p->setId(i);
QStandardItem *si = new QStandardItem();
si->setTextAlignment(Qt::AlignCenter);
si->setCheckable(true);
if (p->isEnabled())
si->setCheckState(Qt::Checked);
else
si->setCheckState(Qt::Unchecked);
setItem(i, j++, si);
setItem(i, j++, QGSSTANDARDITEM(i) /*create_item<int>(i)*/);
setItem(i, j++, QGSSTANDARDITEM(p->pixelCoords().x()) /*create_item<double>( p->pixelCoords().x() )*/);
setItem(i, j++, QGSSTANDARDITEM(-p->pixelCoords().y()) /*create_item<double>(-p->pixelCoords().y() )*/);
setItem(i, j++, QGSSTANDARDITEM(p->mapCoords().x()) /*create_item<double>( p->mapCoords().x() )*/);
setItem(i, j++, QGSSTANDARDITEM(p->mapCoords().y()) /*create_item<double>( p->mapCoords().y() )*/);
double residual = -1.f;
double dX, dY;
// Calculate residual if transform is available and up-to-date
if (mGeorefTransform && bTransformUpdated && mGeorefTransform->parametersInitialized())
{
QgsPoint dst;
// Transform from world to raster coordinate:
// This is the transform direction used by the warp operation.
// As transforms of order >=2 are not invertible, we are only
// interested in the residual in this direction
mGeorefTransform->transformWorldToRaster(p->mapCoords(), dst);
dX = (dst.x() - p->pixelCoords().x());
dY = (dst.y() - p->pixelCoords().y());
residual = sqrt(dX*dX + dY*dY);
}
else
{
dX = dY = residual = 0;
}
if (residual >= 0.f) {
setItem(i, j++, QGSSTANDARDITEM(dX) /*create_item<double>(dX)*/);
setItem(i, j++, QGSSTANDARDITEM(-dY) /*create_item<double>(-dY)*/);
setItem(i, j++, QGSSTANDARDITEM(residual) /*create_item<double>(residual)*/);
}
else {
setItem(i, j++, QGSSTANDARDITEM("n/a") /*create_std_item("n/a")*/);
setItem(i, j++, QGSSTANDARDITEM("n/a") /*create_std_item("n/a")*/);
setItem(i, j++, QGSSTANDARDITEM("n/a") /*create_std_item("n/a")*/);
}
}
//sort(); // Sort data
//reset(); // Signal to views that the model has changed
}
// --------------------------- public slots -------------------------------- //
void QgsGCPListModel::replaceDataPoint(QgsGeorefDataPoint *newDataPoint, int i)
{
mGCPList->replace(i, newDataPoint);
}
void QgsGCPListModel::onGCPListModified()
{
}
void QgsGCPListModel::onTransformationModified()
{
}
#if 0
template <class T> QNumericItem<T> *create_item(const T value, bool isEditable = true)
{
QNumericItem<T> *item = new QNumericItem<T>(value);
item->setEditable(isEditable);
return item;
}
QStandardItem *create_std_item(const QString &S, bool isEditable = false)
{
QStandardItem *std_item = new QStandardItem(S);
std_item->setEditable(isEditable);
return std_item;
}
#endif