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