summaryrefslogtreecommitdiff
path: root/src/imports/location/qdeclarative3dgraphicsgeomap.cpp
blob: 1d5a8d2e43c4a5d804974d177e6671f5fea5aabd (plain)
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtLocation module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qdeclarative3dgraphicsgeomap_p.h"
#include "qdeclarativegeomapmousearea_p.h"

#include "qdeclarativecoordinate_p.h"
#include "qdeclarativegeoserviceprovider_p.h"
#include <Qt3D/qglview.h>
#include <Qt3D/qglsubsurface.h>
#include <QtCore/QCoreApplication>
#include <QtCore/qnumeric.h>
#include <QThread>

#include "tilecache.h"
#include "tile.h"
#include "cameradata.h"
#include <Qt3D/qglscenenode.h>
#include <Qt3D/qglbuilder.h>
#include <Qt3D/qgeometrydata.h>
#include "qdeclarativegeomapitem_p.h"
#include <cmath>

#include <qgeoserviceprovider.h>
#include <qgeomappingmanager.h>

#include <QDeclarativeContext>
#include <QtDeclarative/qdeclarativeinfo.h>
#include <QModelIndex>
#include <QSGCanvas>
#include <QSGEngine>
#include <QtGui/QGuiApplication>

#include <QDebug>

QT_BEGIN_NAMESPACE

/*!
    \qmlclass Map

    \brief The Map element displays a map.
    \inherits QDeclarativeItem

    \ingroup qml-location-maps

    The Map element can be used be used to display a map of the world.  The
    bulk of the functionality is provided by a mapping plugin described
    by the Plugin element associated with the Map.

    Various map objects can be added to the map.  These map objects are
    specified in terms of coordinates and metres.

    MapItems can be directly added to the Map element and it will display them
    automatically. The various objects that can be added include:

    \list
    \endlist

    Of the above list, MapItemView is a special case and not a MapItem as such.
    Here is a small example to illustrate this:

    \snippet doc/src/snippets/declarative/declarative-map.qml Basic MapItems and View on Map

    Mouse handling is done by adding MapMouseArea items as children of either
    MapItems or the Map item itself.

    The Map element is part of the \bold{QtLocation 5.0} module.
*/

QDeclarative3DGraphicsGeoMap::QDeclarative3DGraphicsGeoMap(QSGItem *parent)
    : QSGPaintedItem(parent),
      plugin_(0),
      serviceProvider_(0),
      mappingManager_(0),
      center_(0),
      initialCoordinate(0),
//      mapType_(NoMap),
//      connectivityMode_(NoConnectivity),
      componentCompleted_(false),
      flickable_(0),
      pinchArea_(0),
      mouseGrabberItem_(0),
      canvas_(0),
      touchTimer_(-1),
      tileCache_(0)
{
    QLOC_TRACE0;
    initialCoordinate = new QGeoCoordinate(-27.0, 153.0);
    zoomLevel_ = 8;
    size_ = QSizeF(100.0, 100.0);
    setAcceptHoverEvents(false);
    setAcceptedMouseButtons(Qt::LeftButton | Qt::MidButton | Qt::RightButton);
    setFlags(QSGItem::ItemHasContents);

    tileCache_ = new TileCache();
    map_ = new Map(tileCache_, this);
    connect(map_,
            SIGNAL(updateRequired()),
            this,
            SLOT(update()));
    connect(map_,
            SIGNAL(cameraDataChanged(CameraData)),
            this,
            SLOT(cameraDataChanged(CameraData)));
    // Create internal flickable and pinch area.
    flickable_ = new QDeclarativeGeoMapFlickable(map_, this);
    pinchArea_ = new QDeclarativeGeoMapPinchArea(this, this);
    setRenderTarget(QSGPaintedItem::FramebufferObject);
}

// this function is only called & executed in rendering thread
QSGNode* QDeclarative3DGraphicsGeoMap::updatePaintNode(QSGNode* node, UpdatePaintNodeData* data)
{
    Q_UNUSED(node);
    Q_UNUSED(data);
#ifdef QSGSHADEREFFECTSOURCE_AVAILABLE
    // add any pending map items
    if (updateMutex_.tryLock()) {
        if (canvas_) {
            for (int i = 0; i < mapItemsPending_.count(); i++) {
                mapItemsPending_.at(i)->setMap(this);
                mapItems_.append(mapItemsPending_.at(i));
                map_->addMapItem(mapItemsPending_.at(i)->mapItem());
            }
            mapItemsPending_.clear();
        }

        // perform any updates needed by items
        // can be optimized if lot of map items: usually the operation is no-op (static map items)
        for (int i = 0; i < mapItems_.count(); ++i)
            mapItems_.at(i)->updateItem();
        updateMutex_.unlock();
    } else {
        QLOC_TRACE1("===== Map item update will be missed, mutex not acquired =====");
    }
#endif
    update(); // is this needed
    return QSGPaintedItem::updatePaintNode(node, data);
}

QDeclarative3DGraphicsGeoMap::~QDeclarative3DGraphicsGeoMap()
{
    // TODO we do not clear the map items atm
//    if (mapData_) {
//        qDeleteAll(mapViews_);
//        // Remove map objects, we can't allow MapItem
//        // to delete the objects because they are owned
//        // by the declarative elements.
//        QList<QDeclarativeGeoMapItem*> objects = MapItems_;
//        for (int i = 0; i < objects.size(); ++i) {
//            mapData_->removeMapItem(objects.at(i)->MapItem());
//        }
//        delete mapData_;
//    }
    mouseAreas_.clear();
    if (serviceProvider_)
        delete serviceProvider_;
    if (initialCoordinate) {
        delete initialCoordinate;
    }
}

void QDeclarative3DGraphicsGeoMap::componentComplete()
{
    QLOC_TRACE0;
    componentCompleted_ = true;
    populateMap();
    map_->resize(width(), height());
    CameraData cameraData = map_->cameraData();
    map_->setCameraData(cameraData);
    map_->update();
    if (mappingManager_)
        pinchArea_->zoomLevelLimits(mappingManager_->minimumZoomLevel(), mappingManager_->maximumZoomLevel());
    QSGItem::componentComplete();
}

QDeclarativeGeoMapFlickable* QDeclarative3DGraphicsGeoMap::flick()
{
    return flickable_;
}

void QDeclarative3DGraphicsGeoMap::itemChange(ItemChange change, const ItemChangeData & data)
{
    QLOC_TRACE0;
    if (change == ItemSceneChange)
        canvas_ = data.canvas;
}

void QDeclarative3DGraphicsGeoMap::populateMap()
{
    if (!componentCompleted_)
        return;
    QObjectList kids = children();
    for (int i = 0; i < kids.size(); ++i) {
        // dispatch items appropriately
        QDeclarativeGeoMapItemView* mapView = qobject_cast<QDeclarativeGeoMapItemView*>(kids.at(i));
        if (mapView) {
            mapViews_.append(mapView);
            setupMapView(mapView);
            continue;
        }
        QDeclarativeGeoMapItem* mapItem = qobject_cast<QDeclarativeGeoMapItem*>(kids.at(i));
        if (mapItem) {
            addMapItem(mapItem);
        }
        QDeclarativeGeoMapMouseArea *mapMouseArea = qobject_cast<QDeclarativeGeoMapMouseArea*>(kids.at(i));
        if (mapMouseArea) {
            mapMouseArea->setMap(this);
            mouseAreas_.append(mapMouseArea);
        }
    }
}

void QDeclarative3DGraphicsGeoMap::setupMapView(QDeclarativeGeoMapItemView *view)
{
    Q_UNUSED(view)
    view->setMapData(this);
    view->repopulate();
}

class ViewportSubsurface : public QGLSubsurface
{
public:
    ViewportSubsurface(QGLAbstractSurface *surface, const QRect &region,
                       qreal adjust)
        : QGLSubsurface(surface, region), m_adjust(adjust) {}

    qreal aspectRatio() const;
    ~ViewportSubsurface() {}

private:
    qreal m_adjust;
};

qreal ViewportSubsurface::aspectRatio() const
{
    return QGLSubsurface::aspectRatio() * m_adjust;
}

void QDeclarative3DGraphicsGeoMap::updateAspectRatio()
{
    map_->resize(width(), height());
    if (!map_->autoUpdate())
        map_->update();
}

void QDeclarative3DGraphicsGeoMap::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
    setSize(QSizeF(newGeometry.width(), newGeometry.height()));
    updateAspectRatio();
    QSGItem::geometryChanged(newGeometry, oldGeometry);
}

void QDeclarative3DGraphicsGeoMap::keyPressEvent(QKeyEvent *e)
{
    QLOC_TRACE2(" key: ", e->key());
    CameraData cameraData = map_->cameraData();
    if (e->key() == Qt::Key_Left) {
        if (e->modifiers() & Qt::ShiftModifier) {
            QGeoCoordinate coord = cameraData.center();
            coord.setLongitude(coord.longitude() - 1);
            cameraData.setCenter(coord);
        } else {
            cameraData.setBearing(cameraData.bearing() - 5.0);
        }
    } else if (e->key() == Qt::Key_Right) {
        if (e->modifiers() & Qt::ShiftModifier) {
            QGeoCoordinate coord = cameraData.center();
            coord.setLongitude(coord.longitude() + 1);
            cameraData.setCenter(coord);
        } else {
            cameraData.setBearing(cameraData.bearing() + 5.0);
        }
    } else if (e->key() == Qt::Key_Up) {
        if (e->modifiers() & Qt::ShiftModifier) {
            QGeoCoordinate coord = cameraData.center();
            coord.setLatitude(coord.latitude() + 1);
            cameraData.setCenter(coord);
        } else {
            cameraData.setTilt(cameraData.tilt() - 5.0);
        }
    } else if (e->key() == Qt::Key_Down) {
        if (e->modifiers() & Qt::ShiftModifier) {
            QGeoCoordinate coord = cameraData.center();
            coord.setLatitude(coord.latitude() - 1);
            cameraData.setCenter(coord);
        } else {
            cameraData.setTilt(cameraData.tilt() + 5.0);
        }
    } else if (e->key() == Qt::Key_Plus) {
        if (e->modifiers() & Qt::ShiftModifier) {
            cameraData.setDistance(cameraData.distance() / 2.0);
            cameraData.setZoomLevel(cameraData.zoomLevel() + 1);
            map_->setCameraData(cameraData);
            if (!map_->autoUpdate())
                map_->update();
        } else {
            cameraData.setDistance(cameraData.distance() / 1.1);
        }
    } else if (e->key() == Qt::Key_Minus) {
        if (e->modifiers() & Qt::ShiftModifier) {
            if (cameraData.zoomLevel() != 1)
                cameraData.setZoomLevel(cameraData.zoomLevel() - 1);
            cameraData.setDistance(cameraData.distance() * 2.0);
            map_->setCameraData(cameraData);
            if (!map_->autoUpdate())
                map_->update();
        } else {
            cameraData.setDistance(cameraData.distance() * 1.1);
        }
    } else if (e->key() == Qt::Key_U) {
        map_->setCameraData(cameraData);
        if (!map_->autoUpdate())
            map_->update();
    }
    map_->setCameraData(cameraData);
    update();
}

void QDeclarative3DGraphicsGeoMap::paint(QPainter* p)
{
    if (!isVisible())
        return;

    QGLPainter painter(p);
    if (!painter.begin()) {
        qmlInfo(this) << tr("GL graphics system is not active; cannot use 3D items");
        return;
    }
    // No stereo rendering, set the eye as neutral
    painter.setEye(QGL::NoEye);
    // TODO this needs to be figured out (or confirmed as invalid thing).
    // Currently applied transforms for this Map3D element - how to get/apply current transforms?
    // QTransform transform = painter->combinedTransform();
    // Then we get the rectangle that is gotten by applying the QTransform on the rect
    // --> this is the viewport for Map3D
    // QRect viewport = transform.mapRect(boundingRect()).toRect();

    // boundingRect is in local coordinates. We need to map it to the scene coordinates
    // in order to render to correct area.
    QRect viewport = mapRectToScene(boundingRect()).toRect();
    qreal adjust = 1.0f;
    ViewportSubsurface surface(painter.currentSurface(), viewport, adjust);
    painter.pushSurface(&surface);
    earlyDraw(&painter);
    if (map_->glCamera()) {
        painter.setCamera(map_->glCamera());
    } else {
        QGLCamera defCamera;
        painter.setCamera(&defCamera);
    }
    paintGL(&painter);
    // Draw the children items
    painter.popSurface();
    // QSG does not expect anyone to alter gl context state; restore defaults.
    // Default heaps of things, because we cannot be sure what the Qt3D internally
    // sets.
    restoreDefaults(&painter);
}

void QDeclarative3DGraphicsGeoMap::setCameraData(const CameraData &camera)
{
    map_->setCameraData(camera);
    if (!map_->autoUpdate())
        map_->update();
}

CameraData QDeclarative3DGraphicsGeoMap::cameraData() const
{
    return map_->cameraData();
}

void QDeclarative3DGraphicsGeoMap::restoreDefaults(QGLPainter *painter)
{
    // Disable the effect to return control to the GL paint engine.
    painter->disableEffect();

    // Try to restore the default options
    glDisable(GL_CULL_FACE);
    glDisable(GL_DEPTH_TEST);
    // Set the default depth buffer options.
    glDepthFunc(GL_LESS);
    glDepthMask(GL_TRUE);
#if defined(QT_OPENGL_ES)
    glDepthRangef(0.0f, 1.0f);
#else
    glDepthRange(0.0f, 1.0f);
#endif
    // Set the default blend options.
    glDisable(GL_BLEND);
    if (painter->hasOpenGLFeature(QOpenGLFunctions::BlendColor))
        painter->glBlendColor(0, 0, 0, 0);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    if (painter->hasOpenGLFeature(QOpenGLFunctions::BlendEquation))
        painter->glBlendEquation(GL_FUNC_ADD);
    else if (painter->hasOpenGLFeature(QOpenGLFunctions::BlendEquationSeparate))
        painter->glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
}

#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE 0x809D
#endif

void QDeclarative3DGraphicsGeoMap::earlyDraw(QGLPainter *painter)
{
    glClearColor(0.0,0.0,0.0,0.0);
    // Depth buffer has been cleared already, but color buffer hasn't
    glClear(GL_COLOR_BUFFER_BIT);
    // Force the effect to be updated.
    painter->disableEffect();
#ifdef GL_RESCALE_NORMAL
    // Scale normals by a scale factor derived from modelview matrix.
    // Note: normals need to be unit length.
    glEnable(GL_RESCALE_NORMAL);
#endif

#if !defined(QT_OPENGL_ES_2)
    glShadeModel(GL_SMOOTH);
    glEnable(GL_MULTISAMPLE);
#endif

    // Set the default effect for the scene.
    painter->setStandardEffect(QGL::LitMaterial);
    painter->setFaceColor(QGL::AllFaces, Qt::white);
}

void QDeclarative3DGraphicsGeoMap::paintGL(QGLPainter *painter)
{
    if (map_) {
        painter->projectionMatrix().scale(1,-1, 1); // qt3d and qsg interpret y differently
        map_->paintGL(painter);
    }
}

/*!
    \qmlproperty Plugin Map::plugin

    This property holds the plugin which provides the mapping functionality.

    This is write-once property.  Once the map has a plugin associated with
    it any attempted modifications of the plugin will be ignored.
*/

void QDeclarative3DGraphicsGeoMap::setPlugin(QDeclarativeGeoServiceProvider *plugin)
{
    if (plugin_) {
        qmlInfo(this) << tr("Plugin is a write-once property, and cannot be set again.");
        return;
    }
    plugin_ = plugin;
    emit pluginChanged(plugin_);
    serviceProvider_ = new QGeoServiceProvider(plugin_->name(),
                                               plugin_->parameterMap());
    if (serviceProvider_->error() != QGeoServiceProvider::NoError) {
        qWarning() << serviceProvider_->errorString();
        delete serviceProvider_;
        serviceProvider_ = 0;
        return;
    }
    mappingManager_ = serviceProvider_->mappingManager();
    if (!mappingManager_ || serviceProvider_->error() != QGeoServiceProvider::NoError) {
        qWarning() << serviceProvider_->errorString();
        delete serviceProvider_;
        serviceProvider_ = 0;
        delete mappingManager_;
        mappingManager_ = 0;
        return;
    }

    map_->setMappingManager(mappingManager_);

//    mapData_ = mappingManager_->createMapData();
//    mapData_->init();
    //mapData_->setParentItem(this);

    // setters
//    mapData_->setWindowSize(size_);
//    mapData_->setZoomLevel(zoomLevel_);

//    if (center_)
//        mapData_->setCenter(center_->coordinate());
//    else
//        mapData_->setCenter(*initialCoordinate);

//    mapData_->setMapType(QGraphicsGeoMap::MapType(mapType_));
//    mapData_->setConnectivityMode(QGraphicsGeoMap::ConnectivityMode(connectivityMode_));

    // Populate the map objects.
    populateMap();
    // setup signals
//    connect(mapData_,
//            SIGNAL(updateMapDisplay(QRectF)),
//            this,
//            SLOT(updateMapDisplay(QRectF)));

//    connect(mapData_,
//            SIGNAL(centerChanged(QGeoCoordinate)),
//            this,
//            SLOT(internalCenterChanged(QGeoCoordinate)));

//    connect(mapData_,
//            SIGNAL(mapTypeChanged(QGraphicsGeoMap::MapType)),
//            this,
//            SLOT(internalMapTypeChanged(QGraphicsGeoMap::MapType)));

//    connect(mapData_,
//            SIGNAL(connectivityModeChanged(QGraphicsGeoMap::ConnectivityMode)),
//            this,
//            SLOT(internalConnectivityModeChanged(QGraphicsGeoMap::ConnectivityMode)));

//    connect(mapData_,
//            SIGNAL(windowSizeChanged(QSizeF)),
//            this,
//            SIGNAL(sizeChanged(QSizeF)));

//    connect(mapData_,
//            SIGNAL(zoomLevelChanged(qreal)),
//            this,
//            SIGNAL(zoomLevelChanged(qreal)));
}

void QDeclarative3DGraphicsGeoMap::updateMapDisplay(const QRectF &target)
{
    Q_UNUSED(target);
    QSGItem::update();
}

QDeclarativeGeoServiceProvider* QDeclarative3DGraphicsGeoMap::plugin() const
{
    return plugin_;
}

/*!
    \qmlproperty qreal Map::minimumZoomLevel

    This property holds the minimum valid zoom level for the map.
*/

qreal QDeclarative3DGraphicsGeoMap::minimumZoomLevel() const
{
    if (mappingManager_)
        return mappingManager_->minimumZoomLevel();
    else
        return -1.0;
}

/*!
    \qmlproperty qreal Map::maximumZoomLevel

    This property holds the maximum valid zoom level for the map.
*/

qreal QDeclarative3DGraphicsGeoMap::maximumZoomLevel() const
{
    if (mappingManager_)
        return mappingManager_->maximumZoomLevel();
    else
        return -1.0;
}

// TODO make these more QML like
//QList<MapType> QDeclarative3DGraphicsGeoMap::supportedMapTypes() const;
//QList<ConnectivityMode> QDeclarative3DGraphicsGeoMap::supportedConnectivityModes() const;

/*!
    \qmlproperty QSizeF Map::size

    This property holds the size of the map viewport.
*/
void QDeclarative3DGraphicsGeoMap::setSize(const QSizeF &size)
{
//    if (mapData_) {
//        setWidth(size.width());
//        setHeight(size.height());
//        mapData_->setWindowSize(size);
//    } else {
        if (size_ == size)
            return;

        size_ = size;

        emit sizeChanged(size_);
//    }

}

QSizeF QDeclarative3DGraphicsGeoMap::size() const
{
//    if (mapData_)
//        return mapData_->windowSize();
//    else
        return size_;
}

/*!
    \qmlproperty qreal Map::zoomLevel

    This property holds the zoom level for the map.

    Larger values for the zoom level provide more detail.

    The default value is 8.0.
*/
void QDeclarative3DGraphicsGeoMap::setZoomLevel(qreal zoomLevel)
{
    if (zoomLevel_ == zoomLevel)
        return;
    if (mappingManager_ &&
            (zoomLevel < mappingManager_->minimumZoomLevel() ||
             zoomLevel > mappingManager_->maximumZoomLevel()))
        return;
    zoomLevel_ = zoomLevel;
    if (map_) {
        CameraData cameraData = map_->cameraData();
        cameraData.setZoomFactor(zoomLevel);
        map_->setCameraData(cameraData);
        if (!map_->autoUpdate())
            map_->update();
    }
    emit zoomLevelChanged(zoomLevel_);
}

qreal QDeclarative3DGraphicsGeoMap::zoomLevel() const
{
    if (map_) {
        return map_->cameraData().zoomFactor();
    } else {
        return zoomLevel_;
    }
}

/*!
    \qmlproperty Coordinate Map::center

    This property holds the coordinate which occupies the center of the
    mapping viewport.

    The default value is an arbitrary valid coordinate.
*/
void QDeclarative3DGraphicsGeoMap::setCenter(QDeclarativeCoordinate *center)
{
    if (center == center_)
        return;
    if (center_)
        center_->disconnect(this);
    center_ = center;
    if (center_) {
        connect(center_,
                SIGNAL(latitudeChanged(double)),
                this,
                SLOT(centerLatitudeChanged(double)));
        connect(center_,
                SIGNAL(longitudeChanged(double)),
                this,
                SLOT(centerLongitudeChanged(double)));
        connect(center_,
                SIGNAL(altitudeChanged(double)),
                this,
                SLOT(centerAltitudeChanged(double)));
        if (center_->coordinate().isValid()) {
            CameraData cameraData = map_->cameraData();
            cameraData.setCenter(center_->coordinate());
            map_->setCameraData(cameraData);
            update();
        }
    }
    emit centerChanged(center_);
}

QDeclarativeCoordinate* QDeclarative3DGraphicsGeoMap::center()
{
    if (!center_) {
        center_ = new QDeclarativeCoordinate(map_->cameraData().center());
        connect(center_,
                SIGNAL(latitudeChanged(double)),
                this,
                SLOT(centerLatitudeChanged(double)));
        connect(center_,
                SIGNAL(longitudeChanged(double)),
                this,
                SLOT(centerLongitudeChanged(double)));
        connect(center_,
                SIGNAL(altitudeChanged(double)),
                this,
                SLOT(centerAltitudeChanged(double)));
    }
    return center_;
}

void QDeclarative3DGraphicsGeoMap::cameraDataChanged(const CameraData &cameraData)
{
    // check what has changed and emit appropriate signals
    if (!center_ || cameraData.center() != center_->coordinate()) {
        QDeclarativeCoordinate* currentCenter = center();
        currentCenter->setCoordinate(cameraData.center());
        emit centerChanged(currentCenter);
    }
    if (cameraData.zoomFactor() != zoomLevel_) {
        zoomLevel_ = cameraData.zoomFactor();
        emit zoomLevelChanged(zoomLevel_);
    }
}

void QDeclarative3DGraphicsGeoMap::centerLatitudeChanged(double latitude)
{
    if (qIsNaN(latitude))
        return;
    CameraData cameraData = map_->cameraData();
    QGeoCoordinate coord = cameraData.center();
    coord.setLatitude(latitude);
    cameraData.setCenter(coord);
    map_->setCameraData(cameraData);
    update();
    emit centerChanged(center_);
}

void QDeclarative3DGraphicsGeoMap::centerLongitudeChanged(double longitude)
{
    if (qIsNaN(longitude))
        return;
    CameraData cameraData = map_->cameraData();
    QGeoCoordinate coord = cameraData.center();
    coord.setLongitude(longitude);
    cameraData.setCenter(coord);
    map_->setCameraData(cameraData);
    update();
    emit centerChanged(center_);
}

void QDeclarative3DGraphicsGeoMap::centerAltitudeChanged(double altitude)
{
    if (qIsNaN(altitude))
        return;
    CameraData cameraData = map_->cameraData();
    QGeoCoordinate coord = cameraData.center();
    coord.setAltitude(altitude);
    cameraData.setCenter(coord);
    map_->setCameraData(cameraData);
    update();
    emit centerChanged(center_);
}

/*!
    \qmlproperty enumeration Map::mapType

    This property holds the type of map to display.

    The type can be one of:
    \list
    \o Map.StreetMap
    \o Map.SatelliteMapDay
    \o Map.SatelliteMapNight
    \o Map.TerrainMap
    \endlist

    The default value is determined by the plugin.
*/
//void QDeclarative3DGraphicsGeoMap::setMapType(QDeclarative3DGraphicsGeoMap::MapType mapType)
//{
//    if (mapData_) {
//        mapData_->setMapType(QGraphicsGeoMap::MapType(mapType));
//    } else {
//        if (mapType_ == mapType)
//            return;

//        mapType_ = mapType;

//        emit mapTypeChanged(mapType_);
//    }
//}

//QDeclarative3DGraphicsGeoMap::MapType QDeclarative3DGraphicsGeoMap::mapType() const
//{
//    if (mapData_) {
//        return QDeclarative3DGraphicsGeoMap::MapType(mapData_->mapType());
//    } else {
//        return mapType_;
//    }
//}

/*!
    \qmlproperty enumeration Map::connectivityMode

    This property holds the connectivity mode used to fetch the map data.

    The mode can be one of:
    \list
    \o Map.OfflineMode
    \o Map.OnlineMode
    \o Map.HybridMode
    \endlist

    The default value is determined by the plugin.
*/
//void QDeclarative3DGraphicsGeoMap::setConnectivityMode(QDeclarative3DGraphicsGeoMap::ConnectivityMode connectivityMode)
//{
//    if (mapData_) {
//        mapData_->setConnectivityMode(QGraphicsGeoMap::ConnectivityMode(connectivityMode));
//    } else {
//        if (connectivityMode_ == connectivityMode)
//            return;

//        connectivityMode_ = connectivityMode;

//        emit connectivityModeChanged(connectivityMode_);
//    }
//}

//QDeclarative3DGraphicsGeoMap::ConnectivityMode QDeclarative3DGraphicsGeoMap::connectivityMode() const
//{
//    if (mapData_)
//        return QDeclarative3DGraphicsGeoMap::ConnectivityMode(mapData_->connectivityMode());
//    else
//        return connectivityMode_;
//}

/*!
    \qmlproperty list<QGeoMapItem> Map::objects
    \default

    This property holds the list of objects associated with this map.

    The various objects that can be added include:
    \list
    \o MapRectangle
    \o MapCircle
    \o MapText
    \o MapImage
    \o MapPolygon
    \o MapPolyline
    \o MapGroup
    \endlist
*/

/*!
    \qmlmethod Map::toCoordinate(QPointF screenPosition)

    Returns the coordinate which corresponds to the screen position
    \a screenPosition.

    Returns an invalid coordinate if \a screenPosition is not within
    the current viewport.

    An example to constraint landmarks of a model to just those
    currently on Map:
    \snippet examples/declarative-location/landmarkmap/landmarkmap.qml Map toCoordinate

*/

QDeclarativeCoordinate* QDeclarative3DGraphicsGeoMap::toCoordinate(QPointF screenPosition) const
{
    QGeoCoordinate coordinate;
    if (map_)
        coordinate = map_->screenPositionToCoordinate(screenPosition);
    // by default objects returned from method call get javascript ownership,
    // so we don't need to worry about this as long as we don't set the parent
    return new QDeclarativeCoordinate(coordinate);
}

/*!
    \qmlmethod Map::toScreenPosition(Coordinate coordinate)

    Returns the screen position which corresponds to the coordinate
    \a coordinate.

    Returns an invalid QPointF if \a coordinate is not within the
    current viewport.
*/

QPointF QDeclarative3DGraphicsGeoMap::toScreenPosition(QDeclarativeCoordinate* coordinate) const
{
    QPointF point;
    if (map_)
        point = map_->coordinateToScreenPosition(coordinate->coordinate());
    return point;
}

void QDeclarative3DGraphicsGeoMap::pan(int dx, int dy)
{
    Q_UNUSED(dx);
    Q_UNUSED(dy);
    qWarning() << __FUNCTION__ << " of Map not implemented."; // TODO
}

void QDeclarative3DGraphicsGeoMap::touchEvent(QTouchEvent *event)
{
    QLOC_TRACE0;
    event->accept();
    pinchArea_->touchEvent(event);
}

void QDeclarative3DGraphicsGeoMap::wheelEvent(QWheelEvent *event)
{
    QLOC_TRACE0;
    event->accept();
    emit wheel(event->delta());
}

// event delivery helpers
bool QDeclarative3DGraphicsGeoMap::deliverMouseEvent(QMouseEvent* event)
{
    QLOC_TRACE2("mouse grabber item: ", mouseGrabberItem_);
    // todo deliver first for map items

    // mouse grabber item is an item that has received the initial
    // press event. all events will be given to that item (can be
    // outside of mouse area) until mouse is released. this enables
    // for example entered() exited() -signals whilst mouse is pressed
    lastMousePosition_ = event->windowPos();

    if (!mouseGrabberItem_ &&
            event->type() == QEvent::MouseButtonPress &&
            (event->button() & event->buttons()) == event->buttons()) {
        QList<QDeclarativeGeoMapMouseArea*> mouseAreas = mouseAreasAt(event->pos());
        for (int i = 0; i < mouseAreas.count(); ++i) {
            QDeclarativeGeoMapMouseArea* item = mouseAreas.at(i);
            QLOC_TRACE2("delivering initial mouse press for: ", item->objectName());
            if (deliverInitialMousePressEvent(item, event)) {
                QLOC_TRACE2("initial mouse press accepted by: ", item->objectName());
                return true;
            }
        }
        QLOC_TRACE1("no item found for initial mouse press");
        return false;
    }
    if (mouseGrabberItem_) {
        bool transformOk;
        const QTransform &transform = itemTransform(mouseGrabberItem_, &transformOk);
        QMouseEvent me(event->type(),                     // event type
                       transform.map(event->windowPos()), // pos coordinates translated into that of mapmousearea (local coords)
                       event->windowPos(),                // window position (coords relative to window)
                       event->screenPos(),                // global position (absolute coords)
                       event->button(),                   // button causing the event
                       event->buttons(),                  // buttons pressed when event was caused
                       event->modifiers());               // any keyboard modifiers held when event was triggered
        me.accept();
        mouseGrabberItem_->mouseEvent(&me);
        QLOC_TRACE2("mouse grabber accepted event: ", me.isAccepted());
        event->setAccepted(me.isAccepted());
        if (me.isAccepted())
            return true;
    }
    return false;
}

bool QDeclarative3DGraphicsGeoMap::deliverInitialMousePressEvent(QDeclarativeGeoMapMouseArea* ma, QMouseEvent* event)
{
    if (ma->acceptedMouseButtons() & event->button()) {
        QPointF p = ma->mapFromScene(event->windowPos());
        if (QRectF(0, 0, ma->width(), ma->height()).contains(p)) {
            QMouseEvent me(event->type(), p, event->windowPos(), event->screenPos(),
                           event->button(), event->buttons(), event->modifiers());
                me.accept();
                mouseGrabberItem_ = ma;
                ma->mouseEvent(&me);
                event->setAccepted(me.isAccepted());
                QLOC_TRACE2("the initial mouse press accepted: ", me.isAccepted());
                if (me.isAccepted())
                    return true;
                QLOC_TRACE1("nulling the mouse grabber");
                mouseGrabberItem_ = 0;
            }
        }
    return false;
}

void QDeclarative3DGraphicsGeoMap::mousePressEvent(QMouseEvent *event)
{
    QLOC_TRACE2(" ~~~~~~~ event, coordinates: ", event->pos());
    bool consumed = deliverMouseEvent(event);
    consumed |= flickable_->mousePressEvent(event);
    if (consumed)
        event->accept();
    else
        event->ignore();
}

// returns list of mouse areas under 'pos'. returned list is in the priority order in which the
// mouse events should be provided (currently does not consider 'z')
QList<QDeclarativeGeoMapMouseArea*> QDeclarative3DGraphicsGeoMap::mouseAreasAt(QPoint pos)
{
    QList<QDeclarativeGeoMapMouseArea*> list;
    for (int i = mouseAreas_.count() - 1; i >= 0; --i) {
        if (mouseAreas_.at(i)->boundingRect().contains(mouseAreas_.at(i)->mapFromScene(pos))) {
            list.append(mouseAreas_.at(i));
        }
    }
    return list;
}

void QDeclarative3DGraphicsGeoMap::mouseReleaseEvent(QMouseEvent *event)
{
    QLOC_TRACE2(" ~~~~~~~ event, coordinates: ", event->pos());
    bool consumed = false;
    if (mouseGrabberItem_) {
        consumed = deliverMouseEvent(event);
        mouseGrabberItem_ = 0;
    }
    consumed |= flickable_->mouseReleaseEvent(event);
    if (consumed)
        event->accept();
    else
        event->ignore();
}

void QDeclarative3DGraphicsGeoMap::mouseDoubleClickEvent(QMouseEvent *event)
{
    QLOC_TRACE2(" ~~~~~~~ event, coordinates: ", event->pos());
    if (!mouseGrabberItem_ && (event->button() & event->buttons()) == event->buttons()) {
        QList<QDeclarativeGeoMapMouseArea*> mouseAreas = mouseAreasAt(event->pos());
        for (int i = 0; i < mouseAreas.count(); ++i) {
            if (deliverInitialMousePressEvent(mouseAreas.at(i), event)) {
                event->accept();
                return;
            }
        }
        event->ignore();
        return;
    }
    if (deliverMouseEvent(event))
        event->accept();
    else
        event->ignore();
}

void QDeclarative3DGraphicsGeoMap::mouseMoveEvent(QMouseEvent *event)
{
    //QLOC_TRACE2(" ~~~~~~~ event, coordinates: ", event->pos());
    bool consumed = false;
    if (mouseGrabberItem_)
        consumed = deliverMouseEvent(event);
    consumed |= flickable_->mouseMoveEvent(event);
    if (consumed)
        event->accept();
    else
        event->ignore();
}

void QDeclarative3DGraphicsGeoMap::internalCenterChanged(const QGeoCoordinate &coordinate)
{
    emit centerChanged(new QDeclarativeCoordinate(coordinate, this));
}

//void QDeclarative3DGraphicsGeoMap::internalMapTypeChanged(QGraphicsGeoMap::MapType mapType)
//{
//    emit mapTypeChanged(QDeclarative3DGraphicsGeoMap::MapType(mapType));
//}

//void QDeclarative3DGraphicsGeoMap::internalConnectivityModeChanged(QGraphicsGeoMap::ConnectivityMode connectivityMode)
//{
//    emit connectivityModeChanged(QDeclarative3DGraphicsGeoMap::ConnectivityMode(connectivityMode));
//}

/*!
    \qmlmethod Map::addMapItem(MapItem)

    Adds the given MapOject to the Map. If the object already
    is on the Map, it will not be added again.

    As an example, consider you have a MapCircle presenting your current position:

    \snippet doc/src/snippets/declarative/testpolyMapItems.qml Basic map position marker definition
    You can add it to Map (alterntively it can be defined as a child element of the Map):

    \snippet doc/src/snippets/declarative/testpolyMapItems.qml Basic add MapItem
    Note: MapItemViews can not be added with this method.
*/

// all map item additions and removals, whether internal or app originated,
// must go through these functions
void QDeclarative3DGraphicsGeoMap::addMapItem(QDeclarativeGeoMapItem *item)
{
    QLOC_TRACE0;
#ifdef QSGSHADEREFFECTSOURCE_AVAILABLE
    if (!item || mapItems_.contains(item) || mapItemsPending_.contains(item))
        return;
    updateMutex_.lock();
    // set pending, we'll add them once we have GL context
    mapItemsPending_.append(item);
    connect(item, SIGNAL(destroyed(QObject*)), this, SLOT(mapItemDestroyed(QObject*)));
    updateMutex_.unlock();
#else
    Q_UNUSED(item);
#endif
}

void QDeclarative3DGraphicsGeoMap::removeMapItem(QDeclarativeGeoMapItem *item)
{
    QLOC_TRACE0;
#ifdef QSGSHADEREFFECTSOURCE_AVAILABLE
    if (!item)
        return;
    if (mapItemsPending_.contains(item)) {
        mapItemsPending_.removeAll(item);
        return;
    }
    if (!mapItems_.contains(item))
        return;
    updateMutex_.lock();
    item->setMap(0);
    // stop listening to destroyed()
    item->disconnect(this);
    // these can be optmized for perf, as we already check the 'contains' above
    mapItems_.removeOne(item);
    mapItemsPending_.removeOne(item);
    map_->removeMapItem(item->mapItem());
    updateMutex_.unlock();
#else
    Q_UNUSED(item);
#endif
}

// TODO clears all items including ones from models/MapItemview which is not intended
void QDeclarative3DGraphicsGeoMap::clearMapItems()
{
    if (mapItems_.isEmpty())
        return;
    updateMutex_.lock();
    mapItems_.clear();
    mapItemsPending_.clear();
    map_->clearMapItems();
    updateMutex_.unlock();
}

// when a map item is getting destroyed, we need make sure resources are released gracefully
// with QML we must prepare for arbitrary deletion times
void QDeclarative3DGraphicsGeoMap::mapItemDestroyed(QObject* item)
{
    QDeclarativeGeoMapItem* mapItem = qobject_cast<QDeclarativeGeoMapItem*>(item);
    if (mapItem)
        removeMapItem(mapItem);
}

void QDeclarative3DGraphicsGeoMap::setActiveMouseArea(QDeclarativeGeoMapMouseArea *area)
{
    Q_UNUSED(area); // TODO
    // to be done when the item picking is clear
}


/*!
    \qmlmethod Map::removeMapItem(MapItem)

    Removes the given MapItem from the Map. If the MapItem does not
    exist, function does nothing.

    As an example, consider you have a MapCircle presenting your current position:
    \snippet doc/src/snippets/declarative/testpolyMapItems.qml Basic map position marker definition

    You can remove it from the Map element:
    \snippet doc/src/snippets/declarative/testpolyMapItems.qml Basic remove MapItem


*/

// This function is strictly for testing purposes
int QDeclarative3DGraphicsGeoMap::testGetDeclarativeMapItemCount()
{
    return mapItems_.count();
}

#include "moc_qdeclarative3dgraphicsgeomap_p.cpp"

QT_END_NAMESPACE