fixed review issues
[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     m_ownLocationCrosshair = 0;
44
45     connect(mapView, SIGNAL(viewScrolled(QPoint)),
46             m_mapEngine, SLOT(setLocation(QPoint)));
47     connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
48             mapView, SLOT(centerToSceneCoordinates(QPoint)));
49     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
50             mapView, SLOT(setZoomLevel(int)));
51     connect(mapView, SIGNAL(viewResized(QSize)),
52             m_mapEngine, SLOT(viewResized(QSize)));
53     connect(mapView, SIGNAL(updateViewContent(QRect)),
54             m_mapEngine, SLOT(receiveViewSceneRect(QRect)));
55     connect(mapView, SIGNAL(viewZoomFinished()),
56             m_mapEngine, SLOT(viewZoomFinished()));
57
58     connect(this, SIGNAL(zoomInKeyPressed()),
59             m_mapEngine, SLOT(zoomIn()));
60     connect(this, SIGNAL(zoomOutKeyPressed()),
61             m_mapEngine, SLOT(zoomOut()));
62
63     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
64             this, SLOT(drawOsmLicense(int, int)));
65     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
66             m_friendsListPanel, SLOT(reDrawFriendsPanel(int, int)));
67     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
68             m_userPanel, SLOT(reDrawUserPanel(int, int)));
69     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
70             friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
71
72     connect(m_zoomButtonPanel->m_zoomInBtn, SIGNAL(clicked()),
73             m_mapEngine, SLOT(zoomIn()));
74     connect(m_zoomButtonPanel->m_zoomOutBtn, SIGNAL(clicked()),
75             m_mapEngine, SLOT(zoomOut()));
76
77     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
78             m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
79     connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
80             m_mapEngine, SLOT(setViewLocation(QPointF)));
81
82     connect(this, SIGNAL(userLocationReady(User*)),
83             m_mapEngine, SLOT(receiveOwnLocation(User*)));
84     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
85             m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
86
87     connect(m_mapEngine, SIGNAL(mapScrolled()),
88             this, SLOT(locationChanged()));
89
90     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
91             this, SLOT(drawOwnLocationCrosshair(int, int)));
92
93     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
94              this, SLOT(setViewPortSize(int, int)));
95
96     connect(m_mapEngine, SIGNAL(requestToGetViewPortContents()),
97             mapView, SLOT(viewportContent()));
98     connect(mapView, SIGNAL(updateViewContent(QRect)),
99             m_mapEngine, SLOT(receiveViewSceneRect(QRect)));
100     connect(this, SIGNAL(requestOwnLocation()),
101             m_mapEngine, SLOT(ownLocation()));
102     connect(m_mapEngine, SIGNAL(ownLocation(QPointF)),
103             this, SIGNAL(ownLocation(QPointF)));
104
105     QHBoxLayout *mapViewLayout = new QHBoxLayout;
106
107     m_osmLicense = new QLabel(this);
108     m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
109     m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
110     m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
111     m_osmLicense->setFont(QFont("Nokia Sans", 9));
112     m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
113                          m_osmLicense->fontMetrics().height());
114
115     m_friendsListPanel->stackUnder(friendsListPanelSidebar);
116     userPanelSidebar->stackUnder(m_friendsListPanel);
117     m_userPanel->stackUnder(userPanelSidebar);
118     m_zoomButtonPanel->stackUnder(m_userPanel);
119     m_osmLicense->stackUnder(m_zoomButtonPanel);
120     mapView->stackUnder(m_osmLicense);
121
122     mapViewLayout->addWidget(mapView);
123     setLayout(mapViewLayout);
124
125     mapViewLayout->setMargin(0);
126
127     m_mapEngine->init();
128
129     setObjectName("Map view");
130 }
131
132 void MapViewScreen::drawOsmLicense(int width, int height)
133 {
134     qDebug() << __PRETTY_FUNCTION__ << width << "x" << height;
135     m_osmLicense->move(width - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
136                         height - m_osmLicense->fontMetrics().height());
137 }
138
139 void MapViewScreen::locationChanged()
140 {
141     qDebug() << __PRETTY_FUNCTION__;
142
143     if (m_autoCenteringEnabled)
144         emit mapLocationChanged();
145 }
146
147 void MapViewScreen::positionReceived(QPointF position, qreal accuracy)
148 {
149     qDebug() << __PRETTY_FUNCTION__;
150
151     if (m_autoCenteringEnabled)
152         m_mapEngine->setViewLocation(position);
153 }
154
155 void MapViewScreen::enableAutoCentering(bool enabled)
156 {
157     qDebug() << __PRETTY_FUNCTION__;
158
159     m_autoCenteringEnabled = enabled;
160     m_mapEngine->setAutoCentering(enabled);
161 }
162
163 void MapViewScreen::drawOwnLocationCrosshair(int width, int height)
164 {
165     qDebug() << __PRETTY_FUNCTION__;
166
167     if (m_drawOwnLocationCrosshair) {
168         m_ownLocationCrosshair->move(width/2 - m_ownLocationCrosshair->pixmap()->width()/2,
169                             height/2 - m_ownLocationCrosshair->pixmap()->height()/2);
170     }
171 }
172
173 void MapViewScreen::setOwnLocationCrosshairVisibility(bool visibility)
174 {   
175     if (visibility == false) {
176
177         if (m_ownLocationCrosshair == 0) {
178             m_ownLocationCrosshair = new QLabel(this);
179             QPixmap crosshairImage(":/res/images/sight.png");
180             m_ownLocationCrosshair->setPixmap(crosshairImage);
181             m_ownLocationCrosshair->setFixedSize(crosshairImage.size());
182         }
183
184         m_ownLocationCrosshair->show();
185         m_drawOwnLocationCrosshair = true;
186         drawOwnLocationCrosshair(m_viewPortWidth, m_viewPortHeight);
187     }
188
189     else {
190         m_ownLocationCrosshair->hide();
191         m_drawOwnLocationCrosshair = false;
192     }
193 }
194
195 void MapViewScreen::setViewPortSize(int width, int height)
196 {
197     m_viewPortWidth = width;
198     m_viewPortHeight = height;
199 }