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

marble

  • kde-4.x
  • kdeedu
  • marble
  • examples
  • cpp
  • marble-game
CountryByShape.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 2014 Abhinav Gangwar <[email protected]>
9 //
10 
11 
12 // Self
13 #include "CountryByShape.h"
14 
15 // Qt
16 #include <QVector>
17 #include <QTime>
18 #include <QVariant>
19 #include <QVariantList>
20 #include <QFileInfo>
21 
22 // Marble
23 #include <marble/MarbleWidget.h>
24 #include <marble/MarbleModel.h>
25 #include <marble/GeoDataTreeModel.h>
26 #include <marble/MarblePlacemarkModel.h>
27 
28 #include <marble/GeoDataDocument.h>
29 #include <marble/GeoDataPlacemark.h>
30 #include <marble/GeoDataGeometry.h>
31 #include <marble/GeoDataPolygon.h>
32 #include <marble/GeoDataLinearRing.h>
33 #include <marble/GeoDataMultiGeometry.h>
34 #include <marble/GeoDataPoint.h>
35 #include <marble/GeoDataCoordinates.h>
36 #include <marble/GeoDataLatLonAltBox.h>
37 
38 namespace Marble
39 {
40 class CountryByShapePrivate
41 {
42 public:
43  CountryByShapePrivate( MarbleWidget *marbleWidget )
44  : m_parent( nullptr ),
45  m_marbleWidget( marbleWidget ),
46  m_countryNames( nullptr ),
47  m_countryBoundaries( nullptr )
48  {
49  m_continentsAndOceans
50  << QStringLiteral("Asia") << QStringLiteral("Africa")
51  << QStringLiteral("North America") << QStringLiteral("South America")
52  << QStringLiteral("Antarctica") << QStringLiteral("Europe")
53  << QStringLiteral("Australia")
54  << QStringLiteral("Arctic Ocean") << QStringLiteral("Indian Ocean")
55  << QStringLiteral("North Atlantic Ocean") << QStringLiteral("North Pacific Ocean")
56  << QStringLiteral("South Pacific Ocean") << QStringLiteral("South Atlantic Ocean")
57  << QStringLiteral("Southern Ocean");
58  }
59 
60  CountryByShape *m_parent;
61  MarbleWidget *m_marbleWidget;
62 
67  GeoDataDocument *m_countryNames;
68 
74  GeoDataDocument *m_countryBoundaries;
75 
86  QStringList m_continentsAndOceans;
87 };
88 
89 CountryByShape::CountryByShape( MarbleWidget *marbleWidget )
90  : QObject(),
91  d( new CountryByShapePrivate(marbleWidget) )
92 {
93  d->m_parent = this;
94  connect( this, SIGNAL(announceHighlight(qreal,qreal,GeoDataCoordinates::Unit)),
95  d->m_marbleWidget, SIGNAL(highlightedPlacemarksChanged(qreal,qreal,GeoDataCoordinates::Unit)) );
96 }
97 
98 CountryByShape::~CountryByShape()
99 {
100  delete d;
101 }
102 
103 void CountryByShape::initiateGame()
104 {
105  if ( !d->m_countryNames ) {
106  const GeoDataTreeModel *const treeModel = d->m_marbleWidget->model()->treeModel();
107  for ( int i = 0; i < treeModel->rowCount(); ++i ) {
108  QVariant const data = treeModel->data ( treeModel->index ( i, 0 ), MarblePlacemarkModel::ObjectPointerRole );
109  GeoDataObject *object = qvariant_cast<GeoDataObject*>( data );
110  Q_ASSERT_X( object, "CountryByShape::initiateGame",
111  "failed to get valid data from treeModel for GeoDataObject" );
112  if (auto doc = geodata_cast<GeoDataDocument>(object)) {
113  QFileInfo fileInfo( doc->fileName() );
114  if (fileInfo.fileName() == QLatin1String("boundaryplacemarks.cache")) {
115  d->m_countryNames = doc;
116  break;
117  }
118  }
119  }
120  }
121 
122  if ( !d->m_countryBoundaries ) {
123  const GeoDataTreeModel *const treeModel = d->m_marbleWidget->model()->treeModel();
124  for ( int i = 0; i < treeModel->rowCount(); ++i ) {
125  QVariant const data = treeModel->data ( treeModel->index ( i, 0 ), MarblePlacemarkModel::ObjectPointerRole );
126  GeoDataObject *object = qvariant_cast<GeoDataObject*>( data );
127  Q_ASSERT_X( object, "MainWindow::initiateGame",
128  "failed to get valid data from treeModel for GeoDataObject" );
129  if (auto doc = geodata_cast<GeoDataDocument>(object)) {
130  QFileInfo fileInfo( doc->fileName() );
131  if (fileInfo.fileName() == QLatin1String("ne_50m_admin_0_countries.pn2")) {
132  d->m_countryBoundaries = doc;
133  break;
134  }
135  }
136  }
137  }
138 
139  d->m_marbleWidget->setHighlightEnabled( true );
140 
141  if ( d->m_countryBoundaries &&
142  d->m_countryNames )
143  {
144  d->m_countryNames->setVisible( false );
145  d->m_marbleWidget->model()->treeModel()->updateFeature( d->m_countryNames );
146  emit gameInitialized();
147  }
148 }
149 
150 void CountryByShape::postQuestion( QObject *gameObject )
151 {
152  //Find a random placemark
153 
154  Q_ASSERT_X( d->m_countryNames, "CountryByShape::postQuestion",
155  "CountryByShapePrivate::m_countryNames is NULL" );
156 
157  QVector<GeoDataPlacemark*> countryPlacemarks = d->m_countryNames->placemarkList();
158 
159  uint randomSeed = uint(QTime::currentTime().msec());
160  qsrand( randomSeed );
161 
162  bool found = false;
163  GeoDataPlacemark *placemark =nullptr;
164  GeoDataPoint *point = nullptr;
165  GeoDataCoordinates coord;
166  GeoDataLatLonAltBox box;
167  QVariantList answerOptions;
168  while ( !found ) {
169  int randomIndex = qrand()%countryPlacemarks.size();
170  placemark = countryPlacemarks[randomIndex];
171  point = dynamic_cast<GeoDataPoint*>( placemark->geometry() );
172  coord = point->coordinates();
173 
174  if ( point ) {
181  Q_ASSERT_X( d->m_countryBoundaries, "CountryByShape::postQuestion",
182  "CountryByShapePrivate::m_countryBoundaries is NULL" );
183 
184  QVector<GeoDataFeature*>::Iterator i = d->m_countryBoundaries->begin();
185  QVector<GeoDataFeature*>::Iterator const end = d->m_countryBoundaries->end();
186  for ( ; i != end; ++i ) {
187  GeoDataPlacemark *country = static_cast<GeoDataPlacemark*>( *i );
188 
189  GeoDataPolygon *polygon = dynamic_cast<GeoDataPolygon*>( country->geometry() );
190  GeoDataLinearRing *linearring = dynamic_cast<GeoDataLinearRing*>( country->geometry() );
191  GeoDataMultiGeometry *multigeom = dynamic_cast<GeoDataMultiGeometry*>( country->geometry() );
192 
193  if ( polygon &&
194  polygon->contains( coord ) &&
195  !d->m_continentsAndOceans.contains(country->name(), Qt::CaseSensitive) )
196  {
197  box = polygon->latLonAltBox();
198  found = true;
199  break;
200  }
201  if ( linearring &&
202  linearring->contains( coord ) &&
203  !d->m_continentsAndOceans.contains(country->name(), Qt::CaseSensitive) )
204  {
205  box = linearring->latLonAltBox();
206  found = true;
207  break;
208  }
209  if ( multigeom ) {
210  QVector<GeoDataGeometry*>::Iterator iter = multigeom->begin();
211  QVector<GeoDataGeometry*>::Iterator const end = multigeom->end();
212 
213  for ( ; iter != end; ++iter ) {
214  GeoDataPolygon *poly = dynamic_cast<GeoDataPolygon*>( *iter );
215  if ( poly &&
216  poly->contains( coord ) &&
217  !d->m_continentsAndOceans.contains(country->name(), Qt::CaseSensitive) )
218  {
219  box = poly->latLonAltBox();
220  found = true;
221  break;
222  }
223  }
224  }
225  if ( found ) {
226  break;
227  }
228  }
229  }
230  }
231  d->m_marbleWidget->setHighlightEnabled( true );
232  emit announceHighlight( coord.longitude(GeoDataCoordinates::Degree),
233  coord.latitude(GeoDataCoordinates::Degree),
234  GeoDataCoordinates::Degree );
235 
241  d->m_marbleWidget->setHighlightEnabled( false );
242 
243  d->m_marbleWidget->centerOn( box, true );
244 
245  answerOptions << placemark->name()
246  << countryPlacemarks[qrand()%countryPlacemarks.size()]->name()
247  << countryPlacemarks[qrand()%countryPlacemarks.size()]->name()
248  << countryPlacemarks[qrand()%countryPlacemarks.size()]->name();
249 
250  // Randomize options in list answerOptions
251  for ( int i = 0; i < answerOptions.size(); ++i ) {
252  QVariant option = answerOptions.takeAt( qrand()%answerOptions.size() );
253  answerOptions.append( option );
254  }
255 
256  if ( gameObject ) {
257  QMetaObject::invokeMethod( gameObject, "countryByShapeQuestion",
258  Q_ARG(QVariant, QVariant(answerOptions)),
259  Q_ARG(QVariant, QVariant(placemark->name())) );
260  }
261 }
262 
263 } // namespace Marble
264 
265 #include "moc_CountryByShape.cpp"
Marble::GeoDataCoordinates::Unit
Unit
enum used constructor to specify the units used
Definition: GeoDataCoordinates.h:57
Marble::GeoDataPoint
A Geometry object representing a 3d point.
Definition: GeoDataPoint.h:47
Marble::CountryByShape::~CountryByShape
~CountryByShape() override
Definition: CountryByShape.cpp:98
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:45
Marble::CountryByShape::postQuestion
void postQuestion(QObject *gameObject)
Definition: CountryByShape.cpp:150
Marble::GeoDataTreeModel
The representation of GeoData in a model This class represents all available data given by kml-data f...
Definition: GeoDataTreeModel.h:33
Marble::GeoDataLinearRing
A LinearRing that allows to store a closed, contiguous set of line segments.
Definition: GeoDataLinearRing.h:67
Marble::GeoDataTreeModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Return the number of Items in the Model.
Definition: GeoDataTreeModel.cpp:120
Marble::GeoDataPolygon::contains
virtual bool contains(const GeoDataCoordinates &coordinates) const
Returns whether the given coordinates lie within the polygon.
Definition: GeoDataPolygon.cpp:246
Marble::GeoDataLinearRing::contains
virtual bool contains(const GeoDataCoordinates &coordinates) const
Returns whether the given coordinates lie within the polygon.
Definition: GeoDataLinearRing.cpp:73
Marble::CountryByShape::CountryByShape
CountryByShape(MarbleWidget *widget)
Definition: CountryByShape.cpp:89
Marble::GeoDataMultiGeometry::end
QVector< GeoDataGeometry * >::Iterator end()
Definition: GeoDataMultiGeometry.cpp:175
MarbleModel.h
This file contains the headers for MarbleModel.
Marble::CountryByShape::gameInitialized
void gameInitialized()
Marble::GeoDataObject
A base class for all geodata objects.
Definition: GeoDataObject.h:48
CountryByShape.h
Marble::GeoDataLineString::latLonAltBox
const GeoDataLatLonAltBox & latLonAltBox() const override
Returns the smallest latLonAltBox that contains the LineString.
Definition: GeoDataLineString.cpp:806
Marble::MarblePlacemarkModel::ObjectPointerRole
The pointer to a specific object.
Definition: MarblePlacemarkModel.h:60
Marble::GeoDataCoordinates::Degree
Definition: GeoDataCoordinates.h:59
Marble::GeoDataTreeModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: GeoDataTreeModel.cpp:203
Marble::CountryByShape::announceHighlight
void announceHighlight(qreal, qreal, GeoDataCoordinates::Unit)
Marble::GeoDataPlacemark::geometry
GeoDataGeometry * geometry()
The geometry of the GeoDataPlacemark is to be rendered to the marble map along with the icon at the c...
Definition: GeoDataPlacemark.cpp:151
Marble::MarbleWidget
A widget class that displays a view of the earth.
Definition: MarbleWidget.h:102
QObject
Marble::GeoDataPolygon
A polygon that can have "holes".
Definition: GeoDataPolygon.h:81
MarblePlacemarkModel.h
GeoDataTreeModel.h
QStringList
QFileInfo
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
QTime::currentTime
QTime currentTime()
QVector< GeoDataPlacemark * >
QLatin1String
Marble::GeoDataFeature::name
QString name() const
The name of the feature.
Definition: GeoDataFeature.cpp:190
Marble::GeoDataPolygon::latLonAltBox
const GeoDataLatLonAltBox & latLonAltBox() const override
Returns the smallest latLonAltBox that contains the Polygon.
Definition: GeoDataPolygon.cpp:134
Marble::GeoDataMultiGeometry
A class that can contain other GeoDataGeometry objects.
Definition: GeoDataMultiGeometry.h:33
CountryByShape
Definition: CountryByShape.qml:13
Marble::GeoDataPoint::coordinates
const GeoDataCoordinates & coordinates() const
Definition: GeoDataPoint.cpp:98
Marble::GeoDataMultiGeometry::begin
QVector< GeoDataGeometry * >::Iterator begin()
Definition: GeoDataMultiGeometry.cpp:167
MarbleWidget.h
This file contains the headers for MarbleWidget.
Marble::GeoDataCoordinates::longitude
qreal longitude(GeoDataCoordinates::Unit unit) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
Definition: GeoDataCoordinates.cpp:204
Marble::CountryByShape::initiateGame
void initiateGame()
Definition: CountryByShape.cpp:103
MarbleWidget
Marble::GeoDataCoordinates::latitude
qreal latitude(GeoDataCoordinates::Unit unit) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
Definition: GeoDataCoordinates.cpp:221
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QVector::size
int size() const
Marble::GeoDataPlacemark
a class representing a point of interest on the map
Definition: GeoDataPlacemark.h:53
Marble::GeoDataLatLonAltBox
A class that defines a 3D bounding box for geographic data.
Definition: GeoDataLatLonAltBox.h:47
Marble::GeoDataTreeModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: GeoDataTreeModel.cpp:373
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Thu Dec 5 2019 03:13:52 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
  •   KmPlot
  • libkeduvocdocument
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   src
  •   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