Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated Gdk.Color by Gdk.RGBA #1764

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions gramps/gui/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,9 @@ def add_color(self, grid, label, index, constant, config=None, col=0):
hexval = colors[scheme]
else:
hexval = colors
color = Gdk.color_parse(hexval)
entry = Gtk.ColorButton(color=color)
rgba = Gdk.RGBA()
rgba.parse(hexval)
entry = Gtk.ColorButton.new_with_rgba(rgba)
color_hex_label = BasicLabel(hexval)
color_hex_label.set_hexpand(True)
entry.connect("notify::color", self.update_color, constant, color_hex_label)
Expand Down
56 changes: 30 additions & 26 deletions gramps/gui/plug/report/_styleeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def __init__(self, name, style, parent):
for widget_name in ("color", "bgcolor", "line_color", "fill_color"):
color = self.top.get_object(widget_name)
label = self.top.get_object(widget_name + "_code")
color.connect("notify::color", self.color_changed, label)
color.connect("notify::rgba", self.rgba_changed, label)

self.top.get_object("style_name").set_text(name)

Expand Down Expand Up @@ -395,10 +395,10 @@ def draw_graphics(self):
self.top.get_object("line_style").set_active(g.get_line_style())
self.top.get_object("line_width").set_value(g.get_line_width())

self.line_color = rgb2color(g.get_color())
self.top.get_object("line_color").set_color(self.line_color)
self.fill_color = rgb2color(g.get_fill_color())
self.top.get_object("fill_color").set_color(self.fill_color)
Comment on lines -398 to -401
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I couldn't find any use of self.line_color/self.fill_color in this file (e.g., the corollary to this function, save_graphics, does not use them), so I think that assignment was a mistake.

line_color = tuple2rgba(g.get_color())
self.top.get_object("line_color").set_rgba(line_color)
fill_color = tuple2rgba(g.get_fill_color())
self.top.get_object("fill_color").set_rgba(fill_color)

self.top.get_object("shadow").set_active(g.get_shadow())
self.top.get_object("shadow_space").set_value(g.get_shadow_space())
Expand Down Expand Up @@ -497,17 +497,17 @@ def draw_paragraph(self):
self.top.get_object("rborder").set_active(p.get_right_border())
self.top.get_object("bborder").set_active(p.get_bottom_border())

color = rgb2color(font.get_color())
self.top.get_object("color").set_color(color)
bg_color = rgb2color(p.get_background_color())
self.top.get_object("bgcolor").set_color(bg_color)
color = tuple2rgba(font.get_color())
self.top.get_object("color").set_rgba(color)
bg_color = tuple2rgba(p.get_background_color())
self.top.get_object("bgcolor").set_rgba(bg_color)

def color_changed(self, color, name, label):
def rgba_changed(self, color, name, label):
"""
Called to set the color code when a color is changed.
"""
rgb = color2rgb(color.get_color())
label.set_text("#%02X%02X%02X" % color2rgb(color.get_color()))
rgb = rgba2tuple(color.get_rgba())
label.set_text("#%02X%02X%02X" % rgb)

def save(self):
"""
Expand All @@ -529,10 +529,10 @@ def save_graphics(self):
g = self.current_style
g.set_line_style(self.top.get_object("line_style").get_active())
g.set_line_width(self.top.get_object("line_width").get_value())
line_color = self.top.get_object("line_color").get_color()
g.set_color(color2rgb(line_color))
fill_color = self.top.get_object("fill_color").get_color()
g.set_fill_color(color2rgb(fill_color))
line_color = self.top.get_object("line_color").get_rgba()
g.set_color(rgba2tuple(line_color))
fill_color = self.top.get_object("fill_color").get_rgba()
g.set_fill_color(rgba2tuple(fill_color))
shadow = self.top.get_object("shadow").get_active()
shadow_space = self.top.get_object("shadow_space").get_value()
g.set_shadow(shadow, shadow_space)
Expand Down Expand Up @@ -599,10 +599,10 @@ def save_paragraph(self):
p.set_right_border(self.top.get_object("rborder").get_active())
p.set_bottom_border(self.top.get_object("bborder").get_active())

color = self.top.get_object("color").get_color()
font.set_color(color2rgb(color))
bg_color = self.top.get_object("bgcolor").get_color()
p.set_background_color(color2rgb(bg_color))
color = self.top.get_object("color").get_rgba()
font.set_color(rgba2tuple(color))
bg_color = self.top.get_object("bgcolor").get_rgba()
p.set_background_color(rgba2tuple(bg_color))

self.style.add_paragraph_style(self.current_name, self.current_style)

Expand Down Expand Up @@ -635,18 +635,22 @@ def change_display(self, obj):
self.draw()


def rgb2color(rgb):
def tuple2rgba(rgb):
"""
Convert a tuple containing RGB values into a Gdk Color.
Convert a tuple containing 8-bit RGB values into a Gdk.RGBA.
"""
return Gdk.Color(rgb[0] << 8, rgb[1] << 8, rgb[2] << 8)
rgba = Gdk.RGBA()
rgba.red = rgb[0] / 0xFF
rgba.green = rgb[1] / 0xFF
rgba.blue = rgb[2] / 0xFF
return rgba


def color2rgb(color):
def rgba2tuple(rgba):
"""
Convert a Gdk Color into a tuple containing RGB values.
Convert a Gdk.RGBA into a tuple containing 8-bit RGB values.
"""
return (color.red >> 8, color.green >> 8, color.blue >> 8)
return (int(rgba.red * 0xFF), int(rgba.green * 0xFF), int(rgba.blue * 0xFF))


def dummy_callback(obj):
Expand Down
10 changes: 3 additions & 7 deletions gramps/plugins/lib/maps/datelayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,9 @@ def do_draw(self, gpsmap, ctx):
self.font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL
)
ctx.set_font_size(int(self.size))
color = Gdk.color_parse(self.color)
ctx.set_source_rgba(
float(color.red / 65535.0),
float(color.green / 65535.0),
float(color.blue / 65535.0),
0.6,
) # transparency
rgba = Gdk.RGBA()
rgba.parse(self.color)
ctx.set_source_rgba(rgba.red, rgba.green, rgba.blue, 0.6)
coord_x = 10
coord_y = 15 + 2 * int(self.size) # Display the oldest date
ctx.move_to(coord_x, coord_y)
Expand Down
20 changes: 6 additions & 14 deletions gramps/plugins/lib/maps/kmllayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ def do_draw(self, gpsmap, ctx):
"""
Draw all the surfaces and paths
"""
color1 = Gdk.color_parse("red")
color2 = Gdk.color_parse("blue")
color1 = Gdk.RGBA()
color1.parse("red")
color2 = Gdk.RGBA()
color2.parse("blue")
for polygons in self.polygons:
for polygon in polygons:
(dummy_name, ptype, dummy_color, dummy_transparency, points) = polygon
Expand All @@ -126,12 +128,7 @@ def do_draw(self, gpsmap, ctx):
map_points.append((coord_x, coord_y))
first = True
ctx.save()
ctx.set_source_rgba(
float(color2.red / 65535.0),
float(color2.green / 65535.0),
float(color2.blue / 65535.0),
0.3,
) # transparency
ctx.set_source_rgba(color2.red, color2.green, color2.blue, 0.3)
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
ctx.set_line_width(3)
Expand Down Expand Up @@ -162,12 +159,7 @@ def do_draw(self, gpsmap, ctx):
map_points.append((coord_x, coord_y))
first = True
ctx.save()
ctx.set_source_rgba(
float(color1.red / 65535.0),
float(color1.green / 65535.0),
float(color1.blue / 65535.0),
0.5,
) # transparency
ctx.set_source_rgba(color1.red, color1.green, color1.blue, 0.5)
ctx.set_line_width(5)
ctx.set_operator(cairo.OPERATOR_ATOP)
for idx_pt in range(0, len(map_points)):
Expand Down
31 changes: 14 additions & 17 deletions gramps/plugins/lib/maps/lifewaylayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,22 @@ def add_way_ref(self, points, color, radius):
radius is the size of the track.
"""
if isinstance(color, str):
color = Gdk.color_parse(color)
self.lifeways_ref.append((points, color, radius))
rgba = Gdk.RGBA()
rgba.parse(color)
else:
rgba = color
self.lifeways_ref.append((points, rgba, radius))

def add_way(self, points, color):
"""
Add a track or life way.
"""
if isinstance(color, str):
color = Gdk.color_parse(color)
self.lifeways.append((points, color))
rgba = Gdk.RGBA()
rgba.parse(color)
else:
rgba = color
self.lifeways.append((points, rgba))

def do_draw(self, gpsmap, ctx):
"""
Expand All @@ -114,13 +120,8 @@ def do_draw(self, gpsmap, ctx):
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
ctx.set_line_width(3)
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
rgba = lifeway[1]
ctx.set_source_rgba(rgba.red, rgba.green, rgba.blue, 0.1)
rds = float(lifeway[2])
for point in lifeway[0]:
conv_pt1 = osmgpsmap.MapPoint.new_degrees(point[0], point[1])
Expand Down Expand Up @@ -154,12 +155,8 @@ def do_draw(self, gpsmap, ctx):
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 = lifeway[1]
ctx.set_source_rgb(
float(color.red / 65535.0),
float(color.green / 65535.0),
float(color.blue / 65535.0),
)
rgba = lifeway[1]
ctx.set_source_rgb(rgba.red, rgba.green, rgba.blue)
first = True
for idx_pt in range(0, len(map_points)):
if first:
Expand Down
9 changes: 5 additions & 4 deletions gramps/plugins/lib/maps/markerlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,12 @@ def do_button_press(self, gpsmap, gdkeventbutton):
def draw_marker(ctx, x01, y01, size, color):
width = 48.0 * size
height = width / 2
color = Gdk.color_parse(color)
rgba = Gdk.RGBA()
rgba.parse(color)
fill_color = (
color.red / 65535.0,
color.green / 65535.0,
color.blue / 65535.0,
rgba.red,
rgba.green,
rgba.blue,
1.0, # transparency
)
stroke_color = (1.0, 0.0, 0.0, 0.5)
Expand Down
10 changes: 3 additions & 7 deletions gramps/plugins/lib/maps/messagelayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,9 @@ def do_draw(self, gpsmap, ctx):
# font = Pango.FontDescription(font_size)
descr = Pango.font_description_from_string(self.font)
descr.set_size(self.size * Pango.SCALE)
color = Gdk.color_parse(self.color)
ctx.set_source_rgba(
float(color.red / 65535.0),
float(color.green / 65535.0),
float(color.blue / 65535.0),
0.9,
) # transparency
rgba = Gdk.RGBA()
rgba.parse(self.color)
ctx.set_source_rgba(rgba.red, rgba.green, rgba.blue, 0.9)
d_width = gpsmap.get_allocation().width
d_width -= 100
ctx.restore()
Expand Down
29 changes: 14 additions & 15 deletions gramps/plugins/view/geomoves.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def build_tree(self):
self.message_layer.clear_messages()
self.message_layer.set_font_attributes(None, None, None)

def draw(self, menu, marks, color):
def draw(self, menu, marks, rgba):
"""
Create all displacements for one person's events.
"""
Expand All @@ -334,10 +334,10 @@ def draw(self, menu, marks, color):
date = mark[6]
if date != " ":
self.date_layer.add_date(date[0:4])
self.lifeway_layer.add_way(points, color)
self.lifeway_layer.add_way(points, rgba)
return False

def _createmap_for_one_person(self, person, color):
def _createmap_for_one_person(self, person, rgba):
"""
Create all markers for each people's event in the database which has
a lat/lon.
Expand Down Expand Up @@ -472,7 +472,7 @@ def _createmap_for_one_person(self, person, color):
)

sort1 = sorted(self.place_list, key=operator.itemgetter(1, 6))
self.draw(None, sort1, color)
self.draw(None, sort1, rgba)
# merge with the last results
merge_list = self.sort
for the_event in sort1:
Expand Down Expand Up @@ -601,16 +601,17 @@ def _createmap(self, person):
self.message_layer.add_message(
_("All descendants for %s") % _nd.display(person)
)
color = Gdk.color_parse(self._config.get("geography.color_base"))
rgba = Gdk.RGBA()
rgba.parse(self._config.get("geography.color_base"))
GLib.timeout_add(
int(self._config.get("geography.generation_interval")),
self.animate_moves,
0,
person,
color,
rgba,
)

def animate_moves(self, index, person, color):
def animate_moves(self, index, person, rgba):
"""
Animate all moves for one generation.
"""
Expand Down Expand Up @@ -659,28 +660,26 @@ def animate_moves(self, index, person, color):
death = high_date
new_list.append([level, plxp, birth, death])
pidx = 0
if isinstance(color, str):
color = Gdk.color_parse(color)
Comment on lines -662 to -663
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function (animate_moves) is only a callback from a timeout added in _createmap, or a timeout added by itself, so we know the input is a Gdk.RGBA, and this check can be removed.

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, color)
color.red = float(color.red - (index) * 3000) % 65535
self._createmap_for_one_person(plxp, rgba)
rgba.red = (rgba.red - index * 3000 / 65535) % 1
if index % 2:
color.green = float((color.green + (index) * 3000) % 65535)
rgba.green = (rgba.green + index * 3000 / 65535) % 1
else:
color.blue = float((color.blue + (index) * 3000) % 65535)
self._createmap_for_one_person(person, color)
rgba.blue = (rgba.blue + index * 3000 / 65535) % 1
self._createmap_for_one_person(person, rgba)
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
GLib.timeout_add(
int(time_to_wait), self.animate_moves, index + 1, person, color
int(time_to_wait), self.animate_moves, index + 1, person, rgba
)
else:
self.started = False
Expand Down