Plot close APs taken from database
[wifihood] / wifiscanner / wifiview.py
1
2 import gtk
3 import gobject
4 try :
5     import hildon
6 except :
7     hildon = False
8
9 import math
10
11 import os
12
13 import wifimap.config
14
15 import wifimap.view
16
17 class mapWidget ( wifimap.view.AbstractmapWidget , gtk.Image ) :
18
19   def __init__ ( self , config , map_size=(800,480) ) :
20     wifimap.view.AbstractmapWidget.__init__( self , config , map_size )
21
22     gtk.Image.__init__(self)
23
24     p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x, self.win_y)
25     self.set_from_pixbuf(p)
26
27     self.composeMap()
28
29   def composeMap( self ) :
30     center_x , center_y = self.win_x / 2 , self.win_y / 2
31
32     # To get the central pixel in the window center, we must shift to the tile origin
33     center_x -= self.refpix_x
34     center_y -= self.refpix_y
35
36     # Ranges should be long enough as to fill the screen
37     # Maybe they should be decided based on self.win_x, self.win_y
38     for i in range(-3,4) :
39       for j in range(-3,4) :
40         file = self.tilename( i , j , self.conf.zoom )
41         if file is None :
42           pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
43           pixbuf.fill( 0x00000000 )
44         else :
45           try :
46             pixbuf = gtk.gdk.pixbuf_new_from_file( file )
47           except gobject.GError , ex :
48             print "Corrupted file %s" % ( file )
49             os.unlink( file )
50             #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.conf.zoom )
51             file = self.tilename( i , j , self.conf.zoom )
52             try :
53               pixbuf = gtk.gdk.pixbuf_new_from_file( file )
54             except :
55               print "Total failure for tile for %s,%s" % ( self.reftile_x + i , self.reftile_y + j )
56               pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
57
58         dest_x = self.tile_size * i + center_x
59         dest_y = self.tile_size * j + center_y
60
61         init_x = 0
62         size_x = self.tile_size
63         if dest_x < 0 :
64            init_x = abs(dest_x)
65            size_x = self.tile_size + dest_x
66            dest_x = 0
67         if dest_x + self.tile_size > self.win_x :
68            size_x = self.win_x - dest_x
69
70         init_y = 0
71         size_y = self.tile_size
72         if dest_y < 0 :
73            init_y = abs(dest_y)
74            size_y = self.tile_size + dest_y
75            dest_y = 0
76         if dest_y + self.tile_size > self.win_y :
77            size_y = self.win_y - dest_y
78
79         if ( size_x > 0 and size_y > 0 ) and ( init_x < self.tile_size and init_y < self.tile_size ) :
80             pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
81         del(pixbuf)
82
83       self.draw_paths
84
85   def draw_paths( self ) :
86
87     center_x , center_y = self.win_x / 2 , self.win_y / 2
88
89     # To get the central pixel in the window center, we must shift to the tile origin
90     center_x -= self.refpix_x
91     center_y -= self.refpix_y
92
93     pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
94     red = pixmap.new_gc()
95     red.foreground = pixmap.get_colormap().alloc_color("red")
96     green = pixmap.new_gc()
97     green.foreground = pixmap.get_colormap().alloc_color("green")
98     blue = pixmap.new_gc()
99     blue.foreground = pixmap.get_colormap().alloc_color("blue")
100
101     filename = "data/wiscan_gui.info.old"
102     fd = open( filename )
103     for line in fd.readlines() :
104         values = line.split()
105         if values[1] == "FIX" :
106             dest_x , dest_y = self.gps2pix( ( float(values[5]) , float(values[6]) ) , ( center_x , center_y ) )
107             pixmap.draw_rectangle(blue, True , dest_x , dest_y , 3 , 3 )
108     fd.close()
109
110     db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
111     db.open()
112     for ap in db.db.execute( "SELECT * FROM ap" ) :
113         if ap[3] > 1 :
114             dest_x , dest_y = self.gps2pix( ( ap[4]/ap[3] , ap[5]/ap[3] ) , ( center_x , center_y ) )
115             pixmap.draw_rectangle(red, True , dest_x , dest_y , 3 , 3 )
116     db.close()
117
118     self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
119
120   def plot_APs( self ) :
121
122     center_x , center_y = self.win_x / 2 , self.win_y / 2
123
124     # To get the central pixel in the window center, we must shift to the tile origin
125     center_x -= self.refpix_x
126     center_y -= self.refpix_y
127
128     pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
129     blue = pixmap.new_gc()
130     blue.foreground = pixmap.get_colormap().alloc_color("blue")
131
132     db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
133     db.open()
134     # NOTE : Intervals for query are just educated guesses to fit in window
135     lat , lon = self.conf.lat , self.conf.lon
136     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 ) ) :
137         if ap[3] > 1 :
138             dest_x , dest_y = self.gps2pix( ( ap[4]/ap[3] , ap[5]/ap[3] ) , ( center_x , center_y ) )
139             pixmap.draw_rectangle(blue, True , dest_x , dest_y , 3 , 3 )
140     db.close()
141
142     self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
143
144
145 if hildon :
146
147     class ZoomDialog ( hildon.TouchSelector ) :
148
149         def __init__ ( self , widget ) :
150             hildon.TouchSelector.__init__( self )
151
152             zooms = gtk.ListStore(str)
153
154             active = index = 0
155             for zoom in range(8,19) :
156                 iter = zooms.append()
157                 zooms.set( iter , 0 , "%2d" % zoom )
158                 if zoom == widget.conf.zoom :
159                     active = index
160                 index += 1
161
162             column = self.append_text_column( zooms , True )
163             #renderer = gtk.CellRendererText()
164             #column = self.append_column( zooms , renderer )
165             #column.set_property('text-column', 0)
166
167             # NOTE : with text=True, we must use 1 instead of 0
168             self.set_active( 0 , active )
169
170 else :
171
172     class ZoomDialog ( gtk.Dialog ) :
173
174         def __init__ ( self , widget ) :
175             gtk.Dialog.__init__( self , "Select zoom level",
176                                  None,
177                                  gtk.DIALOG_MODAL,
178                                  ( gtk.STOCK_OK, gtk.RESPONSE_ACCEPT,
179                                    gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT
180                                    )
181                                  )
182
183             zooms = gtk.ListStore(int)
184             combo = gtk.ComboBox( zooms )
185
186             for zoom in range(8,19) :
187                 iter = zooms.append()
188                 zooms.set( iter , 0 , zoom )
189                 if zoom == widget.conf.zoom :
190                     combo.set_active_iter( iter )
191
192             cell = gtk.CellRendererText()
193             combo.pack_start(cell, True)
194             combo.add_attribute(cell, 'text', 0)
195
196             self.vbox.pack_start(combo , True, True, 0)
197
198             self.connect_object( "response", self.response , combo , widget )
199
200         def response ( self , combo , response  , widget ) :
201             if response == gtk.RESPONSE_ACCEPT :
202                 item = combo.get_active_iter()
203                 model = combo.get_model()
204                 widget.SetZoom( model.get(item,0)[0] )
205             self.destroy()
206
207
208 class AbstractMapWindow:
209
210     def destroy(self, widget, data=None):
211         gtk.main_quit()
212
213     def press_event ( self, widget, event, *args ) :
214       # FIXME : Set only if far enough from borders
215       border_x = 40
216       border_y = 30
217       print "press  ",event.get_coords(),event.get_root_coords()
218       if event.x > border_x and event.y > border_y and event.x < ( self.size_x - border_x ) and event.y < ( self.size_y - border_y ) :
219         self.click_x = event.x
220         self.click_y = event.y
221
222     def release_event ( self, widget, event, *args ) :
223       min_shift = 50
224       print "unpress",event.get_coords(),event.get_root_coords()
225       if self.click_x is not None and self.click_y is not None :
226         delta_x = int( event.x - self.click_x )
227         delta_y = int( event.y - self.click_y )
228         shift = math.sqrt( delta_x * delta_x + delta_y * delta_y )
229         if shift > min_shift :
230           self.map.Shift(delta_x, delta_y)
231         #  if delta_x > 100 :
232         #    self.map.Left()
233         #  elif delta_x < -100 :
234         #    self.map.Right()
235         #  elif delta_y > 100 :
236         #    self.map.Up()
237         #  elif delta_y < -100 :
238         #    self.map.Down()
239       self.click_x , self.click_y = None , None
240
241     def screen_event ( self, widget, event, *args ) :
242       print "REDIOS",event
243       print "      ",widget
244       print "      ",args
245
246
247     def on_button_press ( self, widget, event, *args ) :
248       print "HOLA",event
249
250     def on_key_press ( self, widget, event, *args ) :
251       if event.keyval == gtk.keysyms.Up :
252           self.map.Up()
253       elif event.keyval == gtk.keysyms.Down :
254           self.map.Down()
255       elif event.keyval == gtk.keysyms.Right :
256           self.map.Right()
257       elif event.keyval == gtk.keysyms.Left :
258           self.map.Left()
259       else :
260           print "UNKNOWN",event.keyval
261
262     def __init__( self , map_size=(800,480) ) :
263
264         self.connect("destroy", self.destroy)
265
266         self.set_border_width(10)
267
268         self.connect("key-press-event", self.on_key_press)
269
270         self.vbox = gtk.VBox(False, 0)
271
272         # To get explicit GDK_BUTTON_PRESS instead of paired GDK_LEAVE_NOTIFY & GDK_ENTER_NOTIFY
273 #        self.add_events(gtk.gdk.BUTTON_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.POINTER_MOTION_MASK)
274         self.set_events( gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK )
275         #
276 #        self.connect('motion_notify_event', self.screen_event)
277         self.connect('button_press_event', self.press_event)
278         self.connect('button_release_event', self.release_event)
279         #
280         self.config = wifimap.config.Configuration()
281         self.map = mapWidget( self.config , map_size )
282         self.vbox.pack_end( self.map , True , True , 5)
283
284         self.size_x , self.size_y = map_size
285         self.click_x , self.click_y = None , None
286
287     def zoomdialog ( self , widget ) :
288         dialog = ZoomDialog( widget )
289         dialog.show_all()
290
291     def main(self):
292         gtk.main()
293
294 if hildon :
295
296     class MapWindow ( AbstractMapWindow , hildon.Window ) :
297
298         def __init__(self):
299             hildon.Window.__init__( self )
300             AbstractMapWindow.__init__(self)
301             self.add( self.vbox )
302             self.create_menu( self.vbox )
303             self.show_all()
304
305         def create_menu ( self , vbox ) :
306
307             self.menubar = menubar = hildon.AppMenu()
308
309             #zoomlevel = hildon.Button(gtk.HILDON_SIZE_AUTO,
310             #                          hildon.BUTTON_ARRANGEMENT_VERTICAL,
311             #                          "Zoom level", None)
312             #zoomlevel.connect_object( "clicked", self.zoomstack, self.map )
313             selector = ZoomDialog( self.map )
314             zoomlevel = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,
315                                           hildon.BUTTON_ARRANGEMENT_VERTICAL)
316             zoomlevel.set_title( "Zoom" )
317             zoomlevel.set_selector( selector )
318             zoomlevel.connect_object( "value-changed", self.map.ZoomChange , selector )
319             menubar.append( zoomlevel )
320
321             menubar.show_all()
322             self.set_app_menu( menubar )
323
324 else :
325
326     class MapWindow ( AbstractMapWindow , gtk.Window ) :
327
328         def __init__(self):
329             gtk.Window.__init__( self , gtk.WINDOW_TOPLEVEL )
330             AbstractMapWindow.__init__(self)
331             self.add( self.vbox )
332             self.create_menu( self.vbox )
333             self.show_all()
334
335             self.resize( self.size_x , self.size_y)
336
337         def create_menu ( self , vbox ) :
338
339             menubar = gtk.MenuBar()
340
341             zoomlevel = gtk.MenuItem( label="Zoom level" )
342             zoomlevel.connect_object( "activate", self.zoomdialog, self.map )
343             menubar.append( zoomlevel )
344
345             vbox.pack_start(menubar,True,True,5)
346