Switch to the new spin animator for the progress indicator widget

This commit is contained in:
Kovid Goyal 2020-12-04 22:25:24 +05:30
parent 042a642f65
commit 261f153fe6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 31 additions and 89 deletions

View File

@ -503,6 +503,7 @@ class JobsButton(QWidget): # {{{
self.num_jobs = 0 self.num_jobs = 0
self.mouse_over = False self.mouse_over = False
self.pi = ProgressIndicator(self, self.style().pixelMetric(QStyle.PixelMetric.PM_ToolBarIconSize)) self.pi = ProgressIndicator(self, self.style().pixelMetric(QStyle.PixelMetric.PM_ToolBarIconSize))
self.pi.setVisible(False)
self._jobs = QLabel('') self._jobs = QLabel('')
self._jobs.mouseReleaseEvent = self.mouseReleaseEvent self._jobs.mouseReleaseEvent = self.mouseReleaseEvent
self.update_label() self.update_label()
@ -562,9 +563,11 @@ class JobsButton(QWidget): # {{{
def start(self): def start(self):
self.pi.startAnimation() self.pi.startAnimation()
self.pi.setVisible(True)
def stop(self): def stop(self):
self.pi.stopAnimation() self.pi.stopAnimation()
self.pi.setVisible(False)
def jobs(self): def jobs(self):
return self.num_jobs return self.num_jobs

View File

@ -17,22 +17,18 @@ extern int qt_defaultDpiX();
QProgressIndicator::QProgressIndicator(QWidget* parent, int size, int interval) QProgressIndicator::QProgressIndicator(QWidget* parent, int size, int interval)
: QWidget(parent), : QWidget(parent),
m_angle(0),
m_timerId(-1),
m_delay(interval),
m_displaySize(size, size), m_displaySize(size, size),
m_dark(Qt::black), m_animator(this)
m_light(Qt::white)
{ {
Q_UNUSED(interval);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
setFocusPolicy(Qt::NoFocus); setFocusPolicy(Qt::NoFocus);
m_dark = this->palette().color(QPalette::WindowText); QObject::connect(&m_animator, SIGNAL(updated()), this, SLOT(update()));
m_light = this->palette().color(QPalette::Window);
} }
bool QProgressIndicator::isAnimated () const bool QProgressIndicator::isAnimated () const
{ {
return (m_timerId != -1); return m_animator.is_running();
} }
void QProgressIndicator::setDisplaySize(QSize size) void QProgressIndicator::setDisplaySize(QSize size)
@ -49,68 +45,38 @@ void QProgressIndicator::setSizeHint(QSize size)
update(); update();
} }
void QProgressIndicator::startAnimation() void QProgressIndicator::startAnimation()
{ {
m_angle = 0; if (!m_animator.is_running()) {
m_animator.start();
if (m_timerId == -1) update();
m_timerId = startTimer(m_delay); emit running_state_changed(true);
}
} }
void QProgressIndicator::start() { startAnimation(); } void QProgressIndicator::start() { startAnimation(); }
void QProgressIndicator::stopAnimation() void QProgressIndicator::stopAnimation()
{ {
if (m_timerId != -1) if (m_animator.is_running()) {
killTimer(m_timerId); m_animator.stop();
m_timerId = -1;
update(); update();
emit running_state_changed(false);
}
} }
void QProgressIndicator::stop() { stopAnimation(); } 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 QSize QProgressIndicator::sizeHint() const
{ {
return m_displaySize; 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*/) void QProgressIndicator::paintEvent(QPaintEvent * /*event*/)
{ {
QPainter painter(this); 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() static inline QByteArray detectDesktopEnvironment()

View File

@ -56,7 +56,7 @@ public:
painter.save(); painter.save();
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing);
QRectF rect(bounds); 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); QPen pen(color);
pen.setWidthF(width); pen.setWidthF(width);
float ht = width / 2 + 1; float ht = width / 2 + 1;
@ -96,16 +96,9 @@ private:
class QProgressIndicator : public QWidget class QProgressIndicator : public QWidget
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(int delay READ animationDelay WRITE setAnimationDelay)
Q_PROPERTY(QSize displaySize READ displaySize WRITE setDisplaySize) Q_PROPERTY(QSize displaySize READ displaySize WRITE setDisplaySize)
public: public:
QProgressIndicator(QWidget* parent = 0, int size = 64, int interval = 10); QProgressIndicator(QWidget* parent = 0, int size = 64, int interval = 0);
/*! 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; }
/*! Returns a Boolean value indicating whether the component is currently animated. /*! Returns a Boolean value indicating whether the component is currently animated.
\return Animation state. \return Animation state.
@ -114,7 +107,6 @@ public:
bool isAnimated () const; bool isAnimated () const;
virtual QSize sizeHint() const; virtual QSize sizeHint() const;
int heightForWidth(int w) const;
QSize displaySize() const { return m_displaySize; } QSize displaySize() const { return m_displaySize; }
public slots: public slots:
/*! Starts the spin animation. /*! Starts the spin animation.
@ -129,18 +121,6 @@ public slots:
void stopAnimation(); void stopAnimation();
void stop(); 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) /*! Set the size of this widget (used by sizeHint)
* \sa displaySize * \sa displaySize
*/ */
@ -148,15 +128,13 @@ public slots:
void setDisplaySize(int size) { setDisplaySize(QSize(size, size)); } void setDisplaySize(int size) { setDisplaySize(QSize(size, size)); }
void setSizeHint(int size); void setSizeHint(int size);
void setSizeHint(QSize size); void setSizeHint(QSize size);
signals:
void running_state_changed(bool);
protected: protected:
virtual void timerEvent(QTimerEvent * event);
virtual void paintEvent(QPaintEvent * event); virtual void paintEvent(QPaintEvent * event);
private: private:
int m_angle;
int m_timerId;
int m_delay;
QSize m_displaySize; QSize m_displaySize;
QColor m_dark, m_light; SpinAnimator m_animator;
}; };
int load_style(QHash<int,QString> icon_map, int transient_scroller=0); int load_style(QHash<int,QString> icon_map, int transient_scroller=0);

View File

@ -43,9 +43,7 @@ class QProgressIndicator : QWidget {
public: public:
QProgressIndicator(QWidget *parent /TransferThis/ = 0, int size = 64, int interval = 10); QProgressIndicator(QWidget *parent /TransferThis/ = 0, int size = 64, int interval = 0);
int animationDelay() const;
bool isAnimated () const; bool isAnimated () const;
@ -62,10 +60,6 @@ public slots:
void stopAnimation(); void stopAnimation();
void stop(); void stop();
void setAnimationDelay(int delay);
void set_colors(const QColor & dark, const QColor & light);
void setDisplaySize(QSize size); void setDisplaySize(QSize size);
void setDisplaySize(int size); void setDisplaySize(int size);
void setSizeHint(int size); void setSizeHint(int size);
@ -73,10 +67,11 @@ public slots:
protected: protected:
virtual void timerEvent(QTimerEvent * event);
virtual void paintEvent(QPaintEvent * event); virtual void paintEvent(QPaintEvent * event);
signals:
void running_state_changed(bool);
}; };
int load_style(QHash<int,QString> icon_map, int transient_scroller); int load_style(QHash<int,QString> icon_map, int transient_scroller);

View File

@ -13,7 +13,7 @@ class LoadingOverlay(QWidget):
def __init__(self, parent=None): def __init__(self, parent=None):
QWidget.__init__(self, parent) QWidget.__init__(self, parent)
self.l = l = QVBoxLayout(self) self.l = l = QVBoxLayout(self)
self.pi = ProgressIndicator(self, 96, 80) self.pi = ProgressIndicator(self, 96)
self.setVisible(False) self.setVisible(False)
self.label = QLabel(self) self.label = QLabel(self)
self.label.setText('<i>testing with some long and wrap worthy message that should hopefully still render well') self.label.setText('<i>testing with some long and wrap worthy message that should hopefully still render well')