08584895a87a12eaf47a3fa72550e7feae3790c3
[gpssportsniffer] / tilesMap.h
1 #ifndef TILESMAP_H
2 #define TILESMAP_H
3
4 #include "log.h"
5 #include "track.h"
6 #include "utils.h"
7
8 #include <math.h>
9 //#include <qmobilityglobal.h>
10 #include <qgeopositioninfosource.h>
11 #include <qgeopositioninfo.h>
12 #include <qnetworkconfigmanager.h>
13 #include <qnetworksession.h>
14 #include <QPainter>
15 #include <QNetworkReply>
16 #include <QDir>
17 #include <QThread>
18
19
20 // Use the QtMobility namespace
21 QTM_USE_NAMESPACE
22
23 // tile size in pixels
24 const int tdim=256;
25
26 class TileRequest{
27
28 public:
29
30     QDateTime dateTime;
31     bool save;
32     int zoom;
33
34     TileRequest(){}
35
36     TileRequest(int z, QDateTime time, bool s){
37         dateTime=time;
38         save=s;
39         zoom=z;
40     }
41
42 };
43
44 /*
45 class ThreadSaver: public QThread
46 {
47     Q_OBJECT
48
49 public:
50     ThreadSaver(QImage qi, QString fs);
51
52 protected:
53     void run();
54 private:
55     QImage img;
56     QString fileString;
57 };
58
59 */
60 class TilesMap: public QObject
61 {
62     Q_OBJECT
63
64 public:
65
66     TilesMap(QNetworkSession *session, QObject *parent, Log *log,int zoom, MapType mapType);
67     ~TilesMap();
68
69     inline void setZoom(int z){zoom=z;}
70     inline int getZoom(){return zoom;}
71
72     inline void setWidth(int w){width=w;}
73     inline int getWidth(){return width;}
74
75     inline void setHeight(int h){height=h;}
76     inline int getHeight(){return height;}
77
78     inline void setNetwork(bool status){network=status;}
79     inline bool getNetwork(){return network;}
80
81     inline void setMapType(MapType m){mapType=m;}
82     inline MapType getMapType(){return mapType;}
83
84     //inline void setLatitude(qreal lat){latitude=lat;}
85     inline qreal getLatitude(){return latitude;}
86
87     //inline void setLongitude(qreal lng){longitude=lng;}
88     inline qreal getLongitude(){return longitude;}
89
90     inline QRect getTilesRect(){return m_tilesRect;}
91
92     QPointF coordinate2tile(qreal, qreal, int);
93     qreal tilex2long(qreal, int);
94     qreal tiley2lat(qreal, int);
95
96     bool mapIsOnCache(int zoom,int x,int y);
97     void updateTiles();
98     void updateTiles(qreal lat, qreal lng);
99
100     int downloadMaps(Track* track_p);
101     void downloadTiles(qreal lat, qreal lng, int zoom, int* numTiles);
102     void downloadWindow(int *numTiles, int zoom);
103
104     void updatePosition(GpsPoint point);
105     void render(QPainter *p, const QRect &rect);  
106     void pan(const QPoint &delta);
107     void clearAllMaps();
108
109
110 private slots:
111      void handleReplies(QNetworkReply *reply);
112      void getTiles();
113
114 signals:
115     void updated(const QRect &rect);
116     void tilesDownloaded(int value);
117     void pointsRequested(int value);
118
119 protected:
120     QRect tileRect(const QPoint &tp);
121
122 private:
123
124     int zoom;
125     bool network;
126     int width;
127     int height;
128
129     int tilesD;
130
131     qreal latitude;
132     qreal longitude;
133
134     QDir m_mapsDir;
135
136     QNetworkSession* m_session;
137     Log *m_log;
138     MapType mapType;
139
140     QPoint m_offset;
141     QRect m_tilesRect;
142     QPixmap m_emptyTile;
143     QHash<QPoint, QPixmap> m_tileMaps;
144     QNetworkAccessManager *m_manager;
145     QUrl m_url;
146     QString mapUrlProvider();
147     QList<QNetworkReply*> m_pendingReplies;
148     QHash<QPoint, TileRequest> m_tileRequests;
149
150     bool timeout(QDateTime qdt);
151     QString mapsDir();
152     QString fileUrlProvider();
153     QString urlProvider();
154
155 };
156
157 #endif // TILESMAP_H