~ubuntu-branches/ubuntu/saucy/merkaartor/saucy

« back to all changes in this revision

Viewing changes to QMapControl/point.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2009-09-13 00:52:12 UTC
  • mto: (1.2.7 upstream) (0.1.3 upstream) (3.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20090913005212-pjecal8zxm07x0fj
ImportĀ upstreamĀ versionĀ 0.14+svnfixes~20090912

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2007 by Kai Winter   *
3
 
 *   kaiwinter@gmx.de   *
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 2 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, write to the                         *
17
 
 *   Free Software Foundation, Inc.,                                       *
18
 
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 
 ***************************************************************************/
20
 
#include "point.h"
21
 
 
22
 
Point::Point()
23
 
{}
24
 
Point::Point(const Point& point)
25
 
        :Geometry(point.getName()), X(point.getLongitude()), Y(point.getLatitude())
26
 
{
27
 
        visible = point.isVisible();
28
 
        mywidget = 0;
29
 
        mypixmap = 0;
30
 
        this->pen = point.pen;
31
 
        homelevel = -1;
32
 
        minsize = QSize(-1,-1);
33
 
        maxsize = QSize(-1,-1);
34
 
}
35
 
//protected: 
36
 
Point::Point(double x, double y, QString name, enum Alignment alignment)
37
 
        : Geometry(name), X(x), Y(y), alignment(alignment)
38
 
{
39
 
        GeometryType = "Point";
40
 
        mywidget = 0;
41
 
        mypixmap = 0;
42
 
        visible = true;
43
 
        homelevel = -1;
44
 
        minsize = QSize(-1,-1);
45
 
        maxsize = QSize(-1,-1);
46
 
}
47
 
 
48
 
Point::Point(double x, double y, QWidget* widget, QString name, enum Alignment alignment)
49
 
        : Geometry(name), X(x), Y(y), mywidget(widget), alignment(alignment)
50
 
{
51
 
//      Point(x, y, name, alignment);
52
 
        GeometryType = "Point";
53
 
        mypixmap = 0;
54
 
        visible = true;
55
 
        size = widget->size();
56
 
        homelevel = -1;
57
 
        minsize = QSize(-1,-1);
58
 
        maxsize = QSize(-1,-1);
59
 
        mywidget->show();
60
 
}
61
 
Point::Point(double x, double y, QPixmap* pixmap, QString name, enum Alignment alignment)
62
 
        : Geometry(name), X(x), Y(y), mypixmap(pixmap), alignment(alignment)
63
 
{
64
 
        GeometryType = "Point";
65
 
        mywidget = 0;
66
 
        visible = true;
67
 
        size = pixmap->size();
68
 
        homelevel = -1;
69
 
        minsize = QSize(-1,-1);
70
 
        maxsize = QSize(-1,-1);
71
 
}
72
 
/*
73
 
Point& Point::operator=(const Point& rhs)
74
 
{
75
 
        if (this == &rhs)
76
 
                return *this;
77
 
        else
78
 
        {
79
 
                X = rhs.X;
80
 
                Y = rhs.Y;
81
 
                size = rhs.size;
82
 
                
83
 
                mywidget = rhs.mywidget;
84
 
                mypixmap = rhs.mypixmap;
85
 
                alignment = rhs.alignment;
86
 
                homelevel = rhs.homelevel;
87
 
                minsize = rhs.minsize;
88
 
                maxsize = rhs.maxsize;
89
 
        }
90
 
}
91
 
*/
92
 
Point::~Point()
93
 
{
94
 
        delete mywidget;
95
 
        delete mypixmap;
96
 
}
97
 
 
98
 
void Point::setVisible(bool visible)
99
 
{
100
 
        this->visible = visible;
101
 
        if (mywidget !=0)
102
 
        {
103
 
                mywidget->setVisible(visible);
104
 
        }
105
 
}
106
 
 
107
 
QRectF Point::getBoundingBox()
108
 
{
109
 
        //TODO: have to be calculated in relation to alignment...
110
 
        return QRectF(QPointF(X, Y), displaysize);
111
 
}
112
 
 
113
 
double Point::getLongitude() const
114
 
{
115
 
        return X;
116
 
}
117
 
double Point::getLatitude() const
118
 
{
119
 
        return Y;
120
 
}
121
 
QPointF Point::getCoordinate() const
122
 
{
123
 
        return QPointF(X, Y);
124
 
}
125
 
 
126
 
void Point::draw(QPainter* painter, const MapAdapter* mapadapter, const QRect &viewport, const QPoint offset)
127
 
{
128
 
        if (!visible)
129
 
                return;
130
 
        
131
 
        if (homelevel > 0)
132
 
        {
133
 
 
134
 
                int currentzoom = mapadapter->getMaxZoom() < mapadapter->getMinZoom() ? mapadapter->getMinZoom() - mapadapter->getZoom() : mapadapter->getZoom();
135
 
                                
136
 
//              int currentzoom = mapadapter->getZoom();
137
 
                int diffzoom = homelevel-currentzoom;
138
 
                int viewheight = size.height();
139
 
                int viewwidth = size.width();
140
 
                viewheight = int(viewheight / pow(2, diffzoom+0.0));
141
 
                viewwidth = int(viewwidth / pow(2, diffzoom+0.0));
142
 
                
143
 
                if (minsize.height()!= -1 && viewheight < minsize.height())
144
 
                        viewheight = minsize.height();
145
 
                else if (maxsize.height() != -1 && viewheight > maxsize.height())
146
 
                        viewheight = maxsize.height();
147
 
                
148
 
                
149
 
                if (minsize.width()!= -1 && viewwidth < minsize.width())
150
 
                        viewwidth = minsize.width();
151
 
                else if (maxsize.width() != -1 && viewwidth > maxsize.width())
152
 
                        viewwidth = maxsize.width();
153
 
                
154
 
                
155
 
                displaysize = QSize(viewwidth, viewheight);
156
 
        }
157
 
        else
158
 
        {
159
 
                displaysize = size;
160
 
        }
161
 
        
162
 
        
163
 
        if (mypixmap !=0)
164
 
        {
165
 
                const QPointF c = QPointF(X, Y);
166
 
                QPoint point = mapadapter->coordinateToDisplay(c);
167
 
 
168
 
                if (viewport.contains(point))
169
 
                {
170
 
                        QPoint alignedtopleft = getAlignedPoint(point);
171
 
                        painter->drawPixmap(alignedtopleft.x(), alignedtopleft.y(), displaysize.width(), displaysize.height(), *mypixmap);
172
 
                }
173
 
                
174
 
        }
175
 
        else if (mywidget!=0)
176
 
        {
177
 
                drawWidget(mapadapter, offset);
178
 
        }
179
 
        
180
 
}
181
 
 
182
 
void Point::drawWidget(const MapAdapter* mapadapter, const QPoint offset)
183
 
{
184
 
        const QPointF c = QPointF(X, Y);
185
 
        QPoint point = mapadapter->coordinateToDisplay(c);
186
 
        point -= offset;
187
 
 
188
 
        QPoint alignedtopleft = getAlignedPoint(point);
189
 
        mywidget->setGeometry(alignedtopleft.x(), alignedtopleft.y(), displaysize.width(), displaysize.height());
190
 
}
191
 
 
192
 
QPoint Point::getAlignedPoint(const QPoint point) const
193
 
{
194
 
        QPoint alignedtopleft;
195
 
        if (alignment == Middle)
196
 
        {
197
 
                alignedtopleft.setX(point.x()-displaysize.width()/2);
198
 
                alignedtopleft.setY(point.y()-displaysize.height()/2);
199
 
        }
200
 
        else if (alignment == TopLeft)
201
 
        {
202
 
                alignedtopleft.setX(point.x());
203
 
                alignedtopleft.setY(point.y());
204
 
        }
205
 
        else if (alignment == TopRight)
206
 
        {
207
 
                alignedtopleft.setX(point.x()-displaysize.width());
208
 
                alignedtopleft.setY(point.y());
209
 
        }
210
 
        else if (alignment == BottomLeft)
211
 
        {
212
 
                alignedtopleft.setX(point.x());
213
 
                alignedtopleft.setY(point.y()-displaysize.height());
214
 
        }
215
 
        else if (alignment == BottomRight)
216
 
        {
217
 
                alignedtopleft.setX(point.x()-displaysize.width());
218
 
                alignedtopleft.setY(point.y()-displaysize.height());
219
 
        }
220
 
        return alignedtopleft;
221
 
}
222
 
 
223
 
 
224
 
bool Point::Touches(Point* p, const MapAdapter* mapadapter)
225
 
{
226
 
        if (this->isVisible() == false)
227
 
                return false;
228
 
        if (mypixmap == 0)
229
 
                return false;
230
 
        
231
 
        QPointF c = p->getCoordinate();
232
 
                                // coordinate nach pixel umrechnen
233
 
        QPoint pxOfPoint = mapadapter->coordinateToDisplay(c);
234
 
                // size/2 Pixel toleranz aufaddieren
235
 
        QPoint p1;
236
 
        QPoint p2;
237
 
 
238
 
        switch (alignment)
239
 
        {
240
 
                case Middle:
241
 
                        p1 = pxOfPoint - QPoint(displaysize.width()/2,displaysize.height()/2);
242
 
                        p2 = pxOfPoint + QPoint(displaysize.width()/2,displaysize.height()/2);
243
 
                        break;
244
 
                case TopLeft:
245
 
                        p1 = pxOfPoint - QPoint(displaysize.width(),displaysize.height());
246
 
                        p2 = pxOfPoint;
247
 
                        break;
248
 
                case TopRight:
249
 
                        p1 = pxOfPoint - QPoint(0, displaysize.height());
250
 
                        p2 = pxOfPoint + QPoint(displaysize.width(),0);
251
 
                        break;
252
 
                case BottomLeft:
253
 
                        p1 = pxOfPoint - QPoint(displaysize.width(), 0);
254
 
                        p2 = pxOfPoint + QPoint(0, displaysize.height());
255
 
                        break;                  
256
 
                case BottomRight:
257
 
                        p1 = pxOfPoint;
258
 
                        p2 = pxOfPoint + QPoint(displaysize.width(), displaysize.height());
259
 
                        break;
260
 
        }
261
 
 
262
 
                // "Bounding Box" in koordinate umrechnen
263
 
        QPointF c1 = mapadapter->displayToCoordinate(p1);
264
 
        QPointF c2 = mapadapter->displayToCoordinate(p2);
265
 
                
266
 
 
267
 
        if(this->getLongitude()>=c1.x() && this->getLongitude()<=c2.x())
268
 
        {
269
 
                if (this->getLatitude()<=c1.y() && this->getLatitude()>=c2.y())
270
 
                {
271
 
                        emit(geometryClickEvent(this, QPoint(0,0)));
272
 
                        return true;
273
 
                }
274
 
        }
275
 
        return false;
276
 
}
277
 
 
278
 
void Point::setCoordinate(QPointF point)
279
 
{
280
 
//      emit(updateRequest(this));
281
 
//      emit(updateRequest(QRectF(X, Y, size.width(), size.height())));
282
 
        X = point.x();
283
 
        Y = point.y();
284
 
//      emit(updateRequest(this));
285
 
        emit(updateRequest(QRectF(X, Y, size.width(), size.height())));
286
 
        
287
 
        emit(positionChanged(this));
288
 
}
289
 
QList<Point*> Point::getPoints()
290
 
{
291
 
        //TODO: assigning temp?!
292
 
        QList<Point*> points;
293
 
        points.append(this);
294
 
        return points;
295
 
}
296
 
 
297
 
QWidget* Point::getWidget()
298
 
{
299
 
        return mywidget;
300
 
}
301
 
 
302
 
QPixmap* Point::getPixmap()
303
 
{
304
 
        return mypixmap;
305
 
}
306
 
 
307
 
void Point::setBaselevel(int zoomlevel)
308
 
{
309
 
        homelevel = zoomlevel;
310
 
}
311
 
void Point::setMinsize(QSize minsize)
312
 
{
313
 
        this->minsize = minsize;
314
 
}
315
 
void Point::setMaxsize(QSize maxsize)
316
 
{
317
 
        this->maxsize = maxsize;
318
 
}
319
 
Point::Alignment Point::getAlignment() const
320
 
{
321
 
        return alignment;
322
 
}