source: subversion/applications/routing/pyroute/gui.py @ 5303

Last change on this file since 5303 was 5303, checked in by ojw, 12 years ago

Pan

  • Property svn:executable set to *
File size: 3.8 KB
Line 
1#!/usr/bin/env python
2import pygtk
3pygtk.require('2.0')
4import gobject
5import gtk
6import sys
7from gtk import gdk
8from loadOsm import *
9import cairo
10
11def update(mapWidget):
12  return(True)
13
14class Projection:
15  def __init__(self):
16    pass
17  def setView(self,x,y,w,h):
18    self.w = w / 2
19    self.h = h / 2
20    self.xc = x + self.w
21    self.yc = y + self.h
22  def recentre(self,lat,lon,scale):
23    self.lat = lat
24    self.lon = lon
25    self.scale = scale  # TODO: scale and scaleCosLat
26  def nudge(self,dx,dy,scale):
27    self.lat = self.lat + dy * scale * self.scale
28    self.lon = self.lon + dx * scale * self.scale
29  def ll2xy(self,lat,lon):
30    px = (lon - self.lon) / self.scale
31    py = (lat - self.lat) / self.scale
32    x = self.xc + self.w * px
33    y = self.yc - self.h * py
34    return(x,y)
35  def xy2ll(self,x,y):
36    pass
37   
38class MapWidget(gtk.Widget):
39  __gsignals__ = { \
40    'realize': 'override',
41    'expose-event' : 'override',
42    'size-allocate': 'override',
43    'size-request': 'override'}
44  def __init__(self, osmDataFile):
45    gtk.Widget.__init__(self)
46    self.draw_gc = None
47    self.timer = gobject.timeout_add(200, update, self)
48    self.data = LoadOsm(osmDataFile)
49    self.projection = Projection()
50    self.projection.recentre(51.524,-0.129, 0.02)
51  def move(self,dx,dy):
52    self.projection.nudge(-dx,dy,1.0/self.rect.width)
53    self.window.invalidate_rect((0,0,self.rect.width,self.rect.height),False)
54  def draw(self, cr):
55    # Just nodes:
56    cr.set_source_rgb(0.0, 0.0, 0.0)
57    cr.set_line_cap(cairo.LINE_CAP_ROUND)
58    for k,v in self.data.nodes.items():
59      x,y = self.projection.ll2xy(v[0],v[1])
60      cr.move_to(x,y)
61      cr.line_to(x,y)
62      cr.stroke()
63 
64  def do_realize(self):
65    self.set_flags(self.flags() | gtk.REALIZED)
66    self.window = gdk.Window( \
67      self.get_parent_window(),
68      width = self.allocation.width,
69      height = self.allocation.height,
70      window_type = gdk.WINDOW_CHILD,
71      wclass = gdk.INPUT_OUTPUT,
72      event_mask = self.get_events() | gdk.EXPOSURE_MASK)
73    self.window.set_user_data(self)
74    self.style.attach(self.window)
75    self.style.set_background(self.window, gtk.STATE_NORMAL)
76    self.window.move_resize(*self.allocation)
77  def do_size_request(self, allocation):
78    pass
79  def do_size_allocate(self, allocation):
80    self.allocation = allocation
81    if self.flags() & gtk.REALIZED:
82      self.window.move_resize(*allocation)
83  def _expose_cairo(self, event, cr):
84    self.rect = self.allocation
85    self.projection.setView( \
86      self.rect.x, 
87      self.rect.y, 
88      self.rect.width, 
89      self.rect.height)
90    self.draw(cr)
91  def do_expose_event(self, event):
92    self.chain(event)
93    cr = self.window.cairo_create()
94    return self._expose_cairo(event, cr)
95
96class GuiBase:
97  """Wrapper class for a GUI interface"""
98  def __init__(self, osmDataFile):
99    # Create the window
100    win = gtk.Window()
101    win.set_title('map')
102    win.connect('delete-event', gtk.main_quit)
103    win.resize(600,800)
104    win.move(50, gtk.gdk.screen_height() - 850)
105   
106    # Events
107    event_box = gtk.EventBox()
108    event_box.connect("button_press_event", lambda w,e: self.pressed(e))
109    event_box.connect("button_release_event", lambda w,e: self.released(e))
110    event_box.connect("motion_notify_event", lambda w,e: self.moved(e))
111    win.add(event_box)
112   
113    # Create the map
114    self.mapWidget = MapWidget(osmDataFile)
115    event_box.add(self.mapWidget)
116   
117    # Finalise the window
118    win.show_all()
119    gtk.main()
120   
121  def pressed(self, event):
122    self.dragx = event.x
123    self.dragy = event.y
124  def released(self, event):
125    pass
126  def moved(self, event):
127    """Drag-handler"""
128    self.mapWidget.move(event.x - self.dragx, event.y - self.dragy)
129    self.dragx = event.x
130    self.dragy = event.y
131
132if __name__ == "__main__":
133  program = GuiBase(sys.argv[1])
Note: See TracBrowser for help on using the repository browser.