1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# Copyright (C) 2012 David Planella <david.planella@ubuntu.com>
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU General Public License version 3, as published
6
# by the Free Software Foundation.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
# PURPOSE. See the GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License along
14
# with this program. If not, see <http://www.gnu.org/licenses/>.
17
from gi.repository import Gtk, GtkChamplain, Clutter, Champlain
18
from qreator_lib.helpers import get_data_file
19
from gi.repository import GtkClutter
22
class QRCodeLocationGtk(object):
23
def __init__(self, qr_code_update_func):
24
self.qr_code_update_func = qr_code_update_func
25
self.builder = Gtk.Builder()
27
self.builder.add_from_file(
28
get_data_file('ui', '%s.ui' % ('QrCodeLocation',)))
29
self.grid = self.builder.get_object('qr_code_location')
32
map_widget = GtkChamplain.Embed()
33
map_widget.set_hexpand(True)
34
map_widget.set_vexpand(True)
38
map_grid.set_size_request(-1, 250)
39
map_grid.attach(map_widget, 0, 0, 1, 1)
41
self.map_view = map_widget.get_view()
42
self.map_view.set_reactive(True)
43
map_widget.connect('button-release-event',
44
self.on_map_widget_button_press_event,
47
# Get the current location, center the map on it, and initialize
49
latitude, longitude = get_current_location()
50
self.builder.get_object('lat_entry').set_text(str(latitude))
51
self.builder.get_object('lon_entry').set_text(str(longitude))
52
self.map_view.center_on(latitude, longitude)
53
if latitude == 0 and longitude == 0:
54
# In case something went wrong in getting the current location
55
self.map_view.set_zoom_level(1)
57
self.map_view.set_zoom_level(15)
58
self.map_view.set_kinetic_mode(True)
60
scale = Champlain.Scale()
61
scale.connect_view(self.map_view)
62
self.map_view.bin_layout_add(scale, Clutter.BinAlignment.START,
63
Clutter.BinAlignment.END)
67
def on_activated(self):
70
def on_map_widget_button_press_event(self, actor, event, view):
71
x, y = event.get_coords()
72
lat, lon = view.x_to_longitude(x), view.y_to_latitude(y)
74
self.builder.get_object('lat_entry').set_text(str(lat))
75
self.builder.get_object('lon_entry').set_text(str(lon))
77
self.qr_code_update_func('geo:{0},{1}'.format(str(lon), str(lat)))
82
def get_current_location():
83
'''Gets the current location from geolocation via IP (only method
84
currently supported)'''
86
#POS_PROVIDER = 'Ubuntu GeoIP'
87
#location = Geoclue.DiscoverLocation()
89
#location.set_position_provider(POS_PROVIDER)
90
#position = location.get_location_info()
93
bus = dbus.SessionBus()
94
geoclue = bus.get_object(
95
'org.freedesktop.Geoclue.Providers.UbuntuGeoIP',
96
'/org/freedesktop/Geoclue/Providers/UbuntuGeoIP')
97
position_info = geoclue.GetPosition(
98
dbus_interface='org.freedesktop.Geoclue.Position')
101
position['timestamp'] = position_info[1]
102
position['latitude'] = position_info[2]
103
position['longitude'] = position_info[3]
104
position['altitude'] = position_info[4]
106
return position['latitude'], position['longitude']