from PyQt5 import QtCore, QtWidgets
[docs]
class MouseController(object):
def __init__(self, Parent):
"""
Mouse event controller.
Args:
Parent (): The main class of this application
"""
self.parent = Parent
self.__connect_event()
def __connect_event(self):
self.parent.ui.label_Original_Image.mouseDoubleClickEvent = self.mouseDoubleclick_event
self.parent.ui.label_Result_Image.mouseDoubleClickEvent = self.mouseDoubleclick_event
self.parent.ui.label_Original_Image.mousePressEvent = self.mouse_event
self.parent.ui.label_Original_Image.wheelEvent = self.mouse_wheelEvent_ori_label
self.parent.ui.label_Result_Image.wheelEvent = self.mouse_wheelEvent
self.parent.ui.label_Result_Image.mouseReleaseEvent = self.mouse_release_event
self.parent.ui.label_Original_Image.mouseMoveEvent = self.mouseMovedOriImage
[docs]
def mouse_event(self, e):
"""
Specify coordinate from mouse left event to generate anypoint view and recenter image.
Args:
e (): Coordinate point return by pyqt core
Returns:
"""
if self.parent.image is not None:
if e.button() == QtCore.Qt.LeftButton:
pos_x = round(e.x())
pos_y = round(e.y())
ratio_x, ratio_y = self.init_ori_ratio(self.parent.image)
coordinate_X = round(pos_x * ratio_x)
coordinate_Y = round(pos_y * ratio_y)
# print(coordinate_X, coordinate_Y)
if self.parent.normal_view:
pass
if self.parent.anypoint_view:
self.parent.anypoint.alpha, self.parent.anypoint.beta = self.parent.moildev.get_alpha_beta(
coordinate_X, coordinate_Y, self.parent.anypoint.anypoint_mode)
self.parent.anypoint.process_to_anypoint()
elif self.parent.panorama_view:
print("comming soon")
else:
pass
[docs]
def mouseDoubleclick_event(self, e):
"""
Reset to default by mouse event.
Args:
e ():
Returns:
"""
if self.parent.image is not None:
if self.parent.normal_view:
pass
else:
self.parent.anypoint.resetAlphaBeta()
self.parent.anypoint.process_to_anypoint()
[docs]
def mouse_wheelEvent(self, e):
"""
Resize image using mouse wheel event.
"""
if self.parent.image is not None:
modifiers = QtWidgets.QApplication.keyboardModifiers()
if modifiers == QtCore.Qt.ControlModifier:
wheel_counter = e.angleDelta()
if wheel_counter.y() / 120 == -1:
if self.parent.width_result_image == 1000:
pass
else:
self.parent.width_result_image -= 100
if wheel_counter.y() / 120 == 1:
if self.parent.width_result_image == 4000:
pass
else:
self.parent.width_result_image += 100
self.parent.show_to_window()
[docs]
def mouse_wheelEvent_ori_label(self, e):
"""
Resize image using mouse wheel event.
"""
if self.parent.image is not None:
modifiers = QtWidgets.QApplication.keyboardModifiers()
if modifiers == QtCore.Qt.ControlModifier:
wheel_counter = e.angleDelta()
if wheel_counter.y() / 120 == -1:
if self.parent.anypoint_view:
if self.parent.anypoint.zoom_any == 14:
pass
else:
self.parent.anypoint.zoom_any += 1
self.parent.anypoint.anypoint()
if wheel_counter.y() / 120 == 1:
if self.parent.anypoint_view:
if self.parent.anypoint.zoom_any == 2:
pass
else:
self.parent.anypoint.zoom_any -= 1
self.parent.anypoint.anypoint()
[docs]
def mouse_release_event(self, e):
"""
Mouse release event right click to show menu. the menu can select is show maximum, show minimum,
save image, and show info.
Args:
e ():
Returns:
None.
"""
if e.button() == QtCore.Qt.LeftButton:
pass
else:
if self.parent.image is None:
pass
else:
self.menuMouseEvent(e)
[docs]
def mouseMovedOriImage(self, e):
"""
Mouse move event to look in surrounding view in result label image.
Args:
e ():
Returns:
"""
pos_x = round(e.x())
pos_y = round(e.y())
ratio_x, ratio_y = self.init_ori_ratio(self.parent.image)
coordinate_X = round(pos_x * ratio_x)
coordinate_Y = round(pos_y * ratio_y)
if self.parent.normal_view:
pass
if self.parent.anypoint_view:
self.parent.anypoint.alpha, self.parent.anypoint.beta = self.parent.moildev.get_alpha_beta(
coordinate_X, coordinate_Y, self.parent.anypoint.anypoint_mode)
self.parent.anypoint.process_to_anypoint()
elif self.parent.panorama_view:
print("comming soon")
else:
pass
[docs]
def init_ori_ratio(self, image):
"""
Calculate the initial ratio of the image.
Returns:
ratio_x : ratio width between image and ui window.
ratio_y : ratio height between image and ui window.
center : find the center image on window user interface.
"""
h = self.parent.ui.label_Original_Image.height()
w = self.parent.ui.label_Original_Image.width()
height, width = image.shape[:2]
ratio_x = width / w
ratio_y = height / h
return ratio_x, ratio_y
[docs]
def init_result_ratio(self, image):
"""
Calculate the initial ratio of the image.
Returns:
ratio_x : ratio width between image and ui window.
ratio_y : ratio height between image and ui window.
center : find the center image on window user interface.
"""
h = self.parent.ui.label_Result_Image.height()
w = self.parent.ui.label_Result_Image.width()
height, width = image.shape[:2]
ratio_x = width / w
ratio_y = height / h
return ratio_x, ratio_y