Subversion Repositories Projects

Rev

Rev 391 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
305 KeyOz 1
/*
2
*
3
* This file is part of QMapControl,
4
* an open-source cross-platform map widget
5
*
6
* Copyright (C) 2007 - 2008 Kai Winter
7
*
8
* This program is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU Lesser General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
11
* (at your option) any later version.
12
*
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
* GNU Lesser General Public License for more details.
17
*
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with QMapControl. If not, see <http://www.gnu.org/licenses/>.
20
*
21
* Contact e-mail: kaiwinter@gmx.de
22
* Program URL   : http://qmapcontrol.sourceforge.net/
23
*
24
*/
25
 
26
#ifndef MAPCONTROL_H
27
#define MAPCONTROL_H
28
 
29
#include <QtGui>
30
 
31
#include "layermanager.h"
32
#include "layer.h"
33
#include "mapadapter.h"
34
#include "geometry.h"
35
#include "imagemanager.h"
36
 
37
//! QMapControl namespace
38
namespace qmapcontrol
39
{
40
    class LayerManager;
41
    class MapAdapter;
42
    class Layer;
43
 
44
    //! The control element of the widget and also the widget itself
45
    /*!
46
     * This is the main widget.
47
     * To this control layers can be added.
48
     * A MapControl have to be instantiated with a QSize which sets the size the widget takes in a layout.
49
     * The given size is also the size, which is asured to be filled with map images.
50
     *
51
     * @author Kai Winter <kaiwinter@gmx.de>
52
     */
53
    class MapControl : public QWidget
54
    {
55
        Q_OBJECT
56
 
57
    public:
58
        //! Declares what actions the mouse move has on the map
59
        enum MouseMode
60
        {
61
            Panning, /*!< The map is moved */
62
            Dragging, /*!< A rectangular can be drawn */
63
            None, /*!< Mouse move events have no efect to the map */
64
        };
65
 
66
        //! The constructor of MapControl
67
        /*!
68
         * The MapControl is the widget which displays the maps.
69
         * The size describes the area, which gets filled with map data
70
         * When you give no MouseMode, the mouse is moving the map.
71
         * You can change the MouseMode on runtime, to e.g. Dragging, which lets the user drag a rectangular box.
72
         * After the dragging a signal with the size of the box is emitted.
73
         * The mousemode ´None´ can be used, to completely define the control of the map yourself.
74
         * @param size the size which the widget should fill with map data
75
         * @param mousemode the way mouseevents are handled
76
         */
77
        MapControl ( QSize size, MouseMode mousemode = Panning );
78
 
79
        ~MapControl();
80
 
81
        //! adds a layer
82
        /*!
83
         * If multiple layers are added, they are painted in the added order.
84
         * @param layer the layer which should be added
85
         */
86
        void addLayer ( Layer* layer );
87
 
88
        //! returns the layer with the given name
89
        /*!
90
         * @param  layername name of the wanted layer
91
         * @return the layer with the given name
92
         */
93
        Layer* layer ( const QString& layername ) const;
94
 
95
        //! returns the names of all layers
96
        /*!
97
         * @return returns a QList with the names of all layers
98
         */
99
        QList<QString> layers() const;
100
 
101
        //! returns the number of existing layers
102
        /*!
103
         * @return returns the number of existing layers
104
         */
105
        int numberOfLayers() const;
106
 
107
        //! returns the coordinate of the center of the map
108
        /*!
109
         * @return returns the coordinate of the middle of the screen
110
         */
111
        QPointF currentCoordinate() const;
112
 
113
        //! returns the current zoom level
114
        /*!
115
         * @return returns the current zoom level
116
         */
117
        int currentZoom() const;
118
 
119
        //! sets the middle of the map to the given coordinate
120
        /*!
121
         * @param  coordinate the coordinate which the view´s middle should be set to
122
         */
123
        void setView ( const QPointF& coordinate ) const;
124
 
125
        //! sets the view, so all coordinates are visible
126
        /*!
127
         * @param  coordinates the Coorinates which should be visible
128
         */
129
        void setView ( const QList<QPointF> coordinates ) const;
130
 
131
        //! sets the view and zooms in, so all coordinates are visible
132
        /*!
133
         * The code of setting the view to multiple coordinates is "brute force" and pretty slow.
134
         * Have to be reworked.
135
         * @param  coordinates the Coorinates which should be visible
136
         */
137
        void setViewAndZoomIn ( const QList<QPointF> coordinates ) const;
138
 
139
        //! sets the view to the given Point
140
        /*!
141
         *
142
         * @param point the geometric point the view should be set to
143
         */
144
        void setView ( const Point* point ) const;
145
 
146
        //! Keeps the center of the map on the Geometry, even when it moves
147
        /*!
148
         * To stop the following the method stopFollowing() have to be called
149
         * @param  geometry the Geometry which should stay centered.
150
         */
151
        void followGeometry ( const Geometry* geometry ) const;
152
 
153
        //TODO:
154
        // void followGeometry(const QList<Geometry*>) const;
155
 
156
        //! Stops the following of a Geometry
157
        /*!
158
         * if the view is set to follow a Geometry this method stops the trace.
159
         * See followGeometry().
160
         * @param geometry the Geometry which should not followed anymore
161
         */
162
        void stopFollowing ( Geometry* geometry );
163
 
164
        //! Smoothly moves the center of the view to the given Coordinate
165
        /*!
166
         * @param  coordinate the Coordinate which the center of the view should moved to
167
         */
168
        void moveTo     ( QPointF coordinate );
169
 
170
        //! sets the Mouse Mode of the MapControl
171
        /*!
172
         * There are three MouseModes declard by an enum.
173
         * The MouesMode Dragging draws an rectangular in the map while the MouseButton is pressed.
174
         * When the Button is released a boxDragged() signal is emitted.
175
         *
176
         * The second MouseMode (the default) is Panning, which allows to drag the map around.
177
         * @param mousemode the MouseMode
178
         */
179
        void setMouseMode ( MouseMode mousemode );
180
 
181
        //! returns the current MouseMode
182
        /*!
183
         * For a explanation for the MouseModes see setMouseMode()
184
         * @return the current MouseMode
185
         */
186
        MapControl::MouseMode mouseMode();
187
 
188
        //int rotation;
189
 
190
        //! Enable persistent caching of map tiles
191
        /*!
192
         * Call this method to allow the QMapControl widget to save map tiles
193
         * persistent (also over application restarts).
194
         * Tiles are stored in the subdirectory "QMapControl.cache" within the
195
         * user's home directory. This can be changed by giving a path.
196
         * @param path the path to the cache directory
197
         */
198
        void enablePersistentCache ( const QDir& path=QDir::homePath() + "/QMapControl.cache" );
199
 
200
 
201
        //! Sets the proxy for HTTP connections
202
        /*!
203
         * This method sets the proxy for HTTP connections.
204
         * This is not provided by the current Qtopia version!
205
         * @param host the proxy´s hostname or ip
206
         * @param port the proxy´s port
207
         */
208
        void setProxy ( QString host, int port );
209
 
210
        //! Displays the scale within the widget
211
        /*!
212
         *
213
         * @param show true if the scale should be displayed
214
         */
215
        void showScale ( bool show );
216
 
217
    private:
218
        LayerManager* layermanager;
219
        QPoint screen_middle; // middle of the widget (half size)
220
 
221
        QPoint pre_click_px; // used for scrolling (MouseMode Panning)
222
        QPoint current_mouse_pos; // used for scrolling and dragging (MouseMode Panning/Dragging)
223
 
224
        QSize size; // size of the widget
225
 
226
        bool mousepressed;
227
        MouseMode mymousemode;
228
        bool scaleVisible;
229
 
230
        bool m_loadingFlag;
231
 
232
        QMutex moveMutex; // used for method moveTo()
233
        QPointF target; // used for method moveTo()
234
        int steps; // used for method moveTo()
235
 
236
        QPointF clickToWorldCoordinate ( QPoint click );
237
        MapControl& operator= ( const MapControl& rhs );
238
        MapControl ( const MapControl& old );
239
 
240
    protected:
241
        void paintEvent ( QPaintEvent* evnt );
242
        void mousePressEvent ( QMouseEvent* evnt );
243
        void mouseReleaseEvent ( QMouseEvent* evnt );
244
        void mouseMoveEvent ( QMouseEvent* evnt );
245
 
246
    signals:
247
        // void mouseEvent(const QMouseEvent* evnt);
248
 
249
        //! Emitted AFTER a MouseEvent occured
250
        /*!
251
         * This signals allows to receive click events within the MapWidget together with the world coordinate.
252
         * It is emitted on MousePressEvents and MouseReleaseEvents.
253
         * The kind of the event can be obtained by checking the events type.
254
         * @param  evnt the QMouseEvent that occured
255
         * @param  coordinate the corresponding world coordinate
256
         */
257
        void mouseEventCoordinate ( const QMouseEvent* evnt, const QPointF coordinate );
258
 
259
        //! Emitted, after a Rectangular is dragged.
260
        /*!
261
         * It is possible to select a rectangular area in the map, if the MouseMode is set to Dragging.
262
         * The coordinates are in world coordinates
263
         * @param  QRectF the dragged Rect
264
         */
265
        void boxDragged ( const QRectF );
266
 
267
        //! This signal is emitted, when a Geometry is clicked
268
        /*!
269
         * @param geometry The clicked Geometry object
270
         * @param coord_px  The coordinate in pixel coordinates
271
         */
272
        void geometryClicked ( Geometry* geometry, QPoint coord_px );
273
 
274
        //! This signal is emitted, after the view have changed
275
        /*!
276
         * @param coordinate The current coordinate
277
         * @param zoom The current zoom
278
         */
279
        void viewChanged ( const QPointF &coordinate, int zoom );
280
 
281
    public slots:
282
        //! zooms in one step
283
        void zoomIn();
284
 
285
        //! zooms out one step
286
        void zoomOut();
287
 
288
        //! sets the given zoomlevel
289
        /*!
290
         * @param zoomlevel the zoomlevel
291
         */
292
        void setZoom ( int zoomlevel );
293
 
294
        //! scrolls the view to the left
295
        void scrollLeft ( int pixel=10 );
296
 
297
        //! scrolls the view to the right
298
        void scrollRight ( int pixel=10 );
299
 
300
        //! scrolls the view up
301
        void scrollUp ( int pixel=10 );
302
 
303
        //! scrolls the view down
304
        void scrollDown ( int pixel=10 );
305
 
306
        //! scrolls the view by the given point
307
        void scroll ( const QPoint scroll );
308
 
309
        //! updates the map for the given rect
310
        /*!
311
         * @param rect the area which should be repainted
312
         */
313
        void updateRequest ( QRect rect );
314
 
315
        //! updates the hole map by creating a new offscreen image
316
        /*!
317
         *
318
         */
319
        void updateRequestNew();
320
 
321
        //! Resizes the map to the given size
322
        /*!
323
         * @param newSize The new size
324
         */
325
        void resize(const QSize newSize);
326
 
327
    private slots:
328
        void tick();
329
        void loadingFinished();
330
        void positionChanged ( Geometry* geom );
331
    };
332
}
333
#endif