Made a few cosmetic changes
[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(friendsLocationsReady(QList<User*>&)),
83             m_mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
84
85     connect(m_mapEngine, SIGNAL(mapScrolled()),
86             this, SLOT(locationChanged()));
87
88     QHBoxLayout *mapViewLayout = new QHBoxLayout;
89
90     m_osmLicense = new QLabel(this);
91     m_osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
92     m_osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
93     m_osmLicense->setText("<font color='black'>" + OSM_LICENSE + "</font>");
94     m_osmLicense->setFont(QFont("Nokia Sans", 9));
95     m_osmLicense->resize(m_osmLicense->fontMetrics().width(OSM_LICENSE),
96                          m_osmLicense->fontMetrics().height());
97
98     m_friendsListPanel->stackUnder(friendsListPanelSidebar);
99     userPanelSidebar->stackUnder(m_friendsListPanel);
100     m_userPanel->stackUnder(userPanelSidebar);
101     m_zoomButtonPanel->stackUnder(m_userPanel);
102     m_osmLicense->stackUnder(m_zoomButtonPanel);
103     mapView->stackUnder(m_osmLicense);
104
105     mapViewLayout->addWidget(mapView);
106     setLayout(mapViewLayout);
107
108     mapViewLayout->setMargin(0);
109
110     m_mapEngine->init();
111
112     setObjectName("Map view");
113 }
114
115 void MapViewScreen::drawOsmLicense(int width, int height)
116 {
117     qDebug() << __PRETTY_FUNCTION__ << width << "x" << height;
118     m_osmLicense->move(width - m_osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
119                         height - m_osmLicense->fontMetrics().height());
120 }
121
122 void MapViewScreen::locationChanged()
123 {
124     qDebug() << __PRETTY_FUNCTION__;
125
126     if (m_autoCenteringEnabled)
127         emit mapLocationChanged();
128 }
129
130 void MapViewScreen::positionReceived(QPointF position, qreal accuracy)
131 {
132     qDebug() << __PRETTY_FUNCTION__;
133
134     if (m_autoCenteringEnabled)
135         m_mapEngine->setViewLocation(position);
136 }
137
138 void MapViewScreen::enableAutoCentering(bool enabled)
139 {
140     qDebug() << __PRETTY_FUNCTION__;
141
142     m_autoCenteringEnabled = enabled;
143     m_mapEngine->setAutoCentering(enabled);
144 }