Skip to content

Commit 77ba0a9

Browse files
committed
Add getters and setters
1 parent d16fb3a commit 77ba0a9

File tree

2 files changed

+82
-27
lines changed

2 files changed

+82
-27
lines changed

src/pyqt_loading_button/loading_button.py

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ def __init__(self, parent=None):
1616
self.__running = False
1717

1818
# Animation settings
19-
self.__animation_type = AnimationType.Dots
20-
self.__animation_speed = 1000
21-
self.__animation_width = 20
22-
self.__animation_thickness = 4
19+
self.__animation_type = AnimationType.Circle
20+
self.__animation_speed = 2000
21+
self.__animation_width = 15
22+
self.__animation_stroke_width = 3
2323
self.__animation_color = QColor(0, 0, 0)
2424

2525
# Animation settings (Circle)
@@ -34,6 +34,7 @@ def __init__(self, parent=None):
3434
# Animation settings (Dots)
3535
self.__dots_speed_coefficient = 0.3
3636
self.__dots_single_speed = int(self.__animation_speed * self.__dots_speed_coefficient)
37+
self.__dots_easing_curve = QEasingCurve.Type.InOutSine
3738
self.__dots_offset_1 = 0
3839
self.__dots_offset_2 = 0
3940
self.__dots_offset_3 = 0
@@ -59,35 +60,35 @@ def __init__(self, parent=None):
5960

6061
# Animation timelines (Dots)
6162
self.__timeline_dots_up_1 = QTimeLine(self.__dots_single_speed, self)
62-
self.__timeline_dots_up_1.setFrameRange(0, self.__animation_thickness)
63-
self.__timeline_dots_up_1.setEasingCurve(QEasingCurve.Type.InOutSine)
63+
self.__timeline_dots_up_1.setFrameRange(0, self.__animation_stroke_width)
64+
self.__timeline_dots_up_1.setEasingCurve(self.__dots_easing_curve)
6465
self.__timeline_dots_up_1.frameChanged.connect(self.__handle_timeline_dots_up_1)
6566

6667
self.__timeline_dots_down_1 = QTimeLine(self.__dots_single_speed, self)
67-
self.__timeline_dots_down_1.setFrameRange(self.__animation_thickness, 0)
68-
self.__timeline_dots_down_1.setEasingCurve(QEasingCurve.Type.InOutSine)
68+
self.__timeline_dots_down_1.setFrameRange(self.__animation_stroke_width, 0)
69+
self.__timeline_dots_down_1.setEasingCurve(self.__dots_easing_curve)
6970
self.__timeline_dots_down_1.frameChanged.connect(self.__handle_timeline_dots_down_1)
7071
self.__timeline_dots_up_1.finished.connect(self.__timeline_dots_down_1.start)
7172

7273
self.__timeline_dots_up_2 = QTimeLine(self.__dots_single_speed, self)
73-
self.__timeline_dots_up_2.setFrameRange(0, self.__animation_thickness)
74-
self.__timeline_dots_up_2.setEasingCurve(QEasingCurve.Type.InOutSine)
74+
self.__timeline_dots_up_2.setFrameRange(0, self.__animation_stroke_width)
75+
self.__timeline_dots_up_2.setEasingCurve(self.__dots_easing_curve)
7576
self.__timeline_dots_up_2.frameChanged.connect(self.__handle_timeline_dots_up_2)
7677

7778
self.__timeline_dots_down_2 = QTimeLine(self.__dots_single_speed, self)
78-
self.__timeline_dots_down_2.setFrameRange(self.__animation_thickness, 0)
79-
self.__timeline_dots_down_2.setEasingCurve(QEasingCurve.Type.InOutSine)
79+
self.__timeline_dots_down_2.setFrameRange(self.__animation_stroke_width, 0)
80+
self.__timeline_dots_down_2.setEasingCurve(self.__dots_easing_curve)
8081
self.__timeline_dots_down_2.frameChanged.connect(self.__handle_timeline_dots_down_2)
8182
self.__timeline_dots_up_2.finished.connect(self.__timeline_dots_down_2.start)
8283

8384
self.__timeline_dots_up_3 = QTimeLine(self.__dots_single_speed, self)
84-
self.__timeline_dots_up_3.setFrameRange(0, self.__animation_thickness)
85-
self.__timeline_dots_up_3.setEasingCurve(QEasingCurve.Type.InOutSine)
85+
self.__timeline_dots_up_3.setFrameRange(0, self.__animation_stroke_width)
86+
self.__timeline_dots_up_3.setEasingCurve(self.__dots_easing_curve)
8687
self.__timeline_dots_up_3.frameChanged.connect(self.__handle_timeline_dots_up_3)
8788

8889
self.__timeline_dots_down_3 = QTimeLine(self.__dots_single_speed, self)
89-
self.__timeline_dots_down_3.setFrameRange(self.__animation_thickness, 0)
90-
self.__timeline_dots_down_3.setEasingCurve(QEasingCurve.Type.InOutSine)
90+
self.__timeline_dots_down_3.setFrameRange(self.__animation_stroke_width, 0)
91+
self.__timeline_dots_down_3.setEasingCurve(self.__dots_easing_curve)
9192
self.__timeline_dots_down_3.frameChanged.connect(self.__handle_timeline_dots_down_3)
9293
self.__timeline_dots_down_3.finished.connect(self.__timeline_dots_up_1.start)
9394
self.__timeline_dots_up_3.finished.connect(self.__timeline_dots_down_3.start)
@@ -133,7 +134,8 @@ def __handle_timeline_circle_increase_span(self):
133134
self.update()
134135

135136
def __handle_timeline_circle_increase_span_start(self):
136-
self.__circle_previous_additional_rotation = (self.__circle_previous_additional_rotation + self.__circle_additional_rotation) % 360
137+
self.__circle_previous_additional_rotation = (self.__circle_previous_additional_rotation +
138+
self.__circle_additional_rotation) % 360
137139
self.__timeline_circle_increase_span.start()
138140

139141
def __handle_timeline_dots_up_1(self, value):
@@ -171,12 +173,15 @@ def paintEvent(self, event):
171173

172174
painter = QPainter(self)
173175
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
174-
painter.setPen(QPen(self.__animation_color, self.__animation_thickness, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap))
176+
painter.setPen(QPen(self.__animation_color, self.__animation_stroke_width,
177+
Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap))
175178

176-
diameter = self.__animation_width - self.__animation_thickness
179+
diameter = self.__animation_width - self.__animation_stroke_width
177180
x = math.floor((self.width() - diameter) / 2)
178181
y = math.ceil((self.height() - diameter) / 2)
179-
rotation = (self.__timeline_circle_rotation.currentFrame() - self.__circle_additional_rotation - self.__circle_previous_additional_rotation) % 360 * 16
182+
rotation = (self.__timeline_circle_rotation.currentFrame() -
183+
self.__circle_additional_rotation -
184+
self.__circle_previous_additional_rotation) % 360 * 16
180185
span = self.__circle_span * 16
181186

182187
painter.drawArc(x, y, diameter, diameter, rotation, span)
@@ -187,15 +192,18 @@ def paintEvent(self, event):
187192
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
188193
painter.setBrush(self.__animation_color)
189194

190-
true_width = math.ceil(self.__animation_width / 3) * 2 + self.__animation_thickness
195+
true_width = math.ceil(self.__animation_width / 3) * 2 + self.__animation_stroke_width
191196
x_dot_1 = math.ceil((self.width() - true_width) / 2)
192197
x_dot_2 = x_dot_1 + math.ceil(self.__animation_width / 3)
193198
x_dot_3 = x_dot_2 + math.ceil(self.__animation_width / 3)
194-
y = math.ceil((self.height() - self.__animation_thickness) / 2)
199+
y = math.ceil((self.height() - self.__animation_stroke_width) / 2)
195200

196-
painter.drawEllipse(x_dot_1, y - self.__dots_offset_1, self.__animation_thickness, self.__animation_thickness)
197-
painter.drawEllipse(x_dot_2, y - self.__dots_offset_2, self.__animation_thickness, self.__animation_thickness)
198-
painter.drawEllipse(x_dot_3, y - self.__dots_offset_3, self.__animation_thickness, self.__animation_thickness)
201+
painter.drawEllipse(x_dot_1, y - self.__dots_offset_1,
202+
self.__animation_stroke_width, self.__animation_stroke_width)
203+
painter.drawEllipse(x_dot_2, y - self.__dots_offset_2,
204+
self.__animation_stroke_width, self.__animation_stroke_width)
205+
painter.drawEllipse(x_dot_3, y - self.__dots_offset_3,
206+
self.__animation_stroke_width, self.__animation_stroke_width)
199207

200208
def text(self) -> str:
201209
return self.__text
@@ -205,5 +213,52 @@ def setText(self, text: str) -> None:
205213
if not self.__running:
206214
super().setText(self.__text)
207215

208-
def setAction(self, action):
216+
def setAction(self, action: callable):
209217
self.__action = action
218+
219+
def isRunning(self) -> bool:
220+
return self.__running
221+
222+
def getAnimationType(self) -> AnimationType:
223+
return self.__animation_type
224+
225+
def setAnimationType(self, animation_type: AnimationType):
226+
self.__animation_type = animation_type
227+
228+
def getAnimationSpeed(self) -> int:
229+
return self.__animation_speed
230+
231+
def setAnimationSpeed(self, speed: int):
232+
self.__animation_speed = speed
233+
234+
self.__circle_span_speed = int(self.__animation_speed * self.__circle_speed_coefficient)
235+
self.__dots_single_speed = int(self.__animation_speed * self.__dots_speed_coefficient)
236+
237+
self.__timeline_circle_rotation.setDuration(self.__animation_speed)
238+
self.__timeline_circle_decrease_span.setDuration(self.__circle_span_speed)
239+
self.__timeline_circle_increase_span.setDuration(self.__circle_span_speed)
240+
241+
self.__timeline_dots_up_1.setDuration(self.__dots_single_speed)
242+
self.__timeline_dots_down_1.setDuration(self.__dots_single_speed)
243+
self.__timeline_dots_up_2.setDuration(self.__dots_single_speed)
244+
self.__timeline_dots_down_2.setDuration(self.__dots_single_speed)
245+
self.__timeline_dots_up_3.setDuration(self.__dots_single_speed)
246+
self.__timeline_dots_down_3.setDuration(self.__dots_single_speed)
247+
248+
def getAnimationWidth(self) -> int:
249+
return self.__animation_width
250+
251+
def setAnimationWidth(self, width: int):
252+
self.__animation_width = width
253+
254+
def getAnimationStrokeWidth(self) -> int:
255+
return self.__animation_stroke_width
256+
257+
def setAnimationStrokeWidth(self, width: int):
258+
self.__animation_stroke_width = width
259+
260+
def getAnimationColor(self) -> QColor:
261+
return self.__animation_color
262+
263+
def setAnimationColor(self, color: QColor):
264+
self.__animation_color = color

src/pyqt_loading_button/worker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Worker(QThread):
55

66
finished = Signal()
77

8-
def __init__(self, action):
8+
def __init__(self, action: callable):
99
super(Worker, self).__init__()
1010

1111
self.__action = action

0 commit comments

Comments
 (0)