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 "gis/proj_x.h"
20 #include "gis/wpt/CDetailsWpt.h"
21 #include "gis/wpt/CGisItemWpt.h"
22 #include "helpers/CElevationDialog.h"
23 #include "helpers/CInputDialog.h"
24 #include "helpers/CLinksDialog.h"
25 #include "helpers/CPositionDialog.h"
26 #include "helpers/CWptIconManager.h"
27 #include "units/IUnit.h"
28 #include "widgets/CTextEditWidget.h"
29 
30 #include <QtWidgets>
31 
CDetailsWpt(CGisItemWpt & wpt,QWidget * parent)32 CDetailsWpt::CDetailsWpt(CGisItemWpt& wpt, QWidget* parent)
33     : QDialog(parent)
34     , wpt(wpt)
35 {
36     setupUi(this);
37     photoAlbum->hide();
38 
39     setupGui();
40 
41     toolLock->setDisabled(wpt.isOnDevice());
42 
43     connect(labelPosition, &QLabel::linkActivated, this, static_cast<void (CDetailsWpt::*)(const QString&)>(&CDetailsWpt::slotLinkActivated));
44     connect(labelElevation, &QLabel::linkActivated, this, static_cast<void (CDetailsWpt::*)(const QString&)>(&CDetailsWpt::slotLinkActivated));
45     connect(labelProximity, &QLabel::linkActivated, this, static_cast<void (CDetailsWpt::*)(const QString&)>(&CDetailsWpt::slotLinkActivated));
46     connect(textCmtDesc, &QTextBrowser::anchorClicked, this, static_cast<void (CDetailsWpt::*)(const QUrl&)   >(&CDetailsWpt::slotLinkActivated));
47 
48     connect(lineName, &CLineEdit::textEdited, this, &CDetailsWpt::slotNameChanged);
49     connect(lineName, &CLineEdit::editingFinished, this, &CDetailsWpt::slotNameChangeFinished);
50     connect(toolIcon, &QToolButton::clicked, this, &CDetailsWpt::slotChangeIcon);
51     connect(toolLock, &QToolButton::toggled, this, &CDetailsWpt::slotChangeReadOnlyMode);
52 
53     connect(listHistory, &CHistoryListWidget::sigChanged, this, &CDetailsWpt::setupGui);
54 
55     connect(toolAddImage, &QToolButton::clicked, photoAlbum, &CPhotoAlbum::slotAddImage);
56     connect(toolDelImage, &QToolButton::clicked, photoAlbum, &CPhotoAlbum::slotDelImage);
57     connect(photoAlbum, &CPhotoAlbum::sigChanged, this, &CDetailsWpt::slotChangedImages);
58 }
59 
~CDetailsWpt()60 CDetailsWpt::~CDetailsWpt()
61 {
62 }
63 
64 
setupGui()65 void CDetailsWpt::setupGui()
66 {
67     if(originator)
68     {
69         return;
70     }
71     originator = true;
72 
73     setWindowTitle(wpt.getName());
74 
75     QString val, unit;
76     QString strPos;
77     QPointF pos = wpt.getPosition();
78     IUnit::degToStr(pos.x(), pos.y(), strPos);
79 
80     bool isReadOnly = wpt.isReadOnly();
81 
82     toolIcon->setIcon(wpt.getIcon());
83     toolIcon->setObjectName(wpt.getIconName());
84     lineName->setReadOnly(isReadOnly);
85     lineName->setText(wpt.getName());
86     labelPosition->setText(IGisItem::toLink(isReadOnly, "position", strPos, ""));
87 
88     labelTainted->setVisible(wpt.isTainted());
89 
90     QString elevationStr = "----";
91     if(wpt.getElevation() != NOINT)
92     {
93         IUnit::self().meter2elevation(wpt.getElevation(), val, unit);
94         elevationStr = QString("%1 %2").arg(val, unit);
95     }
96     labelElevation->setText(IGisItem::toLink(isReadOnly, "elevation", elevationStr, ""));
97 
98 
99     QString proxStr = "----";
100     if(wpt.getProximity() != NOFLOAT)
101     {
102         IUnit::self().meter2elevation(wpt.getProximity(), val, unit);
103         proxStr = QString("%1 %2").arg(val, unit);
104     }
105     labelProximity->setText(IGisItem::toLink(isReadOnly, "proximity", proxStr, ""));
106 
107 
108     if(wpt.getTime().isValid())
109     {
110         labelTime->setText(IUnit::datetime2string(wpt.getTime(), false, QPointF(pos.x() * DEG_TO_RAD, pos.y() * DEG_TO_RAD)));
111     }
112 
113     textCmtDesc->document()->clear();
114     textCmtDesc->append(IGisItem::createText(isReadOnly, wpt.getComment(), wpt.getDescription(), wpt.getLinks()));
115     textCmtDesc->moveCursor (QTextCursor::Start);
116     textCmtDesc->ensureCursorVisible();
117 
118     toolLock->setChecked(isReadOnly);
119     labelNogo->setVisible(wpt.isNogo());
120 
121     listHistory->setupHistory(wpt);
122 
123     const QList<CGisItemWpt::image_t>& images = wpt.getImages();
124     photoAlbum->reload(images);
125 
126     toolAddImage->setVisible(!isReadOnly);
127     toolDelImage->setVisible(!isReadOnly && !images.isEmpty());
128 
129     originator = false;
130 }
131 
slotNameChanged(const QString & name)132 void CDetailsWpt::slotNameChanged(const QString& name)
133 {
134     setWindowTitle(name);
135 }
136 
slotNameChangeFinished()137 void CDetailsWpt::slotNameChangeFinished()
138 {
139     lineName->clearFocus();
140 
141     const QString& name = lineName->text();
142     slotNameChanged(name);
143 
144     if(name != wpt.getName())
145     {
146         wpt.setName(name);
147         setupGui();
148     }
149 }
150 
slotLinkActivated(const QString & link)151 void CDetailsWpt::slotLinkActivated(const QString& link)
152 {
153     if(link == "elevation")
154     {
155         QVariant var(wpt.getElevation());
156         CElevationDialog dlg(this, var, QVariant(NOINT), wpt.getPosition());
157         if(dlg.exec() == QDialog::Accepted)
158         {
159             wpt.setElevation(var.toInt());
160         }
161     }
162     else if(link == "proximity")
163     {
164         QVariant var(wpt.getProximity() * IUnit::self().baseFactor);
165         CInputDialog dlg(this, tr("Enter new proximity range."), var, QVariant(NOFLOAT), IUnit::self().baseUnit);
166         dlg.setOption(tr("Is no-go area"), wpt.isNogo());
167         if(dlg.exec() == QDialog::Accepted)
168         {
169             wpt.setProximity(var.toDouble() / IUnit::self().baseFactor);
170             wpt.setNogo(dlg.optionIsChecked());
171         }
172     }
173     else if(link == "position")
174     {
175         QPointF pos = wpt.getPosition();
176         CPositionDialog dlg(this, pos);
177         if(dlg.exec() == QDialog::Accepted)
178         {
179             wpt.setPosition(pos);
180         }
181     }
182 
183     setupGui();
184 }
185 
slotLinkActivated(const QUrl & url)186 void CDetailsWpt::slotLinkActivated(const QUrl& url)
187 {
188     if(url.toString() == "comment")
189     {
190         CTextEditWidget dlg(wpt.getComment(), this);
191         if(dlg.exec() == QDialog::Accepted)
192         {
193             wpt.setComment(dlg.getHtml());
194         }
195         setupGui();
196     }
197     else if(url.toString() == "description")
198     {
199         CTextEditWidget dlg(wpt.getDescription(), this);
200         if(dlg.exec() == QDialog::Accepted)
201         {
202             wpt.setDescription(dlg.getHtml());
203         }
204         setupGui();
205     }
206     else if(url.toString() == "links")
207     {
208         QList<IGisItem::link_t> links = wpt.getLinks();
209         CLinksDialog dlg(links, this);
210         if(dlg.exec() == QDialog::Accepted)
211         {
212             wpt.setLinks(links);
213         }
214         setupGui();
215     }
216     else
217     {
218         QDesktopServices::openUrl(url);
219     }
220 }
221 
slotChangeIcon()222 void CDetailsWpt::slotChangeIcon()
223 {
224     if(!wpt.isReadOnly())
225     {
226         QString iconName = CWptIconManager::self().selectWptIcon(this);
227         if(!iconName.isEmpty())
228         {
229             wpt.setIcon(iconName);
230             setupGui();
231         }
232     }
233 }
234 
slotChangeReadOnlyMode(bool on)235 void CDetailsWpt::slotChangeReadOnlyMode(bool on)
236 {
237     wpt.setReadOnlyMode(on);
238     setupGui();
239 }
240 
slotChangedImages(const QList<CGisItemWpt::image_t> & images)241 void CDetailsWpt::slotChangedImages(const QList<CGisItemWpt::image_t>& images)
242 {
243     wpt.setImages(images);
244     setupGui();
245 }
246