1 /*
2  *    Copyright 2015-2019 Kai Pastor
3  *
4  *    This file is part of OpenOrienteering.
5  *
6  *    OpenOrienteering is free software: you can redistribute it and/or modify
7  *    it under the terms of the GNU General Public License as published by
8  *    the Free Software Foundation, either version 3 of the License, or
9  *    (at your option) any later version.
10  *
11  *    OpenOrienteering is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    GNU General Public License for more details.
15  *
16  *    You should have received a copy of the GNU General Public License
17  *    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "crs_param_widgets.h"
21 
22 #include <QAbstractButton>
23 #include <QCompleter>
24 #include <QHBoxLayout>
25 #include <QLineEdit>
26 #include <QPushButton>
27 #include <QRegExp>
28 #include <QRegExpValidator>
29 #include <QStringList>
30 #include <QVariant>
31 
32 #include "core/crs_template.h"
33 #include "core/crs_template_implementation.h"
34 #include "core/georeferencing.h"
35 
36 // IWYU pragma: no_forward_declare QCompleter
37 // IWYU pragma: no_forward_declare QHBoxLayout
38 // IWYU pragma: no_forward_declare QPushButton
39 
40 
41 namespace OpenOrienteering {
42 
43 namespace  {
44 
makeZoneList()45 QStringList makeZoneList()
46 {
47 	QStringList zone_list;
48 	zone_list.reserve((60 + 9) * 2);
49 	for (int i = 1; i <= 60; ++i)
50 	{
51 		QString zone = QString::number(i);
52 		zone_list << QString::fromLatin1("%1 N").arg(zone) << QString::fromLatin1("%1 S").arg(zone);
53 		if (i < 10)
54 			zone_list << QString::fromLatin1("0%1 N").arg(zone) << QString::fromLatin1("0%1 S").arg(zone);
55 	}
56 	return zone_list;
57 }
58 
59 }  // namespace
60 
61 
62 
UTMZoneEdit(CRSParameterWidgetObserver & observer,QWidget * parent)63 UTMZoneEdit::UTMZoneEdit(CRSParameterWidgetObserver& observer, QWidget* parent)
64  : QWidget(parent)
65  , observer(observer)
66 {
67 	auto const zone_regexp = QRegExp(QString::fromLatin1("(?:[0-5]?[1-9]|[1-6]0)(?: [NS])?"));
68 	auto const zone_list = makeZoneList();
69 
70 	line_edit = new QLineEdit();
71 	line_edit->setValidator(new QRegExpValidator(zone_regexp, line_edit));
72 	auto* completer = new QCompleter(zone_list, line_edit);
73 	completer->setMaxVisibleItems(4);
74 	line_edit->setCompleter(completer);
75 	connect(line_edit, &QLineEdit::textChanged, this, &UTMZoneEdit::textChanged);
76 
77 	auto* button = new QPushButton(tr("Calculate"));
78 	connect(button, &QAbstractButton::clicked, this, &UTMZoneEdit::calculateValue);
79 
80 	auto* layout = new QHBoxLayout();
81 	layout->setMargin(0);
82 	layout->addWidget(line_edit, 1);
83 	layout->addWidget(button, 0);
84 	setLayout(layout);
85 
86 	calculateValue();
87 }
88 
89 // This non-inline definition is required to emit a (single) vtable.
90 UTMZoneEdit::~UTMZoneEdit() = default;
91 
92 
text() const93 QString UTMZoneEdit::text() const
94 {
95 	return line_edit->text();
96 }
97 
setText(const QString & text)98 void UTMZoneEdit::setText(const QString& text)
99 {
100 	line_edit->setText(text);
101 }
102 
calculateValue()103 bool UTMZoneEdit::calculateValue()
104 {
105 	auto georef = observer.georeferencing();
106 	auto zone = CRSTemplates::UTMZoneParameter::calculateUTMZone(georef.getGeographicRefPoint());
107 	if (!zone.isNull())
108 	{
109 		setText(zone.toString());
110 	}
111 
112 	return !zone.isNull();
113 }
114 
115 
116 }  // namespace OpenOrienteering
117