From 1fe42b7f49baf05f063dee0504cb984907091c85 Mon Sep 17 00:00:00 2001 From: Serge Noiraud Date: Mon, 10 Sep 2012 18:55:23 +0000 Subject: [PATCH] Geography: convert to gtk3 and some cleaning. not finished, but it works. svn: r20358 --- src/plugins/lib/maps/datelayer.py | 4 +-- src/plugins/lib/maps/geography.py | 46 +++++++++++++++++--------- src/plugins/lib/maps/lifewaylayer.py | 31 ++++++++--------- src/plugins/lib/maps/markerlayer.py | 7 ++-- src/plugins/lib/maps/messagelayer.py | 15 ++++++--- src/plugins/lib/maps/osmGps.py | 8 ----- src/plugins/lib/maps/selectionlayer.py | 32 ++++++++++-------- src/plugins/view/geomoves.py | 20 +++++------ 8 files changed, 86 insertions(+), 77 deletions(-) diff --git a/src/plugins/lib/maps/datelayer.py b/src/plugins/lib/maps/datelayer.py index b68234fb1..ffd2d44a4 100644 --- a/src/plugins/lib/maps/datelayer.py +++ b/src/plugins/lib/maps/datelayer.py @@ -46,6 +46,7 @@ _LOG = logging.getLogger("maps.datelayer") # #------------------------------------------------------------------------- from gi.repository import Gtk +from gi.repository import Gdk #------------------------------------------------------------------------- # @@ -109,11 +110,10 @@ class DateLayer(GObject.GObject, osmgpsmap.MapLayer): if date > self.last or self.last == " ": self.last = date - def do_draw(self, gpsmap, drawable): + def do_draw(self, gpsmap, ctx): """ Draw the two extreme dates """ - ctx = drawable.cairo_create() ctx.select_font_face(self.font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) diff --git a/src/plugins/lib/maps/geography.py b/src/plugins/lib/maps/geography.py index 766563285..5a47b3dc4 100644 --- a/src/plugins/lib/maps/geography.py +++ b/src/plugins/lib/maps/geography.py @@ -41,6 +41,7 @@ from math import pi #------------------------------------------------------------------------- from gi.repository import Gtk from gi.repository import GdkPixbuf +import cairo #------------------------------------------------------------------------- # @@ -145,14 +146,12 @@ class GeoGraphyView(OsmGps, NavigationView): self.place_list = [] self.places_found = [] self.select_fct = None - self.geo_mainmap = GdkPixbuf.Pixbuf.new_from_file_at_size( + self.geo_mainmap = cairo.ImageSurface.create_from_png( os.path.join(ROOT_DIR, "images", "48x48", - ('gramps-geo-mainmap' + '.png' )), - 48, 48) - self.geo_altmap = GdkPixbuf.Pixbuf.new_from_file_at_size( + ('gramps-geo-mainmap' + '.png' ))) + self.geo_altmap = cairo.ImageSurface.create_from_png( os.path.join(ROOT_DIR, "images", "48x48", - ('gramps-geo-altmap' + '.png' )), - 48, 48) + ('gramps-geo-altmap' + '.png' ))) if ( config.get('geography.map_service') in ( constants.OPENSTREETMAP, constants.OPENSTREETMAP_RENDERER )): default_image = self.geo_mainmap @@ -162,10 +161,9 @@ class GeoGraphyView(OsmGps, NavigationView): for ident in ( gen.lib.EventType.BIRTH, gen.lib.EventType.DEATH, gen.lib.EventType.MARRIAGE ): - self.geo_othermap[ident] = GdkPixbuf.Pixbuf.new_from_file_at_size( + self.geo_othermap[ident] = cairo.ImageSurface.create_from_png( os.path.join(ROOT_DIR, "images", "48x48", - (constants.ICONS.get(int(ident), default_image) + '.png' )), - 48, 48) + (constants.ICONS.get(int(ident), default_image) + '.png' ))) def change_page(self): """ @@ -175,6 +173,29 @@ class GeoGraphyView(OsmGps, NavigationView): self.uistate.clear_filter_results() self.end_selection = None + def do_size_request(self, requisition): + """ + Overridden method to handle size request events. + """ + requisition.width = 400 + requisition.height = 300 + + def do_get_preferred_width(self): + """ GTK3 uses width for height sizing model. This method will + override the virtual method + """ + req = Gtk.Requisition() + self.do_size_request(req) + return req.width, req.width + + def do_get_preferred_height(self): + """ GTK3 uses width for height sizing model. This method will + override the virtual method + """ + req = Gtk.Requisition() + self.do_size_request(req) + return req.height, req.height + def on_delete(self): """ Save all modified environment @@ -585,10 +606,6 @@ class GeoGraphyView(OsmGps, NavigationView): level_start = self.osm.props.zoom p1lat, p1lon = self.begin_selection.get_degrees() p2lat, p2lon = self.end_selection.get_degrees() - #p1lat = 180 * self.begin_selection.rlat / pi - #p1lon = 180 * self.begin_selection.rlon / pi - #p2lat = 180 * self.end_selection.rlat / pi - #p2lon = 180 * self.end_selection.rlon / pi lat = p1lat + ( p2lat - p1lat ) / 2 lon = p1lon + ( p2lon - p1lon ) / 2 # We center the map on the center of the region @@ -754,7 +771,6 @@ class GeoGraphyView(OsmGps, NavigationView): Edit the selected family at the marker position """ _LOG.debug("edit_family : %s" % mark[11]) - # need to add code here to edit the family. family = self.dbstate.db.get_family_from_gramps_id(mark[11]) try: EditFamily(self.dbstate, self.uistate, [], family) @@ -766,7 +782,6 @@ class GeoGraphyView(OsmGps, NavigationView): Edit the selected event at the marker position """ _LOG.debug("edit_event : %s" % mark[10]) - # need to add code here to edit the event. event = self.dbstate.db.get_event_from_gramps_id(mark[10]) try: EditEvent(self.dbstate, self.uistate, [], event) @@ -833,7 +848,6 @@ class GeoGraphyView(OsmGps, NavigationView): """ Edit the selected place at the marker position """ - # need to add code here to edit the event. self.select_fct.close() place = self.dbstate.db.get_place_from_gramps_id(self.mark[9]) place.set_latitude(str(plat)) diff --git a/src/plugins/lib/maps/lifewaylayer.py b/src/plugins/lib/maps/lifewaylayer.py index 739b9eace..dc189e05f 100644 --- a/src/plugins/lib/maps/lifewaylayer.py +++ b/src/plugins/lib/maps/lifewaylayer.py @@ -45,6 +45,7 @@ _LOG = logging.getLogger("maps.lifeway") # #------------------------------------------------------------------------- from gi.repository import Gtk +from gi.repository import Gdk #------------------------------------------------------------------------- # @@ -98,31 +99,31 @@ class LifeWayLayer(GObject.GObject, osmgpsmap.MapLayer): """ Add a track or life way. """ + if isinstance(color,str): + color = Gdk.color_parse(color) self.lifeways.append((points, color)) - def do_draw(self, gpsmap, drawable): + def do_draw(self, gpsmap, ctx): """ Draw all tracks or life ways. """ - ctx = drawable.cairo_create() for lifeway in self.lifeways_ref: ctx.set_line_cap(cairo.LINE_CAP_ROUND) ctx.set_line_join(cairo.LINE_JOIN_ROUND) ctx.set_line_width(3) - color = Gdk.color_parse(lifeway[1]) + color = lifeway[1] ctx.set_source_rgba(float(color.red / 65535.0), float(color.green / 65535.0), float(color.blue / 65535.0), 0.1) # transparency - ggc = drawable.new_gc() rds = float(lifeway[2]) for point in lifeway[0]: - conv_pt1 = osmgpsmap.point_new_degrees(point[0], point[1]) + conv_pt1 = osmgpsmap.MapPoint.new_degrees(point[0], point[1]) coord_x1, coord_y1 = gpsmap.convert_geographic_to_screen(conv_pt1) - conv_pt2 = osmgpsmap.point_new_degrees(point[0]+rds, point[1]) + conv_pt2 = osmgpsmap.MapPoint.new_degrees(point[0]+rds, point[1]) coord_x2, coord_y2 = gpsmap.convert_geographic_to_screen(conv_pt2) coy = abs(coord_y2-coord_y1) - conv_pt2 = osmgpsmap.point_new_degrees(point[0], point[1]+rds) + conv_pt2 = osmgpsmap.MapPoint.new_degrees(point[0], point[1]+rds) coord_x2, coord_y2 = gpsmap.convert_geographic_to_screen(conv_pt2) cox = abs(coord_x2-coord_x1) cox = cox if cox > 0.001 else 0.001 @@ -135,25 +136,21 @@ class LifeWayLayer(GObject.GObject, osmgpsmap.MapLayer): ctx.translate(coord_x1, coord_y1/coz) ctx.arc(0.0, 0.0, cox, 0.0, 2*pi) ctx.fill() + ctx.set_line_width(2.0) + ctx.arc(0.0, 0.0, cox, 0.0, 2*pi) + ctx.set_source_rgba(1.0,0.0,0.0,0.5) + ctx.stroke() ctx.restore() - top_left = osmgpsmap.point_new_degrees(point[0] + lifeway[2], - point[1] - lifeway[2]) - bottom_right = osmgpsmap.point_new_degrees(point[0] - lifeway[2], - point[1] + lifeway[2]) - crd_x, crd_y = gpsmap.convert_geographic_to_screen(top_left) - crd_x2, crd_y2 = gpsmap.convert_geographic_to_screen(bottom_right) - drawable.draw_arc(ggc, False, crd_x, crd_y, crd_x2 - crd_x, - crd_y2 - crd_y, 0, 360*64) for lifeway in self.lifeways: ctx.set_operator(cairo.OPERATOR_ATOP) ctx.set_line_width(3.0) map_points = [] for point in lifeway[0]: - conv_pt = osmgpsmap.point_new_degrees(point[0], point[1]) + conv_pt = osmgpsmap.MapPoint.new_degrees(point[0], point[1]) coord_x, coord_y = gpsmap.convert_geographic_to_screen(conv_pt) map_points.append((coord_x, coord_y)) - color = Gdk.color_parse(lifeway[1]) + color = lifeway[1] ctx.set_source_rgb(float(color.red / 65535.0), float(color.green / 65535.0), float(color.blue / 65535.0)) diff --git a/src/plugins/lib/maps/markerlayer.py b/src/plugins/lib/maps/markerlayer.py index e6e5ee695..dddc850c5 100644 --- a/src/plugins/lib/maps/markerlayer.py +++ b/src/plugins/lib/maps/markerlayer.py @@ -115,13 +115,12 @@ class MarkerLayer(GObject.GObject, osmgpsmap.MapLayer): self.min_value = count self.nb_ref_by_places = self.max_references / self.max_places - def do_draw(self, gpsmap, drawable): + def do_draw(self, gpsmap, ctx): """ Draw all markers here. Calculate where to draw the marker. Depending of the average, minimum and maximum value, resize the marker. We use cairo to resize the marker. """ - ctx = drawable.cairo_create() max_interval = self.max_value - self.nb_ref_by_places min_interval = self.nb_ref_by_places - self.min_value if max_interval == 0: # This to avoid divide by zero @@ -144,7 +143,7 @@ class MarkerLayer(GObject.GObject, osmgpsmap.MapLayer): size -= (0.3 * ((self.nb_ref_by_places - mark) / min_interval) ) - conv_pt = osmgpsmap.point_new_degrees(float(marker[0][0]), + conv_pt = osmgpsmap.MapPoint.new_degrees(float(marker[0][0]), float(marker[0][1])) coord_x, coord_y = gpsmap.convert_geographic_to_screen(conv_pt) ctx.translate(coord_x, coord_y) @@ -156,7 +155,7 @@ class MarkerLayer(GObject.GObject, osmgpsmap.MapLayer): # So we shift the image position. posY = - int( 48 * size + 0.5 ) - 10 posX = - int(( 48 * size ) / 6 + 0.5 ) - 10 - ctx.set_source_pixbuf(marker[1], posX, posY) + ctx.set_source_surface(marker[1], posX, posY) ctx.paint() ctx.restore() _LOG.debug("%s" % time.strftime("end drawing : " diff --git a/src/plugins/lib/maps/messagelayer.py b/src/plugins/lib/maps/messagelayer.py index 59ec3237e..0ff5434d6 100644 --- a/src/plugins/lib/maps/messagelayer.py +++ b/src/plugins/lib/maps/messagelayer.py @@ -48,6 +48,7 @@ _LOG = logging.getLogger("maps.messagelayer") from gi.repository import Gdk #from gi.repository import Cairo import cairo +import pangocairo #------------------------------------------------------------------------- # @@ -112,11 +113,10 @@ class MessageLayer(GObject.GObject, osmgpsmap.MapLayer): """ self.message.append(message) - def do_draw(self, gpsmap, drawable): + def do_draw(self, gpsmap, ctx): """ Draw the two extreme dates """ - ctx = drawable.cairo_create() ctx.select_font_face(self.font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) @@ -128,12 +128,19 @@ class MessageLayer(GObject.GObject, osmgpsmap.MapLayer): 0.9) # transparency coord_x = 100 coord_y = int(self.size) # Show the first line under the zoom button - (d_width, d_height) = drawable.get_size() + #(d_width, d_height) = drawable.get_size() + pcr = pangocairo.CairoContext(ctx) + ctx_layout = pcr.create_layout() + (d_width, d_height) = ctx_layout.get_size() + print d_width, d_height d_width -= 100 for line in self.message: line_to_print = line (x_bearing, y_bearing, width, height, x_advance, y_advance) = ctx.text_extents(line_to_print) - while ( width > d_width): + #while ( width > d_width): + # BUG here d_with is negative + while ( width > 1000): + print width, " > ", d_width line_length = len(line_to_print) character_length = int(width/line_length) + 1 max_length = int(d_width / character_length) - 5 diff --git a/src/plugins/lib/maps/osmGps.py b/src/plugins/lib/maps/osmGps.py index 54a446730..449a99183 100644 --- a/src/plugins/lib/maps/osmGps.py +++ b/src/plugins/lib/maps/osmGps.py @@ -28,7 +28,6 @@ # #------------------------------------------------------------------------- import os -from math import pi from gi.repository import GObject #------------------------------------------------------------------------ @@ -250,9 +249,6 @@ class OsmGps(): """ current = osmmap.convert_screen_to_geographic(int(event.x), int(event.y)) lat, lon = current.get_degrees() - #lat = 180 * current.rlat / pi - #lon = 180 * current.rlon / pi - #print event.x, event.y, lat, lon if self.zone_selection: # We draw a rectangle to show the selected region. layer = self.get_selection_layer() @@ -307,12 +303,8 @@ class OsmGps(): mouse button 3 : call the menu """ lat, lon = self.osm.get_event_location(event).get_degrees() - #lat = 180 * self.osm.get_event_location(event).rlat / pi - #lon = 180 * self.osm.get_event_location(event).rlon / pi current = osm.convert_screen_to_geographic(int(event.x), int(event.y)) lat, lon = current.get_degrees() - #lat = 180 * current.rlat / pi - #lon = 180 * current.rlon / pi if event.button == 1: if self.end_selection is not None: self.activate_selection_zoom(osm, event) diff --git a/src/plugins/lib/maps/selectionlayer.py b/src/plugins/lib/maps/selectionlayer.py index f584dd686..6498f1014 100644 --- a/src/plugins/lib/maps/selectionlayer.py +++ b/src/plugins/lib/maps/selectionlayer.py @@ -76,20 +76,20 @@ class SelectionLayer(GObject.GObject, osmgpsmap.MapLayer): """ self.rectangles.append((cp1, cp2)) - def do_draw(self, gpsmap, drawable): + def do_draw(self, gpsmap, ctx): """ draw the circles and the rectangles """ - ggc = drawable.new_gc() for circle in self.circles: - top_left = osmgpsmap.point_new_degrees(circle[1] + circle[0], + top_left = osmgpsmap.MapPoint.new_degrees(circle[1] + circle[0], circle[2] - circle[0]) - bottom_right = osmgpsmap.point_new_degrees(circle[1] - circle[0], + bottom_right = osmgpsmap.MapPoint.new_degrees(circle[1] - circle[0], circle[2] + circle[0]) crd_x, crd_y = gpsmap.convert_geographic_to_screen(top_left) crd_x2, crd_y2 = gpsmap.convert_geographic_to_screen(bottom_right) - drawable.draw_arc(ggc, False, crd_x, crd_y, crd_x2 - crd_x, - crd_y2 - crd_y, 0, 360*64) + #drawable.draw_arc(ggc, False, crd_x, crd_y, crd_x2 - crd_x, + # crd_y2 - crd_y, 0, 360*64) + ctx.arc(crd_x, crd_y, crd_x2 - crd_x, crd_y2 - crd_y, 0, 360*64) for rectangle in self.rectangles: top_left, bottom_right = rectangle crd_x, crd_y = gpsmap.convert_geographic_to_screen(top_left) @@ -97,18 +97,22 @@ class SelectionLayer(GObject.GObject, osmgpsmap.MapLayer): # be sure when can select a region in all case. if ( crd_x < crd_x2 ): if ( crd_y < crd_y2 ): - drawable.draw_rectangle(ggc, False, crd_x, crd_y, - crd_x2 - crd_x, crd_y2 - crd_y) + #drawable.draw_rectangle(ggc, False, crd_x, crd_y, + # crd_x2 - crd_x, crd_y2 - crd_y) + ctx.rectangle(crd_x, crd_y, crd_x2 - crd_x, crd_y2 - crd_y) else: - drawable.draw_rectangle(ggc, False, crd_x, crd_y2, - crd_x2 - crd_x, crd_y - crd_y2) + #drawable.draw_rectangle(ggc, False, crd_x, crd_y2, + # crd_x2 - crd_x, crd_y - crd_y2) + ctx.rectangle(crd_x, crd_y2, crd_x2 - crd_x, crd_y - crd_y2) else: if ( crd_y < crd_y2 ): - drawable.draw_rectangle(ggc, False, crd_x2, crd_y, - crd_x - crd_x2, crd_y2 - crd_y) + #drawable.draw_rectangle(ggc, False, crd_x2, crd_y, + # crd_x - crd_x2, crd_y2 - crd_y) + ctx.rectangle(crd_x2, crd_y, crd_x - crd_x2, crd_y2 - crd_y) else: - drawable.draw_rectangle(ggc, False, crd_x2, crd_y2, - crd_x - crd_x2, crd_y - crd_y2) + #drawable.draw_rectangle(ggc, False, crd_x2, crd_y2, + # crd_x - crd_x2, crd_y - crd_y2) + ctx.rectangle(crd_x2, crd_y2, crd_x - crd_x2, crd_y - crd_y2) def do_render(self, gpsmap): """ diff --git a/src/plugins/view/geomoves.py b/src/plugins/view/geomoves.py index 0e458b66a..a992c0aba 100644 --- a/src/plugins/view/geomoves.py +++ b/src/plugins/view/geomoves.py @@ -521,30 +521,26 @@ class GeoMoves(GeoGraphyView): death = high_date new_list.append([level, plxp, birth, death]) pidx = 0; - color = Gdk.color_parse(color) - #try: - # color = Gdk.color_parse(color) - #except: - # # We have already a Gdk.color - # pass + if isinstance(color, str) : + color = Gdk.color_parse(color) for (level, plxp, birth, death) in sorted(new_list, key=operator.itemgetter(0,2)): if index == int(self._config.get("geography.maximum_generations")): break if level == index: pidx += 1 - self._createmap_for_one_person(plxp, str(color)) - color.red = (float(color.red - (index)*3000)) + self._createmap_for_one_person(plxp, color) + color.red = (float(color.red - (index)*3000)%65535) if ( index % 2 ): - color.green = float((color.green + (index)*3000)) + color.green = float((color.green + (index)*3000)%65535) else: - color.blue = float((color.blue + (index)*3000)) - self._createmap_for_one_person(person, str(color)) + color.blue = float((color.blue + (index)*3000)%65535) + self._createmap_for_one_person(person, color) if index < int(self._config.get("geography.maximum_generations")): time_to_wait = int(self._config.get("geography.generation_interval")) self._create_markers() # process next generation in a few milliseconds GObject.timeout_add(time_to_wait, self.animate_moves, - index+1, person, str(color)) + index+1, person, color) else: self.started = False return False