Modify to increase similarity with current development head
authorJavier Palacios <javiplx@gmail.com>
Fri, 2 Nov 2012 10:43:34 +0000 (11:43 +0100)
committerJavier Palacios <javiplx@gmail.com>
Fri, 2 Nov 2012 10:43:34 +0000 (11:43 +0100)
wifimap/gps.py
wifimap/view.py

index aaaf4fe..5b9684b 100644 (file)
@@ -1,5 +1,8 @@
 
-import location
+try :
+    import location
+except :
+    import hildongtk.location as location
 
 
 import gobject
index ddaac4b..bb5c3ae 100755 (executable)
@@ -22,6 +22,27 @@ class AbstractmapWidget :
     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 )
 
+  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 SetZoom( self , zoom ) :
+        self.hide()
+        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.conf.zoom = zoom
+        self.composeMap()
+        self.show()
+
   def lon2tilex ( self , lon , zoom ) :
     number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
     return int( number[1] ) , int( self.tile_size * number[0] )
@@ -73,17 +94,6 @@ class AbstractmapWidget :
 
 class interactiveMapWidget :
 
-  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 Shift( self , dx , dy ) :
     self.hide()
 
@@ -103,16 +113,6 @@ class interactiveMapWidget :
     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.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.conf.zoom = zoom
-    self.composeMap()
-    self.show()
-
   def Up( self ) :
     self.hide()
     self.reftile_y -= 1
@@ -137,32 +137,20 @@ class interactiveMapWidget :
     self.composeMap()
     self.show()
 
-class simpleMapWidget ( AbstractmapWidget , gtk.DrawingArea ) :
+class simpleMapWidget ( AbstractmapWidget , gtk.Image ) :
 
     def __init__ ( self , config , map_size=(800,480) ) :
         AbstractmapWidget.__init__( self , config , map_size )
 
-        gtk.DrawingArea.__init__(self)
-        self.set_size_request(map_size[0] , map_size[1])
+        gtk.Image.__init__(self)
 
-        self.mapimage = gtk.Image()
         p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x, self.win_y)
-        self.mapimage.set_from_pixbuf(p)
-
-        self.connect("expose-event", self.expose)
-
-    def expose(self, area, event):
+        self.set_from_pixbuf(p)
+    
         self.composeMap()
-        # FIXME : The two below could slow the displaying
-        self.draw_paths()
-        self.plot_APs()
 
     def composeMap( self ) :
-        center_x , center_y = self.win_x / 2 , self.win_y / 2
-
-        # To get the central pixel in the window center, we must shift to the tile origin
-        center_x -= self.refpix_x
-        center_y -= self.refpix_y
+        center_x , center_y = self.center()
 
         # Ranges should be long enough as to fill the screen
         # Maybe they should be decided based on self.win_x, self.win_y
@@ -208,13 +196,13 @@ class simpleMapWidget ( AbstractmapWidget , gtk.DrawingArea ) :
                    size_y = self.win_y - dest_y
 
                 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.mapimage.get_pixbuf(), dest_x , dest_y )
+                    pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
                 del(pixbuf)
 
-        gc = self.style.fg_gc[gtk.STATE_NORMAL]
-        self.window.draw_pixbuf( gc, self.mapimage.get_pixbuf(), 0, 0, 0, 0, self.win_x, self.win_y )
+        self.draw_paths()
+        self.plot_APs()
 
-    def draw_paths( self ) :
+    def center( self ) :
 
         center_x , center_y = self.win_x / 2 , self.win_y / 2
 
@@ -222,41 +210,47 @@ class simpleMapWidget ( AbstractmapWidget , gtk.DrawingArea ) :
         center_x -= self.refpix_x
         center_y -= self.refpix_y
 
-        red = self.window.new_gc()
-        red.foreground = self.get_colormap().alloc_color("red")
+        return center_x , center_y
+
+    def plot( self , pixmap , coords , colorname , radius=3 ) :
+
+        center_x , center_y = self.center()
+
+        gc = pixmap.new_gc()
+        gc.foreground = pixmap.get_colormap().alloc_color( colorname )
+
+        dest_x , dest_y = self.gps2pix( coords , ( center_x , center_y ) )
+        pixmap.draw_rectangle(gc, True , dest_x , dest_y , radius , radius )
+
+    def draw_paths( self ) :
+
+        pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
 
-        filename = "data/wiscan_gui.info.old"
+        filename = "/tmp/wiscan_gui.info"
         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 ) )
-                self.window.draw_rectangle(red, True , dest_x , dest_y , 3 , 3 )
+                self.plot( pixmap , ( float(values[5]) , float(values[6]) ) , "red" )
         fd.close()
 
-    def plot_APs( self ) :
-
-        center_x , center_y = self.win_x / 2 , self.win_y / 2
+        self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
 
-        # To get the central pixel in the window center, we must shift to the tile origin
-        center_x -= self.refpix_x
-        center_y -= self.refpix_y
+    def plot_APs( self ) :
 
-        blue = self.window.new_gc()
-        blue.foreground = self.get_colormap().alloc_color("blue")
+        pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
 
         db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
         db.open()
         # NOTE : Intervals for query are just educated guesses to fit in window
         lat , lon = self.conf.lat , self.conf.lon
-        for ap in db.db.execute( "SELECT * FROM ap where lat/n>%f and lat/n<%f and lon/n>%f and lon/n<%f" % ( lat - 0.003 , lat + 0.003 , lon - 0.007 , lon + 0.007 ) ) :
-            if ap[3] > 1 :
-                dest_x , dest_y = self.gps2pix( ( ap[4]/ap[3] , ap[5]/ap[3] ) , ( center_x , center_y ) )
-                self.window.draw_rectangle(blue, True , dest_x , dest_y , 3 , 3 )
+        for ap in db.db.execute( "SELECT lat,lon FROM ap" ) :
+            self.plot( pixmap , ( ap[0] , ap[1] ) , "blue" )
         db.close()
 
+        self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
+
 class mapWidget ( simpleMapWidget , interactiveMapWidget ) :
 
     pass
 
-