From 3973d843e559f4ab48a534b1c17d4aced096702b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 4 Dec 2020 18:32:01 +0530 Subject: [PATCH] Make the spin animator a little parametrizable --- .../gui2/progress_indicator/QProgressIndicator.h | 16 ++++++++-------- .../progress_indicator/QProgressIndicator.sip | 4 ++-- src/calibre/gui2/progress_indicator/__init__.py | 13 +++++++++---- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/calibre/gui2/progress_indicator/QProgressIndicator.h b/src/calibre/gui2/progress_indicator/QProgressIndicator.h index 615197aa20..38a341164f 100644 --- a/src/calibre/gui2/progress_indicator/QProgressIndicator.h +++ b/src/calibre/gui2/progress_indicator/QProgressIndicator.h @@ -14,7 +14,7 @@ class SpinAnimator: public QObject { Q_PROPERTY(int arc_rotation READ get_arc_rotation WRITE set_arc_rotation) Q_PROPERTY(int overall_rotation READ get_overall_rotation WRITE set_overall_rotation) public: - SpinAnimator(QObject* parent = 0) : + SpinAnimator(QObject* parent = 0, const int speed_factor=300) : QObject(parent), m_arc_length(0), m_arc_rotation(0), @@ -24,7 +24,7 @@ public: { QPropertyAnimation *a; #define S(property, duration) a = new QPropertyAnimation(this, QByteArray(#property), this); a->setEasingCurve(QEasingCurve::InOutCubic); a->setDuration(duration); a->setLoopCount(-1); m_animation.addAnimation(a); - S(arc_length, 1400); + S(arc_length, 7 * speed_factor); const float arc_length_max = 0.734f, arc_length_min = 0.01f; a->setStartValue(arc_length_min); a->setKeyValueAt(0.25, arc_length_min); @@ -32,14 +32,14 @@ public: a->setKeyValueAt(0.75, arc_length_max); a->setEndValue(arc_length_min); - S(arc_rotation, 1400); + S(arc_rotation, 7 * speed_factor); a->setStartValue(0); a->setKeyValueAt(0.25, 0); a->setKeyValueAt(0.5, 45); a->setKeyValueAt(0.75, 45); a->setEndValue(360); - S(overall_rotation, 2000); + S(overall_rotation, 10 * speed_factor); a->setStartValue(0); a->setEndValue(360); #undef S @@ -48,15 +48,15 @@ public: void start() { m_animation.start(); } void stop() { m_animation.stop(); } bool is_running() { return m_animation.state() == QAbstractAnimation::Running; } - void draw(QPainter &painter, QRect bounds, const QColor &color) { + void draw(QPainter &painter, QRect bounds, const QColor &color, const float thickness=0.f) { m_has_pending_updates = false; painter.save(); painter.setRenderHint(QPainter::Antialiasing); QRectF rect(bounds); - float thickness = std::max(3.f, std::min((float)rect.width() / 10.f, 24.f)); + float width = thickness > 0.f ? thickness : std::max(3.f, std::min((float)rect.width() / 10.f, 24.f)); QPen pen(color); - pen.setWidthF(thickness); - float ht = thickness / 2 + 1; + pen.setWidthF(width); + float ht = width / 2 + 1; rect.adjust(ht, ht, -ht, -ht); pen.setCapStyle(Qt::RoundCap); painter.setPen(pen); diff --git a/src/calibre/gui2/progress_indicator/QProgressIndicator.sip b/src/calibre/gui2/progress_indicator/QProgressIndicator.sip index f7dba6b5c5..a12a7fd698 100644 --- a/src/calibre/gui2/progress_indicator/QProgressIndicator.sip +++ b/src/calibre/gui2/progress_indicator/QProgressIndicator.sip @@ -19,11 +19,11 @@ class SpinAnimator : QObject { %End public: - SpinAnimator(QWidget *parent /TransferThis/ = 0); + SpinAnimator(QWidget *parent /TransferThis/ = 0, int speed_factor=300); float get_arc_length() const ; int get_arc_rotation() const ; int get_overall_rotation() const ; - void draw(QPainter &painter, QRect bounds, const QColor &color); + void draw(QPainter &painter, QRect bounds, const QColor &color, const float thickness=0.f); void start(); void stop(); bool is_running(); diff --git a/src/calibre/gui2/progress_indicator/__init__.py b/src/calibre/gui2/progress_indicator/__init__.py index 5da401267d..c7d6710e5a 100644 --- a/src/calibre/gui2/progress_indicator/__init__.py +++ b/src/calibre/gui2/progress_indicator/__init__.py @@ -95,7 +95,7 @@ class WaitLayout(QStackedLayout): def develop(): - from PyQt5.Qt import QPalette, QPainter, QRect + from PyQt5.Qt import QPalette, QPainter from calibre.gui2 import Application from calibre_extensions.progress_indicator import SpinAnimator @@ -109,14 +109,19 @@ def develop(): def paintEvent(self, ev): p = QPainter(self) pal = self.palette() - self.a.draw(p, QRect(0, 0, 64, 64), pal.color(QPalette.ColorRole.WindowText)) + self.a.draw(p, self.rect(), pal.color(QPalette.ColorRole.WindowText)) p.end() app = Application([]) + d = QDialog() + d.resize(64, 64) + l = QVBoxLayout(d) w = Widget() - w.show() + l.addWidget(w) w.a.start() - app.exec_() + d.exec_() + del d + del app if __name__ == '__main__':