Marble

PlacemarkEditHeader.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2014 Mikhail Ivchenko <ematirov@gmail.com>
4
5#include "PlacemarkEditHeader.h"
6#include "ui_PlacemarkEditHeader.h"
7
8#include "MarbleDebug.h"
9#include <QFileDialog>
10
11namespace Marble
12{
13
14class PlacemarkEditHeaderPrivate : public Ui::PlacemarkEditHeaderPrivate
15{
16public:
17 explicit PlacemarkEditHeaderPrivate(PlacemarkEditHeader *_q);
18 ~PlacemarkEditHeaderPrivate();
19 void init(QWidget *parent);
20 void setNotation(GeoDataCoordinates::Notation notation);
21 GeoDataCoordinates::Notation notation() const;
22 void setName(const QString &name);
23 QString name() const;
24 void setIconLink(const QString &iconLink);
25 QString iconLink() const;
26 qreal longitude() const;
27 void setLongitude(qreal longitude);
28 qreal latitude() const;
29 void setLatitude(qreal latitude);
30 QString id() const;
31 QString setId(const QString &id, bool isNew = false);
32 QStringList idFilter() const;
33 void setIdFilter(const QStringList &filter);
34 bool isIdVisible() const;
35 void setIdVisible(bool visible);
36 bool isIdValid() const;
37 void loadIconFile();
38 bool positionVisible() const;
39 void setPositionVisible(bool visible);
40 QString targetId() const;
41 void setTargetId(const QString &targetId);
42 QStringList targetIdList() const;
43 void setTargetIdList(const QStringList &targetIdList);
44 bool isTargetIdVisible() const;
45 void setTargetIdVisible(bool visible);
46 void updateValues();
47 void selectAll();
48
49private:
50 PlacemarkEditHeader *const q;
51 bool m_positionVisible;
52 QStringList m_idFilter;
53 bool m_idWasEdited;
54 bool m_idValid;
55 QString m_idString;
56 QString m_targetIdString;
57};
58
59PlacemarkEditHeaderPrivate::PlacemarkEditHeaderPrivate(PlacemarkEditHeader *_q)
60 : q(_q)
61 , m_positionVisible(true)
62 , m_idWasEdited(false)
63{
64}
65
66PlacemarkEditHeaderPrivate::~PlacemarkEditHeaderPrivate() = default;
67
68void PlacemarkEditHeaderPrivate::init(QWidget *parent)
69{
70 setupUi(parent);
71 m_longitude->setDimension(LatLonEdit::Longitude);
72 m_latitude->setDimension(LatLonEdit::Latitude);
73 m_targetId->setVisible(false);
74 targetIdLabel->setVisible(false);
75 setNotation(GeoDataCoordinates::DMS);
76 QObject::connect(iconLinkButton, SIGNAL(clicked()), q, SLOT(loadIconFile()));
77}
78
79void PlacemarkEditHeaderPrivate::setNotation(GeoDataCoordinates::Notation notation)
80{
81 m_longitude->setNotation(notation);
82 m_latitude->setNotation(notation);
83}
84
85GeoDataCoordinates::Notation PlacemarkEditHeaderPrivate::notation() const
86{
87 return m_longitude->notation();
88}
89
90void PlacemarkEditHeaderPrivate::setName(const QString &name)
91{
92 m_name->setText(name);
93 if (!m_idWasEdited) {
94 setId(name);
95 }
96}
97
98QString PlacemarkEditHeaderPrivate::name() const
99{
100 return m_name->text();
101}
102
103void PlacemarkEditHeaderPrivate::setIconLink(const QString &iconLink)
104{
105 QPixmap pixmap(iconLink);
106 if (!pixmap.isNull()) {
107 QIcon icon(pixmap);
108 m_iconLink->setText(iconLink);
109 iconLinkButton->setText(QString());
110 iconLinkButton->setIcon(icon);
111 }
112}
113
114QString PlacemarkEditHeaderPrivate::iconLink() const
115{
116 return m_iconLink->text();
117}
118
119qreal PlacemarkEditHeaderPrivate::longitude() const
120{
121 return m_longitude->value();
122}
123
124void PlacemarkEditHeaderPrivate::setLongitude(qreal longitude)
125{
126 m_longitude->setValue(longitude);
127}
128
129qreal PlacemarkEditHeaderPrivate::latitude() const
130{
131 return m_latitude->value();
132}
133
134void PlacemarkEditHeaderPrivate::setLatitude(qreal latitude)
135{
136 m_latitude->setValue(latitude);
137}
138
139QString PlacemarkEditHeaderPrivate::id() const
140{
141 return m_id->text();
142}
143
144QString PlacemarkEditHeaderPrivate::setId(const QString &id, bool isNew)
145{
146 QString newId = id.toLower().replace(QLatin1Char(' '), QLatin1Char('_'));
147 if (newId == m_idString && !isNew) {
148 return newId;
149 }
150 if (!isNew) {
151 m_idFilter.removeOne(m_idString);
152 }
153 if (m_idFilter.contains(newId) && m_id->hasFocus()) {
154 m_idValid = false;
155 } else {
156 m_idValid = true;
157 if (m_idFilter.contains(newId)) {
159 for (const QString &filterString : std::as_const(m_idFilter)) {
160 if (filterString.startsWith(newId)) {
161 filter.append(filterString);
162 }
163 }
164 int i = 2;
165 while (filter.contains(newId + QString::number(i))) {
166 i++;
167 }
168 newId += QString::number(i);
169 }
170 }
171 m_idString = newId;
172 m_idFilter.append(newId);
173 m_id->setText(newId);
174 return newId;
175}
176
177QStringList PlacemarkEditHeaderPrivate::idFilter() const
178{
179 return m_idFilter;
180}
181
182void PlacemarkEditHeaderPrivate::setIdFilter(const QStringList &filter)
183{
184 m_idFilter = filter;
185 setId(id(), true);
186}
187
188bool PlacemarkEditHeaderPrivate::isIdVisible() const
189{
190 return m_id->isVisible() && idLabel->isVisible();
191}
192
193void PlacemarkEditHeaderPrivate::setIdVisible(bool visible)
194{
195 m_id->setVisible(visible);
196 idLabel->setVisible(visible);
197}
198
199bool PlacemarkEditHeaderPrivate::isIdValid() const
200{
201 return m_idValid;
202}
203
204void PlacemarkEditHeaderPrivate::loadIconFile()
205{
206 const QString filename = QFileDialog::getOpenFileName(q, QObject::tr("Open File"), QString(), QObject::tr("All Supported Files (*.png)"));
207 if (filename.isNull()) {
208 return;
209 }
210
211 setIconLink(filename);
212 iconLinkButton->setIcon(QIcon(filename));
213}
214
215bool PlacemarkEditHeaderPrivate::positionVisible() const
216{
217 return m_positionVisible;
218}
219
220void PlacemarkEditHeaderPrivate::setPositionVisible(bool visible)
221{
222 m_longitude->setVisible(visible);
223 m_latitude->setVisible(visible);
224 positionLabel->setVisible(visible);
225 m_positionVisible = visible;
226}
227
228QString PlacemarkEditHeaderPrivate::targetId() const
229{
230 return m_targetId->currentText();
231}
232
233void PlacemarkEditHeaderPrivate::setTargetId(const QString &targetId)
234{
235 m_targetId->setCurrentIndex(m_targetId->findText(targetId));
236 m_targetIdString = targetId;
237}
238
239QStringList PlacemarkEditHeaderPrivate::targetIdList() const
240{
241 QStringList result;
242 for (int i = 0; i < m_targetId->count(); ++i) {
243 result.append(m_targetId->itemText(i));
244 }
245 return result;
246}
247
248void PlacemarkEditHeaderPrivate::setTargetIdList(const QStringList &targetIdList)
249{
250 QString current;
251 if (m_targetId->currentIndex() != -1) {
252 current = m_targetId->currentText();
253 } else {
254 current = m_targetIdString;
255 }
256 m_targetId->clear();
257 m_targetId->addItems(targetIdList);
258 setTargetId(current);
259}
260
261bool PlacemarkEditHeaderPrivate::isTargetIdVisible() const
262{
263 return m_targetId->isVisible() && targetIdLabel->isVisible();
264}
265
266void PlacemarkEditHeaderPrivate::setTargetIdVisible(bool visible)
267{
268 m_targetId->setVisible(visible);
269 targetIdLabel->setVisible(visible);
270}
271
272void PlacemarkEditHeaderPrivate::updateValues()
273{
274 if (m_idString != id()) {
275 setId(id());
276 m_idWasEdited = true;
277 } else if (!m_idWasEdited && isIdVisible()) {
278 setId(name());
279 }
280}
281
282void PlacemarkEditHeaderPrivate::selectAll()
283{
284 m_name->selectAll();
285}
286
287}
288
289using namespace Marble;
290
291PlacemarkEditHeader::PlacemarkEditHeader(QWidget *parent,
293 const QString &name,
294 const QString &iconLink,
295 const QString &id,
296 const QStringList &idFilter)
297 : QWidget(parent)
298 , d(new PlacemarkEditHeaderPrivate(this))
299{
300 d->init(this);
301 d->setNotation(notation);
302 d->setName(name);
303 d->setIconLink(iconLink);
304 d->setIdFilter(idFilter);
305 d->setId(id.isEmpty() ? d->name() : id);
306 connect(d->m_longitude, SIGNAL(valueChanged(qreal)), this, SLOT(updateValues()));
307 connect(d->m_latitude, SIGNAL(valueChanged(qreal)), this, SLOT(updateValues()));
308 connect(d->m_name, SIGNAL(textChanged(QString)), this, SLOT(updateValues()));
309 connect(d->m_iconLink, SIGNAL(textChanged(QString)), this, SLOT(updateValues()));
310 connect(d->m_id, SIGNAL(textChanged(QString)), this, SLOT(updateValues()));
311}
312
313PlacemarkEditHeader::~PlacemarkEditHeader()
314{
315 delete d;
316}
317
318QString PlacemarkEditHeader::name() const
319{
320 return d->name();
321}
322
323QString PlacemarkEditHeader::iconLink() const
324{
325 return d->iconLink();
326}
327
328qreal PlacemarkEditHeader::latitude() const
329{
330 return d->latitude();
331}
332
333qreal PlacemarkEditHeader::longitude() const
334{
335 return d->longitude();
336}
337
338GeoDataCoordinates::Notation PlacemarkEditHeader::notation() const
339{
340 return d->notation();
341}
342
343bool PlacemarkEditHeader::positionVisible() const
344{
345 return d->positionVisible();
346}
347
348QString PlacemarkEditHeader::id() const
349{
350 return d->id();
351}
352
353QStringList PlacemarkEditHeader::idFilter() const
354{
355 return d->idFilter();
356}
357
358bool PlacemarkEditHeader::isIdVisible() const
359{
360 return d->isIdVisible();
361}
362
363bool PlacemarkEditHeader::isIdValid() const
364{
365 return d->isIdValid();
366}
367
368QString PlacemarkEditHeader::targetId() const
369{
370 return d->targetId();
371}
372
373QStringList PlacemarkEditHeader::targetIdList() const
374{
375 return d->targetIdList();
376}
377
378bool PlacemarkEditHeader::isTargetIdVisible() const
379{
380 return d->isTargetIdVisible();
381}
382
383void PlacemarkEditHeader::setName(const QString &name)
384{
385 d->setName(name);
386}
387
388void PlacemarkEditHeader::setIconLink(const QString &iconLink)
389{
390 d->setIconLink(iconLink);
391}
392
393void PlacemarkEditHeader::setLatitude(qreal latitude)
394{
395 d->setLatitude(latitude);
396}
397
398void PlacemarkEditHeader::setLongitude(qreal longitude)
399{
400 d->setLongitude(longitude);
401}
402
403void PlacemarkEditHeader::setNotation(GeoDataCoordinates::Notation notation)
404{
405 d->setNotation(notation);
406}
407
408void PlacemarkEditHeader::setPositionVisible(bool visible)
409{
410 d->setPositionVisible(visible);
411}
412
413QString PlacemarkEditHeader::setId(const QString &id)
414{
415 return d->setId(id);
416}
417
418void PlacemarkEditHeader::setIdFilter(const QStringList &filter)
419{
420 d->setIdFilter(filter);
421}
422
423void PlacemarkEditHeader::setIdVisible(bool visible)
424{
425 d->setIdVisible(visible);
426}
427
428void PlacemarkEditHeader::setTargetId(const QString &targetId)
429{
430 d->setTargetId(targetId);
431}
432
433void PlacemarkEditHeader::setTargetIdList(const QStringList &targetIdList)
434{
435 d->setTargetIdList(targetIdList);
436}
437
438void PlacemarkEditHeader::setTargetIdVisible(bool visible)
439{
440 d->setTargetIdVisible(visible);
441}
442
443void PlacemarkEditHeader::selectAll()
444{
445 d->selectAll();
446}
447
448void PlacemarkEditHeader::setReadOnly(bool state)
449{
450 d->m_longitude->setDisabled(state);
451 d->m_latitude->setDisabled(state);
452 d->m_name->setReadOnly(state);
453 d->m_iconLink->setReadOnly(state);
454 d->iconLinkButton->setDisabled(state);
455 d->m_id->setReadOnly(state);
456 d->m_targetId->setDisabled(state);
457 d->m_iconLink->setReadOnly(state);
458
459 d->idLabel->setVisible(!state);
460 d->m_id->setVisible(!state);
461}
462
463void PlacemarkEditHeader::updateValues()
464{
465 disconnect(d->m_id, SIGNAL(textChanged(QString)), this, SLOT(updateValues()));
466 d->updateValues();
467 connect(d->m_id, SIGNAL(textChanged(QString)), this, SLOT(updateValues()));
468 Q_EMIT valueChanged();
469}
470
471#include "moc_PlacemarkEditHeader.cpp"
Notation
enum used to specify the notation / numerical system
@ DMS
"Sexagesimal DMS" notation (base-60)
QString name(GameStandardAction id)
Binds a QML item to a specific geodetic location in screen coordinates.
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
void append(QList< T > &&value)
bool removeOne(const AT &t)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QString tr(const char *sourceText, const char *disambiguation, int n)
void clear()
bool isNull() const const
QString number(double n, char format, int precision)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QString toLower() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:55:54 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.