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
1=== modified file 'a4lib/app.py'
2--- a4lib/app.py 2010-12-22 22:55:51 +0000
3+++ a4lib/app.py 2010-12-30 20:33:31 +0000
4@@ -273,8 +273,6 @@
5
6 widget_name = gtk.Buildable.get_name(widget)
7 if widget.get_active():
8- if widget_name == 'PathToolbutton':
9- self.player.set_show_path(widget.get_active())
10 for i in toggle_modes.keys():
11 if i != widget_name:
12 obj = self.builder.get_object(i)
13@@ -290,6 +288,8 @@
14 self.player.set_target_image(self._ask_image_dialog())
15 if not any(self.builder.get_object(i).get_active() for i in toggle_modes.keys()):
16 self.player.set_editor_mode()
17+ path_widget = self.builder.get_object('PathToolbutton')
18+ self.player.set_show_path(path_widget.get_active())
19 self.drawing_area.queue_draw()
20
21 def on_drawing_area_expose(self, widget, event):
22
23=== modified file 'a4lib/editor.py'
24--- a4lib/editor.py 2010-12-22 22:55:51 +0000
25+++ a4lib/editor.py 2010-12-30 20:33:31 +0000
26@@ -18,11 +18,24 @@
27 return obj_dict[name]
28
29
30-class Editor(GtkCairoPlayer):
31-
32- def __init__(self, file_name, drawing_area, treeview, force_loading=False):
33- GtkCairoPlayer.__init__(self, file_name, drawing_area, force_loading)
34+def cairo_draw_rect(ctx, x0, y0, x1, y1, r, g, b, alpha):
35+ ctx.set_source_rgb(0, 0xff, 0)
36+ ctx.move_to(x0, y0)
37+ ctx.line_to(x1, y0)
38+ ctx.line_to(x1, y1)
39+ ctx.line_to(x0, y1)
40+ ctx.line_to(x0, y0)
41+ ctx.set_line_width(0.2)
42+ ctx.stroke()
43+ ctx.rectangle(x0, y0, x1 - x0, y1 - y0)
44+ ctx.set_source_rgba(0, 0, 0.5, 0.50)
45+ ctx.fill()
46+
47+
48+class PresentationPathHandler(object):
49+ def __init__(self, presentation, treeview):
50 self.treeview = treeview
51+ self.presentation = presentation
52 self.model = gtk.ListStore(int, str, gtk.gdk.Pixbuf)
53
54 self.treeview.set_model(self.model)
55@@ -33,6 +46,46 @@
56 self.treeview.set_item_width(120)
57 self.treeview.set_size_request(130, -1)
58
59+ def draw_region_on_iconview(self, region, position=0):
60+ pixbuf_height = abs(int(100 * region.height / region.width))
61+ pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, 100,
62+ pixbuf_height)
63+ pix_data = pixbuf.get_pixels_array()
64+ pixbuf_size = (pixbuf.get_width(), pixbuf.get_height())
65+ surface = cairo.ImageSurface.create_for_data(pix_data, cairo.FORMAT_ARGB32,
66+ pixbuf_size[0], pixbuf_size[1], pixbuf.get_rowstride())
67+ cr = cairo.Context(surface)
68+ region.render(cr, self.presentation, pixbuf_size)
69+
70+ self.model.append([position, str(position), pixbuf])
71+
72+ def draw_regions_on_iconview(self, regions):
73+ for n, region in enumerate(regions):
74+ self.draw_region_on_iconview(region, n)
75+
76+ def highlight_path_region(self, context, region):
77+ context.save()
78+ context.translate(region.xc, region.yc)
79+ context.rotate(region.rotate)
80+ context.translate(-region.xc, -region.yc)
81+ x0 = region.xc - region.width / 2
82+ y0 = region.yc - region.height / 2
83+ x1 = region.xc + region.width / 2
84+ y1 = region.yc + region.height / 2
85+ cairo_draw_rect(context, x0, y0, x1, y1, 0, 0, 0.5, 0.50)
86+ context.restore()
87+
88+ def render(self, context):
89+ for i in xrange(0, len(self.presentation.path)):
90+ region = self.presentation.get_path_region(i)
91+ self.highlight_path_region(context, region)
92+
93+
94+class Editor(GtkCairoPlayer):
95+
96+ def __init__(self, file_name, drawing_area, treeview, force_loading=False):
97+ GtkCairoPlayer.__init__(self, file_name, drawing_area, force_loading)
98+ self.path_handler = PresentationPathHandler(self.presentation, treeview)
99 ## used to keep status of mouse actions:
100 ## value currently used: [None,'start','moving']
101 self.action_status = None
102@@ -46,9 +99,8 @@
103 self.current_object = None
104 self.target_filename = None
105
106- for i in xrange(0, len(self.presentation.path)):
107- region = self.presentation.get_path_region(i)
108- self.draw_region_on_iconview(region, i)
109+ ph = self.path_handler
110+ ph.draw_regions_on_iconview(self.presentation.get_path_regions())
111
112 def set_show_path(self, flag=True):
113 self.show_path = flag
114@@ -124,36 +176,16 @@
115 self.action_status = None
116 return True
117
118- def draw_selection_box(self, context, region=None):
119- context.save()
120- if not region:
121- context.set_matrix(cairo.Matrix(1, 0, 0, 1, 0, 0))
122- x0 = self.selection_start_position[0]
123- y0 = self.selection_start_position[1]
124- x1 = self.mouse_position[0]
125- y1 = self.mouse_position[1]
126- else:
127- context.translate(region.xc, region.yc)
128- context.rotate(region.rotate)
129- context.translate(-region.xc, -region.yc)
130- x0 = region.xc - region.width / 2
131- y0 = region.yc - region.height / 2
132- x1 = region.xc + region.width / 2
133- y1 = region.yc + region.height / 2
134-
135- context.set_source_rgb(0, 0xff, 0)
136- context.move_to(x0, y0)
137- context.line_to(x1, y0)
138- context.line_to(x1, y1)
139- context.line_to(x0, y1)
140- context.line_to(x0, y0)
141- context.set_line_width(0.2)
142- context.stroke()
143-
144- context.rectangle(x0, y0, x1 - x0, y1 - y0)
145- context.set_source_rgba(0, 0, 0.5, 0.50)
146- context.fill()
147- context.restore()
148+ def draw_selection_box(self, context):
149+ win_size = self.area.window.get_size()
150+ cr = self.current_region
151+ x0 = self.selection_start_position[0]
152+ y0 = self.selection_start_position[1]
153+ x1 = self.mouse_position[0]
154+ y1 = self.mouse_position[1]
155+ x0, y0 = cr.get_point_from_window_coordinates(win_size, (x0, y0))
156+ x1, y1 = cr.get_point_from_window_coordinates(win_size, (x1, y1))
157+ cairo_draw_rect(context, x0, y0, x1, y1, 0, 0, 0.5, 0.50)
158
159 def render(self, context):
160 # render a transparent box (should indicate the selection)
161@@ -161,47 +193,26 @@
162 # render an icon for rotating the whole image.
163 GtkCairoPlayer.render(self, context)
164 if self.show_path:
165- for i in xrange(0, len(self.presentation.path)):
166- region = self.presentation.get_path_region(i)
167- self.draw_selection_box(context, region)
168+ ## render the path over the presentation
169+ self.path_handler.render(context)
170+ ## render the selection (TODO: not only for path)
171 if self.editor_mode == 'path' and self.action_status:
172 self.draw_selection_box(context)
173
174 def add_treeview_icon(self, rect=None):
175- ## * trasforma rect in Regione
176+ ## convert a rect into a Region()
177 win_size = self.area.window.get_size()
178- scale = self.current_region.get_window_scale_factor(win_size)
179- translate = self.current_region.get_window_fit_vector(win_size)
180- translate = translate[0] / 2.0, translate[1] / 2.0
181- rect2 = (((rect[0][0] - translate[0]) / scale,
182- (rect[0][1] - translate[1]) / scale),
183- ((rect[1][0] - translate[0]) / scale,
184- (rect[1][1] - translate[1]) / scale))
185- xc = (rect2[0][0] + rect2[1][0]) / 2 - self.current_region.width / 2
186- yc = (rect2[0][1] + rect2[1][1]) / 2 - self.current_region.height / 2
187- r = self.current_region.rotate
188- xc1 = math.cos(r) * xc - math.sin(r) * yc
189- yc1 = math.sin(r) * xc + math.cos(r) * yc
190- region = Region(
191- self.current_region.xc + xc1,
192- self.current_region.yc + yc1,
193+
194+ cr = self.current_region
195+ rect2 = (cr.get_point_from_window_coordinates(win_size, rect[0]),
196+ cr.get_point_from_window_coordinates(win_size, rect[1]))
197+ xc = (rect2[0][0] + rect2[1][0]) / 2
198+ yc = (rect2[0][1] + rect2[1][1]) / 2
199+ region = Region(xc, yc,
200 (rect2[1][0] - rect2[0][0]),
201 (rect2[1][1] - rect2[0][1]),
202- self.current_region.rotate)
203+ cr.rotate)
204 ## * metti la regione in lista
205 self.presentation.path.append(region)
206 ## * usa la regione per la pixbuf.
207- self.draw_region_on_iconview(region, len(self.presentation.path) - 1)
208-
209- def draw_region_on_iconview(self, region, position=0):
210- pixbuf_height = abs(int(100 * region.height / region.width))
211- pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, 100,
212- pixbuf_height)
213- pix_data = pixbuf.get_pixels_array()
214- pixbuf_size = (pixbuf.get_width(), pixbuf.get_height())
215- surface = cairo.ImageSurface.create_for_data(pix_data, cairo.FORMAT_ARGB32,
216- pixbuf_size[0], pixbuf_size[1], pixbuf.get_rowstride())
217- cr = cairo.Context(surface)
218- region.render(cr, self.presentation, pixbuf_size)
219-
220- self.model.append([position, str(position), pixbuf])
221+ self.path_handler.draw_region_on_iconview(region, len(self.presentation.path) - 1)
222
223=== modified file 'a4lib/presentation.py'
224--- a4lib/presentation.py 2010-12-13 13:56:31 +0000
225+++ a4lib/presentation.py 2010-12-30 20:33:31 +0000
226@@ -123,13 +123,19 @@
227 out_file = open(self.file_name, 'w')
228 out_file.write(etree.tostring(self.svgtree, pretty_print=True))
229
230+ def _get_region_from_path(self, path_element):
231+ if isinstance(path_element, Region):
232+ return path_element
233+ props = self.get_item_properties_by_id(path_element.id)
234+ p = Region.from_obj_props(props)
235+ return p
236+
237 def get_path_region(self, index):
238 a = self.path[index]
239- if isinstance(a, Region):
240- return a
241- props = self.get_item_properties_by_id(a.id)
242- p = Region.from_obj_props(props)
243- return p
244+ return self._get_region_from_path(a)
245+
246+ def get_path_regions(self):
247+ return [self._get_region_from_path(a) for a in self.path]
248
249 def _handle_transform(self, target_element=None):
250 """Handle SVG transform tags and build a list of tranformations."""
251
252=== modified file 'a4lib/presentation_objects.py'
253--- a4lib/presentation_objects.py 2010-12-22 22:55:51 +0000
254+++ a4lib/presentation_objects.py 2010-12-30 20:33:31 +0000
255@@ -24,6 +24,10 @@
256 self.element.attrib[key] = str(piuprops[key])
257
258 @property
259+ def get_position(self):
260+ return self.element.attrib['x'], self.element.attrib['y']
261+
262+ @property
263 def get_id(self):
264 return self.element.attrib['id']
265
266@@ -38,7 +42,7 @@
267 yc = float(self.element.attrib['y'])
268 xc1 = math.cos(-rotate) * xc - math.sin(-rotate) * yc
269 yc1 = math.sin(-rotate) * xc + math.cos(-rotate) * yc
270- self.element.attrib['transform'] = ("rotate(%s)" % str(math.degrees(rotate))) + \
271+ self.element.attrib['transform'] = ("rotate(%s)" % str(math.degrees(rotate))) + \
272 (" translate(%s,%s)" % (xc1 - xc, yc1 - yc))
273
274 def set_size(self, width, height):

Subscribers

People subscribed via source and target branches