Merge branch 'master' into userinfo
[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     connect(mapView, SIGNAL(viewScrolled(QPoint)),
44             m_mapEngine, SLOT(setLocation(QPoint)));
45     connect(m_mapEngine, SIGNAL(locationChanged(QPoint)),
46             mapView, SLOT(centerToSceneCoordinates(QPoint)));
47     connect(m_mapEngine, SIGNAL(zoomLevelChanged(int)),
48             mapView, SLOT(setZoomLevel(int)));
49     connect(mapView, SIGNAL(viewResized(QSize)),
50             m_mapEngine, SLOT(viewResized(QSize)));
51     connect(mapView, SIGNAL(viewContentChanged(QPoint)),
52             m_mapEngine, SLOT(alignImmovableItems(QPoint)));
53     connect(mapView, SIGNAL(viewZoomFinished()),
54             m_mapEngine, SLOT(viewZoomFinished()));
55
56     connect(this, SIGNAL(zoomInKeyPressed()),
57             m_mapEngine, SLOT(zoomIn()));
58     connect(this, SIGNAL(zoomOutKeyPressed()),
59             m_mapEngine, SLOT(zoomOut()));
60
61     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
62             this, SLOT(drawOsmLicense(int, int)));
63     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
64             m_friendsListPanel, SLOT(reDrawFriendsPanel(int, int)));
65     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
66             m_userPanel, SLOT(reDrawUserPanel(int, int)));
67     connect(mapView, SIGNAL(viewResizedNewSize(int, int)),
68             friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
69
70     connect(m_zoomButtonPanel->m_zoomInBtn, SIGNAL(clicked()),
71             m_mapEngine, SLOT(zoomIn()));
72     connect(m_zoomButtonPanel->m_zoomOutBtn, SIGNAL(clicked()),
73             m_mapEngine, SLOT(zoomOut()));
74
75     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
76             m_friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
77     connect(m_friendsListPanel, SIGNAL(findFriend(QPointF)),
78             m_mapEngine, SLOT(setViewLocation(QPointF)));
79
80     connect(this, SIGNAL(userLocationReady(User*)),
81             m_mapEngine, SLOT(receiveOwnLocation(User*)));
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(this, SIGNAL(userLocationReady(User*)),
91             m_userPanel, SLOT(receiveUserData(User*)));
92
93     QHBoxLayout *mapViewLayout = new QHBoxLayout;
94
95     m_osmLicense = new QLabel(this);
96     m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
97     m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
98     m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
99     m_osmLicense->setFont(QFont("Nokia Sans", 9));
100     m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
101                          m_osmLicense->fontMetrics().height());
102
103     m_friendsListPanel->stackUnder(friendsListPanelSidebar);
104     userPanelSidebar->stackUnder(m_friendsListPanel);
105     m_userPanel->stackUnder(userPanelSidebar);
106     m_zoomButtonPanel->stackUnder(m_userPanel);
107     m_osmLicense->stackUnder(m_zoomButtonPanel);
108     mapView->stackUnder(m_osmLicense);
109
110     mapViewLayout->addWidget(mapView);
111     setLayout(mapViewLayout);
112
113     mapViewLayout->setMargin(0);
114
115     m_mapEngine->init();
116
117     setObjectName("Map view");
118 }
119
120 void MapViewScreen::drawOsmLicense(int width, int height)
121 {
122     qDebug() << __PRETTY_FUNCTION__ << width << "x" << height;
123     m_osmLicense->move(width - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
124                         height - m_osmLicense->fontMetrics().height());
125 }
126
127 void MapViewScreen::locationChanged()
128 {
129     qDebug() << __PRETTY_FUNCTION__;
130
131     if (m_autoCenteringEnabled)
132         emit mapLocationChanged();
133 }
134
135 void MapViewScreen::positionReceived(QPointF position, qreal accuracy)
136 {
137     qDebug() << __PRETTY_FUNCTION__;
138
139     if (m_autoCenteringEnabled)
140         m_mapEngine->setViewLocation(position);
141 }
142
143 void MapViewScreen::enableAutoCentering(bool enabled)
144 {
145     qDebug() << __PRETTY_FUNCTION__;
146
147     m_autoCenteringEnabled = enabled;
148     m_mapEngine->setAutoCentering(enabled);
149 }