Zoom map using HW increase and decrease keys
[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 "../map/mapengine.h"
25 #include "friendlistpanel.h"
26 #include "userpanel.h"
27 #include "panelcommon.h"
28
29 #include "panelsidebar.h"
30
31 MapViewScreen::MapViewScreen(QWidget *parent)
32    : QWidget(parent),
33      m_autoCenteringEnabled(false)
34 {
35     MapView *mapView = new MapView(this);
36     mapEngine = new MapEngine(this);
37     mapView->setScene(mapEngine->scene());
38
39     FriendListPanel *friendsListPanel = new FriendListPanel(this);
40     UserInfoPanel *userPanel = new UserInfoPanel(this);
41     PanelSideBar *userPanelSidebar = new PanelSideBar(this, LEFT);
42     PanelSideBar *friendsListPanelSidebar = new PanelSideBar(this, RIGHT);
43
44     connect(mapView, SIGNAL(viewScrolled(QPoint)),
45             mapEngine, SLOT(setLocation(QPoint)));
46     connect(mapEngine, SIGNAL(locationChanged(QPoint)),
47             mapView, SLOT(centerToSceneCoordinates(QPoint)));
48     connect(mapEngine, SIGNAL(zoomLevelChanged(int)),
49             mapView, SLOT(setZoomLevel(int)));
50     connect(mapView, SIGNAL(viewResized(QSize)),
51             mapEngine, SLOT(viewResized(QSize)));
52     connect(mapView, SIGNAL(viewContentChanged(QPoint)),
53             mapEngine, SLOT(alignImmovableItems(QPoint)));
54     connect(mapView, SIGNAL(viewZoomFinished()),
55             mapEngine, SLOT(viewZoomFinished()));
56
57     connect(this, SIGNAL(zoomInKeyPressed()),
58             mapEngine, SLOT(zoomIn()));
59     connect(this, SIGNAL(zoomOutKeyPressed()),
60             mapEngine, SLOT(zoomOut()));
61
62     connect(mapView, SIGNAL(viewResizedNewSize(int,int)),
63             this, SLOT(drawOsmLicense(int, int)));
64
65     connect(mapView, SIGNAL(viewResizedNewSize(int,int)),
66             friendsListPanel, SLOT(reDrawFriendsPanel(int,int)));
67     connect(mapView, SIGNAL(viewResizedNewSize(int,int)),
68             userPanel, SLOT(reDrawUserPanel(int,int)));
69     connect(mapView, SIGNAL(viewResizedNewSize(int,int)),
70             friendsListPanelSidebar, SLOT(reDrawSidebar(int,int)));
71
72     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
73             friendsListPanel, SLOT(friendInfoReceived(QList<User*>&)));
74
75     connect(this, SIGNAL(userLocationReady(User*)),
76             mapEngine, SLOT(receiveOwnLocation(User*)));
77     connect(this, SIGNAL(friendsLocationsReady(QList<User*>&)),
78             mapEngine, SIGNAL(friendsLocationsReady(QList<User*>&)));
79
80     QHBoxLayout *mapViewLayout = new QHBoxLayout;
81
82     osmLicense = new QLabel(this);
83     osmLicense->setAttribute(Qt::WA_TranslucentBackground, true);
84     osmLicense->setAttribute(Qt::WA_TransparentForMouseEvents, true);
85     osmLicense->setText("<font color='black'>"+OSM_LICENSE+"</font>");
86     osmLicense->setFont(QFont("Nokia Sans", 9));
87     osmLicense->resize(osmLicense->fontMetrics().width(OSM_LICENSE),
88                        osmLicense->fontMetrics().height());
89
90     friendsListPanel->stackUnder(friendsListPanelSidebar);
91     userPanelSidebar->stackUnder(friendsListPanel);
92     userPanel->stackUnder(userPanelSidebar);
93     osmLicense->stackUnder(userPanel);
94     mapView->stackUnder(osmLicense);
95
96     mapViewLayout->addWidget(mapView);
97     setLayout(mapViewLayout);
98
99     mapViewLayout->setMargin(0);
100
101     mapEngine->init();
102
103     setObjectName("Map view");
104 }
105
106 void MapViewScreen::drawOsmLicense(int width, int height)
107 {
108     qDebug() << __PRETTY_FUNCTION__ << width << "x" << height;
109     osmLicense->move(width - osmLicense->fontMetrics().width(OSM_LICENSE) - PANEL_PEEK_AMOUNT,
110                      height - osmLicense->fontMetrics().height());
111 }
112
113 void MapViewScreen::positionReceived(QPointF position)
114 {
115     qDebug() << __PRETTY_FUNCTION__;
116
117     if (m_autoCenteringEnabled)
118         mapEngine->setViewLocation(position);
119 }
120
121 void MapViewScreen::enableAutoCentering(bool enabled)
122 {
123     qDebug() << __PRETTY_FUNCTION__;
124
125     m_autoCenteringEnabled = enabled;
126 }