Implement drawing of APs from database
[wifihood] / wifiscanner / wifiview
index 90bca29..185ca13 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 
 import gtk
-import gobject , gconf
+import gobject
 try :
     import hildon
 except :
@@ -12,36 +12,13 @@ import math
 
 import os
 
-class WifihoodConfig :
+import wifimap.config , wifimap.db
 
-    def __init__ ( self ) :
-        self.lat , self.lon = 40.40491 , -3.6774
-        self.zoom = 11
-        self.mapsdir = "/home/user/MyDocs/.maps"
+class mapWidget ( gtk.Image ) :
 
-        self.client = gconf.client_get_default()
+  def __init__ ( self , config ) :
 
-    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 )
+    self.conf = config
     gtk.Image.__init__(self)
 
     # Maximum width should be 800, but actually gets reduced
@@ -51,9 +28,8 @@ class mapWidget ( gtk.Image , WifihoodConfig ) :
     p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x, self.win_y)
     self.set_from_pixbuf(p)
 
-    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.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
+    self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
 
     self.composeMap()
 
@@ -77,6 +53,16 @@ class mapWidget ( gtk.Image , WifihoodConfig ) :
     tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
     return math.degrees( math.atan( math.sinh( tiley ) ) )
 
+  def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
+
+    x_pos = self.lon2tilex( lon , self.conf.zoom )
+    y_pos = self.lat2tiley( lat , self.conf.zoom )
+
+    dest_x = self.tile_size * ( x_pos[0] - self.reftile_x ) + center_x + x_pos[1]
+    dest_y = self.tile_size * ( y_pos[0] - self.reftile_y ) + center_y + y_pos[1]
+
+    return dest_x , dest_y
+
   def composeMap( self ) :
     center_x , center_y = self.win_x / 2 , self.win_y / 2
 
@@ -88,7 +74,7 @@ class mapWidget ( gtk.Image , WifihoodConfig ) :
     # 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 , self.zoom )
+        file = self.tilename( i , j , self.conf.zoom )
         if file is None :
           pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
           pixbuf.fill( 0x00000000 )
@@ -98,8 +84,8 @@ class mapWidget ( gtk.Image , WifihoodConfig ) :
           except gobject.GError , ex :
             print "Corrupted file %s" % ( file )
             os.unlink( file )
-            #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.zoom )
-            file = self.tilename( i , j , self.zoom )
+            #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.conf.zoom )
+            file = self.tilename( i , j , self.conf.zoom )
             try :
               pixbuf = gtk.gdk.pixbuf_new_from_file( file )
             except :
@@ -131,6 +117,34 @@ class mapWidget ( gtk.Image , WifihoodConfig ) :
             pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
         del(pixbuf)
 
+    pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
+    red = pixmap.new_gc()
+    red.foreground = pixmap.get_colormap().alloc_color("red")
+    green = pixmap.new_gc()
+    green.foreground = pixmap.get_colormap().alloc_color("green")
+    blue = pixmap.new_gc()
+    blue.foreground = pixmap.get_colormap().alloc_color("blue")
+
+    filename = "data/wiscan_gui.info.old"
+    fd = open( filename )
+    for line in fd.readlines() :
+        values = line.split()
+        if values[1] == "FIX" :
+            dest_x , dest_y = self.gps2pix( ( float(values[5]) , float(values[6]) ) , ( center_x , center_y ) )
+            pixmap.draw_rectangle(blue, True , dest_x , dest_y , 3 , 3 )
+    fd.close()
+
+    db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
+    db.open()
+    for ap in db.execute( "SELECT * FROM ap" ) :
+        if ap[3] > 1 :
+            dest_x , dest_y = self.gps2pix( ( ap[4]/ap[3] , ap[5]/ap[3] ) , ( center_x , center_y ) )
+            pixmap.draw_rectangle(red, True , dest_x , dest_y , 3 , 3 )
+    db.close()
+
+    self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
+
+
   def tilename ( self , x , y , zoom ) :
     file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
     try :
@@ -153,7 +167,7 @@ class mapWidget ( gtk.Image , WifihoodConfig ) :
     return file
 
   def tile2file( self , tilex , tiley , zoom ) :
-    rootdir = "%s/OpenStreetMap I/%s" % ( self.mapsdir , zoom )
+    rootdir = "%s/%s/%s" % ( self.conf.mapsdir , self.conf.mapclass , zoom )
     if not os.path.isdir( rootdir ) :
       os.mkdir(rootdir)
     rootsubdir = "%s/%s" % ( rootdir , tilex )
@@ -178,15 +192,15 @@ class mapWidget ( gtk.Image , WifihoodConfig ) :
     model = selector.get_model(0)
     active = selector.get_active(0)
     value = model.get( model.get_iter(active) , 0 )
-    self.SetZoom( value[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 )
+    lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.conf.zoom )
+    lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.conf.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.conf.zoom = zoom
     self.composeMap()
     self.show()
 
@@ -219,21 +233,22 @@ if hildon :
     class ZoomDialog ( hildon.TouchSelector ) :
 
         def __init__ ( self , widget ) :
-            hildon.TouchSelector.__init__( self ) # , text=True )
+            hildon.TouchSelector.__init__( self )
 
-            zooms = gtk.ListStore(int,str)
+            zooms = gtk.ListStore(str)
 
             active = index = 0
             for zoom in range(8,19) :
                 iter = zooms.append()
-                zooms.set( iter , 0 , zoom , 1 , "Level %2d" % zoom )
-                if zoom == widget.zoom :
+                zooms.set( iter , 0 , "%2d" % zoom )
+                if zoom == widget.conf.zoom :
                     active = index
                 index += 1
 
-            renderer = gtk.CellRendererText()
-            column = self.append_column( zooms , renderer )
-            column.set_property('text-column', 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 )
@@ -257,7 +272,7 @@ else :
             for zoom in range(8,19) :
                 iter = zooms.append()
                 zooms.set( iter , 0 , zoom )
-                if zoom == widget.zoom :
+                if zoom == widget.conf.zoom :
                     combo.set_active_iter( iter )
 
             cell = gtk.CellRendererText()
@@ -357,7 +372,8 @@ class MapWindow:
         self.window.connect('button_press_event', self.press_event)
         self.window.connect('button_release_event', self.release_event)
         #
-        self.map = mapWidget()
+        self.config = wifimap.config.Configuration()
+        self.map = mapWidget( self.config )
         vbox.pack_end( self.map , True , True , 5)
 
         self.create_menu( vbox )