-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
209 lines (154 loc) · 6.92 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import logging
import inspect
from PySide6.QtGui import QAction,QDrag
from PySide6.QtWidgets import QApplication, QMainWindow, QDockWidget, QWidget, QVBoxLayout, QLabel, QMenuBar,QLineEdit,QPushButton,QGridLayout,QHBoxLayout,QComboBox,QTreeWidget,QTreeWidgetItem,QGraphicsView
from PySide6.QtCore import Qt,QMimeData
import qtpynodeeditor as nodeeditor
from qtpynodeeditor.type_converter import TypeConverter
from qtpynodeeditor import ConnectionGeometry, StyleCollection
from Data.INPUT.input_number import NumberSourceDataModel
from Data.OUTPUT.output_number import NumberDisplayModel
from Data.Type_Convertors import DecimalData,IntegerData
from Main_Operands import Basic_Arithmetic
from Handlers import FlowView
def integer_to_decimal_converter(data: IntegerData) -> DecimalData:
return DecimalData(float(data.number))
def decimal_to_integer_converter(data: DecimalData) -> IntegerData:
return IntegerData(int(data.number))
def get_classes_from_module(module):
class_list = []
for name in dir(module): # Get all attributes in the module
model_class = getattr(module, name, None) # Retrieve the attribute
# Check if it's a class and belongs to the given module (not inherited)
if inspect.isclass(model_class) and model_class.__module__ == module.__name__:
class_list.append(name)
return class_list
def operation_doc(main_window, custom_flow_view):
# Retrieve the list of module names from Main_Operands.Basic_Arithmetic
modules = get_classes_from_module(Basic_Arithmetic)
# Create a dockable menu
dock_widget = QDockWidget("Operation Menu", main_window)
dock_widget.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
# Add content to the dockable menu
dock_content = QWidget()
dock_layout = QVBoxLayout(dock_content)
# Add a tree structure with the main category and clickable subsections
tree_widget = QTreeWidget(dock_content)
tree_widget.setHeaderHidden(True)
main_category_item = QTreeWidgetItem(tree_widget)
main_category_item.setText(0, "Basic Arithmetic")
main_category_item.setFlags(Qt.ItemIsEnabled) # Disable selection & prevent collapsing
# Subsections (modules) as clickable items
for module_name in modules:
module_item = QTreeWidgetItem(main_category_item)
module_item.setText(0, module_name)
tree_widget.expandAll()
# Connect item click to adding nodes
tree_widget.itemClicked.connect(lambda item, _: handle_item_click(item, custom_flow_view))
dock_layout.addWidget(tree_widget)
dock_widget.setWidget(dock_content)
main_window.addDockWidget(Qt.LeftDockWidgetArea, dock_widget)
return dock_widget
def handle_item_click(item, custom_flow_view):
if item.parent() is not None:
class_name = item.text(0) # Get the clicked class name
try:
# Dynamically retrieve the class from the module
model_class = getattr(Basic_Arithmetic, class_name, None)
if model_class is not None:
# Instantiate the class
node_model = model_class()
# Create a node in the scene
node = custom_flow_view.scene.create_node(node_model)
# Position node in the center of the scene
center_point = custom_flow_view.mapToScene(custom_flow_view.viewport().rect().center())
node.graphics_object.setPos(center_point)
else:
print(f"Class '{class_name}' not found in Main_Operands.Basic_Arithmetic")
except ModuleNotFoundError:
print("Error: Module not found. Check if it exists and is correctly imported.")
style_json = '''
{
"FlowViewStyle": {
"BackgroundColor": [255, 255, 240],
"FineGridColor": [245, 245, 230],
"CoarseGridColor": [235, 235, 220]
},
"NodeStyle": {
"NormalBoundaryColor": "darkgray",
"SelectedBoundaryColor": "deepskyblue",
"GradientColor0": "mintcream",
"GradientColor1": "mintcream",
"GradientColor2": "mintcream",
"GradientColor3": "mintcream",
"ShadowColor": [200, 200, 200],
"FontColor": [10, 10, 10],
"FontColorFaded": [100, 100, 100],
"ConnectionPointColor": "white",
"PenWidth": 2.0,
"HoveredPenWidth": 2.5,
"ConnectionPointDiameter": 10.0,
"Opacity": 1.0
},
"ConnectionStyle": {
"ConstructionColor": "gray",
"NormalColor": "black",
"SelectedColor": "gray",
"SelectedHaloColor": "deepskyblue",
"HoveredColor": "deepskyblue",
"LineWidth": 3.0,
"ConstructionLineWidth": 2.0,
"PointDiameter": 10.0,
"UseDataDefinedColors": false
}
}
'''
def main(app):
registry = nodeeditor.DataModelRegistry()
# Register models with style directly
operations = (Basic_Arithmetic.AdditionModel, Basic_Arithmetic.DivisionModel, Basic_Arithmetic.ModuloModel,
Basic_Arithmetic.MultiplicationModel,Basic_Arithmetic.SubtractionModel,)
input_data = (NumberSourceDataModel,)
output_data = (NumberDisplayModel,)
for operations in operations:
registry.register_model(operations, category='Operations', style=None)
for input_data in input_data:
registry.register_model(input_data, category='Input', style=None)
for output_data in output_data:
registry.register_model(output_data, category='OutPut', style=None)
# Register type converters
dec_converter = TypeConverter(DecimalData.data_type, IntegerData.data_type, decimal_to_integer_converter)
int_converter = TypeConverter(IntegerData.data_type, DecimalData.data_type, integer_to_decimal_converter)
registry.register_type_converter(DecimalData.data_type, IntegerData.data_type, dec_converter)
registry.register_type_converter(IntegerData.data_type, DecimalData.data_type, int_converter)
# Create scene and view
scene = nodeeditor.FlowScene(registry=registry)
view = FlowView.CustomFlowView(scene)
view.setDragMode(QGraphicsView.RubberBandDrag)
# Create the main window
Main_Window = QMainWindow()
Main_Window.setWindowTitle("Control Design Systems")
# Set the view as the central widget
Main_Window.setCentralWidget(view)
dock_widget = operation_doc(Main_Window,view)
# Create a menu bar with an action to toggle the docked widget
menubar = Main_Window.menuBar()
view_menu = menubar.addMenu("View")
toggle_action = QAction("Toggle Dock", Main_Window)
toggle_action.triggered.connect(lambda: toggle_dock(dock_widget))
view_menu.addAction(toggle_action)
# Show the main window
view.setWindowTitle("view")
Main_Window.showMaximized()
Main_Window.show()
return scene, view, Main_Window
def toggle_dock(dock_widget):
if dock_widget.isVisible():
dock_widget.hide()
else:
dock_widget.show()
if __name__ == '__main__':
logging.basicConfig(level='DEBUG')
app = QApplication([])
scene, view, main_window = main(app)
app.exec_()