source: flair-src/trunk/tools/FlairGCS/src/mapwidget.cpp @ 10

Last change on this file since 10 was 10, checked in by Sanahuja Guillaume, 5 years ago

lic

File size: 11.5 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5/****************************************************************************
6**
7** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
8** All rights reserved.
9** Contact: Nokia Corporation (qt-info@nokia.com)
10**
11** This file is part of the documentation of Qt. It was originally
12** published as part of Qt Quarterly.
13**
14** $QT_BEGIN_LICENSE:LGPL$
15** Commercial Usage
16** Licensees holding valid Qt Commercial licenses may use this file in
17** accordance with the Qt Commercial License Agreement provided with the
18** Software or, alternatively, in accordance with the terms contained in
19** a written agreement between you and Nokia.
20**
21** GNU Lesser General Public License Usage
22** Alternatively, this file may be used under the terms of the GNU Lesser
23** General Public License version 2.1 as published by the Free Software
24** Foundation and appearing in the file LICENSE.LGPL included in the
25** packaging of this file.  Please review the following information to
26** ensure the GNU Lesser General Public License version 2.1 requirements
27** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
28**
29** In addition, as a special exception, Nokia gives you certain additional
30** rights.  These rights are described in the Nokia Qt LGPL Exception
31** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
32**
33** GNU General Public License Usage
34** Alternatively, this file may be used under the terms of the GNU
35** General Public License version 3.0 as published by the Free Software
36** Foundation and appearing in the file LICENSE.GPL included in the
37** packaging of this file.  Please review the following information to
38** ensure the GNU General Public License version 3.0 requirements will be
39** met: http://www.gnu.org/copyleft/gpl.html.
40**
41** If you have questions regarding the use of this file, please contact
42** Nokia at qt-info@nokia.com.
43** $QT_END_LICENSE$
44**
45****************************************************************************/
46
47#include <QApplication>
48#include <QGeoCoordinate>
49#include <QGraphicsScene>
50
51#include <QTimer>
52#include <QMenu>
53#include "mapwidget.h"
54#include "Landmark.h"
55#include "Map.h"
56
57using namespace QtMobility;
58
59MapWidget::MapWidget(Map* map,QWidget *parent)
60    : QGraphicsView(parent) {
61    setCursor(Qt::ArrowCursor);
62    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
63    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
64
65    geoMap = 0;
66    dragging = false;
67    pressed = false;
68    landmark_match=NULL;
69    is_uptodate=true;
70    this->map=map;
71
72    m_scene = new QGraphicsScene(this);
73    setScene(m_scene);
74    setMouseTracking(true);
75
76    landmarks=new QList<Landmark*>;
77    landmarks_old=new QList<Landmark*>;
78}
79
80QGraphicsScene *MapWidget::scene(void) const
81{
82    return m_scene;
83}
84
85void MapWidget::setMap(QGraphicsGeoMap *geoMap)
86{
87    m_scene->clear();
88    m_scene->addItem(geoMap);
89    this->geoMap = geoMap;
90    geoMap->resize(m_scene->sceneRect().width(), m_scene->sceneRect().height());
91}
92
93void MapWidget::mousePressEvent(QMouseEvent *event)
94{
95    if (event->button() == Qt::LeftButton)
96    {
97        pressed = true;
98        dragStartPosition = event->pos();
99        QTimer::singleShot(qApp->startDragTime(), this, SLOT(initDrag()));
100        event->accept();
101    }
102
103    if (event->button() == Qt::RightButton)
104    {
105        QMenu *menu = new QMenu("nom", this);
106
107        if(cursor().shape()!=Qt::PointingHandCursor)
108        {
109            QAction *b,*z;//*a,
110            QList<QAction*> go_to_actions;
111            QList<QAction*> centered_actions;
112
113            QMenu *centered_menu=menu->addMenu("centered");
114            for(int i=0;i<points.count();i++)
115            {
116                QAction* action=centered_menu->addAction(points.at(i));
117                centered_actions.append(action);
118                action->setCheckable(true);
119                if(map->centeredPoint()==i)
120                {
121                    action->setChecked(true);
122                }
123                else
124                {
125                    action->setChecked(false);
126                }
127            }
128
129            menu->addSeparator();
130
131            b=menu->addAction("place checkpoint");
132            QMenu *go_to_menu=menu->addMenu("go to");
133            for(int i=0;i<landmarks->count();i++)
134            {
135                QAction* action=go_to_menu->addAction(QString("checkpoint %1").arg(i+1));
136                go_to_actions.append(action);
137            }
138            if(landmarks->count()==0) go_to_menu->setEnabled(false);
139
140            map->appendmenu(menu);
141            z=map->execmenu(this,menu,event->globalPos());
142
143            for(int i=0;i<centered_actions.count();i++)
144            {
145                if(z==centered_actions.at(i))
146                {
147                    if(centered_actions.at(i)->isChecked())
148                    {
149                        map->setCenteredPoint(i);
150                    }
151                    else
152                    {
153                        map->setCenteredPoint(-1);
154                    }
155
156                    break;
157                }
158            }
159
160            if(z==b)
161            {
162                Landmark* landmark=new Landmark(geoMap,geoMap->screenPositionToCoordinate(event->pos()),QString("%1").arg(landmarks->count()+1));
163                landmarks->append(landmark);
164                is_uptodate=false;
165            }
166            for(int i=0;i<go_to_actions.count();i++)
167            {
168                if(z==go_to_actions.at(i))
169                {
170                    map->setCenteredPoint(-1);
171                    geoMap->setCenter(landmarks->at(i)->coordinate());
172                    break;
173                }
174            }
175
176        }
177        else
178        {
179            QAction *a,*z;
180            a=menu->addAction("delete");
181            z=menu->exec(event->globalPos());
182
183            if(z==a)
184            {
185                int i;
186                for(i=0;i<landmarks->count();i++)
187                {
188                    if(landmarks->at(i)->contains(geoMap->screenPositionToCoordinate(event->pos())))
189                    {
190                        delete landmarks->at(i);
191                        landmarks->removeAt(i);
192                        break;
193                    }
194                }
195                for(int j=i;j<landmarks->count();j++)
196                {
197                    landmarks->at(j)->setText(QString("%1").arg(j+1));
198                }
199            }
200        }
201
202        delete menu;
203    }
204}
205
206void MapWidget::initDrag(void)
207{
208    if (pressed && map->isCentered() && landmark_match==NULL) {
209        dragging = true;
210    }
211}
212
213void MapWidget::mouseMoveEvent(QMouseEvent *event)
214{
215    if (!geoMap)
216        return;
217
218    Qt::CursorShape new_cursor;
219    if(dragging==true)
220    {
221        new_cursor=Qt::ClosedHandCursor;
222    }
223    else
224    {
225        if(map->isCentered())
226        {
227            if(pressed==true)
228            {
229                new_cursor=Qt::ForbiddenCursor;
230            }
231            else
232            {
233                new_cursor=Qt::ArrowCursor;
234            }
235        }
236        else
237        {
238            new_cursor=Qt::OpenHandCursor;
239        }
240        if(landmark_match!=NULL) new_cursor=Qt::PointingHandCursor;
241    }
242
243
244    for(int i=0;i<landmarks->count();i++)
245    {
246        if(landmarks->at(i)->contains(geoMap->screenPositionToCoordinate(event->pos())))
247        {
248            if(pressed && landmark_match==NULL)
249            {
250                landmark_match=landmarks->at(i);
251                landmark_match->setColor(Qt::red);
252                is_uptodate=false;
253            }
254            new_cursor=Qt::PointingHandCursor;
255            break;
256        }
257    }
258
259    if(new_cursor!=cursor().shape()) setCursor(new_cursor);
260
261    QPoint v = event->pos() - dragStartPosition;
262
263    if (dragging) {
264        geoMap->pan(-v.x(), -v.y());
265        dragStartPosition = event->pos();
266
267    } else if(landmark_match!=NULL){
268        landmark_match->setCoordinate(geoMap->screenPositionToCoordinate(event->pos()));
269
270    } else if (pressed && !map->isCentered() &&
271        v.manhattanLength() >= qApp->startDragDistance()) {
272        dragging = true;
273
274    } else {
275        dragStartPosition = event->pos();
276        emit positionChanged(geoMap->screenPositionToCoordinate(event->pos()));
277    }
278
279    event->accept();
280}
281
282void MapWidget::mouseReleaseEvent(QMouseEvent *event)
283{
284    pressed = false;
285    landmark_match=NULL;
286
287    if (dragging) {
288        QPoint v = event->pos() - dragStartPosition;
289        geoMap->pan(-v.x(), -v.y());
290        dragging = false;
291    }
292
293    event->accept();
294}
295
296void MapWidget::resizeEvent(QResizeEvent *event)
297{
298    if (geoMap) {
299        m_scene->setSceneRect(QRectF(0, 0, event->size().width(), event->size().height()));
300        geoMap->resize(event->size().width(), event->size().height());
301    }
302
303    QGraphicsView::resizeEvent(event);
304}
305
306void MapWidget::wheelEvent(QWheelEvent *event)
307{
308    int steps = event->delta() / 120;
309    int zoom = qBound(geoMap->minimumZoomLevel(), geoMap->zoomLevel() + steps,
310                      geoMap->maximumZoomLevel());
311
312    if (zoom != geoMap->zoomLevel()) geoMap->setZoomLevel(zoom);
313    //if(!centered) geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
314}
315
316void MapWidget::mouseDoubleClickEvent(QMouseEvent *event)
317{
318    if(!map->isCentered()) geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
319}
320
321bool MapWidget::IsUptodate(void)
322{
323    return is_uptodate;
324}
325
326void MapWidget::SetUptodate(void)
327{
328    for(int i=0;i<landmarks_old->count();i++)
329    {
330        delete landmarks_old->at(i);
331    }
332    landmarks_old->clear();
333
334    for(int i=0;i<landmarks->count();i++)
335    {
336        landmarks->at(i)->setColor(Qt::white);
337        Landmark* landmark=new Landmark(geoMap,landmarks->at(i)->coordinate(),QString("%1").arg(landmarks->count()+1));
338        landmarks_old->append(landmark);
339        landmarks_old->at(i)->setVisible(false);
340    }
341
342    is_uptodate=true;
343}
344
345void MapWidget::Reset(void)
346{
347    for(int i=0;i<landmarks->count();i++)
348    {
349        delete landmarks->at(i);
350    }
351    landmarks->clear();
352
353    for(int i=0;i<landmarks_old->count();i++)
354    {
355        Landmark* landmark=new Landmark(geoMap,landmarks_old->at(i)->coordinate(),QString("%1").arg(landmarks->count()+1));
356        landmarks->append(landmark);
357        landmarks->at(i)->setColor(Qt::white);
358    }
359
360    is_uptodate=true;
361}
362
363QList<Landmark*>* MapWidget::Landmarks(void)
364{
365    return landmarks;
366}
367
368bool MapWidget::LandmarkToSend(int index)
369{
370    if(index>=landmarks_old->count()) return true;
371
372    if(landmarks->at(index)->coordinate()!=landmarks_old->at(index)->coordinate())
373        return true;
374    else
375        return false;
376}
377void MapWidget::RemoveLandmarks(void)
378{
379    for(int i=0;i<landmarks->count();i++)
380    {
381        landmarks->at(i)->RemoveLandmark();
382    }
383    for(int i=0;i<landmarks_old->count();i++)
384    {
385        landmarks_old->at(i)->RemoveLandmark();
386    }
387}
388
389void MapWidget::AddLandmarks(QGraphicsGeoMap *geoMap)
390{
391    for(int i=0;i<landmarks->count();i++)
392    {
393        landmarks->at(i)->AddLandmark(geoMap);
394    }
395    for(int i=0;i<landmarks_old->count();i++)
396    {
397        landmarks_old->at(i)->AddLandmark(geoMap);
398    }
399}
400
401void MapWidget::AddLandmark(const QGeoCoordinate &coordinate)
402{
403    Landmark* landmark=new Landmark(geoMap,coordinate,QString("%1").arg(landmarks->count()+1));
404    landmarks->append(landmark);
405    landmark->setColor(Qt::white);
406    landmark=new Landmark(geoMap,coordinate,QString("%1").arg(landmarks_old->count()+1));
407    landmarks_old->append(landmark);
408    landmark->setColor(Qt::white);
409    landmark->setVisible(false);
410}
411
412void MapWidget::AddPoint(QString name)
413{
414    points.append(name);
415}
Note: See TracBrowser for help on using the repository browser.