#!/usr/bin/env python
import gtk
-import gobject
+import gobject , gconf
+try :
+ import hildon
+except :
+ hildon = False
import urllib2
import math
import os
-class mapWidget(gtk.Image):
+class WifihoodConfig :
+
+ def __init__ ( self ) :
+ self.lat , self.lon = 40.40491 , -3.6774
+ self.zoom = 11
+ self.mapsdir = "/home/user/MyDocs/.maps"
+
+ self.client = gconf.client_get_default()
+
+ def read ( self , parentdir="/apps/wifihood" ) :
+ lat = self.client.get_float( "%s/lattitude" % parentdir )
+ if lat : self.lat = lat
+ lon = self.client.get_float( "%s/longitude" % parentdir )
+ if lon : self.lon = lon
+ zoom = self.client.get_int( "%s/zoom" % parentdir )
+ if zoom : self.zoom = zoom
+ mapsdir = self.client.get_string( "%s/mapsdir" % parentdir )
+ if mapsdir : self.mapsdir = mapsdir
+
+ def save ( self , parentdir="/apps/wifihood" ) :
+ self.client.set_float( "%s/lattitude" % parentdir , self.lat )
+ self.client.set_float( "%s/longitude" % parentdir , self.lon )
+ self.client.set_int( "%s/zoom" % parentdir , self.zoom )
+ self.client.set_string( "%s/mapsdir" % parentdir , self.mapsdir )
+
+class mapWidget ( gtk.Image , WifihoodConfig ) :
def __init__(self):
+ WifihoodConfig.__init__( self )
gtk.Image.__init__(self)
# Maximum width should be 800, but actually gets reduced
self.win_x , self.win_y = 800 , 480
+ self.tile_size = 256
p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x, self.win_y)
self.set_from_pixbuf(p)
- zoom = 11
- #lat = 40.4400226381
- #lon = -3.6880185362
- lat = 40.40491
- lon = -3.6774
- self.reftile_x , self.reftile_y = self.lon2tilex( lon , zoom ) , self.lat2tiley( lat , zoom )
- # Half the size of a tile
- self.refpix_x , self.refpix_y = 128 , 128
- self.refpix_x , self.refpix_y = 0 , 0
+ self.read()
+ self.reftile_x , self.refpix_x = self.lon2tilex( self.lon , self.zoom )
+ self.reftile_y , self.refpix_y = self.lat2tiley( self.lat , self.zoom )
self.composeMap()
- def lon2tilex ( self , lon , zoom=11 ) :
- return int( ( lon + 180 ) / 360 * 2 ** zoom )
+ def lon2tilex ( self , lon , zoom ) :
+ number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
+ return int( number[1] ) , int( self.tile_size * number[0] )
- def lat2tiley ( self , lat , zoom=11 ) :
+ def lat2tiley ( self , lat , zoom ) :
lat = lat * math.pi / 180
- return int( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
+ number = math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
+ return int( number[1] ) , int( self.tile_size * number[0] )
+
+ def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
+ tilex = float(tilex)
+ pixx = float(pixx)
+ return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
+
+ def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
+ tiley = float(tiley)
+ pixy = float(pixy)
+ tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
+ return math.degrees( math.atan( math.sinh( tiley ) ) )
def composeMap( self ) :
center_x , center_y = self.win_x / 2 , self.win_y / 2
# Maybe they should be decided based on self.win_x, self.win_y
for i in range(-3,4) :
for j in range(-3,4) :
- file = self.tilename( i , j )
+ file = self.tilename( i , j , self.zoom )
if file is None :
- pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 256, 256 )
+ pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
pixbuf.fill( 0x00000000 )
else :
try :
except gobject.GError , ex :
print "Corrupted file %s" % ( file )
os.unlink( file )
- #file = self.tilename( self.reftile_x + i , self.reftile_y + j )
- file = self.tilename( i , j )
+ #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.zoom )
+ file = self.tilename( i , j , self.zoom )
try :
pixbuf = gtk.gdk.pixbuf_new_from_file( file )
except :
print "Total failure for tile for %s,%s" % ( self.reftile_x + i , self.reftile_y + j )
- pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 256, 256 )
+ pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
- dest_x = 256 * i + center_x
- dest_y = 256 * j + center_y
+ dest_x = self.tile_size * i + center_x
+ dest_y = self.tile_size * j + center_y
init_x = 0
- size_x = 256
+ size_x = self.tile_size
if dest_x < 0 :
init_x = abs(dest_x)
- size_x = 256 + dest_x
+ size_x = self.tile_size + dest_x
dest_x = 0
- if dest_x + 256 > self.win_x :
+ if dest_x + self.tile_size > self.win_x :
size_x = self.win_x - dest_x
init_y = 0
- size_y = 256
+ size_y = self.tile_size
if dest_y < 0 :
init_y = abs(dest_y)
- size_y = 256 + dest_y
+ size_y = self.tile_size + dest_y
dest_y = 0
- if dest_y + 256 > self.win_y :
+ if dest_y + self.tile_size > self.win_y :
size_y = self.win_y - dest_y
- if ( size_x > 0 and size_y > 0 ) and ( init_x < 256 and init_y < 256 ) :
+ if ( size_x > 0 and size_y > 0 ) and ( init_x < self.tile_size and init_y < self.tile_size ) :
pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
del(pixbuf)
- def tilename ( self , x , y , zoom=11 ) :
+ def tilename ( self , x , y , zoom ) :
file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
try :
os.stat(file)
return None
return file
- def tile2file( self , tilex , tiley , zoom=11 ) :
- basedir = "/home/user/MyDocs/.maps/OpenStreetMap I"
- rootdir = "%s/%s" % ( basedir , zoom )
+ def tile2file( self , tilex , tiley , zoom ) :
+ rootdir = "%s/OpenStreetMap I/%s" % ( self.mapsdir , zoom )
if not os.path.isdir( rootdir ) :
os.mkdir(rootdir)
rootsubdir = "%s/%s" % ( rootdir , tilex )
def Shift( self , dx , dy ) :
self.hide()
- tile_x , tile_y = ( self.refpix_x - dx ) / 256 , ( self.refpix_y - dy ) / 256
+ tile_x , tile_y = ( self.refpix_x - dx ) / self.tile_size , ( self.refpix_y - dy ) / self.tile_size
self.reftile_x += tile_x
self.reftile_y += tile_y
- self.refpix_x -= dx + 256 * tile_x
- self.refpix_y -= dy + 256 * tile_y
+ self.refpix_x -= dx + self.tile_size * tile_x
+ self.refpix_y -= dy + self.tile_size * tile_y
+
+ self.composeMap()
+ self.show()
+
+ def ZoomChange ( self , selector ) :
+ model = selector.get_model(0)
+ active = selector.get_active(0)
+ value = model.get( model.get_iter(active) , 0 )
+ self.SetZoom( int(value[0]) )
+ def SetZoom( self , zoom ) :
+ self.hide()
+ lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.zoom )
+ lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.zoom )
+ self.reftile_x , self.refpix_x = self.lon2tilex( lon , zoom )
+ self.reftile_y , self.refpix_y = self.lat2tiley( lat , zoom )
+ self.zoom = zoom
self.composeMap()
self.show()
self.composeMap()
self.show()
-class MapWindow:
+if hildon :
+
+ class ZoomDialog ( hildon.TouchSelector ) :
+
+ def __init__ ( self , widget ) :
+ hildon.TouchSelector.__init__( self )
+
+ zooms = gtk.ListStore(str)
+
+ active = index = 0
+ for zoom in range(8,19) :
+ iter = zooms.append()
+ zooms.set( iter , 0 , "%2d" % zoom )
+ if zoom == widget.zoom :
+ active = index
+ index += 1
+
+ column = self.append_text_column( zooms , True )
+ #renderer = gtk.CellRendererText()
+ #column = self.append_column( zooms , renderer )
+ #column.set_property('text-column', 0)
+
+ # NOTE : with text=True, we must use 1 instead of 0
+ self.set_active( 0 , active )
+
+else :
+
+ class ZoomDialog ( gtk.Dialog ) :
+
+ def __init__ ( self , widget ) :
+ gtk.Dialog.__init__( self , "Select zoom level",
+ None,
+ gtk.DIALOG_MODAL,
+ ( gtk.STOCK_OK, gtk.RESPONSE_ACCEPT,
+ gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT
+ )
+ )
+
+ zooms = gtk.ListStore(int)
+ combo = gtk.ComboBox( zooms )
- def delete_event(self, widget, event, data=None):
- # If you return FALSE in the "delete_event" signal handler,
- # GTK will emit the "destroy" signal. Returning TRUE means
- # you don't want the window to be destroyed.
- # This is useful for popping up 'are you sure you want to quit?'
- # type dialogs.
- print "delete event occurred"
+ for zoom in range(8,19) :
+ iter = zooms.append()
+ zooms.set( iter , 0 , zoom )
+ if zoom == widget.zoom :
+ combo.set_active_iter( iter )
- # Change FALSE to TRUE and the main window will not be destroyed
- # with a "delete_event".
- return False
+ cell = gtk.CellRendererText()
+ combo.pack_start(cell, True)
+ combo.add_attribute(cell, 'text', 0)
+
+ self.vbox.pack_start(combo , True, True, 0)
+
+ self.connect_object( "response", self.response , combo , widget )
+
+ def response ( self , combo , response , widget ) :
+ if response == gtk.RESPONSE_ACCEPT :
+ item = combo.get_active_iter()
+ model = combo.get_model()
+ widget.SetZoom( model.get(item,0)[0] )
+ self.destroy()
+
+
+class MapWindow:
def destroy(self, widget, data=None):
- print "destroy signal occurred"
gtk.main_quit()
def press_event ( self, widget, event, *args ) :
def __init__(self):
- self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
-
- self.window.connect("delete_event", self.delete_event)
+ if hildon :
+ self.window = hildon.Window()
+ program = hildon.Program.get_instance()
+ program.add_window(self.window)
+ gtk.set_application_name( "Wifi View" )
+ else :
+ self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
+
self.window.connect("destroy", self.destroy)
self.window.set_border_width(10)
-
+
self.window.connect("key-press-event", self.on_key_press)
-
+
+ vbox = gtk.VBox(False, 0)
+ self.window.add( vbox )
+
# To get explicit GDK_BUTTON_PRESS instead of paired GDK_LEAVE_NOTIFY & GDK_ENTER_NOTIFY
# self.window.add_events(gtk.gdk.BUTTON_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.POINTER_MOTION_MASK)
self.window.set_events( gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK )
self.window.connect('button_release_event', self.release_event)
#
self.map = mapWidget()
- self.window.add( self.map )
-
+ vbox.pack_end( self.map , True , True , 5)
+
+ self.create_menu( vbox )
+
# and the window
self.window.show_all()
self.size_x , self.size_y = 800 , 480
self.click_x , self.click_y = None , None
+ def zoomdialog ( self , widget ) :
+ dialog = ZoomDialog( widget )
+ dialog.show_all()
+
+ def create_menu ( self , vbox ) :
+ if hildon :
+
+ self.menubar = menubar = hildon.AppMenu()
+
+ #zoomlevel = hildon.Button(gtk.HILDON_SIZE_AUTO,
+ # hildon.BUTTON_ARRANGEMENT_VERTICAL,
+ # "Zoom level", None)
+ #zoomlevel.connect_object( "clicked", self.zoomstack, self.map )
+ selector = ZoomDialog( self.map )
+ zoomlevel = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,
+ hildon.BUTTON_ARRANGEMENT_VERTICAL)
+ zoomlevel.set_title( "Zoom" )
+ zoomlevel.set_selector( selector )
+ zoomlevel.connect_object( "value-changed", self.map.ZoomChange , selector )
+ menubar.append( zoomlevel )
+
+ menubar.show_all()
+ self.window.set_app_menu( menubar )
+
+ else :
+
+ menubar = gtk.MenuBar()
+
+ zoomlevel = gtk.MenuItem( label="Zoom level" )
+ zoomlevel.connect_object( "activate", self.zoomdialog, self.map )
+ menubar.append( zoomlevel )
+
+ vbox.pack_start(menubar,True,True,5)
+
def main(self):
gtk.main()