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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2023-05-15
* Description : geolocation engine based on Marble.
* (c) 2007-2022 Marble Team
* https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
*
* SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* ============================================================ */
#include "CompassFloatItem.h"
// Qt includes
#include <QRect>
#include <QColor>
#include <QPainter>
#include <QPainterPath>
#include <QPushButton>
#include <QSvgRenderer>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "ui_CompassConfigWidget.h"
#include "ViewportParams.h"
namespace Marble
{
CompassFloatItem::CompassFloatItem()<--- Member variable 'CompassFloatItem::m_isInitialized' is not initialized in the constructor. [+]Member variable 'CompassFloatItem::m_isInitialized' is not initialized in the constructor. Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.
: AbstractFloatItem(nullptr),
m_svgobj(nullptr),
m_polarity(0),
m_themeIndex(0),
m_configDialog(nullptr),
m_uiConfigWidget(nullptr)
{
}
CompassFloatItem::CompassFloatItem(const MarbleModel* marbleModel)
: AbstractFloatItem(marbleModel, QPointF(-1.0, 10.0), QSizeF(75.0, 75.0)),
m_isInitialized(false),
m_svgobj(nullptr),
m_compass(),
m_polarity(0),
m_themeIndex(0),
m_configDialog(nullptr),
m_uiConfigWidget(nullptr)
{
}
CompassFloatItem::~CompassFloatItem()
{
delete m_svgobj;
}
QStringList CompassFloatItem::backendTypes() const
{
return QStringList(QStringLiteral("compass"));
}
QString CompassFloatItem::name() const
{
return i18n("Compass");
}
QString CompassFloatItem::guiString() const
{
return i18n("&Compass");
}
QString CompassFloatItem::nameId() const
{
return QStringLiteral("compass");
}
QString CompassFloatItem::version() const
{
return QStringLiteral("1.0");
}
QString CompassFloatItem::description() const
{
return i18n("A plugin to show a float item that provides a compass over the map.");
}
QString CompassFloatItem::copyrightYears() const
{
return QStringLiteral("2009, 2010");
}
QVector<PluginAuthor> CompassFloatItem::pluginAuthors() const
{
return QVector<PluginAuthor>()
<< PluginAuthor(QStringLiteral("Dennis Nienhüser"), QStringLiteral("nienhueser@kde.org"))
<< PluginAuthor(QStringLiteral("Torsten Rahn"), QStringLiteral("tackat@kde.org"));
}
QIcon CompassFloatItem::icon() const
{
return QIcon::fromTheme(QStringLiteral("compass"));
}
void CompassFloatItem::initialize()
{
readSettings();
m_isInitialized = true;
}
bool CompassFloatItem::isInitialized() const
{
return m_isInitialized;
}
QPainterPath CompassFloatItem::backgroundShape() const
{
QRectF contentRect = this->contentRect();
QPainterPath path;
int fontheight = QFontMetrics(font()).ascent();
int compassLength = static_cast<int>(contentRect.height()) - 5 - fontheight;
path.addEllipse(QRectF(QPointF(marginLeft() + padding() + (contentRect.width() - compassLength) / 2,
marginTop() + padding() + 5 + fontheight),
QSize(compassLength, compassLength)).toRect());
return path;
}
void CompassFloatItem::setProjection(const ViewportParams* viewport)
{
// figure out the polarity ...
if (m_polarity != viewport->polarity())
{
m_polarity = viewport->polarity();
update();
}
AbstractFloatItem::setProjection(viewport);
}
void CompassFloatItem::paintContent(QPainter* painter)
{
painter->save();
QRectF compassRect(contentRect());
const QString dirstr =
(m_polarity == +1) ? i18n("N") :
(m_polarity == -1) ? i18n("S") :
/*else*/ QString();
int fontheight = QFontMetrics(font()).ascent();
int fontwidth = QFontMetrics(font()).boundingRect(dirstr).width();
QPen outlinepen(background().color());
outlinepen.setWidth(2);
QBrush outlinebrush(pen().color());
QPainterPath outlinepath;
const QPointF baseline(0.5 * (qreal)(compassRect.width() - fontwidth),
(qreal)(fontheight) + 2.0);
outlinepath.addText(baseline, font(), dirstr);
painter->setPen(outlinepen);
painter->setBrush(outlinebrush);
painter->drawPath(outlinepath);
painter->setPen(Qt::NoPen);
painter->drawPath(outlinepath);
int compassLength = static_cast<int>(compassRect.height()) - 5 - fontheight;
QSize compassSize(compassLength, compassLength);
// Rerender compass pixmap if the size has changed
if (m_compass.isNull() || m_compass.size() != compassSize)
{
m_compass = QPixmap(compassSize);
m_compass.fill(Qt::transparent);
QPainter mapPainter(&m_compass);
mapPainter.setViewport(m_compass.rect());
m_svgobj->render(&mapPainter);
}
painter->drawPixmap(QPoint(static_cast<int>(compassRect.width() - compassLength) / 2, fontheight + 5), m_compass);
painter->restore();
}
QDialog* CompassFloatItem::configDialog()
{
if (!m_configDialog)
{
m_configDialog = new QDialog();
m_uiConfigWidget = new Ui::CompassConfigWidget;
m_uiConfigWidget->setupUi(m_configDialog);
readSettings();
connect(m_uiConfigWidget->m_buttonBox, SIGNAL(accepted()),
SLOT(writeSettings()));
connect(m_uiConfigWidget->m_buttonBox, SIGNAL(rejected()),
SLOT(readSettings()));
QPushButton* applyButton = m_uiConfigWidget->m_buttonBox->button(QDialogButtonBox::Apply);
connect(applyButton, SIGNAL(clicked()),
this, SLOT(writeSettings()));
}
return m_configDialog;
}
QHash<QString, QVariant> CompassFloatItem::settings() const
{
QHash<QString, QVariant> result = AbstractFloatItem::settings();
result.insert(QStringLiteral("theme"), m_themeIndex);
return result;
}
void CompassFloatItem::setSettings(const QHash<QString, QVariant>& settings)
{
AbstractFloatItem::setSettings(settings);
m_themeIndex = settings.value(QStringLiteral("theme"), 0).toInt();
readSettings();
}
void CompassFloatItem::readSettings()
{
if (m_uiConfigWidget && m_themeIndex >= 0 && m_themeIndex < m_uiConfigWidget->m_themeList->count())
{
m_uiConfigWidget->m_themeList->setCurrentRow(m_themeIndex);
}
QString theme = QStringLiteral(":/compass.svg");
switch (m_themeIndex)
{
case 1:
theme = QStringLiteral(":/compass-arrows.svg");
break;
case 2:
theme = QStringLiteral(":/compass-atom.svg");
break;
case 3:
theme = QStringLiteral(":/compass-magnet.svg");
break;
}
delete m_svgobj;
m_svgobj = new QSvgRenderer(theme, this);
m_compass = QPixmap();
}
void CompassFloatItem::writeSettings()
{
if (m_uiConfigWidget)
{
m_themeIndex = m_uiConfigWidget->m_themeList->currentRow();
}
readSettings();
update();
Q_EMIT settingsChanged(nameId());
}
} // namespace Marble
#include "moc_CompassFloatItem.cpp"
|