Skip to content

Commit c055877

Browse files
committed
ProfileExtractionDialog: simplify shape_to_param/param_to_shape methods
1 parent 274c40d commit c055877

File tree

1 file changed

+35
-63
lines changed

1 file changed

+35
-63
lines changed

cdl/core/gui/profiledialog.py

Lines changed: 35 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def update_cs_panels_state(self):
163163
def accept(self) -> None:
164164
"""Accept"""
165165
if self.shape is not None:
166-
self.shape_to_param(self.shape, self.param)
166+
self.shape_to_param()
167167
super().accept()
168168

169169
def reset_to_initial(self) -> None:
@@ -190,79 +190,51 @@ def __tool_job_finished(self):
190190
self.shape = self.cstool.get_last_final_shape()
191191
assert self.shape is not None
192192
self.shape.set_readonly(True)
193-
self.shape_to_param(self.shape, self.param)
193+
self.shape_to_param()
194194
self.update_cs_panels_state()
195195

196-
@staticmethod
197-
def shape_to_param(
198-
shape: AnnotatedPoint | AnnotatedRectangle,
199-
param: cdl.param.LineProfileParam | cdl.param.AverageProfileParam,
200-
) -> None:
201-
"""Shape to param
202-
203-
Args:
204-
shape: Annotated shape
205-
param: Profile parameters
206-
"""
207-
if isinstance(shape, AnnotatedPoint):
208-
assert isinstance(param, cdl.param.LineProfileParam)
209-
x, y = shape.get_pos()
210-
param.row, param.col = int(np.round(y)), int(np.round(x))
211-
elif isinstance(shape, AnnotatedSegment):
212-
assert isinstance(param, cdl.param.SegmentProfileParam)
213-
x1, y1, x2, y2 = shape.get_rect()
214-
param.row1, param.row2 = sorted([int(np.round(y1)), int(np.round(y2))])
215-
param.col1, param.col2 = sorted([int(np.round(x1)), int(np.round(x2))])
196+
def shape_to_param(self) -> None:
197+
"""Shape to param"""
198+
p = self.param
199+
if isinstance(self.shape, AnnotatedPoint):
200+
assert isinstance(p, cdl.param.LineProfileParam)
201+
x, y = self.shape.get_pos()
202+
p.row, p.col = int(np.round(y)), int(np.round(x))
203+
elif isinstance(self.shape, AnnotatedSegment):
204+
assert isinstance(p, cdl.param.SegmentProfileParam)
205+
x1, y1, x2, y2 = self.shape.get_rect()
206+
p.row1, p.row2 = sorted([int(np.round(y1)), int(np.round(y2))])
207+
p.col1, p.col2 = sorted([int(np.round(x1)), int(np.round(x2))])
216208
else:
217-
assert isinstance(param, cdl.param.AverageProfileParam)
218-
x1, y1, x2, y2 = shape.get_rect()
219-
param.row1, param.row2 = sorted([int(np.round(y1)), int(np.round(y2))])
220-
param.col1, param.col2 = sorted([int(np.round(x1)), int(np.round(x2))])
209+
assert isinstance(p, cdl.param.AverageProfileParam)
210+
x1, y1, x2, y2 = self.shape.get_rect()
211+
p.row1, p.row2 = sorted([int(np.round(y1)), int(np.round(y2))])
212+
p.col1, p.col2 = sorted([int(np.round(x1)), int(np.round(x2))])
221213

222-
@staticmethod
223-
def param_to_shape(
224-
param: cdl.param.LineProfileParam
225-
| cdl.param.AverageProfileParam
226-
| cdl.param.SegmentProfileParam,
227-
shape: AnnotatedPoint | AnnotatedRectangle | AnnotatedSegment,
228-
) -> None:
229-
"""Param to shape
230-
231-
Args:
232-
param: Profile parameters
233-
shape: Annotated shape
234-
"""
235-
if isinstance(shape, AnnotatedPoint):
236-
assert isinstance(param, cdl.param.LineProfileParam)
237-
shape.set_pos(param.col, param.row)
238-
elif isinstance(shape, AnnotatedSegment):
239-
assert isinstance(param, cdl.param.SegmentProfileParam)
240-
shape.set_rect(param.col1, param.row1, param.col2, param.row2)
214+
def param_to_shape(self) -> None:
215+
"""Param to shape"""
216+
p = self.param
217+
if isinstance(self.shape, AnnotatedPoint):
218+
assert isinstance(p, cdl.param.LineProfileParam)
219+
self.shape.set_pos(p.col, p.row)
220+
elif isinstance(self.shape, AnnotatedSegment):
221+
assert isinstance(p, cdl.param.SegmentProfileParam)
222+
self.shape.set_rect(p.col1, p.row1, p.col2, p.row2)
241223
else:
242-
assert isinstance(param, cdl.param.AverageProfileParam)
243-
shape.set_rect(param.col1, param.row1, param.col2, param.row2)
224+
assert isinstance(p, cdl.param.AverageProfileParam)
225+
self.shape.set_rect(p.col1, p.row1, p.col2, p.row2)
244226

245227
def edit_values(self) -> None:
246228
"""Edit values"""
247-
p = self.param
248-
self.shape_to_param(self.shape, p)
249-
if p.edit(parent=self, apply=self.apply_callback):
250-
self.param_to_shape(p, self.shape)
229+
self.shape_to_param()
230+
if self.param.edit(parent=self, apply=lambda _param: self.apply_callback()):
231+
self.param_to_shape()
251232
self.update_cs_panels_state()
252233
self.get_plot().replot()
253234

254-
def apply_callback(
255-
self,
256-
param: cdl.param.LineProfileParam
257-
| cdl.param.AverageProfileParam
258-
| cdl.param.SegmentProfileParam,
259-
) -> None:
260-
"""Apply callback
261-
262-
Args:
263-
param: Profile parameters
264-
"""
265-
self.param_to_shape(param, self.shape)
235+
def apply_callback(self) -> None:
236+
"""Apply callback"""
237+
self.param_to_shape()
266238
self.update_cs_panels_state()
267239
self.get_plot().replot()
268240

0 commit comments

Comments
 (0)