A4

Merge lp:~gaspa/a4/path-refactoring into lp:a4

Proposed by Andrea Gasparini
Status: Merged
Merged at revision: 108
Proposed branch: lp:~gaspa/a4/path-refactoring
Merge into: lp:a4
Diff against target: 274 lines (+100/-79)
4 files modified
a4lib/app.py (+2/-2)
a4lib/editor.py (+82/-71)
a4lib/presentation.py (+11/-5)
a4lib/presentation_objects.py (+5/-1)
To merge this branch: bzr merge lp:~gaspa/a4/path-refactoring
Reviewer Review Type Date Requested Status
A4 Developers Pending
Review via email: mp+44764@code.launchpad.net

Description of the change

some refactoring for path handling, and some calculations of region properties.

To post a comment you must log in.
lp:~gaspa/a4/path-refactoring updated
113. By Andrea Gasparini

show_path does not really hides paths

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'a4lib/app.py'
--- a4lib/app.py 2010-12-22 22:55:51 +0000
+++ a4lib/app.py 2010-12-30 20:33:31 +0000
@@ -273,8 +273,6 @@
273273
274 widget_name = gtk.Buildable.get_name(widget)274 widget_name = gtk.Buildable.get_name(widget)
275 if widget.get_active():275 if widget.get_active():
276 if widget_name == 'PathToolbutton':
277 self.player.set_show_path(widget.get_active())
278 for i in toggle_modes.keys():276 for i in toggle_modes.keys():
279 if i != widget_name:277 if i != widget_name:
280 obj = self.builder.get_object(i)278 obj = self.builder.get_object(i)
@@ -290,6 +288,8 @@
290 self.player.set_target_image(self._ask_image_dialog())288 self.player.set_target_image(self._ask_image_dialog())
291 if not any(self.builder.get_object(i).get_active() for i in toggle_modes.keys()):289 if not any(self.builder.get_object(i).get_active() for i in toggle_modes.keys()):
292 self.player.set_editor_mode()290 self.player.set_editor_mode()
291 path_widget = self.builder.get_object('PathToolbutton')
292 self.player.set_show_path(path_widget.get_active())
293 self.drawing_area.queue_draw()293 self.drawing_area.queue_draw()
294294
295 def on_drawing_area_expose(self, widget, event):295 def on_drawing_area_expose(self, widget, event):
296296
=== modified file 'a4lib/editor.py'
--- a4lib/editor.py 2010-12-22 22:55:51 +0000
+++ a4lib/editor.py 2010-12-30 20:33:31 +0000
@@ -18,11 +18,24 @@
18 return obj_dict[name]18 return obj_dict[name]
1919
2020
21class Editor(GtkCairoPlayer):21def cairo_draw_rect(ctx, x0, y0, x1, y1, r, g, b, alpha):
2222 ctx.set_source_rgb(0, 0xff, 0)
23 def __init__(self, file_name, drawing_area, treeview, force_loading=False):23 ctx.move_to(x0, y0)
24 GtkCairoPlayer.__init__(self, file_name, drawing_area, force_loading)24 ctx.line_to(x1, y0)
25 ctx.line_to(x1, y1)
26 ctx.line_to(x0, y1)
27 ctx.line_to(x0, y0)
28 ctx.set_line_width(0.2)
29 ctx.stroke()
30 ctx.rectangle(x0, y0, x1 - x0, y1 - y0)
31 ctx.set_source_rgba(0, 0, 0.5, 0.50)
32 ctx.fill()
33
34
35class PresentationPathHandler(object):
36 def __init__(self, presentation, treeview):
25 self.treeview = treeview37 self.treeview = treeview
38 self.presentation = presentation
26 self.model = gtk.ListStore(int, str, gtk.gdk.Pixbuf)39 self.model = gtk.ListStore(int, str, gtk.gdk.Pixbuf)
2740
28 self.treeview.set_model(self.model)41 self.treeview.set_model(self.model)
@@ -33,6 +46,46 @@
33 self.treeview.set_item_width(120)46 self.treeview.set_item_width(120)
34 self.treeview.set_size_request(130, -1)47 self.treeview.set_size_request(130, -1)
3548
49 def draw_region_on_iconview(self, region, position=0):
50 pixbuf_height = abs(int(100 * region.height / region.width))
51 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, 100,
52 pixbuf_height)
53 pix_data = pixbuf.get_pixels_array()
54 pixbuf_size = (pixbuf.get_width(), pixbuf.get_height())
55 surface = cairo.ImageSurface.create_for_data(pix_data, cairo.FORMAT_ARGB32,
56 pixbuf_size[0], pixbuf_size[1], pixbuf.get_rowstride())
57 cr = cairo.Context(surface)
58 region.render(cr, self.presentation, pixbuf_size)
59
60 self.model.append([position, str(position), pixbuf])
61
62 def draw_regions_on_iconview(self, regions):
63 for n, region in enumerate(regions):
64 self.draw_region_on_iconview(region, n)
65
66 def highlight_path_region(self, context, region):
67 context.save()
68 context.translate(region.xc, region.yc)
69 context.rotate(region.rotate)
70 context.translate(-region.xc, -region.yc)
71 x0 = region.xc - region.width / 2
72 y0 = region.yc - region.height / 2
73 x1 = region.xc + region.width / 2
74 y1 = region.yc + region.height / 2
75 cairo_draw_rect(context, x0, y0, x1, y1, 0, 0, 0.5, 0.50)
76 context.restore()
77
78 def render(self, context):
79 for i in xrange(0, len(self.presentation.path)):
80 region = self.presentation.get_path_region(i)
81 self.highlight_path_region(context, region)
82
83
84class Editor(GtkCairoPlayer):
85
86 def __init__(self, file_name, drawing_area, treeview, force_loading=False):
87 GtkCairoPlayer.__init__(self, file_name, drawing_area, force_loading)
88 self.path_handler = PresentationPathHandler(self.presentation, treeview)
36 ## used to keep status of mouse actions:89 ## used to keep status of mouse actions:
37 ## value currently used: [None,'start','moving']90 ## value currently used: [None,'start','moving']
38 self.action_status = None91 self.action_status = None
@@ -46,9 +99,8 @@
46 self.current_object = None99 self.current_object = None
47 self.target_filename = None100 self.target_filename = None
48101
49 for i in xrange(0, len(self.presentation.path)):102 ph = self.path_handler
50 region = self.presentation.get_path_region(i)103 ph.draw_regions_on_iconview(self.presentation.get_path_regions())
51 self.draw_region_on_iconview(region, i)
52104
53 def set_show_path(self, flag=True):105 def set_show_path(self, flag=True):
54 self.show_path = flag106 self.show_path = flag
@@ -124,36 +176,16 @@
124 self.action_status = None176 self.action_status = None
125 return True177 return True
126178
127 def draw_selection_box(self, context, region=None):179 def draw_selection_box(self, context):
128 context.save()180 win_size = self.area.window.get_size()
129 if not region:181 cr = self.current_region
130 context.set_matrix(cairo.Matrix(1, 0, 0, 1, 0, 0))182 x0 = self.selection_start_position[0]
131 x0 = self.selection_start_position[0]183 y0 = self.selection_start_position[1]
132 y0 = self.selection_start_position[1]184 x1 = self.mouse_position[0]
133 x1 = self.mouse_position[0]185 y1 = self.mouse_position[1]
134 y1 = self.mouse_position[1]186 x0, y0 = cr.get_point_from_window_coordinates(win_size, (x0, y0))
135 else:187 x1, y1 = cr.get_point_from_window_coordinates(win_size, (x1, y1))
136 context.translate(region.xc, region.yc)188 cairo_draw_rect(context, x0, y0, x1, y1, 0, 0, 0.5, 0.50)
137 context.rotate(region.rotate)
138 context.translate(-region.xc, -region.yc)
139 x0 = region.xc - region.width / 2
140 y0 = region.yc - region.height / 2
141 x1 = region.xc + region.width / 2
142 y1 = region.yc + region.height / 2
143
144 context.set_source_rgb(0, 0xff, 0)
145 context.move_to(x0, y0)
146 context.line_to(x1, y0)
147 context.line_to(x1, y1)
148 context.line_to(x0, y1)
149 context.line_to(x0, y0)
150 context.set_line_width(0.2)
151 context.stroke()
152
153 context.rectangle(x0, y0, x1 - x0, y1 - y0)
154 context.set_source_rgba(0, 0, 0.5, 0.50)
155 context.fill()
156 context.restore()
157189
158 def render(self, context):190 def render(self, context):
159 # render a transparent box (should indicate the selection)191 # render a transparent box (should indicate the selection)
@@ -161,47 +193,26 @@
161 # render an icon for rotating the whole image.193 # render an icon for rotating the whole image.
162 GtkCairoPlayer.render(self, context)194 GtkCairoPlayer.render(self, context)
163 if self.show_path:195 if self.show_path:
164 for i in xrange(0, len(self.presentation.path)):196 ## render the path over the presentation
165 region = self.presentation.get_path_region(i)197 self.path_handler.render(context)
166 self.draw_selection_box(context, region)198 ## render the selection (TODO: not only for path)
167 if self.editor_mode == 'path' and self.action_status:199 if self.editor_mode == 'path' and self.action_status:
168 self.draw_selection_box(context)200 self.draw_selection_box(context)
169201
170 def add_treeview_icon(self, rect=None):202 def add_treeview_icon(self, rect=None):
171 ## * trasforma rect in Regione203 ## convert a rect into a Region()
172 win_size = self.area.window.get_size()204 win_size = self.area.window.get_size()
173 scale = self.current_region.get_window_scale_factor(win_size)205
174 translate = self.current_region.get_window_fit_vector(win_size)206 cr = self.current_region
175 translate = translate[0] / 2.0, translate[1] / 2.0207 rect2 = (cr.get_point_from_window_coordinates(win_size, rect[0]),
176 rect2 = (((rect[0][0] - translate[0]) / scale,208 cr.get_point_from_window_coordinates(win_size, rect[1]))
177 (rect[0][1] - translate[1]) / scale),209 xc = (rect2[0][0] + rect2[1][0]) / 2
178 ((rect[1][0] - translate[0]) / scale,210 yc = (rect2[0][1] + rect2[1][1]) / 2
179 (rect[1][1] - translate[1]) / scale))211 region = Region(xc, yc,
180 xc = (rect2[0][0] + rect2[1][0]) / 2 - self.current_region.width / 2
181 yc = (rect2[0][1] + rect2[1][1]) / 2 - self.current_region.height / 2
182 r = self.current_region.rotate
183 xc1 = math.cos(r) * xc - math.sin(r) * yc
184 yc1 = math.sin(r) * xc + math.cos(r) * yc
185 region = Region(
186 self.current_region.xc + xc1,
187 self.current_region.yc + yc1,
188 (rect2[1][0] - rect2[0][0]),212 (rect2[1][0] - rect2[0][0]),
189 (rect2[1][1] - rect2[0][1]),213 (rect2[1][1] - rect2[0][1]),
190 self.current_region.rotate)214 cr.rotate)
191 ## * metti la regione in lista215 ## * metti la regione in lista
192 self.presentation.path.append(region)216 self.presentation.path.append(region)
193 ## * usa la regione per la pixbuf.217 ## * usa la regione per la pixbuf.
194 self.draw_region_on_iconview(region, len(self.presentation.path) - 1)218 self.path_handler.draw_region_on_iconview(region, len(self.presentation.path) - 1)
195
196 def draw_region_on_iconview(self, region, position=0):
197 pixbuf_height = abs(int(100 * region.height / region.width))
198 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, 100,
199 pixbuf_height)
200 pix_data = pixbuf.get_pixels_array()
201 pixbuf_size = (pixbuf.get_width(), pixbuf.get_height())
202 surface = cairo.ImageSurface.create_for_data(pix_data, cairo.FORMAT_ARGB32,
203 pixbuf_size[0], pixbuf_size[1], pixbuf.get_rowstride())
204 cr = cairo.Context(surface)
205 region.render(cr, self.presentation, pixbuf_size)
206
207 self.model.append([position, str(position), pixbuf])
208219
=== modified file 'a4lib/presentation.py'
--- a4lib/presentation.py 2010-12-13 13:56:31 +0000
+++ a4lib/presentation.py 2010-12-30 20:33:31 +0000
@@ -123,13 +123,19 @@
123 out_file = open(self.file_name, 'w')123 out_file = open(self.file_name, 'w')
124 out_file.write(etree.tostring(self.svgtree, pretty_print=True))124 out_file.write(etree.tostring(self.svgtree, pretty_print=True))
125125
126 def _get_region_from_path(self, path_element):
127 if isinstance(path_element, Region):
128 return path_element
129 props = self.get_item_properties_by_id(path_element.id)
130 p = Region.from_obj_props(props)
131 return p
132
126 def get_path_region(self, index):133 def get_path_region(self, index):
127 a = self.path[index]134 a = self.path[index]
128 if isinstance(a, Region):135 return self._get_region_from_path(a)
129 return a136
130 props = self.get_item_properties_by_id(a.id)137 def get_path_regions(self):
131 p = Region.from_obj_props(props)138 return [self._get_region_from_path(a) for a in self.path]
132 return p
133139
134 def _handle_transform(self, target_element=None):140 def _handle_transform(self, target_element=None):
135 """Handle SVG transform tags and build a list of tranformations."""141 """Handle SVG transform tags and build a list of tranformations."""
136142
=== modified file 'a4lib/presentation_objects.py'
--- a4lib/presentation_objects.py 2010-12-22 22:55:51 +0000
+++ a4lib/presentation_objects.py 2010-12-30 20:33:31 +0000
@@ -24,6 +24,10 @@
24 self.element.attrib[key] = str(piuprops[key])24 self.element.attrib[key] = str(piuprops[key])
2525
26 @property26 @property
27 def get_position(self):
28 return self.element.attrib['x'], self.element.attrib['y']
29
30 @property
27 def get_id(self):31 def get_id(self):
28 return self.element.attrib['id']32 return self.element.attrib['id']
2933
@@ -38,7 +42,7 @@
38 yc = float(self.element.attrib['y'])42 yc = float(self.element.attrib['y'])
39 xc1 = math.cos(-rotate) * xc - math.sin(-rotate) * yc43 xc1 = math.cos(-rotate) * xc - math.sin(-rotate) * yc
40 yc1 = math.sin(-rotate) * xc + math.cos(-rotate) * yc44 yc1 = math.sin(-rotate) * xc + math.cos(-rotate) * yc
41 self.element.attrib['transform'] = ("rotate(%s)" % str(math.degrees(rotate))) + \45 self.element.attrib['transform'] = ("rotate(%s)" % str(math.degrees(rotate))) + \
42 (" translate(%s,%s)" % (xc1 - xc, yc1 - yc))46 (" translate(%s,%s)" % (xc1 - xc, yc1 - yc))
4347
44 def set_size(self, width, height):48 def set_size(self, width, height):

Subscribers

People subscribed via source and target branches