• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
AbstractDataPlugin.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2009 Bastian Holst <bastianholst@gmx.de>
9 //
10 
11 // Self
12 #include "AbstractDataPlugin.h"
13 
14 // Marble
15 #include "AbstractDataPluginModel.h"
16 #include "AbstractDataPluginItem.h"
17 #include "GeoPainter.h"
18 #include "GeoSceneLayer.h"
19 #include "MarbleModel.h"
20 #include "ViewportParams.h"
21 #include "MarbleDebug.h"
22 
23 // Qt
24 #include <QEvent>
25 #include <QTimer>
26 #include <QMouseEvent>
27 #include <QRegion>
28 #if QT_VERSION < 0x050000
29  #include <QDeclarativeComponent>
30  #include <QDeclarativeContext>
31  #include <QDeclarativeItem>
32  typedef QDeclarativeComponent QQmlComponent;
33  typedef QDeclarativeContext QQmlContext;
34  typedef QDeclarativeItem QQuickItem;
35 #else
36  #include <QQmlComponent>
37  #include <QQmlContext>
38  #include <QQuickItem>
39  #include <QGraphicsItem>
40 #endif
41 
42 namespace Marble
43 {
44 
45 class AbstractDataPluginPrivate
46 {
47  public:
48  AbstractDataPluginPrivate()
49  : m_model( 0 ),
50  m_numberOfItems( 10 ),
51  m_delegate( 0 ),
52  m_delegateParent( 0 )
53  {
54  m_updateTimer.setSingleShot( true );
55  }
56 
57  ~AbstractDataPluginPrivate() {
58  delete m_model;
59  }
60 
61  AbstractDataPluginModel *m_model;
62  quint32 m_numberOfItems;
63  QQmlComponent* m_delegate;
64  QGraphicsItem* m_delegateParent;
65  QMap<AbstractDataPluginItem*,QQuickItem*> m_delegateInstances;
66  QTimer m_updateTimer;
67 };
68 
69 AbstractDataPlugin::AbstractDataPlugin( const MarbleModel *marbleModel )
70  : RenderPlugin( marbleModel ),
71  d( new AbstractDataPluginPrivate )
72 {
73  connect( &d->m_updateTimer, SIGNAL(timeout()), this, SIGNAL(repaintNeeded()) );
74 }
75 
76 AbstractDataPlugin::~AbstractDataPlugin()
77 {
78  delete d;
79 }
80 
81 bool AbstractDataPlugin::isInitialized() const
82 {
83  return model() != 0;
84 }
85 
86 QStringList AbstractDataPlugin::backendTypes() const
87 {
88  return QStringList( name() );
89 }
90 
91 QString AbstractDataPlugin::renderPolicy() const
92 {
93  return QString( "ALWAYS" );
94 }
95 
96 QStringList AbstractDataPlugin::renderPosition() const
97 {
98  return QStringList( "ALWAYS_ON_TOP" );
99 }
100 
101 bool AbstractDataPlugin::render( GeoPainter *painter, ViewportParams *viewport,
102  const QString& renderPos, GeoSceneLayer * layer)
103 {
104  Q_UNUSED( renderPos );
105  Q_UNUSED( layer );
106 
107  if ( d->m_delegate ) {
108  handleViewportChange( viewport );
109  } else {
110  QList<AbstractDataPluginItem*> items = d->m_model->items( viewport, numberOfItems() );
111  painter->save();
112 
113  // Paint the most important item at last
114  for( int i = items.size() - 1; i >= 0; --i ) {
115  items.at( i )->paintEvent( painter, viewport );
116  }
117 
118  painter->restore();
119  }
120 
121  return true;
122 }
123 
124 AbstractDataPluginModel *AbstractDataPlugin::model()
125 {
126  return d->m_model;
127 }
128 
129 const AbstractDataPluginModel *AbstractDataPlugin::model() const
130 {
131  return d->m_model;
132 }
133 
134 void AbstractDataPlugin::setModel( AbstractDataPluginModel* model )
135 {
136  if ( d->m_model ) {
137  disconnect( d->m_model, SIGNAL(itemsUpdated()), this, SLOT(delayedUpdate()) );
138  delete d->m_model;
139  }
140  d->m_model = model;
141 
142  connect( d->m_model, SIGNAL(itemsUpdated()), this, SLOT(delayedUpdate()) );
143  connect( d->m_model, SIGNAL(favoriteItemsChanged(QStringList)), this,
144  SLOT(favoriteItemsChanged(QStringList)) );
145  connect( d->m_model, SIGNAL(favoriteItemsOnlyChanged()), this,
146  SIGNAL(favoriteItemsOnlyChanged()) );
147 
148  emit favoritesModelChanged();
149 }
150 
151 quint32 AbstractDataPlugin::numberOfItems() const
152 {
153  return d->m_numberOfItems;
154 }
155 
156 void AbstractDataPlugin::setNumberOfItems( quint32 number )
157 {
158  bool changed = ( number != d->m_numberOfItems );
159  d->m_numberOfItems = number;
160 
161  if ( changed )
162  emit changedNumberOfItems( number );
163 }
164 
165 QList<AbstractDataPluginItem *> AbstractDataPlugin::whichItemAt( const QPoint& curpos )
166 {
167  if ( d->m_model && enabled() && visible()) {
168  return d->m_model->whichItemAt( curpos );
169  }
170  else {
171  return QList<AbstractDataPluginItem *>();
172  }
173 }
174 
175 RenderPlugin::RenderType AbstractDataPlugin::renderType() const
176 {
177  return OnlineRenderType;
178 }
179 
180 void AbstractDataPlugin::setDelegate( QQmlComponent *delegate, QGraphicsItem* parent )
181 {
182  qDeleteAll( d->m_delegateInstances.values() );
183  d->m_delegateInstances.clear();
184 
185  d->m_delegate = delegate;
186  d->m_delegateParent = parent;
187 }
188 
189 void AbstractDataPlugin::setFavoriteItemsOnly( bool favoriteOnly )
190 {
191  if ( d->m_model && d->m_model->isFavoriteItemsOnly() != favoriteOnly ) {
192  d->m_model->setFavoriteItemsOnly( favoriteOnly );
193  }
194 }
195 
196 bool AbstractDataPlugin::isFavoriteItemsOnly() const
197 {
198  return d->m_model && d->m_model->isFavoriteItemsOnly();
199 }
200 
201 QObject *AbstractDataPlugin::favoritesModel()
202 {
203  return d->m_model ? d->m_model->favoritesModel() : 0;
204 }
205 
206 void AbstractDataPlugin::handleViewportChange( const ViewportParams *viewport )
207 {
208  QList<AbstractDataPluginItem*> orphane = d->m_delegateInstances.keys();
209  QList<AbstractDataPluginItem*> const items = d->m_model->items( viewport, numberOfItems() );
210  foreach( AbstractDataPluginItem* item, items ) {
211  qreal x, y;
212  Marble::GeoDataCoordinates const coordinates = item->coordinate();
213  bool const visible = viewport->screenCoordinates( coordinates.longitude(), coordinates.latitude(), x, y );
214 
215  if ( !d->m_delegateInstances.contains( item ) ) {
216  if ( !visible ) {
217  // We don't have, but don't need it either. Shouldn't happen though as the model checks for it already.
218  continue;
219  }
220 
221  // Create a new QML object instance using the delegate as the factory. The original
222  // data plugin item is set as the context object, i.e. all its properties are available
223  // to QML directly with their names
224  QQmlContext *context = new QQmlContext( qmlContext( d->m_delegate ) );
225  context->setContextObject( item );
226  QList<QByteArray> const dynamicProperties = item->dynamicPropertyNames();
227  foreach( const QByteArray &property, dynamicProperties ) {
228  context->setContextProperty( property, item->property( property ) );
229  }
230 
231  QObject* component = d->m_delegate->create( context );
232  QQuickItem* newItem = qobject_cast<QQuickItem*>( component );
233  QGraphicsItem* graphicsItem = qobject_cast<QGraphicsItem*>( component );
234  if ( graphicsItem && newItem ) {
235  graphicsItem->setParentItem( d->m_delegateParent );
236  }
237 
238  if ( newItem ) {
239  d->m_delegateInstances[item] = newItem;
240  } else {
241  mDebug() << "Failed to create delegate";
242  continue;
243  }
244  } else if ( !visible ) {
245  // Previously visible but not anymore => needs to be deleted. Orphane list takes care of it later.
246  // Shouldn't happen though as the model checks for it already.
247  continue;
248  }
249 
250  Q_ASSERT( visible );
251  QQuickItem* declarativeItem = d->m_delegateInstances[item];
252  Q_ASSERT( declarativeItem );
253 
254  // Make sure we have a valid bounding rect for collision detection
255  item->setProjection( viewport );
256  item->setSize( QSizeF( declarativeItem->boundingRect().size() ) );
257 
258  int shiftX( 0 ), shiftY( 0 );
259  switch( declarativeItem->transformOrigin() ) {
260  case QQuickItem::TopLeft:
261  case QQuickItem::Top:
262  case QQuickItem::TopRight:
263  break;
264  case QQuickItem::Left:
265  case QQuickItem::Center:
266  case QQuickItem::Right:
267  shiftY = declarativeItem->height() / 2;
268  break;
269  case QQuickItem::BottomLeft:
270  case QQuickItem::Bottom:
271  case QQuickItem::BottomRight:
272  shiftY = declarativeItem->height();
273  break;
274  }
275 
276  switch( declarativeItem->transformOrigin() ) {
277  case QQuickItem::TopLeft:
278  case QQuickItem::Left:
279  case QQuickItem::BottomLeft:
280  break;
281  case QQuickItem::Top:
282  case QQuickItem::Center:
283  case QQuickItem::Bottom:
284  shiftX = declarativeItem->width() / 2;
285  break;
286  case QQuickItem::TopRight:
287  case QQuickItem::Right:
288  case QQuickItem::BottomRight:
289  shiftX = declarativeItem->width();
290  break;
291  }
292 
293  declarativeItem->setX( x - shiftX );
294  declarativeItem->setY( y - shiftY );
295  orphane.removeOne( item );
296  }
297 
298  // Cleanup
299  foreach( AbstractDataPluginItem* item, orphane ) {
300  Q_ASSERT( d->m_delegateInstances.contains( item ) );
301  delete d->m_delegateInstances[item];
302  d->m_delegateInstances.remove( item );
303  }
304 }
305 
306 void AbstractDataPlugin::favoriteItemsChanged( const QStringList& favoriteItems )
307 {
308  Q_UNUSED( favoriteItems )
309 }
310 
311 void AbstractDataPlugin::delayedUpdate()
312 {
313  if ( !d->m_updateTimer.isActive() )
314  {
315  d->m_updateTimer.start( 500 );
316  }
317 }
318 
319 } // namespace Marble
320 
321 #include "AbstractDataPlugin.moc"
Marble::AbstractDataPlugin::setNumberOfItems
void setNumberOfItems(quint32 number)
Set the number of items to be shown at the same time.
Definition: AbstractDataPlugin.cpp:156
QDeclarativeContext::setContextProperty
void setContextProperty(const QString &name, QObject *value)
Marble::RenderPlugin::visible
bool visible() const
is visible
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
AbstractDataPluginModel.h
Marble::AbstractDataPlugin::changedNumberOfItems
void changedNumberOfItems(quint32 number)
Marble::MarbleGraphicsItem::setSize
void setSize(const QSizeF &size)
Set the size of the item.
Definition: MarbleGraphicsItem.cpp:197
Marble::RenderPlugin::repaintNeeded
void repaintNeeded(QRegion dirtyRegion=QRegion())
This signal is emitted if an update of the view is needed.
QByteArray
QGraphicsItem::setParentItem
void setParentItem(QGraphicsItem *newParent)
Marble::AbstractDataPluginItem
Definition: AbstractDataPluginItem.h:28
Marble::GeoPainter
A painter that allows to draw geometric primitives on the map.
Definition: GeoPainter.h:98
QDeclarativeContext::setContextObject
void setContextObject(QObject *object)
MarbleModel.h
This file contains the headers for MarbleModel.
QRectF::size
QSizeF size() const
QList::at
const T & at(int i) const
QMap
QPainter::save
void save()
Marble::AbstractDataPlugin::setFavoriteItemsOnly
void setFavoriteItemsOnly(bool favoriteOnly)
Convenience method to set the favorite item state on the current model.
Definition: AbstractDataPlugin.cpp:189
Marble::AbstractDataPluginModel
An abstract data model (not based on QAbstractModel) for a AbstractDataPlugin.
Definition: AbstractDataPluginModel.h:45
QGraphicsItem
QPoint
Marble::AbstractDataPlugin::renderPosition
QStringList renderPosition() const
Preferred level in the layer stack for the rendering.
Definition: AbstractDataPlugin.cpp:96
Marble::GeoDataCoordinates::latitude
qreal latitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
Definition: GeoDataCoordinates.cpp:751
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
MarbleDebug.h
Marble::AbstractDataPlugin::renderType
virtual RenderType renderType() const
Function for returning the type of plugin this is for.
Definition: AbstractDataPlugin.cpp:175
QList::size
int size() const
QObject::name
const char * name() const
Marble::AbstractDataPlugin::numberOfItems
quint32 numberOfItems() const
Marble::AbstractDataPlugin::isInitialized
bool isInitialized() const
Definition: AbstractDataPlugin.cpp:81
Marble::GeoSceneLayer
Layer of a GeoScene document.
Definition: GeoSceneLayer.h:43
QObject::property
QVariant property(const char *name) const
QTimer
QObject
QDeclarativeContext
QQmlContext
QDeclarativeContext QQmlContext
Definition: AbstractDataPlugin.cpp:33
Marble::AbstractDataPlugin::backendTypes
QStringList backendTypes() const
Returns the name(s) of the backend that the plugin can render.
Definition: AbstractDataPlugin.cpp:86
Marble::ViewportParams::screenCoordinates
bool screenCoordinates(const qreal lon, const qreal lat, qreal &x, qreal &y) const
Get the screen coordinates corresponding to geographical coordinates in the map.
Definition: ViewportParams.cpp:357
Marble::AbstractDataPlugin::isFavoriteItemsOnly
bool isFavoriteItemsOnly() const
Definition: AbstractDataPlugin.cpp:196
Marble::AbstractDataPlugin::render
bool render(GeoPainter *painter, ViewportParams *viewport, const QString &renderPos="NONE", GeoSceneLayer *layer=0)
Renders the content provided by the plugin on the viewport.
Definition: AbstractDataPlugin.cpp:101
QString
QList< AbstractDataPluginItem * >
Marble::AbstractDataPlugin::setModel
void setModel(AbstractDataPluginModel *model)
Set the model of the plugin.
Definition: AbstractDataPlugin.cpp:134
GeoPainter.h
Marble::AbstractDataPlugin::whichItemAt
QList< AbstractDataPluginItem * > whichItemAt(const QPoint &curpos)
This function returns all items at the position curpos.
Definition: AbstractDataPlugin.cpp:165
QStringList
Marble::ViewportParams
A public class that controls what is visible in the viewport of a Marble map.
Definition: ViewportParams.h:44
Marble::AbstractDataPlugin::handleViewportChange
void handleViewportChange(const ViewportParams *viewport)
Definition: AbstractDataPlugin.cpp:206
Marble::AbstractDataPlugin::setDelegate
void setDelegate(QDeclarativeComponent *delegate, QGraphicsItem *parent)
Definition: AbstractDataPlugin.cpp:180
QDeclarativeComponent
Marble::GeoDataCoordinates::longitude
qreal longitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
Definition: GeoDataCoordinates.cpp:739
ViewportParams.h
This file contains the headers for ViewportParams.
QQuickItem
QDeclarativeItem QQuickItem
Definition: AbstractDataPlugin.cpp:34
Marble::AbstractDataPlugin::favoritesModel
QObject * favoritesModel()
Marble::AbstractDataPlugin::favoritesModelChanged
void favoritesModelChanged()
QPainter::restore
void restore()
Marble::MarbleModel
The data model (not based on QAbstractModel) for a MarbleWidget.
Definition: MarbleModel.h:97
Marble::AbstractDataPlugin::favoriteItemsOnlyChanged
void favoriteItemsOnlyChanged()
Marble::RenderPlugin::OnlineRenderType
Definition: RenderPlugin.h:63
QSizeF
QGraphicsItem::boundingRect
virtual QRectF boundingRect() const =0
Marble::AbstractDataPlugin::~AbstractDataPlugin
virtual ~AbstractDataPlugin()
Definition: AbstractDataPlugin.cpp:76
QQmlComponent
QDeclarativeComponent QQmlComponent
Definition: AbstractDataPlugin.cpp:32
Marble::BillboardGraphicsItem::coordinate
GeoDataCoordinates coordinate() const
Definition: BillboardGraphicsItem.cpp:93
Marble::AbstractDataPlugin::AbstractDataPlugin
AbstractDataPlugin(const MarbleModel *marbleModel)
Definition: AbstractDataPlugin.cpp:69
QGraphicsItem::setX
void setX(qreal x)
QGraphicsItem::setY
void setY(qreal y)
QDeclarativeItem
Marble::RenderPlugin::enabled
bool enabled() const
is enabled
QObject::dynamicPropertyNames
QList< QByteArray > dynamicPropertyNames() const
QDeclarativeItem::transformOrigin
TransformOrigin transformOrigin() const
AbstractDataPlugin.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
Marble::RenderPlugin::RenderType
RenderType
A Type of plugin.
Definition: RenderPlugin.h:59
Marble::AbstractDataPlugin::renderPolicy
QString renderPolicy() const
Return how the plugin settings should be used.
Definition: AbstractDataPlugin.cpp:91
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:36
QList::removeOne
bool removeOne(const T &value)
Marble::AbstractDataPlugin::model
AbstractDataPluginModel * model()
Definition: AbstractDataPlugin.cpp:124
Marble::RenderPlugin
The abstract class that creates a renderable item.
Definition: RenderPlugin.h:43
AbstractDataPluginItem.h
GeoSceneLayer.h
Marble::MarbleGraphicsItem::setProjection
virtual void setProjection(const ViewportParams *viewport)
Definition: MarbleGraphicsItem.cpp:272
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:38 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal