-
Notifications
You must be signed in to change notification settings - Fork 105
/
crs_selector.cpp
390 lines (321 loc) · 9.02 KB
/
crs_selector.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* Copyright 2012, 2013 Thomas Schöps
* Copyright 2012-2015 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering 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 3 of the License, or
* (at your option) any later version.
*
* OpenOrienteering is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#include "crs_selector.h"
#include <iterator>
#include <memory>
#include <QtGlobal>
#include <QEvent>
#include <QFormLayout>
#include <QLabel>
#include <QLatin1Char>
#include <QLayoutItem>
#include <QSignalBlocker>
#include <QVariant>
#include <QWidget>
#include "core/crs_template.h"
#include "core/georeferencing.h"
namespace OpenOrienteering {
// Helper functions for parameter widgets
namespace {
static const char* crsParameterWidgetProperty = "CRS parameter widget";
static const char* crsParameterKeyProperty = "CRS parameter key";
inline
void setParameterWidget(QWidget* w, bool value = true)
{
w->setProperty(crsParameterWidgetProperty, QVariant(value));
}
inline
bool isParameterWidget(const QWidget* w)
{
return w->property(crsParameterWidgetProperty).toBool();
}
inline
void setParameterKey(QWidget* w, const QString& value)
{
w->setProperty(crsParameterKeyProperty, QVariant(value));
}
inline
QString ParameterKey(const QWidget* w)
{
return w->property(crsParameterKeyProperty).toString();
}
} // namespace
CRSSelector::CRSSelector(const Georeferencing& georef, QWidget* parent)
: QComboBox(parent)
, georef(georef)
, dialog_layout(nullptr)
, num_custom_items(0)
, configured_crs(nullptr)
{
for (auto&& crs : CRSTemplateRegistry().list())
{
addItem(crs->name(), QVariant(crs->id()));
}
}
CRSSelector::~CRSSelector() = default;
void CRSSelector::setDialogLayout(QFormLayout* dialog_layout)
{
Q_ASSERT(dialog_layout && !this->dialog_layout);
this->dialog_layout = dialog_layout;
using TakingIntArgument = void (QComboBox::*)(int);
connect(this, (TakingIntArgument)&QComboBox::currentIndexChanged,
this, &CRSSelector::crsSelectionChanged);
}
void CRSSelector::addCustomItem(const QString& text, unsigned short id)
{
insertItem(num_custom_items, text, QVariant(int(id)));
if (num_custom_items == 0)
insertSeparator(1);
++num_custom_items;
}
const CRSTemplate* CRSSelector::currentCRSTemplate() const
{
const CRSTemplate* crs = nullptr;
auto item_data = itemData(currentIndex());
if (item_data.type() == QVariant::String)
crs = CRSTemplateRegistry().find(item_data.toString());
return crs;
}
QString CRSSelector::currentCRSSpec() const
{
QString spec;
if (auto crs = currentCRSTemplate())
{
spec = crs->specificationTemplate();
auto field_values = parameters();
Q_ASSERT(field_values.size() == crs->parameters().size());
auto field_value = begin(field_values);
for (auto&& param : crs->parameters())
{
for (auto&& value : param->specValues(*field_value))
spec = spec.arg(value);
++field_value;
}
}
return spec;
}
int CRSSelector::currentCustomItem() const
{
int id = -1;
QVariant item_data = itemData(currentIndex());
if (item_data.type() == QVariant::Int)
id = item_data.toInt();
return id;
}
void CRSSelector::setCurrentCRS(const CRSTemplate* crs, const std::vector<QString>& values)
{
Q_ASSERT(crs);
if (crs)
{
Q_ASSERT(crs->parameters().size() == values.size());
int index = findData(QVariant(crs->id()));
setCurrentIndex(index);
// Explicit call because signals may be blocked.
if (crs == currentCRSTemplate())
{
configureParameterFields(crs, values);
}
}
}
void CRSSelector::setCurrentItem(unsigned short id)
{
QSignalBlocker block(this);
int index = findData(QVariant(id));
setCurrentIndex(index);
// Explicit call because signals may be blocked.
if (currentCustomItem() == int(id))
{
configureParameterFields(nullptr, {});
}
}
std::vector<QString> CRSSelector::parameters() const
{
std::vector<QString> values;
auto crs = currentCRSTemplate();
if (crs && configured_crs == crs)
{
values.reserve(crs->parameters().size());
int row;
QFormLayout::ItemRole role;
dialog_layout->getWidgetPosition(const_cast<CRSSelector*>(this), &row, &role);
for (auto&& param : crs->parameters())
{
++row;
auto field = dialog_layout->itemAt(row, QFormLayout::FieldRole)->widget();
Q_ASSERT(isParameterWidget(field));
values.push_back(param->value(field));
}
}
else if (crs)
{
values.resize(crs->parameters().size());
}
return values;
}
const Georeferencing& CRSSelector::georeferencing() const
{
return georef;
}
void CRSSelector::crsSelectionChanged()
{
configureParameterFields();
emit crsChanged();
}
void CRSSelector::crsParameterEdited()
{
emit crsChanged();
}
void CRSSelector::configureParameterFields()
{
if (dialog_layout)
{
auto crs = currentCRSTemplate();
if (!crs)
configureParameterFields(crs, {});
else if (crs->id() == georef.getProjectedCRSId())
configureParameterFields(crs, georef.getProjectedCRSParameters());
else
configureParameterFields(crs, std::vector<QString>(crs->parameters().size()));
}
}
void CRSSelector::configureParameterFields(const CRSTemplate* crs, const std::vector<QString>& values)
{
if (crs != configured_crs && dialog_layout)
{
removeParameterFields();
addParameterFields(crs);
}
if (crs && configured_crs == crs && !values.empty())
{
int row;
QFormLayout::ItemRole role;
dialog_layout->getWidgetPosition(this, &row, &role);
// Set the new parameter values.
auto parameters = crs->parameters();
Q_ASSERT(parameters.size() == values.size());
auto parameter = begin(parameters);
auto value = begin(values);
for (++row; dialog_layout->rowCount() > row && value != end(values); ++row)
{
auto field_item = dialog_layout->itemAt(row, QFormLayout::FieldRole);
if (!field_item)
continue;
auto field = field_item->widget();
if (!field || !isParameterWidget(field))
continue;
QSignalBlocker block(field);
(*parameter)->setValue(field, *value);
++value;
++parameter;
}
}
}
void CRSSelector::addParameterFields(const CRSTemplate* crs)
{
Q_ASSERT(!configured_crs);
if (dialog_layout && crs && crs != configured_crs)
{
int row;
QFormLayout::ItemRole role;
dialog_layout->getWidgetPosition(this, &row, &role);
// Add the labels and fields of the new parameters.
for (auto&& parameter : crs->parameters())
{
++row;
auto field = parameter->createEditor(*this);
setParameterWidget(field, true);
setParameterKey(field, parameter->id());
auto label = new QLabel(parameter->name() + QLatin1Char(':'));
if (dialog_layout->itemAt(row, QFormLayout::FieldRole))
{
dialog_layout->insertRow(row, label, field);
}
else
{
dialog_layout->setWidget(row, QFormLayout::LabelRole, label);
dialog_layout->setWidget(row, QFormLayout::FieldRole, field);
}
}
configured_crs = crs;
}
}
void CRSSelector::removeParameterFields()
{
if (dialog_layout)
{
int crs_row;
QFormLayout::ItemRole role;
dialog_layout->getWidgetPosition(this, &crs_row, &role);
// Remove the labels and fields of the old parameters.
for (int row = dialog_layout->rowCount()-1; row > crs_row; --row)
{
auto field_item = dialog_layout->itemAt(row, QFormLayout::FieldRole);
if (!field_item)
continue;
auto field = field_item->widget();
if (!field || !isParameterWidget(field))
continue;
auto label_item = dialog_layout->itemAt(row, QFormLayout::LabelRole);
if (auto label = label_item->widget())
{
delete dialog_layout->takeAt(dialog_layout->indexOf(label));
delete label;
}
delete dialog_layout->takeAt(dialog_layout->indexOf(field));
delete field;
}
configured_crs = nullptr;
}
}
void CRSSelector::changeEvent(QEvent* event)
{
switch (event->type())
{
case QEvent::EnabledChange:
if (dialog_layout)
{
bool enabled = isEnabled();
if (auto label = dialog_layout->labelForField(this))
label->setEnabled(enabled);
int row;
QFormLayout::ItemRole role;
dialog_layout->getWidgetPosition(this, &row, &role);
// Remove the labels and fields of the old parameters.
for (++row; dialog_layout->rowCount() > row; ++row)
{
auto field_item = dialog_layout->itemAt(row, QFormLayout::FieldRole);
if (!field_item)
continue;
auto field = field_item->widget();
if (!field || !isParameterWidget(field))
continue;
auto label_item = dialog_layout->itemAt(row, QFormLayout::LabelRole);
if (auto label = label_item->widget())
label->setEnabled(enabled);
field->setEnabled(enabled);
}
configured_crs = nullptr;
}
break;
default:
; // nothing
}
}
} // namespace OpenOrienteering