CFEL - ASG Software Suite  2.5.0
CASS
led.cpp
Go to the documentation of this file.
1 /**
2  * @file led.cpp a led in qt
3  *
4  * @author unknown
5  */
6 
7 #include <cmath>
8 
9 #include <QtCore/QTimer>
10 
11 #include <QtGui/QPainter>
12 #include <QtGui/QGradient>
13 #include <QtGui/QPaintDevice>
14 
15 #include "led.h"
16 
17 LED::LED(QWidget* parent) :
18  QWidget(parent),
19  diameter_(5),
20  color_(QColor("red")),
21  alignment_(Qt::AlignCenter),
22  initialState_(true),
23  state_(true),
24  flashRate_(200),
25  flashing_(false)
26 {
27  timer_ = new QTimer(this);
28  connect(timer_, SIGNAL(timeout()), this, SLOT(toggleState()));
29 
31 }
32 
34 {
35 }
36 
37 
38 double LED::diameter() const
39 {
40  return diameter_;
41 }
42 
43 void LED::setDiameter(double diameter)
44 {
46 
47  pixX_ = round(double(height())/heightMM());
48  pixY_ = round(double(width())/widthMM());
49 
52 
53  update();
54 }
55 
56 
57 QColor LED::color() const
58 {
59  return color_;
60 }
61 
62 void LED::setColor(const QColor& color)
63 {
64  color_ = color;
65  update();
66 }
67 
68 Qt::Alignment LED::alignment() const
69 {
70  return alignment_;
71 }
72 
73 void LED::
74 setAlignment(Qt::Alignment alignment)
75 {
77 
78  update();
79 }
80 
82 {
83  flashRate_ = rate;
84 
85  update();
86 }
87 
88 void LED::setFlashing(bool flashing)
89 {
91 
92  update();
93 }
94 
96 {
97  setFlashing(true);
98 }
99 
101 {
102  setFlashing(false);
103 }
104 
105 
106 void LED::setState(bool state)
107 {
108  state_ = state;
109  update();
110 }
111 
113 {
114  state_ = !state_;
115  update();
116 }
117 
119 {
120  return width;
121 }
122 
124 {
125  return QSize(diamX_, diamY_);
126 }
127 
129 {
130  return QSize(diamX_, diamY_);
131 }
132 
134 {
135  Q_UNUSED(event);
136 
137  QPainter p(this);
138 
139  QRect geo = geometry();
140  int width = geo.width();
141  int height = geo.height();
142 
143  int x=0, y=0;
144  if ( alignment_ & Qt::AlignLeft )
145  x = 0;
146  else if ( alignment_ & Qt::AlignRight )
147  x = width-diamX_;
148  else if ( alignment_ & Qt::AlignHCenter )
149  x = (width-diamX_)/2;
150  else if ( alignment_ & Qt::AlignJustify )
151  x = 0;
152 
153  if ( alignment_ & Qt::AlignTop )
154  y = 0;
155  else if ( alignment_ & Qt::AlignBottom )
156  y = height-diamY_;
157  else if ( alignment_ & Qt::AlignVCenter )
158  y = (height-diamY_)/2;
159 
160  QRadialGradient g(x+diamX_/2, y+diamY_/2, diamX_*0.4,
161  diamX_*0.4, diamY_*0.4);
162 
163  g.setColorAt(0, Qt::white);
164  if ( state_ )
165  g.setColorAt(1, color_);
166  else
167  g.setColorAt(1, Qt::black);
168  QBrush brush(g);
169 
170  p.setPen(color_);
171  p.setRenderHint(QPainter::Antialiasing, true);
172  p.setBrush(brush);
173  p.drawEllipse(x, y, diamX_-1, diamY_-1);
174 
175  if ( flashRate_ > 0 && flashing_ )
177  else
178  timer_->stop();
179 }
180 
181 bool LED::state() const
182 {
183  return state_;
184 }
185 
186 bool LED::isFlashing() const
187 {
188  return flashing_;
189 }
190 
191 int LED::flashRate() const
192 {
193  return flashRate_;
194 }
bool flashing
Definition: led.h:23
int pixX_
Definition: led.h:73
setRenderHint(RenderHint hint, bool on=true)
void setFlashRate(int rate)
Definition: led.cpp:81
bool isFlashing() const
Definition: led.cpp:186
void startFlashing()
Definition: led.cpp:95
setPen(const QPen &pen)
void setAlignment(Qt::Alignment alignment)
Definition: led.cpp:74
QTimer * timer_
Definition: led.h:81
void toggleState()
Definition: led.cpp:112
~LED()
Definition: led.cpp:33
int diamX_
Definition: led.h:78
void setFlashing(bool flashing)
Definition: led.cpp:88
bool state_
Definition: led.h:66
void setColor(const QColor &color)
Definition: led.cpp:62
bool state() const
QSize sizeHint() const
Definition: led.cpp:123
double diameter_
Definition: led.h:62
LED(QWidget *parent=0)
Definition: led.cpp:17
drawEllipse(const QRectF &rectangle)
int pixY_
Definition: led.h:73
QColor color() const
Qt::Alignment alignment_
Definition: led.h:64
Qt::Alignment alignment() const
setBrush(const QBrush &brush)
void stopFlashing()
Definition: led.cpp:100
int flashRate_
Definition: led.h:67
QSize minimumSizeHint() const
Definition: led.cpp:128
int flashRate() const
void paintEvent(QPaintEvent *event)
Definition: led.cpp:133
void setState(bool state)
Definition: led.cpp:106
display the real hit rate
int diamY_
Definition: led.h:78
double diameter() const
int heightForWidth(int width) const
Definition: led.cpp:118
an led in qt
bool flashing_
Definition: led.h:68
start(int msec)
void setDiameter(double diameter)
Definition: led.cpp:43
QColor color_
Definition: led.h:63