From ed67dab721f6a06e0246cd28034f6c6d039ffdf7 Mon Sep 17 00:00:00 2001 From: Serge Noiraud Date: Fri, 12 Dec 2008 12:13:22 +0000 Subject: [PATCH] Adapt GeoView to HtmlView. svn: r11459 --- src/DataViews/GeoView.py | 485 ++++++++++++++++----------------------- 1 file changed, 197 insertions(+), 288 deletions(-) diff --git a/src/DataViews/GeoView.py b/src/DataViews/GeoView.py index 97a4fec9b..c09a0a98e 100644 --- a/src/DataViews/GeoView.py +++ b/src/DataViews/GeoView.py @@ -55,7 +55,7 @@ import gtk #------------------------------------------------------------------------- import logging LOG = logging.getLogger(".GeoView") -LOG.setLevel(logging.DEBUG) +#LOG.setLevel(logging.DEBUG) #------------------------------------------------------------------------- # @@ -97,7 +97,7 @@ if WebKit == NOWEB: pass #no interfaces present, raise Error so that options for GeoView do not show -if WebKit == 0 : +if WebKit == NOWEB : Config.set(Config.GEOVIEW, False) LOG.warning(_("GeoView not enabled, no html plugin for GTK found.")) raise ImportError, 'No GTK html plugin found' @@ -147,11 +147,11 @@ class Renderer(): """ raise NotImplementedError - if (self.browser == WEBKIT): - self.m.open("file://"+htmlfile) - elif (self.browser == MOZIL): - self.m.load_url("file://"+htmlfile) - + def execute_script(self, url): + """ execute script in the current html page + """ + raise NotImplementedError + #------------------------------------------------------------------------- # # Renderer with WebKit @@ -164,10 +164,14 @@ class RendererWebkit(Renderer): def __init__(self): Renderer.__init__(self) self.window = webkit.WebView() + self.browser = WEBKIT def open(self, url): self.window.open(url) + def execute_script(self,url): + self.window.execute_script(url); + class RendererMozilla(Renderer): """ Implementation of Renderer with gtkmozembed @@ -182,10 +186,14 @@ class RendererMozilla(Renderer): self.__set_mozembed_proxy() self.window = gtkmozembed.MozEmbed() self.window.set_size_request(800, 600) + self.browser = MOZIL def open(self, url): self.window.load_url(url) + def execute_script(self,url): + self.window.load_url(url); + def __set_mozembed_proxy(self): """ Try to see if we have some proxy environment variable. @@ -194,9 +202,9 @@ class RendererMozilla(Renderer): """ try: proxy = os.environ['http_proxy'] - data = "" if proxy: host_port = None + prefs = open(MOZEMBED_PATH+MOZEMBED_SUBPATH+"/prefs.js","w+") parts = urlparse.urlparse(proxy) if not parts[0] or parts[0] == 'http': host_port = parts[1] @@ -214,19 +222,17 @@ class RendererMozilla(Renderer): if port and host: port = str(port) - data += 'user_pref("network.proxy.type", 1);\r\n' - data += 'user_pref("network.proxy.http", "'+host+'");\r\n' - data += 'user_pref("network.proxy.http_port", '+port+');\r\n' - data += 'user_pref("network.proxy.no_proxies_on", '\ - '"127.0.0.1,localhost,localhost.localdomain");\r\n' - data += 'user_pref("network.proxy.share_proxy_settings", true);\r\n' - data += 'user_pref("network.http.proxy.pipelining", true);\r\n' - data += 'user_pref("network.http.proxy.keep-alive", true);\r\n' - data += 'user_pref("network.http.proxy.version", 1.1);\r\n' - data += 'user_pref("network.http.sendRefererHeader, 0);\r\n' - fd = file(MOZEMBED_PATH+MOZEMBED_SUBPATH+"/prefs.js","w+") - fd.write(data) - fd.close() + prefs.write('user_pref("network.proxy.type", 1);\r\n') + prefs.write('user_pref("network.proxy.http", "'+host+'");\r\n') + prefs.write('user_pref("network.proxy.http_port", '+port+');\r\n') + prefs.write('user_pref("network.proxy.no_proxies_on", '\ + '"127.0.0.1,localhost,localhost.localdomain");\r\n') + prefs.write('user_pref("network.proxy.share_proxy_settings", true);\r\n') + prefs.write('user_pref("network.http.proxy.pipelining", true);\r\n') + prefs.write('user_pref("network.http.proxy.keep-alive", true);\r\n') + prefs.write('user_pref("network.http.proxy.version", 1.1);\r\n') + prefs.write('user_pref("network.http.sendRefererHeader, 0);\r\n') + prefs.close() except: try: # trying to remove pref.js in case of proxy change. os.remove(MOZEMBED_PATH+MOZEMBED_SUBPATH+"/prefs.js") @@ -267,13 +273,11 @@ class HtmlView(PageView.PageView): frame.set_shadow_type(gtk.SHADOW_NONE) frame.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) frame.add_with_viewport(self.table) + self.bootstrap_handler = self.box.connect("size-request", self.init_parent_signals_for_map) self.table.get_parent().set_shadow_type(gtk.SHADOW_NONE) self.table.set_row_spacings(1) self.table.set_col_spacings(0) - gobject.threads_init() - self.count = 0 - if (WebKit == WEBKIT) : # We use webkit self.renderer = RendererWebkit() @@ -327,6 +331,12 @@ class HtmlView(PageView.PageView): def define_actions(self): pass + def init_parent_signals_for_map(self, widget, event): + # required to properly bootstrap the signal handlers. + # This handler is connected by build_widget. After the outside ViewManager + # has placed this widget we are able to access the parent container. + pass + def create_start_page(self): """ This command creates a default start page, and returns the URL of this @@ -365,76 +375,50 @@ class HtmlView(PageView.PageView): # GeoView # #------------------------------------------------------------------------- -class GeoView(PageView.PersonNavView): +class GeoView(HtmlView): def __init__(self,dbstate,uistate): - PageView.PersonNavView.__init__(self, _('GeoView'), dbstate, uistate) + HtmlView.__init__(self, dbstate, uistate) - global WebKit - - self.dbstate = dbstate self.usedmap = "openstreetmap" self.displaytype = "person" - self.minyear = int(9999) - self.maxyear = int(0) # Create a temporary dot file (handle,self.htmlfile) = tempfile.mkstemp(".html","GeoV", MOZEMBED_PATH ) - # needs to be solved. where to remove it ? - def __del__(self): + def on_delete(self): """ - How to do this the best way. We don't go here. - We need to suppress this temporary file. + We need to suppress the html temporary file. """ try: os.remove(self.htmlfile) except: pass - #def _quit(self, widget): - # gtk.main_quit() - - #def change_page(self): - # self.uistate.clear_filter_results() - def init_parent_signals_for_map(self, widget, event): # required to properly bootstrap the signal handlers. # This handler is connected by build_widget. After the outside ViewManager # has placed this widget we are able to access the parent container. - self.notebook.disconnect(self.bootstrap_handler) - self.notebook.parent.connect("size-allocate", self.size_request_for_map) + self.box.disconnect(self.bootstrap_handler) + self.box.parent.connect("size-allocate", self.size_request_for_map) self.size_request_for_map(widget.parent,event) def request_resize(self): - self.size_request_for_map(self.notebook.parent,None,None) + self.size_request_for_map(self.box.parent,None,None) + self.geo_places(self.htmlfile,self.displaytype) def size_request_for_map(self, widget, event, data=None): v = widget.get_allocation() self.width = v.width self.height = v.height - def add_table_to_notebook( self, table): - frame = gtk.ScrolledWindow(None,None) - frame.set_shadow_type(gtk.SHADOW_NONE) - frame.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC) - frame.add_with_viewport(table) - table.get_parent().set_shadow_type(gtk.SHADOW_NONE) - table.set_row_spacings(1) - table.set_col_spacings(0) - try: - self.notebook.append_page(frame,None) - except: - # for PyGtk < 2.4 - self.notebook.append_page(frame,gtk.Label("")) - def set_active(self): self.key_active_changed = self.dbstate.connect('active-changed', self.goto_active_person) def set_inactive(self): - PageView.PersonNavView.set_inactive(self) + HtmlView.set_inactive(self) self.dbstate.disconnect(self.key_active_changed) def get_stock(self): @@ -445,71 +429,8 @@ class GeoView(PageView.PersonNavView): """ return 'gramps-geo' - def call(self): - self.count = self.count+1 - gobject.idle_add(self.idle_call_js ,self.count) - - def idle_call_js(self,count): - if (self.browser == 1): - self.m.execute_script("javascript:callFromPython("+str(count)+")"); - #self.m.zoom_in(); # make text bigger - elif (self.browser == 2): - self.m.load_url("javascript:callFromPython("+str(count)+")"); - def change_map(self): - if (self.browser == 1): - self.m.execute_script("javascript:mapstraction.swap(map,'"+self.usedmap+"')"); - elif (self.browser == 2): - self.m.load_url("javascript:mapstraction.swap(map,'"+self.usedmap+"')"); - - def build_widget(self): - """ - Builds the interface and returns a gtk.Container type that - contains the interface. This containter will be inserted into - a gtk.Notebook page. - """ - global WebKit - - self.tooltips = gtk.Tooltips() - self.tooltips.enable() - - self.notebook = gtk.Notebook() - self.notebook.set_show_border(False) - self.notebook.set_show_tabs(False) - self.bootstrap_handler = self.notebook.connect("size-request", self.init_parent_signals_for_map) - - self.table_2 = gtk.Table(1,1,False) - self.add_table_to_notebook( self.table_2) - gobject.threads_init() - self.count = 0 - self.browser = 0 - - if (WebKit == 1) : - # We use webkit - self.browser=1 - self.m = webkit.WebView() - elif (WebKit == 2) : - # We use gtkmozembed - self.browser=2 - if hasattr(gtkmozembed, 'set_profile_path'): - set_profile_path = gtkmozembed.set_profile_path - else: - set_profile_path = gtkmozembed.gtk_moz_embed_set_profile_path - if os.path.isdir(MOZEMBED_PATH+MOZEMBED_SUBPATH): - pass - else: - os.system("mkdir -p "+MOZEMBED_PATH+MOZEMBED_SUBPATH) - set_profile_path(MOZEMBED_PATH, MOZEMBED_SUBPATH) - self.set_mozembed_proxy() - self.m = gtkmozembed.MozEmbed() - self.m.set_size_request(800, 600) - - self.table_2.add(self.m) - self.geo_places(None,self.displaytype) - - self.m.show_all() - - return self.notebook + self.renderer.execute_script("javascript:mapstraction.swap(map,'"+self.usedmap+"')"); def ui_definition(self): """ @@ -594,7 +515,7 @@ class GeoView(PageView.PersonNavView): callback=self.event_places, tip=_("Attempt to view places on the Map for all events.")) - PageView.PersonNavView.define_actions(self) + HtmlView.define_actions(self) def goto_active_person(self,handle=None): self.geo_places(self.htmlfile,self.displaytype) @@ -636,14 +557,7 @@ class GeoView(PageView.PersonNavView): self.createHelp(htmlfile) else: self.createMapstraction(htmlfile,displaytype) - LOG.debug("geo_places : in appel page") - if (self.browser == 1): - self.m.open("file://"+htmlfile) - elif (self.browser == 2): - self.m.load_url("file://"+htmlfile) - - if (self.browser != 0): - self.m.show_all() + self.renderer.open("file://"+htmlfile) def select_OpenStreetMap_map(self,handle=None): self.usedmap = "openstreetmap" @@ -686,6 +600,7 @@ class GeoView(PageView.PersonNavView): proxy = os.environ['http_proxy'] data = "" if proxy: + prefs = open(MOZEMBED_PATH+MOZEMBED_SUBPATH+"/prefs.js","w") host_port = None parts = urlparse.urlparse(proxy) if not parts[0] or parts[0] == 'http': @@ -704,20 +619,18 @@ class GeoView(PageView.PersonNavView): if port and host: port = str(port) - data += 'user_pref("network.proxy.type", 1);\r\n' - data += 'user_pref("network.proxy.http", "'+host+'");\r\n' - data += 'user_pref("network.proxy.http_port", '+port+');\r\n' - data += 'user_pref("network.proxy.no_proxies_on", "127.0.0.1,localhost,localhost.localdomain");\r\n' - data += 'user_pref("network.proxy.share_proxy_settings", true);\r\n' - data += 'user_pref("network.http.proxy.pipelining", true);\r\n' - data += 'user_pref("network.http.proxy.keep-alive", true);\r\n' - data += 'user_pref("network.http.proxy.version", 1.1);\r\n' - data += 'user_pref("network.http.sendRefererHeader, 0);\r\n' - fd = file(MOZEMBED_PATH+MOZEMBED_SUBPATH+"/prefs.js","w+") - fd.write(data) - fd.close() + prefs.write('user_pref("network.proxy.type", 1);\r\n') + prefs.write('user_pref("network.proxy.http", "'+host+'");\r\n') + prefs.write('user_pref("network.proxy.http_port", '+port+');\r\n') + prefs.write('user_pref("network.proxy.no_proxies_on", "127.0.0.1,localhost,localhost.localdomain");\r\n') + prefs.write('user_pref("network.proxy.share_proxy_settings", true);\r\n') + prefs.write('user_pref("network.http.proxy.pipelining", true);\r\n') + prefs.write('user_pref("network.http.proxy.keep-alive", true);\r\n') + prefs.write('user_pref("network.http.proxy.version", 1.1);\r\n') + prefs.write('user_pref("network.http.sendRefererHeader, 0);\r\n') + prefs.close() except: - try: # tryng to remove pref.js in case of proxy change. + try: # trying to remove pref.js in case of proxy change. os.remove(MOZEMBED_PATH+MOZEMBED_SUBPATH+"/prefs.js") except: pass @@ -743,86 +656,85 @@ class GeoView(PageView.PersonNavView): self.yearint=10 LOG.debug("period = %d, intvl = %d, interval = %d" % (period, intvl, self.yearint)) - self.geo += " var step = %s;\n" % self.yearint - self.geo += " \n" - self.geo += " \n" - self.geo += " \n" + self.mapview.write(" var step = %s;\n" % self.yearint) + self.mapview.write(" \n") + self.mapview.write(" \n") + self.mapview.write(" \n") if self.displaytype != "places": - self.geo += "
\n" - self.geo += "
\n" - self.geo += " All\n" + self.mapview.write("
\n") + self.mapview.write(" \n") + self.mapview.write(" All\n") for year in range(self.minyear,self.maxyear+self.yearint,self.yearint): - self.geo += " %s\n" % ( year, year ) - self.geo += "
\n" + self.mapview.write(" %s\n" % ( year, year )) + self.mapview.write("
\n") def createMapstractionHeader(self): - self.geo = "\n" - self.geo += "\n" - self.geo += " \n" - self.geo += " \n" - self.geo += " Geo Maps JavaScript API for Gramps\n" - self.geo += " \n" - self.geo += " \n" + self.mapview = open(self.htmlfile,"w+") + self.mapview.write("\n") + self.mapview.write("\n") + self.mapview.write(" \n") + self.mapview.write(" \n") + self.mapview.write(" Geo Maps JavaScript API for Gramps\n") + self.mapview.write(" \n") + self.mapview.write(" \n") if self.usedmap == "microsoft": - self.geo += " \n" + self.mapview.write(" \n") elif self.usedmap == "yahoo": - self.geo += " \n" + self.mapview.write(" \n") elif self.usedmap == "openlayers": - self.geo += " \n" + self.mapview.write(" \n") else: # openstreetmap and google - self.geo += " \n" - self.geo += " \n") + self.mapview.write(" \n" + self.mapview.write(" var point = new LatLonPoint(%s,%s);\n"%(latitude,longitude)) + self.mapview.write(" mapstraction.setCenterAndZoom(point, %d);\n"%2) + self.mapview.write(" my_marker = new Marker(point);\n") + self.mapview.write(" my_marker.setLabel(\"%s\");\n"%_("The author of this module.")) + self.mapview.write(" my_marker.setInfoBubble(\"
") + self.mapview.write("Serge Noiraud
Nantes, France
") + self.mapview.write("%s
\");\n"%_("This request has no geolocation associated.")) + self.mapview.write(" mapstraction.addMarker(my_marker);\n") + self.mapview.write(" \n") def createPersonMarkers(self,db,person,comment): """ @@ -1183,8 +1096,6 @@ class GeoView(PageView.PersonNavView): self.maxlat = float(0.0) self.minlon = float(0.0) self.maxlon = float(0.0) - self.minyear = int(9999) - self.maxyear = int(0) latitude = "" longitude = "" @@ -1209,9 +1120,9 @@ class GeoView(PageView.PersonNavView): descr1, self.center, None) self.center = 0 self.createMapstractionPostHeader() - self.geo += "

%s

"%_("All places in the database with coordinates.") + self.mapview.write("

%s

"%_("All places in the database with coordinates.")) if self.center == 1: - self.geo += "

%s

"%_("Cannot center the map. No selected location.") + self.mapview.write("

%s

"%_("Cannot center the map. No selected location.")) self.create_markers(1) def createMapstractionEvents(self,db): @@ -1269,9 +1180,9 @@ class GeoView(PageView.PersonNavView): descr2, self.center, eventyear) self.center = 0 self.createMapstractionPostHeader() - self.geo += "

%s

"%_("All events in the database with coordinates.") + self.mapview.write("

%s

"%_("All events in the database with coordinates.")) if self.center == 1: - self.geo += "

%s

"%_("Cannot center the map. No selected location.") + self.mapview.write("

%s

"%_("Cannot center the map. No selected location.")) self.create_markers(2) def createMapstractionFamily(self,db): @@ -1315,9 +1226,9 @@ class GeoView(PageView.PersonNavView): _("Child"),index) self.createPersonMarkers(db,child,comment) self.createMapstractionPostHeader() - self.geo += "

%s

"%_("All %s people's family places in the database with coordinates.") % _nd.display(person) + self.mapview.write("

%s

"%_("All %s people's family places in the database with coordinates.") % _nd.display(person)) if self.center == 1: - self.geo += "

%s

"%_("Cannot center the map. No selected location.") + self.mapview.write("

%s

"%_("Cannot center the map. No selected location.")) self.create_markers(3) def createMapstractionPerson(self,db): @@ -1369,9 +1280,9 @@ class GeoView(PageView.PersonNavView): descr1, self.center, eventyear) self.center = 0 self.createMapstractionPostHeader() - self.geo += "

%s

"%_("All event places for %s.") % _nd.display(person) + self.mapview.write("

%s

"%_("All event places for %s.") % _nd.display(person)) if self.center == 1: - self.geo += "

%s

"%_("Cannot center the map. No selected location.") + self.mapview.write("

%s

"%_("Cannot center the map. No selected location.")) self.create_markers(4) def createMapstractionNotImplemented(self,db): @@ -1379,34 +1290,32 @@ class GeoView(PageView.PersonNavView): This function is used to inform the user this work is not implemented. """ LOG.warning('createMapstractionNotImplemented') - self.geo += "

%s ...

"%_("Not yet implemented") + self.mapview.write("

%s ...

"%_("Not yet implemented")) def createHelp(self,filename): - tmpdir = MOZEMBED_PATH - geo = "\n" - geo += "\n" - geo += " \n" - geo += " \n" - geo += " Geo Maps JavaScript API for Gramps\n" - geo += " \n" - geo += " \n" - geo += "
\n" % 600 - geo += "




\n" - geo += "

" - geo += _("You can choose between two maps. One free and a second one.") - geo += "
" - geo += _("The best choice is the free map like openstreetmap.") - geo += "
" - geo += _("You should use the second map only if the first one give no results ...") - geo += "
" - geo += _("You can select Edit/Preferences to choose the second map provider.") - geo += "
" - geo += _("They are : Googlemaps, Yahoo! maps, Microsoft maps and Openlayers.") - geo += "

" - geo += "
\n" - geo += " \n" - geo += "\n" - fd = file(filename,"w+") - fd.write(geo) - fd.close() + help = open(self.htmlfile,"w") + help.write("\n") + help.write("\n") + help.write(" \n") + help.write(" \n") + help.write(" Geo Maps JavaScript API for Gramps\n") + help.write(" \n") + help.write(" \n") + help.write("
\n" % 600) + help.write("




\n") + help.write("

") + help.write(_("You can choose between two maps. One free and a second one.")) + help.write("
") + help.write(_("The best choice is the free map like openstreetmap.")) + help.write("
") + help.write(_("You should use the second map only if the first one give no results ...")) + help.write("
") + help.write(_("You can select Edit/Preferences to choose the second map provider.")) + help.write("
") + help.write(_("They are : Googlemaps, Yahoo! maps, Microsoft maps and Openlayers.")) + help.write("

") + help.write("
\n") + help.write(" \n") + help.write("\n") + help.close()