Marble

LayerManager.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2008 Torsten Rahn <tackat@kde.org>
4// SPDX-FileCopyrightText: 2009 Jens-Michael Hoffmann <jensmh@gmx.de>
5// SPDX-FileCopyrightText: 2011, 2012 Bernahrd Beschow <bbeschow@cs.tu-berlin.de>
6//
7
8// Own
9#include "LayerManager.h"
10
11// Local dir
12#include "AbstractDataPlugin.h"
13#include "AbstractDataPluginItem.h"
14#include "GeoPainter.h"
15#include "LayerInterface.h"
16#include "MarbleDebug.h"
17#include "RenderPlugin.h"
18#include "RenderState.h"
19
20#include <QElapsedTimer>
21
22namespace Marble
23{
24
25class Q_DECL_HIDDEN LayerManager::Private
26{
27public:
28 Private(LayerManager *parent);
29 ~Private();
30
31 void updateVisibility(bool visible, const QString &nameId);
32
33 LayerManager *const q;
34
35 QList<RenderPlugin *> m_renderPlugins;
36 QList<AbstractDataPlugin *> m_dataPlugins;
37 QList<LayerInterface *> m_internalLayers;
38
39 RenderState m_renderState;
40
41 bool m_showBackground;
42 bool m_showRuntimeTrace;
43};
44
45LayerManager::Private::Private(LayerManager *parent)
46 : q(parent)
47 , m_renderPlugins()
48 , m_showBackground(true)
49 , m_showRuntimeTrace(false)
50{
51}
52
53LayerManager::Private::~Private() = default;
54
55void LayerManager::Private::updateVisibility(bool visible, const QString &nameId)
56{
57 Q_EMIT q->visibilityChanged(nameId, visible);
58}
59
60LayerManager::LayerManager(QObject *parent)
61 : QObject(parent)
62 , d(new Private(this))
63{
64}
65
66LayerManager::~LayerManager()
67{
68 delete d;
69}
70
71bool LayerManager::showBackground() const
72{
73 return d->m_showBackground;
74}
75
76bool LayerManager::showRuntimeTrace() const
77{
78 return d->m_showRuntimeTrace;
79}
80
81void LayerManager::addRenderPlugin(RenderPlugin *renderPlugin)
82{
83 d->m_renderPlugins.append(renderPlugin);
84
85 connect(renderPlugin, SIGNAL(settingsChanged(QString)), this, SIGNAL(pluginSettingsChanged()));
86 connect(renderPlugin, SIGNAL(repaintNeeded(QRegion)), this, SIGNAL(repaintNeeded(QRegion)));
87 connect(renderPlugin, SIGNAL(visibilityChanged(bool, QString)), this, SLOT(updateVisibility(bool, QString)));
88
89 // get data plugins
90 auto const dataPlugin = qobject_cast<AbstractDataPlugin *>(renderPlugin);
91 if (dataPlugin) {
92 d->m_dataPlugins.append(dataPlugin);
93 }
94}
95
96QList<AbstractDataPlugin *> LayerManager::dataPlugins() const
97{
98 return d->m_dataPlugins;
99}
100
101QList<AbstractDataPluginItem *> LayerManager::whichItemAt(const QPoint &curpos) const
102{
104
105 for (auto plugin : d->m_dataPlugins) {
106 itemList.append(plugin->whichItemAt(curpos));
107 }
108 return itemList;
109}
110
111void LayerManager::renderLayers(GeoPainter *painter, ViewportParams *viewport)
112{
113 d->m_renderState = RenderState(QStringLiteral("Marble"));
114 QElapsedTimer totalTime;
115 totalTime.start();
116
117 QStringList renderPositions;
118
119 if (d->m_showBackground) {
120 renderPositions << QStringLiteral("STARS") << QStringLiteral("BEHIND_TARGET");
121 }
122
123 renderPositions << QStringLiteral("SURFACE") << QStringLiteral("HOVERS_ABOVE_SURFACE") << QStringLiteral("GRATICULE") << QStringLiteral("PLACEMARKS")
124 << QStringLiteral("ATMOSPHERE") << QStringLiteral("ORBIT") << QStringLiteral("ALWAYS_ON_TOP") << QStringLiteral("FLOAT_ITEM")
125 << QStringLiteral("USER_TOOLS");
126
127 QStringList traceList;
128 for (const auto &renderPosition : renderPositions) {
130
131 // collect all RenderPlugins of current renderPosition
132 for (auto renderPlugin : d->m_renderPlugins) {
133 if (renderPlugin && renderPlugin->renderPosition().contains(renderPosition)) {
134 if (renderPlugin->enabled() && renderPlugin->visible()) {
135 if (!renderPlugin->isInitialized()) {
136 renderPlugin->initialize();
137 Q_EMIT renderPluginInitialized(renderPlugin);
138 }
139 layers.push_back(renderPlugin);
140 }
141 }
142 }
143
144 // collect all internal LayerInterfaces of current renderPosition
145 for (auto layer : d->m_internalLayers) {
146 if (layer && layer->renderPosition().contains(renderPosition)) {
147 layers.push_back(layer);
148 }
149 }
150
151 // sort them according to their zValue()s
152 std::sort(layers.begin(), layers.end(), [](const LayerInterface *const one, const LayerInterface *const two) -> bool {
153 Q_ASSERT(one && two);
154 return one->zValue() < two->zValue();
155 });
156
157 // render the layers of the current renderPosition
158 QElapsedTimer timer;
159 for (auto layer : layers) {
160 timer.start();
161 layer->render(painter, viewport, renderPosition, nullptr);
162 d->m_renderState.addChild(layer->renderState());
163 traceList.append(QStringLiteral("%2 ms %3").arg(timer.elapsed(), 3).arg(layer->runtimeTrace()));
164 }
165 }
166
167 if (d->m_showRuntimeTrace) {
168 const int totalElapsed = totalTime.elapsed();
169 const int fps = 1000.0 / totalElapsed;
170 traceList.append(QStringLiteral("Total: %1 ms (%2 fps)").arg(totalElapsed, 3).arg(fps));
171
172 painter->save();
174 painter->setBackground(Qt::gray);
175 painter->setFont(QFont(QStringLiteral("Sans Serif"), 10, QFont::Bold));
176
177 int i = 0;
178 int const top = 150;
179 int const lineHeight = painter->fontMetrics().height();
180 for (const auto &text : traceList) {
181 painter->setPen(Qt::black);
182 painter->drawText(QPoint(10, top + 1 + lineHeight * i), text);
183 painter->setPen(Qt::white);
184 painter->drawText(QPoint(9, top + lineHeight * i), text);
185 ++i;
186 }
187 painter->restore();
188 }
189}
190
191void LayerManager::setShowBackground(bool show)
192{
193 d->m_showBackground = show;
194}
195
196void LayerManager::setShowRuntimeTrace(bool show)
197{
198 d->m_showRuntimeTrace = show;
199}
200
201void LayerManager::addLayer(LayerInterface *layer)
202{
203 if (!d->m_internalLayers.contains(layer)) {
204 d->m_internalLayers.push_back(layer);
205 }
206}
207
208void LayerManager::removeLayer(LayerInterface *layer)
209{
210 d->m_internalLayers.removeAll(layer);
211}
212
213QList<LayerInterface *> LayerManager::internalLayers() const
214{
215 return d->m_internalLayers;
216}
217
218RenderState LayerManager::renderState() const
219{
220 return d->m_renderState;
221}
222
223}
224
225#include "moc_LayerManager.cpp"
A painter that allows to draw geometric primitives on the map.
Definition GeoPainter.h:86
void drawText(const GeoDataCoordinates &position, const QString &text, qreal xOffset=0.0, qreal yOffset=0.0, qreal width=0.0, qreal height=0.0, const QTextOption &option=QTextOption())
Draws the given text at a given geographic position. The text is drawn starting at the given position...
A public class that controls what is visible in the viewport of a Marble map.
Binds a QML item to a specific geodetic location in screen coordinates.
qint64 elapsed() const const
int height() const const
void append(QList< T > &&value)
iterator begin()
iterator end()
void push_back(parameter_type value)
QFontMetrics fontMetrics() const const
void restore()
void save()
void setBackground(const QBrush &brush)
void setBackgroundMode(Qt::BGMode mode)
void setFont(const QFont &font)
OpaqueMode
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 Mon Nov 18 2024 12:15:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.