-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
380 lines (307 loc) · 13.3 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class CircuitElement(QGraphicsPixmapItem):
def __init__(self, icon):
self.icon = icon
super().__init__(icon)
self.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
self.setPos(0, 0)
self.state = False
def evaluate(self):
pass
def mouseReleaseEvent(self, event):
self.scene().clearSelection()
self.setZValue(1)
self.setCursor(Qt.OpenHandCursor)
super().mouseReleaseEvent(event)
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
if self.isSelected():
for sel_item in self.scene().selectedItems():
sel_item.update()
class AndGate(CircuitElement):
def __init__(self):
super().__init__(QPixmap("res/and.png").scaled(70, 40))
self.inputs = [QPointF(0, 5), QPointF(0, 25)]
self.output = QPointF(60, 15)
self.input_wires = [False, False]
def evaluate(self):
self.state = all(self.input_wires)
def paint(self, painter, option, widget):
# Draw the gate's shape and label
painter.drawPixmap(self.icon.rect(), self.icon)
painter.setFont(QFont("times", 12))
painter.drawText(self.icon.rect(), Qt.AlignCenter, "&")
# Draw the gate's input and output terminals
for point in self.inputs:
painter.drawRect(int(point.x()), int(point.y()), 10, 10)
painter.drawRect(int(self.output.x()), int(self.output.y()), 10, 10)
def mousePressEvent(self, event):
print("AND gate clicked")
if event.button() == Qt.LeftButton:
self.setZValue(2)
self.setCursor(Qt.ClosedHandCursor)
super().mousePressEvent(event)
class OrGate(CircuitElement):
def __init__(self):
super().__init__(QPixmap("res/or.png").scaled(70, 40))
self.inputs = [QPointF(0, 5), QPointF(0, 25)]
self.output = QPointF(60, 15)
self.input_wires = [False, False]
def evaluate(self):
self.state = any(self.input_wires)
def paint(self, painter, option, widget):
# Draw the gate's shape and label
painter.drawPixmap(self.icon.rect(), self.icon)
painter.setFont(QFont("times", 12))
painter.drawText(self.icon.rect(), Qt.AlignCenter, "≥1")
# Draw the gate's input and output terminals
for point in self.inputs:
painter.drawRect(int(point.x()), int(point.y()), 10, 10)
painter.drawRect(int(self.output.x()), int(self.output.y()), 10, 10)
def mousePressEvent(self, event):
print("OR gate clicked")
if event.button() == Qt.LeftButton:
self.setZValue(2)
self.setCursor(Qt.ClosedHandCursor)
super().mousePressEvent(event)
class NotGate(CircuitElement):
def __init__(self):
super().__init__(QPixmap("res/not.png").scaled(70, 40))
self.input = QPointF(0, 15)
self.output = QPointF(60, 15)
self.input_wire = False
def evaluate(self):
self.state = not self.input_wire
def paint(self, painter, option, widget):
# Draw the gate's shape and label
painter.drawPixmap(self.icon.rect(), self.icon)
painter.setFont(QFont("times", 14))
painter.drawText(self.icon.rect(), Qt.AlignCenter, "~ ")
# Draw the gate's input and output terminals
painter.drawRect(int(self.input.x()), int(self.input.y()), 10, 10)
painter.drawRect(int(self.output.x()), int(self.output.y()), 10, 10)
def mousePressEvent(self, event):
print("NOT gate clicked")
if event.button() == Qt.LeftButton:
self.setZValue(2)
self.setCursor(Qt.ClosedHandCursor)
super().mousePressEvent(event)
class XorGate(CircuitElement):
def __init__(self):
super().__init__(QPixmap("res/xor.png").scaled(70, 40))
self.inputs = [QPointF(0, 5), QPointF(0, 25)]
self.output = QPointF(60, 15)
self.input_wires = [False, False]
def evaluate(self):
self.state = self.input_wires[0] != self.input_wires[1]
def paint(self, painter, option, widget):
# Draw the gate's shape and label
painter.drawPixmap(self.icon.rect(), self.icon)
painter.setFont(QFont("times", 12))
painter.drawText(self.icon.rect(), Qt.AlignCenter, "=1")
# Draw the gate's input and output terminals
for point in self.inputs:
painter.drawRect(int(point.x()), int(point.y()), 10, 10)
painter.drawRect(int(self.output.x()), int(self.output.y()), 10, 10)
def mousePressEvent(self, event):
print("XOR gate clicked")
if event.button() == Qt.LeftButton:
self.setZValue(2)
self.setCursor(Qt.ClosedHandCursor)
super().mousePressEvent(event)
class Wire(QGraphicsLineItem):
signal_updated = pyqtSignal()
def __init__(self, input_gate, output_gate, parent=None):
super().__init__(parent=parent)
self.input_gate = input_gate
self.output_gate = output_gate
self.input_gate.add_output(self)
self.output_gate.add_input(self)
self.setFlags(QGraphicsLineItem.ItemIsSelectable |
QGraphicsLineItem.ItemIsMovable)
self.setAcceptHoverEvents(True)
self.setZValue(-1)
self.pen = QPen(QColor(0, 0, 0), 3, Qt.SolidLine)
self.update_line()
def update_line(self):
self.setLine(QLineF(self.mapFromItem(self.input_gate, 0, 0),
self.mapFromItem(self.output_gate, 0, 0)))
def update_output(self):
output_signal = self.input_gate.get_output()
self.output_gate.set_input(output_signal)
self.signal_updated.emit()
def hoverEnterEvent(self, event):
self.setPen(QPen(QColor(255, 0, 0), 3, Qt.SolidLine))
self.update()
def hoverLeaveEvent(self, event):
self.setPen(QPen(QColor(0, 0, 0), 3, Qt.SolidLine))
self.update()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
for item in self.collidingItems():
if isinstance(item, Gate):
if item is not self.input_gate and item is not self.output_gate:
if self.output_gate is item or self.input_gate is item:
self.scene().removeItem(self)
return
if isinstance(self.input_gate, InputGate):
self.input_gate.set_output(None)
elif isinstance(self.output_gate, OutputGate):
self.output_gate.set_input(None)
self.output_gate.remove_input(self)
self.input_gate.remove_output(self)
self.output_gate = item
self.output_gate.add_input(self)
self.update_line()
self.update_output()
return
self.scene().removeItem(self)
class InputPin(QGraphicsItem):
size = 30
def __init__(self, parent=None):
super().__init__(parent)
self.rect = QRectF(-self.size/2, -self.size/2, self.size, self.size)
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setFlag(QGraphicsItem.ItemSendsGeometryChanges)
self.setFlag(QGraphicsItem.ItemSendsScenePositionChanges, True)
self.setAcceptHoverEvents(True)
self.output = QPointF(5, -5)
self._state = False
def boundingRect(self):
return self.rect
def paint(self, painter, option, widget=None):
painter.setBrush(QBrush(QColor("white")))
painter.drawEllipse(self.boundingRect())
if self._state:
painter.setBrush(QBrush(QColor("green")))
else:
painter.setBrush(QBrush(QColor("red")))
painter.drawEllipse(self.boundingRect().adjusted(5, 5, -5, -5))
painter.setPen(QPen(QColor("black")))
painter.drawText(self.boundingRect().adjusted(0, 5, 0, 0),
Qt.AlignHCenter, "{}".format(1 if self._state else 0))
painter.drawRect(int(self.output.x()), int(self.output.y()), 10, 10)
def mouseReleaseEvent(self, event):
self.scene().clearSelection()
self.setZValue(1)
super().mouseReleaseEvent(event)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.setZValue(2)
self._state = not self._state
self.update()
self.scene().update()
print("INPUT pin clicked - {}".format("ON" if self._state else "OFF"))
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
if self.isSelected():
for sel_item in self.scene().selectedItems():
sel_item.update()
def value(self):
return self._state
class OutputPin(QGraphicsItem):
size = 30
def __init__(self, parent=None):
super().__init__(parent)
self.rect = QRectF(-self.size/2, -self.size/2, self.size, self.size)
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setFlag(QGraphicsItem.ItemSendsGeometryChanges)
self.setFlag(QGraphicsItem.ItemSendsScenePositionChanges, True)
self.setAcceptHoverEvents(True)
self.input = QPointF(-15, -5)
self._state = False
def boundingRect(self):
return self.rect
def paint(self, painter, option, widget=None):
painter.setBrush(QBrush(QColor("white")))
painter.drawRect(self.boundingRect())
if self._state:
painter.setBrush(QBrush(QColor("green")))
else:
painter.setBrush(QBrush(QColor("red")))
painter.drawRect(self.boundingRect().adjusted(5, 5, -5, -5))
painter.setPen(QPen(QColor("black")))
painter.drawText(self.boundingRect().adjusted(0, 5, 0, 0),
Qt.AlignHCenter, "{}".format(1 if self._state else 0))
painter.drawRect(int(self.input.x()), int(self.input.y()), 10, 10)
def mousePressEvent(self, event):
self.setZValue(2)
print("OUTPUT pin clicked - {}".format("ON" if self._state else "OFF"))
def mouseReleaseEvent(self, event):
self.scene().clearSelection()
self.setZValue(1)
super().mouseReleaseEvent(event)
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
if self.isSelected():
for sel_item in self.scene().selectedItems():
sel_item.update()
def value(self):
return self._state
def setValue(self, state):
self._state = state
self.update()
self.scene().update()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# Create the main window and set its properties
self.setWindowTitle("Digital Logic Simulator")
self.setGeometry(100, 100, 800, 600)
# Create a toolbar and add it to the main window
toolbar = QToolBar("Tools", self)
self.addToolBar(toolbar)
# Create buttons for the toolbar
and_button = toolbar.addAction("AND")
or_button = toolbar.addAction("OR")
not_button = toolbar.addAction("NOT")
xor_button = toolbar.addAction("XOR")
inp_button = toolbar.addAction("INPUT")
out_button = toolbar.addAction("OUTPUT")
# Connect the buttons to the slot that handles their click events
and_button.triggered.connect(self.andClicked)
or_button.triggered.connect(self.orClicked)
not_button.triggered.connect(self.notClicked)
xor_button.triggered.connect(self.xorClicked)
inp_button.triggered.connect(self.inpPinClicked)
out_button.triggered.connect(self.outPinClicked)
# Create a central widget and set it as the main widget for the main window
self.central_widget = QGraphicsView()
self.setCentralWidget(self.central_widget)
# Create a graphics scene and set it as the scene for the graphics view
self.scene = QGraphicsScene()
self.central_widget.setScene(self.scene)
def andClicked(self):
# Create a new AndGate object and add it to the graphics scene
gate = AndGate()
self.scene.addItem(gate)
def orClicked(self):
# Create a new OrGate object and add it to the graphics scene
gate = OrGate()
self.scene.addItem(gate)
def notClicked(self):
# Create a new NotGate object and add it to the graphics scene
gate = NotGate()
self.scene.addItem(gate)
def xorClicked(self):
# Create a new XorGate object and add it to the graphics scene
gate = XorGate()
self.scene.addItem(gate)
def inpPinClicked(self):
# Create a new InputPin object and add it to the graphics scene
pin = InputPin()
self.scene.addItem(pin)
def outPinClicked(self):
# Create a new OutputPin object and add it to the graphics scene
pin = OutputPin()
self.scene.addItem(pin)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())