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
9// Own
10#include "LayerManager.h"
11
12// Local dir
13#include "MarbleDebug.h"
14#include "AbstractDataPlugin.h"
15#include "AbstractDataPluginItem.h"
16#include "GeoPainter.h"
17#include "RenderPlugin.h"
18#include "LayerInterface.h"
19#include "RenderState.h"
20
21#include <QElapsedTimer>
22
23namespace Marble
24{
25
26class Q_DECL_HIDDEN LayerManager::Private
27{
28 public:
29 Private(LayerManager *parent);
30 ~Private();
31
32 void updateVisibility( bool visible, const QString &nameId );
33
34 LayerManager *const q;
35
36 QList<RenderPlugin *> m_renderPlugins;
37 QList<AbstractDataPlugin *> m_dataPlugins;
38 QList<LayerInterface *> m_internalLayers;
39
40 RenderState m_renderState;
41
42 bool m_showBackground;
43 bool m_showRuntimeTrace;
44};
45
46LayerManager::Private::Private(LayerManager *parent) :
47 q(parent),
48 m_renderPlugins(),
49 m_showBackground(true),
50 m_showRuntimeTrace(false)
51{
52}
53
54LayerManager::Private::~Private()
55{
56}
57
58void LayerManager::Private::updateVisibility( bool visible, const QString &nameId )
59{
60 emit q->visibilityChanged( nameId, visible );
61}
62
63
64LayerManager::LayerManager(QObject *parent) :
65 QObject(parent),
66 d(new Private(this))
67{
68}
69
70LayerManager::~LayerManager()
71{
72 delete d;
73}
74
75bool LayerManager::showBackground() const
76{
77 return d->m_showBackground;
78}
79
80bool LayerManager::showRuntimeTrace() const
81{
82 return d->m_showRuntimeTrace;
83}
84
85void LayerManager::addRenderPlugin(RenderPlugin *renderPlugin)
86{
87 d->m_renderPlugins.append(renderPlugin);
88
89 QObject::connect(renderPlugin, SIGNAL(settingsChanged(QString)),
90 this, SIGNAL(pluginSettingsChanged()));
91 QObject::connect(renderPlugin, SIGNAL(repaintNeeded(QRegion)),
92 this, SIGNAL(repaintNeeded(QRegion)));
93 QObject::connect(renderPlugin, SIGNAL(visibilityChanged(bool,QString)),
94 this, SLOT(updateVisibility(bool,QString)));
95
96 // get data plugins
97 AbstractDataPlugin *const dataPlugin = qobject_cast<AbstractDataPlugin *>(renderPlugin);
98 if(dataPlugin) {
99 d->m_dataPlugins.append(dataPlugin);
100 }
101}
102
103QList<AbstractDataPlugin *> LayerManager::dataPlugins() const
104{
105 return d->m_dataPlugins;
106}
107
108QList<AbstractDataPluginItem *> LayerManager::whichItemAt( const QPoint& curpos ) const
109{
111
112 for( auto *plugin: d->m_dataPlugins ) {
113 itemList.append( plugin->whichItemAt( curpos ) );
114 }
115 return itemList;
116}
117
118void LayerManager::renderLayers( GeoPainter *painter, ViewportParams *viewport )
119{
120 d->m_renderState = RenderState(QStringLiteral("Marble"));
121 QElapsedTimer totalTime;
122 totalTime.start();
123
124 QStringList renderPositions;
125
126 if ( d->m_showBackground ) {
127 renderPositions
128 << QStringLiteral("STARS")
129 << QStringLiteral("BEHIND_TARGET");
130 }
131
132 renderPositions
133 << QStringLiteral("SURFACE")
134 << QStringLiteral("HOVERS_ABOVE_SURFACE")
135 << QStringLiteral("GRATICULE")
136 << QStringLiteral("PLACEMARKS")
137 << QStringLiteral("ATMOSPHERE")
138 << QStringLiteral("ORBIT")
139 << QStringLiteral("ALWAYS_ON_TOP")
140 << QStringLiteral("FLOAT_ITEM")
141 << QStringLiteral("USER_TOOLS");
142
143 QStringList traceList;
144 for( const auto& renderPosition: renderPositions ) {
146
147 // collect all RenderPlugins of current renderPosition
148 for( auto *renderPlugin: d->m_renderPlugins ) {
149 if ( renderPlugin && renderPlugin->renderPosition().contains( renderPosition ) ) {
150 if ( renderPlugin->enabled() && renderPlugin->visible() ) {
151 if ( !renderPlugin->isInitialized() ) {
152 renderPlugin->initialize();
153 emit renderPluginInitialized( renderPlugin );
154 }
155 layers.push_back( renderPlugin );
156 }
157 }
158 }
159
160 // collect all internal LayerInterfaces of current renderPosition
161 for( auto *layer: d->m_internalLayers ) {
162 if ( layer && layer->renderPosition().contains( renderPosition ) ) {
163 layers.push_back( layer );
164 }
165 }
166
167 // sort them according to their zValue()s
168 std::sort( layers.begin(), layers.end(), [] ( const LayerInterface * const one, const LayerInterface * const two ) -> bool {
169 Q_ASSERT( one && two );
170 return one->zValue() < two->zValue();
171 } );
172
173 // render the layers of the current renderPosition
174 QElapsedTimer timer;
175 for( auto *layer: layers ) {
176 timer.start();
177 layer->render( painter, viewport, renderPosition, nullptr );
178 d->m_renderState.addChild( layer->renderState() );
179 traceList.append( QString("%2 ms %3").arg( timer.elapsed(),3 ).arg( layer->runtimeTrace() ) );
180 }
181 }
182
183 if ( d->m_showRuntimeTrace ) {
184 const int totalElapsed = totalTime.elapsed();
185 const int fps = 1000.0/totalElapsed;
186 traceList.append( QString( "Total: %1 ms (%2 fps)" ).arg( totalElapsed, 3 ).arg( fps ) );
187
188 painter->save();
190 painter->setBackground( Qt::gray );
191 painter->setFont( QFont( QStringLiteral("Sans Serif"), 10, QFont::Bold ) );
192
193 int i=0;
194 int const top = 150;
195 int const lineHeight = painter->fontMetrics().height();
196 for ( const auto &text: traceList ) {
197 painter->setPen( Qt::black );
198 painter->drawText( QPoint(10,top+1+lineHeight*i), text );
199 painter->setPen( Qt::white );
200 painter->drawText( QPoint(9,top+lineHeight*i), text );
201 ++i;
202 }
203 painter->restore();
204 }
205}
206
207void LayerManager::setShowBackground( bool show )
208{
209 d->m_showBackground = show;
210}
211
212void LayerManager::setShowRuntimeTrace( bool show )
213{
214 d->m_showRuntimeTrace = show;
215}
216
217void LayerManager::addLayer(LayerInterface *layer)
218{
219 if (!d->m_internalLayers.contains(layer)) {
220 d->m_internalLayers.push_back(layer);
221 }
222}
223
224void LayerManager::removeLayer(LayerInterface *layer)
225{
226 d->m_internalLayers.removeAll(layer);
227}
228
229QList<LayerInterface *> LayerManager::internalLayers() const
230{
231 return d->m_internalLayers;
232}
233
234RenderState LayerManager::renderState() const
235{
236 return d->m_renderState;
237}
238
239}
240
241#include "moc_LayerManager.cpp"
A painter that allows to draw geometric primitives on the map.
Definition GeoPainter.h:89
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)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QFontMetrics fontMetrics() const const
void restore()
void save()
void setBackground(const QBrush &brush)
void setBackgroundMode(Qt::BGMode mode)
void setFont(const QFont &font)
QString arg(Args &&... args) const const
OpaqueMode
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:00:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.