rock_widget_collection  0.1
ProgressIndicator.cc
Go to the documentation of this file.
1 #include "ProgressIndicator.h"
2 
3 #include <QPainter>
4 
6  : QWidget(parent),
7  m_angle(0),
8  m_timerId(-1),
9  m_delay(40),
10  m_displayedWhenStopped(false),
11  m_color(Qt::black),
12  m_penColor(Qt::lightGray)
13 {
14  setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
15  setFocusPolicy(Qt::NoFocus);
16 }
17 
19 {
20  return (m_timerId != -1);
21 }
22 
24 {
25  m_displayedWhenStopped = state;
26 
27  update();
28 }
29 
31 {
32  return m_displayedWhenStopped;
33 }
34 
36 {
37  m_angle = 0;
38 
39  if (m_timerId == -1)
40  m_timerId = startTimer(m_delay);
41 }
42 
44 {
45  if (m_timerId != -1)
46  killTimer(m_timerId);
47 
48  m_timerId = -1;
49 
50  update();
51 }
52 
54 {
55  if (m_timerId != -1)
56  killTimer(m_timerId);
57 
58  m_delay = delay;
59 
60  if (m_timerId != -1)
61  m_timerId = startTimer(m_delay);
62 }
63 
64 void ProgressIndicator::setColor(const QColor & color)
65 {
66  m_color = color;
67 
68  update();
69 }
70 
72 {
73  m_penColor = color;
74 
75  update();
76 }
77 
79 {
80  return QSize(20,20);
81 }
82 
84 {
85  return w;
86 }
87 
88 void ProgressIndicator::timerEvent(QTimerEvent * /*event*/)
89 {
90  m_angle = (m_angle+30)%360;
91 
92  update();
93 }
94 
95 void ProgressIndicator::paintEvent(QPaintEvent * /*event*/)
96 {
97  if (!m_displayedWhenStopped && !isAnimated())
98  return;
99 
100  int width = qMin(this->width(), this->height());
101 
102  QPainter p(this);
103  p.setRenderHint(QPainter::Antialiasing);
104 
105  int outerRadius = (width-1)*0.5;
106  int innerRadius = (width-1)*0.5*0.38;
107 
108  int capsuleHeight = outerRadius - innerRadius;
109  int capsuleWidth = (width > 32 ) ? capsuleHeight *.23 : capsuleHeight *.35;
110  int capsuleRadius = capsuleWidth/2;
111 
112  for (int i=0; i<12; i++)
113  {
114  QColor color = m_color;
115  qreal alpha = 1.0f - (i/12.0f);
116  color.setAlphaF(alpha);
117 
118  QColor penColor = m_penColor;
119  penColor.setAlphaF(alpha);
120  QPen pen;
121  pen.setWidth(1);
122  pen.setColor(penColor);
123  p.setPen(pen);
124 
125  p.setBrush(color);
126  p.save();
127  p.translate(rect().center());
128  p.rotate(m_angle - i*30.0f);
129  p.drawRoundedRect(-capsuleWidth*0.5, -(innerRadius+capsuleHeight), capsuleWidth, capsuleHeight, capsuleRadius, capsuleRadius);
130  p.restore();
131  }
132 }
bool isDisplayedWhenStopped() const
virtual QSize sizeHint() const
int heightForWidth(int w) const
virtual void paintEvent(QPaintEvent *event)
void setAnimationDelay(int delay)
const QColor & penColor() const
ProgressIndicator(QWidget *parent=0)
void setPenColor(const QColor &color)
const QColor & color() const
void setDisplayedWhenStopped(bool state)
virtual void timerEvent(QTimerEvent *event)
void setColor(const QColor &color)
bool isAnimated() const