File: ClickOnThat.cpp

package info (click to toggle)
marble 4%3A16.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 76,596 kB
  • ctags: 22,881
  • sloc: cpp: 177,552; xml: 39,363; ansic: 7,204; python: 2,209; sh: 1,140; makefile: 230; perl: 222; ruby: 97; java: 66
file content (369 lines) | stat: -rw-r--r-- 13,144 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2014 Abhinav Gangwar <abhgang@gmail.com>
//

// Self
#include "ClickOnThat.h"

// Qt
#include <QTime>
#include <QVector>
#include <QVariant>
#include <QStringList>
#include <QDir>

// Marble
#include <marble/MarbleWidget.h>
#include <marble/MarbleMap.h>
#include <marble/MarbleModel.h>
#include <marble/GeoDataTreeModel.h>
#include <marble/MarbleDirs.h>
#include <marble/MarblePlacemarkModel.h>

#include <marble/GeoDataDocument.h>
#include <marble/GeoDataPlacemark.h>
#include <marble/GeoDataStyle.h>
#include <marble/GeoDataStyleMap.h>
#include <marble/GeoDataIconStyle.h>

#include <marble/GeoDataTypes.h>

namespace Marble
{
class ClickOnThatPrivate
{
public:
    ClickOnThatPrivate( MarbleWidget *marbleWidget )
    : m_marbleWidget( marbleWidget ),
      m_parent( 0 ),
      m_correctAnswerPlacemark( 0 ),
      m_selectPinDocument( 0 ),
      m_countryNames( 0 ),
      m_countryBoundaries( 0 )
      {
          m_continentsAndOceans << "Asia" << "Africa" << "North America" << "South America"
          << "Antarctica" << "Europe" << "Australia" << "Arctic Ocean" << "Indian Ocean"
          << "North Atlantic Ocean" << "North Pacific Ocean" << "South Pacific Ocean"
          << "South Atlantic Ocean" << "Southern Ocean" ;
      }

    ~ClickOnThatPrivate()
    {
        delete m_selectPinDocument;
    }

        MarbleWidget *m_marbleWidget;
        ClickOnThat *m_parent;

        /**
         * Store the GeoDataPlacemark also
         * for the correct answer so that
         * we can highlight and zoom in
         * ( to fit in the current view port )
         * to this placemark when user
         * choses to view the right answer.
         */
        GeoDataPlacemark *m_correctAnswerPlacemark;
        GeoDataCoordinates m_correctAnswer;

        /**
         * @p m_selectPinDocument shows a pin
         * on map indicating whether the user
         * has clicked on right country
         */
        GeoDataDocument *m_selectPinDocument;

        /**
        * Document to store point placemarks which
        * have country names ( from file "boundaryplacemarks.cache" )
        */
        GeoDataDocument *m_countryNames;

        /**
        * Document which have placemarks whose geometry
        * specifies the boundaries of a country
        * (from file "ne_50m_admin_0_countries.pn2" )
        */
        GeoDataDocument *m_countryBoundaries;


        /*
        * If the placemark used for posting question
        * represent a continent, the user needs to click exactly on
        * a particular country whose geometry contains this point
        * placemark on map ( Ideally it should be any country
        * within that continent ). Also, oceans are point placemark, so
        * user needs to click exactly on same point this placemark
        * represents ( Ideally it should anywhere within the ocean territory ).
        * So, to avoid such placemark we will use this list.
        */
        QStringList m_continentsAndOceans;
};

ClickOnThat::ClickOnThat( MarbleWidget *marbleWidget )
    : QObject(),
      d( new ClickOnThatPrivate(marbleWidget) )
{
    d->m_parent = this;
    connect( this, SIGNAL(announceHighlight(qreal,qreal,GeoDataCoordinates::Unit)),
             d->m_marbleWidget, SIGNAL(highlightedPlacemarksChanged(qreal,qreal,GeoDataCoordinates::Unit)) );
}

ClickOnThat::~ClickOnThat()
{
    delete d;
}

void ClickOnThat::disablePinDocument()
{
    if ( d->m_selectPinDocument ) {
        d->m_selectPinDocument->setVisible( false );
        d->m_marbleWidget->model()->treeModel()->updateFeature( d->m_selectPinDocument );
    }
}

void ClickOnThat::initiateGame()
{
    /**
     * First remove the GeoDataDocument, which displays
     * country names, from map.
     */
    if ( !d->m_countryNames ) {
        const GeoDataTreeModel *const treeModel = d->m_marbleWidget->model()->treeModel();
        for ( int i = 0; i < treeModel->rowCount(); ++i ) {
            QVariant const data = treeModel->data ( treeModel->index ( i, 0 ), MarblePlacemarkModel::ObjectPointerRole );
            GeoDataObject *object = qvariant_cast<GeoDataObject*>( data );
            Q_ASSERT_X( object, "CountryByShape::initiateGame",
                        "failed to get valid data from treeModel for GeoDataObject" );
            if ( object->nodeType() == GeoDataTypes::GeoDataDocumentType ) {
                GeoDataDocument *doc = static_cast<GeoDataDocument*>( object );
                QFileInfo fileInfo( doc->fileName() );
                if ( fileInfo.fileName() == QString("boundaryplacemarks.cache") ) {
                    d->m_countryNames = doc;
                    break;
                }
            }
        }
    }

    if ( !d->m_countryBoundaries ) {
        const GeoDataTreeModel *const treeModel = d->m_marbleWidget->model()->treeModel();
        for ( int i = 0; i < treeModel->rowCount(); ++i ) {
            QVariant const data = treeModel->data ( treeModel->index ( i, 0 ), MarblePlacemarkModel::ObjectPointerRole );
            GeoDataObject *object = qvariant_cast<GeoDataObject*>( data );
            Q_ASSERT_X( object, "MainWindow::initiateGame",
                        "failed to get valid data from treeModel for GeoDataObject" );
            if ( object->nodeType() == GeoDataTypes::GeoDataDocumentType ) {
                GeoDataDocument *const doc = static_cast<GeoDataDocument*>( object );
                QFileInfo fileInfo( doc->fileName() );
                if ( fileInfo.fileName() == QString("ne_50m_admin_0_countries.pn2") ) {
                    d->m_countryBoundaries = doc;
                    break;
                }
            }
        }
    }

    if ( !d->m_selectPinDocument ) {
        d->m_selectPinDocument = new GeoDataDocument;
        GeoDataPlacemark *pinPlacemark = new GeoDataPlacemark;

        GeoDataStyle::Ptr pinStyle(new GeoDataStyle);
        pinStyle->setId("answer");
        GeoDataIconStyle iconStyle;
        iconStyle.setIconPath( MarbleDirs::path("bitmaps/target.png") );
        pinStyle->setIconStyle( iconStyle );

        GeoDataStyleMap styleMap;
        styleMap.setId("default-map");
        styleMap.insert( "normal", QString("#").append( pinStyle->id()) );

        d->m_selectPinDocument->addStyle( pinStyle );
        d->m_selectPinDocument->addStyleMap( styleMap );

        d->m_selectPinDocument->append( pinPlacemark );
        pinPlacemark->setStyleUrl( QString("#").append(styleMap.id()) );
        d->m_selectPinDocument->setVisible( false );

        // Add this document to treeModel
        d->m_marbleWidget->model()->treeModel()->addDocument( d->m_selectPinDocument );
    }

    d->m_marbleWidget->setHighlightEnabled( true );
    d->m_marbleWidget->centerOn( 23.0, 42.0 );
    d->m_marbleWidget->setDistance( 7500 );
    connect( d->m_marbleWidget, SIGNAL(highlightedPlacemarksChanged(qreal,qreal,GeoDataCoordinates::Unit)),
             this, SLOT(determineResult(qreal,qreal,GeoDataCoordinates::Unit)) );

    if ( d->m_countryBoundaries &&
        d->m_countryNames )
    {
        d->m_countryNames->setVisible( false );
        d->m_marbleWidget->model()->treeModel()->updateFeature( d->m_countryNames );
        emit gameInitialized();
    }
}

void ClickOnThat::postQuestion( QObject *gameObject )
{
    /**
    * Find a random placemark
    */
    Q_ASSERT_X( d->m_countryNames, "ClickOnThat::postQuestion",
                "CountryByShapePrivate::m_countryNames is NULL" );
    QVector<GeoDataPlacemark*> countryPlacemarks = d->m_countryNames->placemarkList();

    uint randomSeed = uint(QTime::currentTime().msec());
    qsrand( randomSeed );

    GeoDataPlacemark *placemark = 0;
    GeoDataPoint *point = 0;
    bool found = false;
    while( !found ) {
        placemark = countryPlacemarks[qrand()%countryPlacemarks.size()];
        if ( !d->m_continentsAndOceans.contains(placemark->name(), Qt::CaseSensitive) ) {
            found = true;
            point = dynamic_cast<GeoDataPoint*>( placemark->geometry() );
        }
    }
    if ( point ) {
        d->m_correctAnswerPlacemark = placemark;
        d->m_correctAnswer = point->coordinates();
        if ( gameObject ) {
            QMetaObject::invokeMethod( gameObject, "clickOnThatQuestion",
                                    Q_ARG(QVariant, QVariant::fromValue(placemark->name())) );
        }
    }
}

void ClickOnThat::updateSelectPin(bool result, const GeoDataCoordinates &clickedPoint )
{
    QDir dir;
    QString iconPath = dir.absolutePath();
    if ( result ) {
        //iconPath = MarbleDirs::path("bitmaps/MapTackRoundHeadGreen.png");
        iconPath = iconPath.append("/MapTackRoundHeadGreen.png");
    }
    else {
        iconPath = iconPath.append("/MapTackRoundHeadRed.png");
    }

    GeoDataStyle::Ptr style = d->m_selectPinDocument->style("answer");
    style->iconStyle().setIconPath( iconPath );
    d->m_selectPinDocument->addStyle( style );

    QVector<GeoDataPlacemark*> placemarkList = d->m_selectPinDocument->placemarkList();
    if ( placemarkList.size() > 0 ) {
        placemarkList[0]->setCoordinate( clickedPoint );
    }

    if ( !d->m_selectPinDocument->isVisible() ) {
        d->m_selectPinDocument->setVisible( true );
    }
    d->m_marbleWidget->model()->treeModel()->updateFeature( d->m_selectPinDocument );
}

void ClickOnThat::determineResult( qreal lon, qreal lat, GeoDataCoordinates::Unit unit )
{
    GeoDataCoordinates coord( lon, lat, 0, unit );

    Q_ASSERT_X( d->m_countryNames, "ClickOnThat::determineResult",
                "CountryByShapePrivate::m_countryBoundaries is NULL" );
    QVector<GeoDataFeature*>::Iterator i = d->m_countryBoundaries->begin();
    QVector<GeoDataFeature*>::Iterator const end = d->m_countryBoundaries->end();

    bool foundStandardPoint = false;
    bool foundClickedPoint = false;
    for ( ; i != end; ++i ) {
        GeoDataPlacemark *country = static_cast<GeoDataPlacemark*>( *i );

        GeoDataPolygon *polygon = dynamic_cast<GeoDataPolygon*>( country->geometry() );
        GeoDataLinearRing *linearring = dynamic_cast<GeoDataLinearRing*>( country->geometry() );
        GeoDataMultiGeometry *multigeom = dynamic_cast<GeoDataMultiGeometry*>( country->geometry() );

        foundClickedPoint = false;
        foundStandardPoint = false;
        if ( polygon &&
            polygon->contains( coord ) &&
            polygon->contains(d->m_correctAnswer) )
        {
            foundClickedPoint = true;
            foundStandardPoint = true;
            d->m_correctAnswerPlacemark = country;
            break;
        }
        if ( linearring &&
            linearring->contains( coord ) &&
            linearring->contains(d->m_correctAnswer) )
        {
            foundClickedPoint = true;
            foundStandardPoint = true;
            d->m_correctAnswerPlacemark = country;
            break;
        }
        if ( multigeom ) {
            QVector<GeoDataGeometry*>::Iterator iter = multigeom->begin();
            QVector<GeoDataGeometry*>::Iterator const end = multigeom->end();

            for ( ; iter != end; ++iter ) {
                GeoDataPolygon *poly  = dynamic_cast<GeoDataPolygon*>( *iter );
                if ( poly &&
                    poly->contains( coord ) )
                {
                    foundClickedPoint = true;
                }
                if ( poly &&
                    poly->contains( d->m_correctAnswer ) )
                {
                    foundStandardPoint = true;
                    d->m_correctAnswerPlacemark = country;
                }
                if ( foundClickedPoint && foundStandardPoint ) {
                    break;
                }
            }
        }
        if ( foundClickedPoint && foundStandardPoint ) {
            break;
        }
    }

    if ( foundClickedPoint && foundStandardPoint ) {
        updateSelectPin( true, coord );
        emit updateResult( true );
    }
    else {
        updateSelectPin( false, coord );
        emit updateResult( false );
    }
}

void ClickOnThat::highlightCorrectAnswer()
{
    disconnect( d->m_marbleWidget, SIGNAL(highlightedPlacemarksChanged(qreal,qreal,GeoDataCoordinates::Unit)),
             this, SLOT(determineResult(qreal,qreal,GeoDataCoordinates::Unit)) );

    emit announceHighlight( d->m_correctAnswer.longitude(GeoDataCoordinates::Degree),
                            d->m_correctAnswer.latitude(GeoDataCoordinates::Degree),
                            GeoDataCoordinates::Degree );
    updateSelectPin( true, d->m_correctAnswer );

    /**
     * Zoom to highlighted placemark
     * so that it fits the current
     * view port
     */
    d->m_marbleWidget->centerOn( *(d->m_correctAnswerPlacemark), true );

    connect( d->m_marbleWidget, SIGNAL(highlightedPlacemarksChanged(qreal,qreal,GeoDataCoordinates::Unit)),
             this, SLOT(determineResult(qreal,qreal,GeoDataCoordinates::Unit)) );
}


}   // namespace Marble