diff --git a/src/calibre/gui2/jobs.py b/src/calibre/gui2/jobs.py index fb53377341..a6082970d7 100644 --- a/src/calibre/gui2/jobs.py +++ b/src/calibre/gui2/jobs.py @@ -503,6 +503,7 @@ class JobsButton(QWidget): # {{{ self.num_jobs = 0 self.mouse_over = False self.pi = ProgressIndicator(self, self.style().pixelMetric(QStyle.PixelMetric.PM_ToolBarIconSize)) + self.pi.setVisible(False) self._jobs = QLabel('') self._jobs.mouseReleaseEvent = self.mouseReleaseEvent self.update_label() @@ -562,9 +563,11 @@ class JobsButton(QWidget): # {{{ def start(self): self.pi.startAnimation() + self.pi.setVisible(True) def stop(self): self.pi.stopAnimation() + self.pi.setVisible(False) def jobs(self): return self.num_jobs diff --git a/src/calibre/gui2/progress_indicator/QProgressIndicator.cpp b/src/calibre/gui2/progress_indicator/QProgressIndicator.cpp index b2e36a6de3..0336f3a3f3 100644 --- a/src/calibre/gui2/progress_indicator/QProgressIndicator.cpp +++ b/src/calibre/gui2/progress_indicator/QProgressIndicator.cpp @@ -17,22 +17,18 @@ extern int qt_defaultDpiX(); QProgressIndicator::QProgressIndicator(QWidget* parent, int size, int interval) : QWidget(parent), - m_angle(0), - m_timerId(-1), - m_delay(interval), m_displaySize(size, size), - m_dark(Qt::black), - m_light(Qt::white) + m_animator(this) { + Q_UNUSED(interval); setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); setFocusPolicy(Qt::NoFocus); - m_dark = this->palette().color(QPalette::WindowText); - m_light = this->palette().color(QPalette::Window); + QObject::connect(&m_animator, SIGNAL(updated()), this, SLOT(update())); } bool QProgressIndicator::isAnimated () const { - return (m_timerId != -1); + return m_animator.is_running(); } void QProgressIndicator::setDisplaySize(QSize size) @@ -49,68 +45,38 @@ void QProgressIndicator::setSizeHint(QSize size) update(); } - - - void QProgressIndicator::startAnimation() { - m_angle = 0; - - if (m_timerId == -1) - m_timerId = startTimer(m_delay); + if (!m_animator.is_running()) { + m_animator.start(); + update(); + emit running_state_changed(true); + } } void QProgressIndicator::start() { startAnimation(); } void QProgressIndicator::stopAnimation() { - if (m_timerId != -1) - killTimer(m_timerId); - - m_timerId = -1; - - update(); + if (m_animator.is_running()) { + m_animator.stop(); + update(); + emit running_state_changed(false); + } } void QProgressIndicator::stop() { stopAnimation(); } -void QProgressIndicator::setAnimationDelay(int delay) -{ - if (m_timerId != -1) - killTimer(m_timerId); - - m_delay = delay; - - if (m_timerId != -1) - m_timerId = startTimer(m_delay); -} - -void QProgressIndicator::set_colors(const QColor & dark, const QColor & light) -{ - m_dark = dark; m_light = light; - - update(); -} - QSize QProgressIndicator::sizeHint() const { return m_displaySize; } -int QProgressIndicator::heightForWidth(int w) const -{ - return w; -} - -void QProgressIndicator::timerEvent(QTimerEvent * /*event*/) -{ - m_angle = (m_angle-2)%360; - - update(); -} - void QProgressIndicator::paintEvent(QPaintEvent * /*event*/) { QPainter painter(this); - draw_snake_spinner(painter, this->rect(), m_angle, m_light, m_dark); + QRect r(this->rect()); + QPoint center(r.center()); + int smaller = std::min(r.width(), r.height()); + m_animator.draw(painter, QRect(center.x() - smaller / 2, center.y() - smaller / 2, smaller, smaller), this->palette().color(QPalette::WindowText)); } static inline QByteArray detectDesktopEnvironment() diff --git a/src/calibre/gui2/progress_indicator/QProgressIndicator.h b/src/calibre/gui2/progress_indicator/QProgressIndicator.h index 3874a88198..d76cb32a5b 100644 --- a/src/calibre/gui2/progress_indicator/QProgressIndicator.h +++ b/src/calibre/gui2/progress_indicator/QProgressIndicator.h @@ -56,7 +56,7 @@ public: painter.save(); painter.setRenderHint(QPainter::Antialiasing); QRectF rect(bounds); - float width = thickness > 0.f ? 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, 18.f)); QPen pen(color); pen.setWidthF(width); float ht = width / 2 + 1; @@ -96,16 +96,9 @@ private: class QProgressIndicator : public QWidget { Q_OBJECT - Q_PROPERTY(int delay READ animationDelay WRITE setAnimationDelay) Q_PROPERTY(QSize displaySize READ displaySize WRITE setDisplaySize) public: - QProgressIndicator(QWidget* parent = 0, int size = 64, int interval = 10); - - /*! Returns the delay between animation steps. - \return The number of milliseconds between animation steps. By default, the animation delay is set to 80 milliseconds. - \sa setAnimationDelay - */ - int animationDelay() const { return m_delay; } + QProgressIndicator(QWidget* parent = 0, int size = 64, int interval = 0); /*! Returns a Boolean value indicating whether the component is currently animated. \return Animation state. @@ -114,7 +107,6 @@ public: bool isAnimated () const; virtual QSize sizeHint() const; - int heightForWidth(int w) const; QSize displaySize() const { return m_displaySize; } public slots: /*! Starts the spin animation. @@ -129,18 +121,6 @@ public slots: void stopAnimation(); void stop(); - /*! Sets the delay between animation steps. - Setting the \a delay to a value larger than 40 slows the animation, while setting the \a delay to a smaller value speeds it up. - \param delay The delay, in milliseconds. - \sa animationDelay - */ - void setAnimationDelay(int delay); - - /*! Sets the color of the components to the given color. - \sa color - */ - void set_colors(const QColor & dark, const QColor & light); - /*! Set the size of this widget (used by sizeHint) * \sa displaySize */ @@ -148,15 +128,13 @@ public slots: void setDisplaySize(int size) { setDisplaySize(QSize(size, size)); } void setSizeHint(int size); void setSizeHint(QSize size); +signals: + void running_state_changed(bool); protected: - virtual void timerEvent(QTimerEvent * event); virtual void paintEvent(QPaintEvent * event); private: - int m_angle; - int m_timerId; - int m_delay; QSize m_displaySize; - QColor m_dark, m_light; + SpinAnimator m_animator; }; int load_style(QHash icon_map, int transient_scroller=0); diff --git a/src/calibre/gui2/progress_indicator/QProgressIndicator.sip b/src/calibre/gui2/progress_indicator/QProgressIndicator.sip index a12a7fd698..9f1aade836 100644 --- a/src/calibre/gui2/progress_indicator/QProgressIndicator.sip +++ b/src/calibre/gui2/progress_indicator/QProgressIndicator.sip @@ -43,9 +43,7 @@ class QProgressIndicator : QWidget { public: - QProgressIndicator(QWidget *parent /TransferThis/ = 0, int size = 64, int interval = 10); - - int animationDelay() const; + QProgressIndicator(QWidget *parent /TransferThis/ = 0, int size = 64, int interval = 0); bool isAnimated () const; @@ -62,10 +60,6 @@ public slots: void stopAnimation(); void stop(); - void setAnimationDelay(int delay); - - void set_colors(const QColor & dark, const QColor & light); - void setDisplaySize(QSize size); void setDisplaySize(int size); void setSizeHint(int size); @@ -73,10 +67,11 @@ public slots: protected: - virtual void timerEvent(QTimerEvent * event); - virtual void paintEvent(QPaintEvent * event); +signals: + void running_state_changed(bool); + }; int load_style(QHash icon_map, int transient_scroller); diff --git a/src/calibre/gui2/viewer/overlay.py b/src/calibre/gui2/viewer/overlay.py index 71d4d81482..28ce7939e1 100644 --- a/src/calibre/gui2/viewer/overlay.py +++ b/src/calibre/gui2/viewer/overlay.py @@ -13,7 +13,7 @@ class LoadingOverlay(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.l = l = QVBoxLayout(self) - self.pi = ProgressIndicator(self, 96, 80) + self.pi = ProgressIndicator(self, 96) self.setVisible(False) self.label = QLabel(self) self.label.setText('testing with some long and wrap worthy message that should hopefully still render well')