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

marble

  • sources
  • kde-4.12
  • 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() const
125 {
126  return d->m_model;
127 }
128 
129 void AbstractDataPlugin::setModel( AbstractDataPluginModel* model )
130 {
131  if ( d->m_model ) {
132  disconnect( d->m_model, SIGNAL(itemsUpdated()), this, SLOT(delayedUpdate()) );
133  delete d->m_model;
134  }
135  d->m_model = model;
136 
137  connect( d->m_model, SIGNAL(itemsUpdated()), this, SLOT(delayedUpdate()) );
138  connect( d->m_model, SIGNAL(favoriteItemsChanged(QStringList)), this,
139  SLOT(favoriteItemsChanged(QStringList)) );
140  connect( d->m_model, SIGNAL(favoriteItemsOnlyChanged()), this,
141  SIGNAL(favoriteItemsOnlyChanged()) );
142 
143  emit favoritesModelChanged();
144 }
145 
146 quint32 AbstractDataPlugin::numberOfItems() const
147 {
148  return d->m_numberOfItems;
149 }
150 
151 void AbstractDataPlugin::setNumberOfItems( quint32 number )
152 {
153  bool changed = ( number != d->m_numberOfItems );
154  d->m_numberOfItems = number;
155 
156  if ( changed )
157  emit changedNumberOfItems( number );
158 }
159 
160 QList<AbstractDataPluginItem *> AbstractDataPlugin::whichItemAt( const QPoint& curpos )
161 {
162  if ( d->m_model && enabled() && visible()) {
163  return d->m_model->whichItemAt( curpos );
164  }
165  else {
166  return QList<AbstractDataPluginItem *>();
167  }
168 }
169 
170 RenderPlugin::RenderType AbstractDataPlugin::renderType() const
171 {
172  return OnlineRenderType;
173 }
174 
175 void AbstractDataPlugin::setDelegate( QQmlComponent *delegate, QGraphicsItem* parent )
176 {
177  qDeleteAll( d->m_delegateInstances.values() );
178  d->m_delegateInstances.clear();
179 
180  d->m_delegate = delegate;
181  d->m_delegateParent = parent;
182 }
183 
184 void AbstractDataPlugin::setFavoriteItemsOnly( bool favoriteOnly )
185 {
186  if ( d->m_model && d->m_model->isFavoriteItemsOnly() != favoriteOnly ) {
187  d->m_model->setFavoriteItemsOnly( favoriteOnly );
188  }
189 }
190 
191 bool AbstractDataPlugin::isFavoriteItemsOnly() const
192 {
193  return d->m_model && d->m_model->isFavoriteItemsOnly();
194 }
195 
196 QObject *AbstractDataPlugin::favoritesModel()
197 {
198  return d->m_model ? d->m_model->favoritesModel() : 0;
199 }
200 
201 void AbstractDataPlugin::handleViewportChange( const ViewportParams *viewport )
202 {
203  QList<AbstractDataPluginItem*> orphane = d->m_delegateInstances.keys();
204  QList<AbstractDataPluginItem*> const items = d->m_model->items( viewport, numberOfItems() );
205  foreach( AbstractDataPluginItem* item, items ) {
206  qreal x, y;
207  Marble::GeoDataCoordinates const coordinates = item->coordinate();
208  bool const visible = viewport->screenCoordinates( coordinates.longitude(), coordinates.latitude(), x, y );
209 
210  if ( !d->m_delegateInstances.contains( item ) ) {
211  if ( !visible ) {
212  // We don't have, but don't need it either. Shouldn't happen though as the model checks for it already.
213  continue;
214  }
215 
216  // Create a new QML object instance using the delegate as the factory. The original
217  // data plugin item is set as the context object, i.e. all its properties are available
218  // to QML directly with their names
219  QQmlContext *context = new QQmlContext( qmlContext( d->m_delegate ) );
220  context->setContextObject( item );
221  QList<QByteArray> const dynamicProperties = item->dynamicPropertyNames();
222  foreach( const QByteArray &property, dynamicProperties ) {
223  context->setContextProperty( property, item->property( property ) );
224  }
225 
226  QObject* component = d->m_delegate->create( context );
227  QQuickItem* newItem = qobject_cast<QQuickItem*>( component );
228  QGraphicsItem* graphicsItem = qobject_cast<QGraphicsItem*>( component );
229  if ( graphicsItem && newItem ) {
230  graphicsItem->setParentItem( d->m_delegateParent );
231  }
232 
233  if ( newItem ) {
234  d->m_delegateInstances[item] = newItem;
235  } else {
236  mDebug() << "Failed to create delegate";
237  continue;
238  }
239  } else if ( !visible ) {
240  // Previously visible but not anymore => needs to be deleted. Orphane list takes care of it later.
241  // Shouldn't happen though as the model checks for it already.
242  continue;
243  }
244 
245  Q_ASSERT( visible );
246  QQuickItem* declarativeItem = d->m_delegateInstances[item];
247  Q_ASSERT( declarativeItem );
248 
249  // Make sure we have a valid bounding rect for collision detection
250  item->setProjection( viewport );
251  item->setSize( QSizeF( declarativeItem->boundingRect().size() ) );
252 
253  int shiftX( 0 ), shiftY( 0 );
254  switch( declarativeItem->transformOrigin() ) {
255  case QQuickItem::TopLeft:
256  case QQuickItem::Top:
257  case QQuickItem::TopRight:
258  break;
259  case QQuickItem::Left:
260  case QQuickItem::Center:
261  case QQuickItem::Right:
262  shiftY = declarativeItem->height() / 2;
263  break;
264  case QQuickItem::BottomLeft:
265  case QQuickItem::Bottom:
266  case QQuickItem::BottomRight:
267  shiftY = declarativeItem->height();
268  break;
269  }
270 
271  switch( declarativeItem->transformOrigin() ) {
272  case QQuickItem::TopLeft:
273  case QQuickItem::Left:
274  case QQuickItem::BottomLeft:
275  break;
276  case QQuickItem::Top:
277  case QQuickItem::Center:
278  case QQuickItem::Bottom:
279  shiftX = declarativeItem->width() / 2;
280  break;
281  case QQuickItem::TopRight:
282  case QQuickItem::Right:
283  case QQuickItem::BottomRight:
284  shiftX = declarativeItem->width();
285  break;
286  }
287 
288  declarativeItem->setX( x - shiftX );
289  declarativeItem->setY( y - shiftY );
290  orphane.removeOne( item );
291  }
292 
293  // Cleanup
294  foreach( AbstractDataPluginItem* item, orphane ) {
295  Q_ASSERT( d->m_delegateInstances.contains( item ) );
296  delete d->m_delegateInstances[item];
297  d->m_delegateInstances.remove( item );
298  }
299 }
300 
301 void AbstractDataPlugin::favoriteItemsChanged( const QStringList& favoriteItems )
302 {
303  Q_UNUSED( favoriteItems )
304 }
305 
306 void AbstractDataPlugin::delayedUpdate()
307 {
308  if ( !d->m_updateTimer.isActive() )
309  {
310  d->m_updateTimer.start( 500 );
311  }
312 }
313 
314 } // namespace Marble
315 
316 #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:151
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.
Marble::AbstractDataPluginItem
Definition: AbstractDataPluginItem.h:28
Marble::GeoPainter
A painter that allows to draw geometric primitives on the map.
Definition: GeoPainter.h:98
MarbleModel.h
This file contains the headers for MarbleModel.
Marble::AbstractDataPlugin::setFavoriteItemsOnly
void setFavoriteItemsOnly(bool favoriteOnly)
Convenience method to set the favorite item state on the current model.
Definition: AbstractDataPlugin.cpp:184
Marble::AbstractDataPluginModel
An abstract data model (not based on QAbstractModel) for a AbstractDataPlugin.
Definition: AbstractDataPluginModel.h:45
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
MarbleDebug.h
Marble::AbstractDataPlugin::renderType
virtual RenderType renderType() const
Function for returning the type of plugin this is for.
Definition: AbstractDataPlugin.cpp:170
Marble::AbstractDataPlugin::model
AbstractDataPluginModel * model() const
Definition: AbstractDataPlugin.cpp:124
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
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:191
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
Marble::AbstractDataPlugin::setModel
void setModel(AbstractDataPluginModel *model)
Set the model of the plugin.
Definition: AbstractDataPlugin.cpp:129
GeoPainter.h
Marble::AbstractDataPlugin::whichItemAt
QList< AbstractDataPluginItem * > whichItemAt(const QPoint &curpos)
This function returns all items at the position curpos.
Definition: AbstractDataPlugin.cpp:160
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:201
Marble::AbstractDataPlugin::setDelegate
void setDelegate(QDeclarativeComponent *delegate, QGraphicsItem *parent)
Definition: AbstractDataPlugin.cpp:175
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()
Marble::MarbleModel
The data model (not based on QAbstractModel) for a MarbleWidget.
Definition: MarbleModel.h:96
Marble::AbstractDataPlugin::favoriteItemsOnlyChanged
void favoriteItemsOnlyChanged()
Marble::RenderPlugin::OnlineRenderType
Definition: RenderPlugin.h:63
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
Marble::PluginInterface::name
virtual QString name() const =0
Returns the user-visible name of the plugin.
Marble::RenderPlugin::enabled
bool enabled() const
is enabled
AbstractDataPlugin.h
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:31
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:267
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:48 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
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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