-
Notifications
You must be signed in to change notification settings - Fork 0
/
__VisualizingEngine.py
352 lines (220 loc) · 12.3 KB
/
__VisualizingEngine.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
from __future__ import annotations
from sys import argv
from time import sleep
from PyQt5.QtCore import QRect, QRunnable, pyqtSlot, QThreadPool, QIODevice
from PyQt5.QtGui import QColor, QPen, QBrush, QPixmap, QPainter, QFont
from PyQt5.QtWidgets import QLabel, QApplication, QMainWindow, QAction, \
QToolBar, QPushButton, QColorDialog
from typing import Union, Callable, TypeVar
from collections import deque
Virtual_Array = TypeVar("Virtual_Array")
app: QApplication = QApplication(argv) # PyQt application that runs the code.
app.setStyle("Fusion") # Style of the tool bar, and its widgets.
Color = Union[tuple[int, int, int], QColor] # Color type used by the classes.
_resolution: list[int, int] = [app.primaryScreen().size().width(),
app.primaryScreen().size().height()] # Resolution of the display in use.
_UPDATER: bool = False # Update counter used by ticker.
def ticker(func: Callable) -> None:
"""Updates the display every other modification"""
global _UPDATER
func() if _UPDATER else ...
_UPDATER = not _UPDATER
class Bar(QRect):
"""Bar class that is responsible for handling comparisons between elements of the array"""
def __init__(self, value: int, index: int, width: int, is_separated: bool, is_positive: bool) -> None:
"""Initializes a QRect object using the appropriate coordinates and dimensions, based on the provided values.
Gives the bar its actual value, and draws it on the canvas, WITHOUT UPDATING THE SCREEN.
"""
if not value:
value += 1
super().__init__(index * (width + is_separated), _resolution[1] - int(value), width - is_separated, int(value)) \
if is_positive else \
super().__init__(index * (width + is_separated),
_resolution[1] // 2 - (int(value) if 0 < int(value) else 0), width - is_separated,
abs(value))
self.val: int = value
def __eq__(self, other: Union[Bar, int]) -> bool:
"""Equality operation between a bar and another bar, or an int"""
return self.val == (other.val if isinstance(other, Bar) else other)
def __ne__(self, other: Union[Bar, int]) -> bool:
"""Non-equality operation between a bar and another bar, or an int"""
return self.val != (other.val if isinstance(other, Bar) else other)
def __lt__(self, other: Union[Bar, int]) -> bool:
"""Less-than operation between a bar and another bar, or an int"""
return self.val < (other.val if isinstance(other, Bar) else other)
def __gt__(self, other: Union[Bar, int]) -> bool:
"""Greater-than operation between a bar and another bar, or an int"""
return self.val > (other.val if isinstance(other, Bar) else other)
def __le__(self, other: Union[Bar, int]) -> bool:
"""Less-than-or-equal operation between a bar and another bar, or an int"""
return self.val <= (other.val if isinstance(other, Bar) else other)
def __ge__(self, other: Union[Bar, int]) -> bool:
"""Greater-than-or-equal operation between a bar and another bar, or an int"""
return self.val >= (other.val if isinstance(other, Bar) else other)
def __add__(self, other: Union[Bar, int]) -> int:
"""Addition operation between a bar and another bar, or an int"""
return self.val + (other.val if isinstance(other, Bar) else other)
def __radd__(self, other: int) -> int:
"""Addition operation between an int and a bar"""
return self + other
def __sub__(self, other: Union[Bar, int]) -> int:
"""Subtraction operation between a bar and another bar, or an int"""
return self.val - (other.val if isinstance(other, Bar) else other)
def __rsub__(self, other: int) -> int:
"""Subtraction operation between an int and a bar"""
return other - self.val
def __mul__(self, other: Union[int, Bar]) -> int:
"""Multiplication operation between a bar and another bar, or an int"""
return self.val * (other.val if isinstance(other, Bar) else other)
def __rmul__(self, other: int) -> int:
"""Multiplication operation between an int and a bar"""
return other * self.val
def __truediv__(self, other: Union[Bar, int]) -> float:
"""Division operation between a bar and another bar, or an int"""
return self.val / (other.val if isinstance(other, Bar) else other)
def __rtruediv__(self, other: int) -> float:
"""Division operation between an int and a bar"""
return other / self.val
def __floordiv__(self, other: Union[Bar, int]) -> int:
"""Integer-Division operation between a bar and another bar, or an int"""
return self.val // (other.val if isinstance(other, Bar) else other)
def __rfloordiv__(self, other: int) -> int:
"""Integer-Division operation between an int and a bar"""
return other // self.val
def __mod__(self, other: Union[Bar, int]) -> int:
"""Modulo operation between a bar and another bar, or an int"""
return self.val % (other.val if isinstance(other, Bar) else other)
def __rmod__(self, other: int) -> int:
"""Modulo operation between an int and a bar"""
return other % self.val
def __pow__(self, power: Union[Bar, int], modulo=None) -> int:
"""Exponentiation operation between a bar and another bar, or an int"""
return self.val ** (power.val if isinstance(power, Bar) else power)
def __rpow__(self, other: int) -> int:
"""Exponentiation operation between an int and a bar"""
return other ** self.val
def __abs__(self) -> int:
"""Returns the absolute value of self.val"""
return abs(self.val)
def __floor__(self) -> int:
"""Returns the floor of self.val"""
return self.val.__floor__()
def __hash__(self) -> int:
"""Hashes self.val"""
return hash(self.val)
def __bool__(self) -> bool:
"""Bool of self.val"""
return bool(self.val)
def __int__(self) -> int:
"""Returns self.val as an integer"""
return self.val
def __float__(self) -> float:
"""Returns self.val as a float"""
return float(self.val)
def __str__(self) -> str:
"""Returns the string representation of QRect"""
return super().__str__()
class ThreadedTask(QRunnable):
"""Class responsible for multi-threading of tasks"""
def __init__(self, parent, func: Callable, *args, **kwargs) -> None:
super(ThreadedTask, self).__init__()
self.func: Callable = func
self.args: list = list(args) # Args to be given to the threaded function.
self.kwargs: dict = kwargs # Keyword Args to be given to the threaded function.
self.parent: Virtual_Array = parent # VisualArray that is used to determine when to pause the thread.
@pyqtSlot()
def run(self) -> None:
"""Function that is called once the thread is started by the thread pool of the visual array"""
while not self.parent.running:
app.processEvents()
"""This try-statement is used to make sure that cache collisions are inconsequential.
These collisions occur once the sorting algorithm is paused and then the size is changed."""
try:
self.func(*self.args, **self.kwargs)
except IndexError:
return
class MainWindow(QMainWindow):
"""This class is responsible for handling the visualization of the different elements of the visual array"""
def __init__(self, color: Color, bar_color: Color, access_color: Color,
selection_color: Color, bar_width: int, is_separated: bool,
only_positive: bool, no_toolBar: bool, delay: float) -> None:
super().__init__()
if not no_toolBar: # Does not have any effect in the app.
self.tool_bar: QToolBar = QToolBar("")
self.tool_bar.setFixedSize(_resolution[0], 21)
self.tool_bar.setMovable(False)
self.tool_bar.setFont(QFont("Monaco", 10, QFont.Monospace))
_resolution[1] -= 20
"""Other attributes"""
self.label: QLabel = QLabel() # Label of the MainWindow objects
self.canvas: QPixmap = QPixmap(_resolution[0], _resolution[1]) # Canvas used to display the elements on.
self.thread_pool: QThreadPool = QThreadPool() # Thread pool responsible for handling threads.
self.label.setPixmap(self.canvas)
self.setCentralWidget(self.label) # Centers the canvas.
self.bar_width: int = bar_width # Bar width of the canvas.
self.is_separated: bool = is_separated # Indicates whether the bars are separated or not.
self.only_positive: bool = only_positive # Indicates whether the array contains only positive values.
self.cache: deque = deque() # Cache used to store access bars.
self.delay: float = delay # Artificial delay to slow down the sorting.
"""Color attributes"""
self.color_wheel: QColorDialog = QColorDialog()
self.color: QColor = QColor(*color)
self.access_color: QColor = QColor(*access_color)
self.selection_color: QColor = QColor(*selection_color)
self.bar_color: QColor = QColor(*bar_color)
def bar_at(self, value: Union[Bar, int], index: int) -> Bar:
"""Erases the contents at the given index.
Creates and loads a bar object of value := value at the given index"""
return self.load_bar(Bar(int(value), index, self.bar_width, self.is_separated, self.only_positive))
def nu_fill(self, q_rect: Union[QRect, list, tuple], color: QColor) -> None:
"""Fills the provided QRect object with the given color"""
if isinstance(q_rect, list) or isinstance(q_rect, tuple):
q_rect: QRect = QRect(*q_rect)
painter: QPainter = QPainter(self.label.pixmap())
painter.setPen(QPen(color))
painter.setBrush(QBrush(color))
painter.drawRect(q_rect)
painter.end()
def load_background(self) -> None:
"""Loads the background"""
self.nu_fill((0, 0, _resolution[0], _resolution[1]), self.color)
def load_bar(self, bar: Bar) -> Bar:
"""Draws the given bar"""
self.nu_fill((bar.x(), 0, self.bar_width, _resolution[1]), self.color)
self.nu_fill(bar, self.bar_color)
return bar
def process_cache(self) -> None:
self.bar_at(self._bar_objects[(i := self.cache.popleft())], i)
def clear_cache(self) -> None:
[self.bar_at(self._bar_objects[(i := self.cache.pop())], i) for _ in range(len(self.cache))]
self.update()
def select(self, index: int) -> None:
"""Marks the index by selection color"""
self.nu_fill(self._bar_objects[index], self.selection_color)
def _real_threaded_fill(self, index: int, color: Color) -> None:
"""Actual function that changes the color of a bar"""
self.nu_fill(self._bar_objects[index], color)
sleep(self.delay)
self.cache.append(index)
def threaded_fill(self, index: int, color: Color) -> None:
"""Creates a thread to change the color of the bar to self.access color for self.delay seconds"""
self.thread_pool.tryStart(ThreadedTask(self, self._real_threaded_fill, index, color))
def threaded_update(self) -> None:
"""Creates a thread that updates the screen"""
self.thread_pool.tryStart(ThreadedTask(self, self.update))
def update(self) -> None:
"""Updates the screen"""
super().update()
app.processEvents()
def create_button(self, function: Callable, tip: str, title: str,
separate: bool = True, push: bool = False,
size: tuple[int, int] = None) -> Union[QPushButton, QAction]:
"""Creates a button with given parameters."""
button: Union[QPushButton, QAction] = QPushButton(title) if push else QAction(title, self)
button.pressed.connect(function) if push else button.triggered.connect(function)
button.setToolTip(tip)
self.tool_bar.addSeparator() if separate else ...
button.setFixedSize(len(title) + 2, 13) if push else ...
button.setFixedSize(*size) if size else ...
self.tool_bar.addAction(button) if isinstance(button, QAction) else self.tool_bar.addWidget(button)
return button