-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.py
213 lines (184 loc) · 8.46 KB
/
Window.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
210
211
212
213
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QFileDialog, QPushButton, QVBoxLayout, QWidget, QColorDialog, QSlider, QLabel
from PyQt5.QtGui import QPainter, QPen, QBrush
from PyQt5.QtCore import Qt, QPoint
class Canvas(QWidget):
def __init__(self):
super().__init__()
self.start_point = None #Start point of the line
self.end_point = None #End point of the line
self.current_points = [] #For freeform drawing
self.lines = [] #To store drawn lines (each as a tuple of start and end points)
self.pen_color = Qt.black #Default color
self.pen_thickness = 3 #Default pen thickness
self.drawing_mode = 'line' #Defaukt drawing mode (line, freeform, rectangle, circle)
def paintEvent(self, event):
#Method called whenever the widget needs to be updated
painter = QPainter(self)
#Draw all stored shapes/lines
for shape in self.lines:
if shape['type'] == 'line':
self.draw_line(painter, shape)
elif shape['type'] == 'freeform':
self.draw_freeform(painter, shape)
elif shape['type'] == 'rectangle':
self.draw_rectangle(painter, shape)
elif shape['type'] == 'circle':
self.draw_circle(painter, shape)
#Draw the shape being actively drawn
if self.start_point and self.end_point:
if self.drawing_mode == 'line':
self.draw_line(painter, {'start': self.start_point, 'end': self.end_point, 'color': self.pen_color, 'thickness': self.pen_thickness })
elif self.drawing_mode == 'rectangle':
self.draw_rectangle(painter, {'start': self.start_point, 'end': self.end_point, 'color': self.pen_color, 'thickness': self.pen_thickness })
elif self.drawing_mode == 'circle':
self.draw_circle(painter, {'start': self.start_point, 'end': self.end_point, 'color': self.pen_color, 'thickness': self.pen_thickness })
#If the user is currently drawing a shape, we draw it as well
if self.current_points:
self.draw_freeform(painter, {'points': self.current_points, 'color': self.pen_color, 'thickness': self.pen_thickness})
def draw_line(self, painter, shape):
pen = QPen(shape['color'], shape['thickness'], Qt.SolidLine)
painter.setPen(pen)
painter.drawLine(shape['start'], shape['end'])
def draw_freeform(self, painter, shape):
pen = Qpen(shape['color'], shape['thcikness'], Qt.SolidLine)
painter.setPen(pen)
for i in range(1, len(shape['points'])):
painter.draw_line(shape['points'][i - 1], shape['points'][i])
def draw_rectangle(self, painter, shape)
pen = QPen(shape['color'], shape['thickness'], Qt.SolidLine)
painter.setPen(pen)
rect = self.get_rect(shape['start'], shape['end'])
painter.drawRect(rect)
def draw_circle(self, painter, shape):
pen = QPen(shape['color'], shape['thickness'], Qt.SolidLine)
painter.setPen(pen)
center, radius = self.get_circle(shape['start'], shape['end'])
painter.drawEllipse(center, radius, radius)
def get_rect(self, start, end):
return QRect(start, end)
def get_circle(self, start, end):
center = start
radius = int((end.x() - start.x()) ** 2 + (end.y() - start.y() ** 2) ** 0,5)
retun(center, radius)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.start_point = event.pos()
if self.drawing_mode == 'freedom'
self.current_points.append(self.start_point)
def mouseMoveEvent(self, event):
if self.start_point:
if self.drawing_mode == 'freeform':
self.current_points.append(event.pos())
else:
self.end_point = event.pos()
self.update
def mouseReleaseEvent(self, event):
#When the mouse button is released, finalize the line and store it
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
if self.drawing_mode == 'freeform':
self.lines.append({'type': 'freeform', 'points': self.current_points, 'color': self.pen_color, 'thickness': self.pen_thickness})
self.current_points = []
elif self.drawing_mode == 'line':
self.lines.append({'type': 'line', 'start': self.start_point, 'end': self.end_point, 'color': self.pen_color, 'thickness': self.pen_thickness})
elif self.drawing_mode == 'rectangle':
self.lines.append({'type': 'rectangle', 'start': self.start_point, 'end': self.end_point, 'color': self.pen_color, 'thickness': self.pen_thickness})
elif self.drawing_mode == 'circle':
self.lines.append({'type': 'circle', 'start': self.start_point, 'end': self.end_point, 'color': self.pen_color, 'thickness': self.pen_thickness})
self.start_point = None
self.end_point = None
self.update() #Updating the canvas
#Function to set the pen color
def set_pen_color(self, color):
self.pen_color = color
def set_pen_thickness(self, thickness):
print(f"Pen thickness set to : {thickness}") #Debugging : Print to check
self.pen_thickness = thickness
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Biomimetic Design Tool")
self.setGeometry(100, 100, 800, 600)
self.create_menu_bar()
# creating the main layout and adding a button
self.init_ui()
def create_menu_bar(self):
menu_bar = self.menuBar()
file_menu = menu_bar.addMenu("File")
open_action = QAction("Open", self)
open_action.triggered.connect(self.open_file_dialog)
save_action = QAction("Save", self)
save_action.triggered.connect(self.save_file_dialog)
exit_action = QAction("Exit", self)
exit_action.triggered.connect(self.close)
#Add Actions To Menu
file_menu.addAction(open_action)
file_menu.addAction(save_action)
file_menu.addAction(exit_action)
def init_ui(self):
#Creating the canvas widget
self.canvas = Canvas()
#Create a button for starting new design
new_design_button = QPushButton("Start New Design", self)
new_design_button.clicked.connect(self.start_new_design)
#Creating a button for clearing the canvas
clear_button = QPushButton("Clear Canvas", self)
clear_button.clicked.connect(self.clear_canvas)
#Creatin a button for the color picker
color_button = QPushButton("Pick Color", self)
color_button.clicked.connect(self.pick_color)
#Creating a slider for adjusting the line thickness
thickness_slider = QSlider(Qt.Horizontal, self)
thickness_slider.setRange(1, 10) #Line thickness between 1 and 10
thickness_slider.setValue(3) #Default value
thickness_slider.valueChanged.connect(self.change_thickness)
#Creating a label for the slider
slider_label = QLabel("Line thickness", self)
#Create a layout and add the button, the canvas
layout = QVBoxLayout()
layout.addWidget(new_design_button)
layout.addWidget(self.canvas)
layout.addWidget(clear_button)
layout.addWidget(color_button)
layout.addWidget(thickness_slider)
layout.addWidget(slider_label)
#create a central widget, set the layout, and assign it to the main window
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def start_new_design(self):
# function that get called when the button is clicked
print("Starting a new Design")
def pick_color(self):
#Open a color dialog and let the user select the color
color = QColorDialog.getColor()
if color.isValid():
self.canvas.set_pen_color(color) #Set the chosen color to the canvas
def change_thickness(self, value):
#Change the pen thickness when the slider is adjusted
print(f"Changing line thickness to {value}")
self.canvas.set_pen_thickness(value)
def open_file_dialog(self):
options = QFileDialog.Options()
file_name , _ = QFileDialog.getOpenFileName(self, "Open Design File", "", "All Files (*);;Design Files (*.des)", options=options)
if file_name:
print(f"Opening file: {file_name}")
def save_file_dialog(self):
options = QFileDialog.Options()
file_name , _ = QFileDialog.getSaveFileName(self, "Save Design File", "", "All Files (*);;Design Files (* .des)", options=options)
if file_name:
print(f"Saving file: {file_name}")
def clear_canvas(self):
#Function to clear canvas
self.canvas.lines = []
self.canvas.update()
print("Clearing Canvas")
#Initialization of the application
app = QApplication(sys.argv)
#Creating instance of the main window
window = MainWindow()
#Show the window
window.show()
#Execute the application loop
sys.exit(app.exec_())