Marble

LayerManager.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2008 Torsten Rahn <[email protected]>
4 // SPDX-FileCopyrightText: 2009 Jens-Michael Hoffmann <[email protected]>
5 // SPDX-FileCopyrightText: 2011, 2012 Bernahrd Beschow <[email protected]>
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 
23 namespace Marble
24 {
25 
26 class 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 
46 LayerManager::Private::Private(LayerManager *parent) :
47  q(parent),
48  m_renderPlugins(),
49  m_showBackground(true),
50  m_showRuntimeTrace(false)
51 {
52 }
53 
54 LayerManager::Private::~Private()
55 {
56 }
57 
58 void LayerManager::Private::updateVisibility( bool visible, const QString &nameId )
59 {
60  emit q->visibilityChanged( nameId, visible );
61 }
62 
63 
64 LayerManager::LayerManager(QObject *parent) :
65  QObject(parent),
66  d(new Private(this))
67 {
68 }
69 
70 LayerManager::~LayerManager()
71 {
72  delete d;
73 }
74 
75 bool LayerManager::showBackground() const
76 {
77  return d->m_showBackground;
78 }
79 
80 bool LayerManager::showRuntimeTrace() const
81 {
82  return d->m_showRuntimeTrace;
83 }
84 
85 void 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 
104 {
105  return d->m_dataPlugins;
106 }
107 
109 {
111 
112  for( auto *plugin: d->m_dataPlugins ) {
113  itemList.append( plugin->whichItemAt( curpos ) );
114  }
115  return itemList;
116 }
117 
118 void 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 ) {
145  QList<LayerInterface*> layers;
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();
189  painter->setBackgroundMode( Qt::OpaqueMode );
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 
207 void LayerManager::setShowBackground( bool show )
208 {
209  d->m_showBackground = show;
210 }
211 
212 void LayerManager::setShowRuntimeTrace( bool show )
213 {
214  d->m_showRuntimeTrace = show;
215 }
216 
217 void LayerManager::addLayer(LayerInterface *layer)
218 {
219  if (!d->m_internalLayers.contains(layer)) {
220  d->m_internalLayers.push_back(layer);
221  }
222 }
223 
224 void LayerManager::removeLayer(LayerInterface *layer)
225 {
226  d->m_internalLayers.removeAll(layer);
227 }
228 
229 QList<LayerInterface *> LayerManager::internalLayers() const
230 {
231  return d->m_internalLayers;
232 }
233 
234 RenderState LayerManager::renderState() const
235 {
236  return d->m_renderState;
237 }
238 
239 }
240 
241 #include "moc_LayerManager.cpp"
void append(const T &value)
QList< AbstractDataPluginItem * > whichItemAt(const QPoint &curpos) const
Returns all items of dataPlugins on the position curpos.
void pluginSettingsChanged()
This signal is emitted when the settings of a plugin changed.
void setBackgroundMode(Qt::BGMode mode)
void repaintNeeded(const QRegion &dirtyRegion=QRegion())
This signal is emitted when the repaint of the view was requested by a plugin.
OpaqueMode
void setBackground(const QBrush &brush)
void push_back(const T &value)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
A public class that controls what is visible in the viewport of a Marble map.
void removeLayer(LayerInterface *layer)
Remove a layer from being included in rendering.
QFontMetrics fontMetrics() const const
Binds a QML item to a specific geodetic location in screen coordinates.
qint64 elapsed() const const
A painter that allows to draw geometric primitives on the map.
Definition: GeoPainter.h:88
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...
Definition: GeoPainter.cpp:284
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void restore()
QList::iterator begin()
QList< AbstractDataPlugin * > dataPlugins() const
Returns a list of all DataPlugins on the layer.
void save()
void setFont(const QFont &font)
QList::iterator end()
void renderPluginInitialized(RenderPlugin *renderPlugin)
Signal that a render item has been initialized.
int height() const const
void addLayer(LayerInterface *layer)
Add a layer to be included in rendering.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 04:12:27 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.