creatumlibre.ui.mode.ui_input_mode

 1from enum import Enum
 2
 3from PyQt6.QtCore import QEvent, Qt
 4
 5
 6class UiMode:
 7    """
 8    Handles UI interaction modes like selecting regions, point clouds, etc.
 9    """
10
11    def __init__(self, name: str, init_key=None, key_action=None):
12        self.name = name
13        self.mode = InputMode.IDLE  # Use Enum for mode state
14        self.init_key = init_key  # Store required key for mode activation
15        self.key_action = key_action  # Store required key for action execution
16
17    def set_mode(self, mode):
18        """Set the UI mode."""
19        self.mode = mode
20        print(f"Active mode: {self.mode}")
21
22    def get_mode(self):
23        """Return the current UI mode."""
24        return self.mode
25
26    def key_interpretation(self):
27        """Interpret key presses based on the active mode."""
28        if self.mode == InputMode.IDLE:
29            return None
30        if self.mode == InputMode.SELECT_REGION:
31            return (
32                self.init_key or "Control"
33            )  #  Default to Control unless explicitly set
34        if self.mode == InputMode.SELECT_POINT_CLOUD:
35            return self.init_key or "Shift"  #  Default to Shift for point clouds
36        return None  #  Future modes can extend this logic
37
38    def reset_mode(self):
39        """Reset the mode to IDLE."""
40        self.mode = InputMode.IDLE
41
42    def key_action_handler(self, event):
43        """Handle key actions based on the current mode."""
44        if event.key() == Qt.Key_Escape:
45            self.reset_mode()
46
47        elif self.mode in (InputMode.SELECT_REGION, InputMode.MOVE_OBJECTS):
48            if event.mouseButton() == Qt.MouseButton.LeftButton:
49                if event.type() == QEvent.Type.MouseButtonPress:
50                    self.set_mode(MOUSE_ACTION.START)
51                elif event.type() == QEvent.Type.MouseMove:
52                    self.set_mode(MOUSE_ACTION.DRAG)
53                elif event.type() == QEvent.Type.MouseButtonRelease:
54                    self.set_mode(MOUSE_ACTION.STOP)
55        elif self.mode == InputMode.SELECT_POINT_CLOUD and event.key() == self.init_key:
56            self.set_mode(InputMode.IDLE)
57        elif self.key_action and event.key() == self.key_action:
58            # Execute the action associated with the key
59            print(f"Executing action for key: {self.key_action}")
60            # Here you would call the actual function that handles the action
61
62
63class InputMode(Enum):
64    IDLE = 0
65    SELECT_REGION = 1
66    SELECT_POINT_CLOUD = 2
67    MOVE_OBJECTS = 3
68
69
70class MOUSE_ACTION(Enum):
71    IDLE = 0  # no mode
72    START = 1  # mouse down
73    DRAG = 2  # mouse move
74    STOP = 3  # mouse up
75
76
77class TransformMode(Enum):
78    NONE = 0
79    SCALE = 1
80    ROTATE = 2
81    PERSPECTIVE = 3
82    TRANSLATE = 4
class UiMode:
 7class UiMode:
 8    """
 9    Handles UI interaction modes like selecting regions, point clouds, etc.
10    """
11
12    def __init__(self, name: str, init_key=None, key_action=None):
13        self.name = name
14        self.mode = InputMode.IDLE  # Use Enum for mode state
15        self.init_key = init_key  # Store required key for mode activation
16        self.key_action = key_action  # Store required key for action execution
17
18    def set_mode(self, mode):
19        """Set the UI mode."""
20        self.mode = mode
21        print(f"Active mode: {self.mode}")
22
23    def get_mode(self):
24        """Return the current UI mode."""
25        return self.mode
26
27    def key_interpretation(self):
28        """Interpret key presses based on the active mode."""
29        if self.mode == InputMode.IDLE:
30            return None
31        if self.mode == InputMode.SELECT_REGION:
32            return (
33                self.init_key or "Control"
34            )  #  Default to Control unless explicitly set
35        if self.mode == InputMode.SELECT_POINT_CLOUD:
36            return self.init_key or "Shift"  #  Default to Shift for point clouds
37        return None  #  Future modes can extend this logic
38
39    def reset_mode(self):
40        """Reset the mode to IDLE."""
41        self.mode = InputMode.IDLE
42
43    def key_action_handler(self, event):
44        """Handle key actions based on the current mode."""
45        if event.key() == Qt.Key_Escape:
46            self.reset_mode()
47
48        elif self.mode in (InputMode.SELECT_REGION, InputMode.MOVE_OBJECTS):
49            if event.mouseButton() == Qt.MouseButton.LeftButton:
50                if event.type() == QEvent.Type.MouseButtonPress:
51                    self.set_mode(MOUSE_ACTION.START)
52                elif event.type() == QEvent.Type.MouseMove:
53                    self.set_mode(MOUSE_ACTION.DRAG)
54                elif event.type() == QEvent.Type.MouseButtonRelease:
55                    self.set_mode(MOUSE_ACTION.STOP)
56        elif self.mode == InputMode.SELECT_POINT_CLOUD and event.key() == self.init_key:
57            self.set_mode(InputMode.IDLE)
58        elif self.key_action and event.key() == self.key_action:
59            # Execute the action associated with the key
60            print(f"Executing action for key: {self.key_action}")
61            # Here you would call the actual function that handles the action

Handles UI interaction modes like selecting regions, point clouds, etc.

UiMode(name: str, init_key=None, key_action=None)
12    def __init__(self, name: str, init_key=None, key_action=None):
13        self.name = name
14        self.mode = InputMode.IDLE  # Use Enum for mode state
15        self.init_key = init_key  # Store required key for mode activation
16        self.key_action = key_action  # Store required key for action execution
name
mode
init_key
key_action
def set_mode(self, mode):
18    def set_mode(self, mode):
19        """Set the UI mode."""
20        self.mode = mode
21        print(f"Active mode: {self.mode}")

Set the UI mode.

def get_mode(self):
23    def get_mode(self):
24        """Return the current UI mode."""
25        return self.mode

Return the current UI mode.

def key_interpretation(self):
27    def key_interpretation(self):
28        """Interpret key presses based on the active mode."""
29        if self.mode == InputMode.IDLE:
30            return None
31        if self.mode == InputMode.SELECT_REGION:
32            return (
33                self.init_key or "Control"
34            )  #  Default to Control unless explicitly set
35        if self.mode == InputMode.SELECT_POINT_CLOUD:
36            return self.init_key or "Shift"  #  Default to Shift for point clouds
37        return None  #  Future modes can extend this logic

Interpret key presses based on the active mode.

def reset_mode(self):
39    def reset_mode(self):
40        """Reset the mode to IDLE."""
41        self.mode = InputMode.IDLE

Reset the mode to IDLE.

def key_action_handler(self, event):
43    def key_action_handler(self, event):
44        """Handle key actions based on the current mode."""
45        if event.key() == Qt.Key_Escape:
46            self.reset_mode()
47
48        elif self.mode in (InputMode.SELECT_REGION, InputMode.MOVE_OBJECTS):
49            if event.mouseButton() == Qt.MouseButton.LeftButton:
50                if event.type() == QEvent.Type.MouseButtonPress:
51                    self.set_mode(MOUSE_ACTION.START)
52                elif event.type() == QEvent.Type.MouseMove:
53                    self.set_mode(MOUSE_ACTION.DRAG)
54                elif event.type() == QEvent.Type.MouseButtonRelease:
55                    self.set_mode(MOUSE_ACTION.STOP)
56        elif self.mode == InputMode.SELECT_POINT_CLOUD and event.key() == self.init_key:
57            self.set_mode(InputMode.IDLE)
58        elif self.key_action and event.key() == self.key_action:
59            # Execute the action associated with the key
60            print(f"Executing action for key: {self.key_action}")
61            # Here you would call the actual function that handles the action

Handle key actions based on the current mode.

class InputMode(enum.Enum):
64class InputMode(Enum):
65    IDLE = 0
66    SELECT_REGION = 1
67    SELECT_POINT_CLOUD = 2
68    MOVE_OBJECTS = 3
IDLE = <InputMode.IDLE: 0>
SELECT_REGION = <InputMode.SELECT_REGION: 1>
SELECT_POINT_CLOUD = <InputMode.SELECT_POINT_CLOUD: 2>
MOVE_OBJECTS = <InputMode.MOVE_OBJECTS: 3>
class MOUSE_ACTION(enum.Enum):
71class MOUSE_ACTION(Enum):
72    IDLE = 0  # no mode
73    START = 1  # mouse down
74    DRAG = 2  # mouse move
75    STOP = 3  # mouse up
IDLE = <MOUSE_ACTION.IDLE: 0>
START = <MOUSE_ACTION.START: 1>
DRAG = <MOUSE_ACTION.DRAG: 2>
STOP = <MOUSE_ACTION.STOP: 3>
class TransformMode(enum.Enum):
78class TransformMode(Enum):
79    NONE = 0
80    SCALE = 1
81    ROTATE = 2
82    PERSPECTIVE = 3
83    TRANSLATE = 4
NONE = <TransformMode.NONE: 0>
SCALE = <TransformMode.SCALE: 1>
ROTATE = <TransformMode.ROTATE: 2>
PERSPECTIVE = <TransformMode.PERSPECTIVE: 3>
TRANSLATE = <TransformMode.TRANSLATE: 4>