Merge branch 'master' of https://vcs.maemo.org/git/gpssportsniffer
[gpssportsniffer] / tilesMap.h
1 /****************************************************************************
2 **
3 **  Copyright (C) 2011  Tito Eritja Real <jtitoo@gmail.com>
4 **
5 **  This program is free software: you can redistribute it and/or modify
6 **  it under the terms of the GNU General Public License as published by
7 **  the Free Software Foundation, either version 3 of the License, or
8 **  (at your option) any later version.
9 **
10 **  This program is distributed in the hope that it will be useful,
11 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 **  GNU General Public License for more details.
14 **
15 **  You should have received a copy of the GNU General Public License
16 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 **
18 ****************************************************************************/
19
20 #ifndef TILESMAP_H
21 #define TILESMAP_H
22
23 #include "log.h"
24 #include "track.h"
25 #include "utils.h"
26
27 #include <math.h>
28 //#include <qmobilityglobal.h>
29 #include <qgeopositioninfosource.h>
30 #include <qgeopositioninfo.h>
31 #include <qnetworkconfigmanager.h>
32 #include <qnetworksession.h>
33 #include <QPainter>
34 #include <QNetworkReply>
35 #include <QDir>
36 #include <QThread>
37
38
39 // Use the QtMobility namespace
40 QTM_USE_NAMESPACE
41
42
43
44 class TileRequest{
45
46 public:
47
48     QDateTime dateTime;
49     bool save;
50     bool downloading;
51     int zoom;
52
53     TileRequest(){}
54
55     TileRequest(int z, QDateTime time, bool s, bool d){
56         dateTime=time;
57         save=s;
58         zoom=z;
59         downloading=d;
60     }
61
62 };
63
64 /*
65 class ThreadSaver: public QThread
66 {
67     Q_OBJECT
68
69 public:
70     ThreadSaver(QImage qi, QString fs);
71
72 protected:
73     void run();
74 private:
75     QImage img;
76     QString fileString;
77 };
78
79 */
80 class TilesMap: public QObject
81 {
82     Q_OBJECT
83
84 public:
85
86     TilesMap(QNetworkSession *session, QObject *parent, Log *log,int zoom, MapType mapType);
87     ~TilesMap();
88
89     inline void setZoom(int z){zoom=z;}
90     inline int getZoom(){return zoom;}
91
92     inline void setWidth(int w){width=w;}
93     inline int getWidth(){return width;}
94
95     inline void setHeight(int h){height=h;}
96     inline int getHeight(){return height;}
97
98     inline void setNetwork(bool status){network=status;}
99     inline bool getNetwork(){return network;}
100
101     inline void setMapType(MapType m){mapType=m;}
102     inline MapType getMapType(){return mapType;}
103
104     inline bool isCacheEnabled(){return cache;}
105     inline void setIsCacheEnabled(bool value){cache=value;}
106
107     //inline void setLatitude(qreal lat){latitude=lat;}
108     inline qreal getLatitude(){return latitude;}
109
110     //inline void setLongitude(qreal lng){longitude=lng;}
111     inline qreal getLongitude(){return longitude;}
112
113     inline QRect getTilesRect(){return m_tilesRect;}
114
115     QPointF coordinate2tile(qreal, qreal, int);
116     qreal tilex2long(qreal, int);
117     qreal tiley2lat(qreal, int);
118
119     bool mapIsOnCache(int zoom,int x,int y);
120     void updateTiles();
121     void updateTiles(qreal lat, qreal lng);
122
123     void cancelDownloading();
124     int downloadMaps(Track* track_p);
125     void downloadTiles(qreal lat, qreal lng, int zoom, int* numTiles);
126     void downloadWindow(int *numTiles, int zoom);
127
128     void updatePosition(GpsPoint point);
129     void render(QPainter *p, const QRect &rect);  
130     void pan(const QPoint &delta);
131     void clearAllMaps();
132
133
134 private slots:
135      void handleReplies(QNetworkReply *reply);
136      void getTiles();
137
138 signals:
139     void updated(const QRect &rect);
140     void tilesDownloaded(int value);
141     void pointsRequested(int value);
142
143 protected:
144     QRect tileRect(const QPoint &tp);
145
146 private:
147
148     int zoom;
149     bool network;
150     int width;
151     int height;
152
153     int tilesD;
154     bool cache;
155
156     qreal latitude;
157     qreal longitude;
158
159     QDir m_mapsDir;
160
161     QNetworkSession* m_session;
162     Log *m_log;
163     MapType mapType;
164
165     QPoint m_offset;
166     QRect m_tilesRect;
167     QPixmap m_emptyTile;
168     QHash<QPoint, QPixmap> m_tileMaps;
169     QNetworkAccessManager *m_manager;
170     QUrl m_url;
171     QString mapUrlProvider();
172     QList<QNetworkReply*> m_pendingReplies;
173     QHash<QPoint, TileRequest> m_tileRequests;
174
175     bool timeout(QDateTime qdt);
176     QString mapsDir();
177     QString fileUrlProvider();
178     QString urlProvider();
179
180 };
181
182 #endif // TILESMAP_H