|
| 1 | +""" |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2012-2014 Alexander Turkin |
| 5 | +Copyright (c) 2014 William Hallatt |
| 6 | +Copyright (c) 2015 Jacob Dawid |
| 7 | +Copyright (c) 2016 Luca Weiss |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in all |
| 17 | +copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +SOFTWARE. |
| 26 | +""" |
| 27 | + |
| 28 | +import math |
| 29 | + |
| 30 | +from PyQt5.QtCore import * |
| 31 | +from PyQt5.QtGui import * |
| 32 | +from PyQt5.QtWidgets import * |
| 33 | + |
| 34 | + |
| 35 | +class QtWaitingSpinner(QWidget): |
| 36 | + |
| 37 | + def __init__(self, parent, centerOnParent=True, disableParentWhenSpinning=False, modality=Qt.NonModal): |
| 38 | + QWidget.__init__(self, parent) |
| 39 | + |
| 40 | + self._centerOnParent = centerOnParent |
| 41 | + self._disableParentWhenSpinning = disableParentWhenSpinning |
| 42 | + |
| 43 | + # WAS IN initialize() |
| 44 | + self._color = QColor(Qt.black) |
| 45 | + self._roundness = 100.0 |
| 46 | + self._minimumTrailOpacity = 3.14159265358979323846 |
| 47 | + self._trailFadePercentage = 80.0 |
| 48 | + self._revolutionsPerSecond = 1.57079632679489661923 |
| 49 | + self._numberOfLines = 20 |
| 50 | + self._lineLength = 10 |
| 51 | + self._lineWidth = 2 |
| 52 | + self._innerRadius = 10 |
| 53 | + self._currentCounter = 0 |
| 54 | + self._isSpinning = False |
| 55 | + |
| 56 | + self._timer = QTimer(self) |
| 57 | + self._timer.timeout.connect(self.rotate) |
| 58 | + self.updateSize() |
| 59 | + self.updateTimer() |
| 60 | + self.hide() |
| 61 | + # END initialize() |
| 62 | + |
| 63 | + self.setWindowModality(modality) |
| 64 | + self.setAttribute(Qt.WA_TranslucentBackground) |
| 65 | + |
| 66 | + def paintEvent(self, QPaintEvent): |
| 67 | + self.updatePosition() |
| 68 | + painter = QPainter(self) |
| 69 | + painter.fillRect(self.rect(), Qt.transparent) |
| 70 | + painter.setRenderHint(QPainter.Antialiasing, True) |
| 71 | + |
| 72 | + if self._currentCounter >= self._numberOfLines: |
| 73 | + self._currentCounter = 0 |
| 74 | + |
| 75 | + painter.setPen(Qt.NoPen) |
| 76 | + for i in range(0, self._numberOfLines): |
| 77 | + painter.save() |
| 78 | + painter.translate( |
| 79 | + self._innerRadius + self._lineLength, self._innerRadius + self._lineLength) |
| 80 | + rotateAngle = float(360 * i) / float(self._numberOfLines) |
| 81 | + painter.rotate(rotateAngle) |
| 82 | + painter.translate(self._innerRadius, 0) |
| 83 | + distance = self.lineCountDistanceFromPrimary( |
| 84 | + i, self._currentCounter, self._numberOfLines) |
| 85 | + color = self.currentLineColor( |
| 86 | + distance, self._numberOfLines, self._trailFadePercentage, |
| 87 | + self._minimumTrailOpacity, self._color) |
| 88 | + painter.setBrush(color) |
| 89 | + painter.drawRoundedRect( |
| 90 | + QRect(0, -self._lineWidth / 2, self._lineLength, |
| 91 | + self._lineWidth), self._roundness, |
| 92 | + self._roundness, Qt.RelativeSize) |
| 93 | + painter.restore() |
| 94 | + |
| 95 | + def start(self): |
| 96 | + self.updatePosition() |
| 97 | + self._isSpinning = True |
| 98 | + self.show() |
| 99 | + |
| 100 | + if self.parentWidget and self._disableParentWhenSpinning: |
| 101 | + self.parentWidget().setEnabled(False) |
| 102 | + |
| 103 | + if not self._timer.isActive(): |
| 104 | + self._timer.start() |
| 105 | + self._currentCounter = 0 |
| 106 | + |
| 107 | + def stop(self): |
| 108 | + self._isSpinning = False |
| 109 | + self.hide() |
| 110 | + |
| 111 | + if self.parentWidget() and self._disableParentWhenSpinning: |
| 112 | + self.parentWidget().setEnabled(True) |
| 113 | + |
| 114 | + if self._timer.isActive(): |
| 115 | + self._timer.stop() |
| 116 | + self._currentCounter = 0 |
| 117 | + |
| 118 | + def setNumberOfLines(self, lines): |
| 119 | + self._numberOfLines = lines |
| 120 | + self._currentCounter = 0 |
| 121 | + self.updateTimer() |
| 122 | + |
| 123 | + def setLineLength(self, length): |
| 124 | + self._lineLength = length |
| 125 | + self.updateSize() |
| 126 | + |
| 127 | + def setLineWidth(self, width): |
| 128 | + self._lineWidth = width |
| 129 | + self.updateSize() |
| 130 | + |
| 131 | + def setInnerRadius(self, radius): |
| 132 | + self._innerRadius = radius |
| 133 | + self.updateSize() |
| 134 | + |
| 135 | + def color(self): |
| 136 | + return self._color |
| 137 | + |
| 138 | + def roundness(self): |
| 139 | + return self._roundness |
| 140 | + |
| 141 | + def minimumTrailOpacity(self): |
| 142 | + return self._minimumTrailOpacity |
| 143 | + |
| 144 | + def trailFadePercentage(self): |
| 145 | + return self._trailFadePercentage |
| 146 | + |
| 147 | + def revolutionsPersSecond(self): |
| 148 | + return self._revolutionsPerSecond |
| 149 | + |
| 150 | + def numberOfLines(self): |
| 151 | + return self._numberOfLines |
| 152 | + |
| 153 | + def lineLength(self): |
| 154 | + return self._lineLength |
| 155 | + |
| 156 | + def lineWidth(self): |
| 157 | + return self._lineWidth |
| 158 | + |
| 159 | + def innerRadius(self): |
| 160 | + return self._innerRadius |
| 161 | + |
| 162 | + def isSpinning(self): |
| 163 | + return self._isSpinning |
| 164 | + |
| 165 | + def setRoundness(self, roundness): |
| 166 | + self._roundness = max(0.0, min(100.0, roundness)) |
| 167 | + |
| 168 | + def setColor(self, color=Qt.black): |
| 169 | + self._color = QColor(color) |
| 170 | + |
| 171 | + def setRevolutionsPerSecond(self, revolutionsPerSecond): |
| 172 | + self._revolutionsPerSecond = revolutionsPerSecond |
| 173 | + self.updateTimer() |
| 174 | + |
| 175 | + def setTrailFadePercentage(self, trail): |
| 176 | + self._trailFadePercentage = trail |
| 177 | + |
| 178 | + def setMinimumTrailOpacity(self, minimumTrailOpacity): |
| 179 | + self._minimumTrailOpacity = minimumTrailOpacity |
| 180 | + |
| 181 | + def rotate(self): |
| 182 | + self._currentCounter += 1 |
| 183 | + if self._currentCounter >= self._numberOfLines: |
| 184 | + self._currentCounter = 0 |
| 185 | + self.update() |
| 186 | + |
| 187 | + def updateSize(self): |
| 188 | + size = (self._innerRadius + self._lineLength) * 2 |
| 189 | + self.setFixedSize(size, size) |
| 190 | + |
| 191 | + def updateTimer(self): |
| 192 | + self._timer.setInterval( |
| 193 | + 1000 / (self._numberOfLines * self._revolutionsPerSecond)) |
| 194 | + |
| 195 | + def updatePosition(self): |
| 196 | + if self.parentWidget() and self._centerOnParent: |
| 197 | + self.move(self.parentWidget().width() / 2 - self.width() / 2, |
| 198 | + self.parentWidget().height() / 2 - self.height() / 2) |
| 199 | + |
| 200 | + def lineCountDistanceFromPrimary(self, current, primary, totalNrOfLines): |
| 201 | + distance = primary - current |
| 202 | + if distance < 0: |
| 203 | + distance += totalNrOfLines |
| 204 | + return distance |
| 205 | + |
| 206 | + def currentLineColor(self, countDistance, totalNrOfLines, trailFadePerc, minOpacity, colorinput): |
| 207 | + color = QColor(colorinput) |
| 208 | + if countDistance == 0: |
| 209 | + return color |
| 210 | + minAlphaF = minOpacity / 100.0 |
| 211 | + distanceThreshold = int( |
| 212 | + math.ceil((totalNrOfLines - 1) * trailFadePerc / 100.0)) |
| 213 | + if countDistance > distanceThreshold: |
| 214 | + color.setAlphaF(minAlphaF) |
| 215 | + else: |
| 216 | + alphaDiff = color.alphaF() - minAlphaF |
| 217 | + gradient = alphaDiff / float(distanceThreshold + 1) |
| 218 | + resultAlpha = color.alphaF() - gradient * countDistance |
| 219 | + # If alpha is out of bounds, clip it. |
| 220 | + resultAlpha = min(1.0, max(0.0, resultAlpha)) |
| 221 | + color.setAlphaF(resultAlpha) |
| 222 | + return color |
0 commit comments