Skip to content

Commit

Permalink
ImagePlot: selection options in a mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
markotoplak committed Sep 24, 2024
1 parent be77297 commit f7c0090
Showing 1 changed file with 56 additions and 49 deletions.
105 changes: 56 additions & 49 deletions orangecontrib/spectroscopy/widgets/owhyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,53 @@ def add_zoom_actions(self, menu):
menu.addAction(zoom_fit)


class ImageSelectionMixin:

def add_selection_actions(self, menu):

select_square = QAction(
"Select (square)", self, triggered=self.plot.vb.set_mode_select_square,
)
select_square.setShortcuts([Qt.Key_S])
select_square.setShortcutContext(Qt.WidgetWithChildrenShortcut)
self.addAction(select_square)
if menu:
menu.addAction(select_square)

select_polygon = QAction(
"Select (polygon)", self, triggered=self.plot.vb.set_mode_select_polygon,
)
select_polygon.setShortcuts([Qt.Key_P])
select_polygon.setShortcutContext(Qt.WidgetWithChildrenShortcut)
self.addAction(select_polygon)
if menu:
menu.addAction(select_polygon)

def select_square(self, p1, p2):
""" Select elements within a square drawn by the user.
A selection needs to contain whole pixels """
x1, y1 = p1.x(), p1.y()
x2, y2 = p2.x(), p2.y()
polygon = [QPointF(x1, y1), QPointF(x2, y1), QPointF(x2, y2), QPointF(x1, y2), QPointF(x1, y1)]
self.select_polygon(polygon)

def select_polygon(self, polygon):
""" Select by a polygon which has to contain whole pixels. """
if self.data and self.lsx and self.lsy:
polygon = [(p.x(), p.y()) for p in polygon]
# a polygon should contain all pixel
shiftx = _shift(self.lsx)
shifty = _shift(self.lsy)
points_edges = [self.data_points + [[shiftx, shifty]],
self.data_points + [[-shiftx, shifty]],
self.data_points + [[shiftx, -shifty]],
self.data_points + [[-shiftx, -shifty]]]
inp = in_polygon(points_edges[0], polygon)
for p in points_edges[1:]:
inp *= in_polygon(p, polygon)
self.make_selection(inp)


class ImageColorLegend(GraphicsWidget):

def __init__(self):
Expand Down Expand Up @@ -647,9 +694,10 @@ def legend_items(self):
return []


class BasicImagePlot(QWidget, OWComponent, SelectionGroupMixin, AxesSettingsMixin,
ImageColorSettingMixin, ImageRGBSettingMixin,
ImageZoomMixin, ConcurrentMixin):
class BasicImagePlot(QWidget, OWComponent, SelectionGroupMixin,
AxesSettingsMixin, ImageSelectionMixin,
ImageColorSettingMixin, ImageRGBSettingMixin,
ImageZoomMixin, ConcurrentMixin):

gamma = Setting(0)

Expand All @@ -661,6 +709,7 @@ def __init__(self, parent):
OWComponent.__init__(self, parent)
SelectionGroupMixin.__init__(self)
AxesSettingsMixin.__init__(self)
ImageSelectionMixin.__init__(self)
ImageColorSettingMixin.__init__(self)
ImageZoomMixin.__init__(self)
ConcurrentMixin.__init__(self)
Expand Down Expand Up @@ -718,35 +767,17 @@ def __init__(self, parent):
# prepare interface according to the new context
self.parent.contextAboutToBeOpened.connect(lambda x: self.init_interface_data(x[0]))

actions = []

self.add_zoom_actions(view_menu)

select_square = QAction(
"Select (square)", self, triggered=self.plot.vb.set_mode_select_square,
)
select_square.setShortcuts([Qt.Key_S])
select_square.setShortcutContext(Qt.WidgetWithChildrenShortcut)
actions.append(select_square)

select_polygon = QAction(
"Select (polygon)", self, triggered=self.plot.vb.set_mode_select_polygon,
)
select_polygon.setShortcuts([Qt.Key_P])
select_polygon.setShortcutContext(Qt.WidgetWithChildrenShortcut)
actions.append(select_polygon)
self.add_selection_actions(view_menu)

if self.saving_enabled:
save_graph = QAction(
"Save graph", self, triggered=self.save_graph,
)
save_graph.setShortcuts([QKeySequence(Qt.ControlModifier | Qt.Key_I)])
actions.append(save_graph)

view_menu.addActions(actions)
self.addActions(actions)
for a in actions:
a.setShortcutVisibleInContextMenu(True)
self.addAction(save_graph)
view_menu.addAction(save_graph)
save_graph.setShortcutVisibleInContextMenu(True)

choose_xy = QWidgetAction(self)
box = gui.vBox(self)
Expand Down Expand Up @@ -844,30 +875,6 @@ def make_selection(self, selected):
self.prepare_settings_for_saving()
self.selection_changed.emit()

def select_square(self, p1, p2):
""" Select elements within a square drawn by the user.
A selection needs to contain whole pixels """
x1, y1 = p1.x(), p1.y()
x2, y2 = p2.x(), p2.y()
polygon = [QPointF(x1, y1), QPointF(x2, y1), QPointF(x2, y2), QPointF(x1, y2), QPointF(x1, y1)]
self.select_polygon(polygon)

def select_polygon(self, polygon):
""" Select by a polygon which has to contain whole pixels. """
if self.data and self.lsx and self.lsy:
polygon = [(p.x(), p.y()) for p in polygon]
# a polygon should contain all pixel
shiftx = _shift(self.lsx)
shifty = _shift(self.lsy)
points_edges = [self.data_points + [[shiftx, shifty]],
self.data_points + [[-shiftx, shifty]],
self.data_points + [[shiftx, -shifty]],
self.data_points + [[-shiftx, -shifty]]]
inp = in_polygon(points_edges[0], polygon)
for p in points_edges[1:]:
inp *= in_polygon(p, polygon)
self.make_selection(inp)

def _points_at_pos(self, pos):
if self.data and self.lsx and self.lsy:
x, y = pos.x(), pos.y()
Expand Down

0 comments on commit f7c0090

Please sign in to comment.