1 /**********************************************************************************************
2     Copyright (C) 2014 Oliver Eichler <oliver.eichler@gmx.de>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 **********************************************************************************************/
18 
19 #include "CMainWindow.h"
20 #include "gis/GeoMath.h"
21 #include "gis/prj/IGisProject.h"
22 #include "gis/proj_x.h"
23 #include "gis/wpt/CGisItemWpt.h"
24 #include "gis/wpt/CProjWpt.h"
25 #include "helpers/CWptIconManager.h"
26 #include "units/IUnit.h"
27 
28 #include <QtWidgets>
29 
CProjWpt(CGisItemWpt & wpt,QWidget * parent)30 CProjWpt::CProjWpt(CGisItemWpt& wpt, QWidget* parent)
31     : QDialog(parent)
32     , wpt(wpt)
33 {
34     setupUi(this);
35 
36     name = wpt.getName();
37 
38     toolIcon->setIcon(wpt.getIcon());
39     toolIcon->setObjectName(wpt.getIconName());
40     labelName->setText(QString("<a href='name'>%2</a>").arg(name));
41 
42     QString val, unit;
43     IUnit::self().meter2distance(0, val, unit);
44     labelDistUnit->setText(unit);
45 
46     connect(labelName, &QLabel::linkActivated, this, &CProjWpt::slotChangeName);
47     connect(toolIcon, &QToolButton::clicked, this, &CProjWpt::slotChangeIcon);
48 }
49 
~CProjWpt()50 CProjWpt::~CProjWpt()
51 {
52 }
53 
slotChangeIcon()54 void CProjWpt::slotChangeIcon()
55 {
56     QString iconName = CWptIconManager::self().selectWptIcon(this);
57     if(!iconName.isEmpty())
58     {
59         QPointF focus;
60         toolIcon->setObjectName(iconName);
61         toolIcon->setIcon(CWptIconManager::self().getWptIconByName(iconName, focus));
62     }
63 }
64 
slotChangeName()65 void CProjWpt::slotChangeName()
66 {
67     QString n = QInputDialog::getText(this, tr("Edit name..."), tr("Enter new waypoint name."), QLineEdit::Normal, wpt.getName());
68     if(n.isEmpty())
69     {
70         return;
71     }
72     name = n;
73     labelName->setText(QString("<a href='name'>%2</a>").arg(name));
74 }
75 
76 
accept()77 void CProjWpt::accept()
78 {
79     qreal dist = IUnit::self().elevation2meter(lineDist->text());
80     qreal bearing = lineBearing->text().toDouble();
81 
82     if((dist <= 0) || (bearing > 180) || (bearing < -180))
83     {
84         return;
85     }
86 
87     IGisProject* project = dynamic_cast<IGisProject*>(wpt.parent());
88     if(nullptr == project)
89     {
90         return;
91     }
92 
93     QPointF pos = wpt.getPosition() * DEG_TO_RAD;
94     pos = GPS_Math_Wpt_Projection(pos, dist, bearing * DEG_TO_RAD) * RAD_TO_DEG;
95 
96     CGisItemWpt* newWpt = new CGisItemWpt(pos, wpt, project);
97 
98     if(name != newWpt->getName())
99     {
100         newWpt->setName(name);
101     }
102 
103     if(toolIcon->objectName() != newWpt->getIconName())
104     {
105         newWpt->setIcon(toolIcon->objectName());
106     }
107 
108     QDialog::accept();
109 }
110