3fdcca9dfdde2333340da363ea9263d1a2d93aa5
[situare] / src / ui / mapviewscreen.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Kaj Wallin - kaj.wallin@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include "mapviewscreen.h"
23 #include "map/mapview.h"
24 #include "panelcommon.h"
25 #include "panelsidebar.h"
26
27 MapViewScreen::MapViewScreen(QWidget *parent)
28    : QWidget(parent),
29      m_autoCenteringEnabled(false)
30 {
31     MapView *mapView = new MapView(this);
32     m_mapEngine = new MapEngine(this);
33     mapView->setScene(m_mapEngine->scene());
34
35     m_friendsListPanel = new FriendListPanel(this);
36     m_userPanel = new UserInfoPanel(this);
37     PanelSideBar *userPanelSidebar = new PanelSideBar(this, LEFT);
38     PanelSideBar *friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
39
40     m_zoomButtonPanel = new ZoomButtonPanel(this, ZOOM_BUTTON_PANEL_POSITION_X,
41                                             ZOOM_BUTTON_PANEL_POSITION_Y);
42
43     ownLocationCrosshair = new QLabel(this);
44     QPixmap crosshairImage(":/res/images/sight.png");
45     ownLocationCrosshair->setPixmap(crosshairImage);
46     ownLocationCrosshair->setFixedSize(crosshairImage.size());
47     ownLocationCrosshair->hide();
48
49     connect(mapView, SIGNAL(viewScrolled(QPoint)),
50             m_mapEngine, SLOT(setLocation(QPoint)));
51     connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
52             mapView, SLOT(centerToSceneCoordinates(QPoint)));
53     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
54             mapView, SLOT(setZoomLevel(int)));
55     connect(mapView, SIGNAL(viewResized(QSize)),
56             m_mapEngine, SLOT(viewResized(QSize)));
57 //    connect(mapView, SIGNAL(viewContentChanged(QRect)),
58 //            m_mapEngine, SLOT(alignImmovableItems(QRect)));
59     connect(mapView, SIGNAL(updateViewContent(QRect)),
60             m_mapEngine, SLOT(receiveViewSceneRect(QRect)));
61     connect(mapView, SIGNAL(viewZoomFinished()),
62             m_mapEngine, SLOT(viewZoomFinished()));
63
64     connect(this, SIGNAL(zoomInKeyPressed()),
65             m_mapEngine, SLOT(zoomIn()));
66     connect(this, SIGNAL(zoomOutKeyPressed()),
67             m_mapEngine, SLOT(zoomOut()));
68
69     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
70             this, SLOT(drawOsmLicense(int, int)));
71     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
72             m_friendsListPanel, SLOT(reDrawFriendsPanel(int, int)));
73     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
74             m_userPanel, SLOT(reDrawUserPanel(int, int)));
75     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
76             friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
77
78     connect(m_zoomButtonPanel->m_zoomInBtn, SIGNAL(clicked()),
79             m_mapEngine, SLOT(zoomIn()));
80     connect(m_zoomButtonPanel->m_zoomOutBtn, SIGNAL(clicked()),
81             m_mapEngine, SLOT(zoomOut()));
82
83     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
84             m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
85     connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
86             m_mapEngine, SLOT(setViewLocation(QPointF)));
87
88     connect(this, SIGNAL(userLocationReady(User*)),
89             m_mapEngine, SLOT(receiveOwnLocation(User*)));
90     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
91             m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
92
93     connect(m_mapEngine, SIGNAL(mapScrolled()),
94             this, SLOT(locationChanged()));
95
96     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
97             this, SLOT(drawOwnLocationCrosshair(int, int)));
98
99     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
100              this, SLOT(setViewPortSize(int, int)));
101
102     connect(m_mapEngine, SIGNAL(requestToGetViewPortContents()),
103             mapView, SLOT(viewportContent()));
104     connect(mapView, SIGNAL(viewContentChanged(QRect)),
105             m_mapEngine, SLOT(receiveViewSceneRect(QRect)));
106     connect(this, SIGNAL(requestOwnLocation()),
107             m_mapEngine, SLOT(ownLocation()));
108     connect(m_mapEngine, SIGNAL(ownLocation(QPointF)),
109             this, SIGNAL(ownLocation(QPointF)));
110
111     QHBoxLayout *mapViewLayout = new QHBoxLayout;
112
113     m_osmLicense = new QLabel(this);
114     m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
115     m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
116     m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
117     m_osmLicense->setFont(QFont("Nokia Sans", 9));
118     m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
119                          m_osmLicense->fontMetrics().height());
120
121     m_friendsListPanel->stackUnder(friendsListPanelSidebar);
122     userPanelSidebar->stackUnder(m_friendsListPanel);
123     m_userPanel->stackUnder(userPanelSidebar);
124     m_zoomButtonPanel->stackUnder(m_userPanel);
125     m_osmLicense->stackUnder(m_zoomButtonPanel);
126     mapView->stackUnder(m_osmLicense);
127
128     mapViewLayout->addWidget(mapView);
129     setLayout(mapViewLayout);
130
131     mapViewLayout->setMargin(0);
132
133     m_mapEngine->init();
134
135     setObjectName("Map view");
136 }
137
138 void MapViewScreen::drawOsmLicense(int width, int height)
139 {
140     qDebug() << __PRETTY_FUNCTION__ << width << "x" << height;
141     m_osmLicense->move(width - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
142                         height - m_osmLicense->fontMetrics().height());
143 }
144
145 void MapViewScreen::locationChanged()
146 {
147     qDebug() << __PRETTY_FUNCTION__;
148
149     if (m_autoCenteringEnabled)
150         emit mapLocationChanged();
151 }
152
153 void MapViewScreen::positionReceived(QPointF position, qreal accuracy)
154 {
155     qDebug() << __PRETTY_FUNCTION__;
156
157     if (m_autoCenteringEnabled)
158         m_mapEngine->setViewLocation(position);
159 }
160
161 void MapViewScreen::enableAutoCentering(bool enabled)
162 {
163     qDebug() << __PRETTY_FUNCTION__;
164
165     m_autoCenteringEnabled = enabled;
166     m_mapEngine->setAutoCentering(enabled);
167 }
168
169 void MapViewScreen::drawOwnLocationCrosshair(int width, int height)
170 {
171     qWarning() << __PRETTY_FUNCTION__;
172     qWarning() << "widht: " << width << "height: " << height;
173
174     if (m_drawOwnLocationCrosshair) {
175         ownLocationCrosshair->move(width/2 - ownLocationCrosshair->pixmap()->width()/2,
176                             height/2 - ownLocationCrosshair->pixmap()->height()/2);
177     }
178
179     qWarning() << "ownLocation test: " << m_mapEngine->ownLocation().x()
180             << m_mapEngine->ownLocation().y();
181
182 }
183
184 void MapViewScreen::setOwnLocationCrosshairVisibility(bool visibility)
185 {   
186     if (visibility == false) {
187         ownLocationCrosshair->show();
188         m_drawOwnLocationCrosshair = true;
189         drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
190     }
191
192     else {
193         ownLocationCrosshair->hide();
194         m_drawOwnLocationCrosshair = false;
195     }
196 }
197
198 void MapViewScreen::setViewPortSize(int width, int height)
199 {
200     m_viewPortWidth = width;
201     m_viewPortHeight = height;
202 }