-
Notifications
You must be signed in to change notification settings - Fork 9
/
mygauge6.cpp
254 lines (214 loc) · 6.13 KB
/
mygauge6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "mygauge6.h"
myGauge6::myGauge6(QWidget *parent) :
QWidget(parent)
{
graphcolor=Qt::darkGreen;
m_currentValue=0;
m_value=0;
m_bReverse=false;
m_bUseAntialiasing=false;
m_bAnimate=true;
m_nMin=0;
m_nMax=100;
updateTimer=new QTimer(this);
updateTimer->setInterval(30);
connect(updateTimer,SIGNAL(timeout()),this,SLOT(UpdateGraph()));
}
void myGauge6::paintEvent(QPaintEvent *)
{
QPainter painter;
painter.begin(this);
if(m_bUseAntialiasing)
{
painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing);
}
drawVariables(&painter);
drawBackground(&painter);
drawMark(&painter);
drawTextRect(&painter);
drawUnderRect(&painter);
drawGauge(&painter);
}
void myGauge6::drawVariables(QPainter *painter)
{
m_leftSpace=(qreal)width()/7;
m_topSpace=(qreal)height()/10;
m_long=m_leftSpace/4;
m_ok=m_long/3;
m_short=m_long/2;
}
void myGauge6::drawBackground(QPainter *painter)
{
painter->save();
QLinearGradient bgGradient(QPointF(0,0),QPointF(0,height()));
bgGradient.setColorAt(0.0,QColor(30,30,30));
bgGradient.setColorAt(0.1,QColor(30,30,30));
bgGradient.setColorAt(0.9,QColor(30,30,30));
bgGradient.setColorAt(1.0,QColor(30,30,30));
painter->setPen(Qt::NoPen);
painter->setBrush(bgGradient);
painter->drawRect(rect());
painter->restore();
}
void myGauge6::SetGraphColor(QColor c)
{
this->graphcolor=c;
}
void myGauge6::drawGauge(QPainter *painter)
{
painter->save();
QRectF gaugeRect(m_gaugeTopLeftPot,m_gaugeBottomRightPot);
QLinearGradient gaugeGradient(gaugeRect.topLeft(),gaugeRect.bottomLeft());
gaugeGradient.setColorAt(0.0,graphcolor);
painter->setPen(Qt::NoPen);
painter->setBrush(gaugeGradient);
qreal length=gaugeRect.height();
qreal increment=(qreal)length/(m_nMax-m_nMin);
qreal value=m_currentValue*increment;
QPointF graphTopLeftPot(m_gaugeTopLeftPot.x(),m_gaugeBottomRightPot.y()-value);
QRectF graphRect(graphTopLeftPot,m_gaugeBottomRightPot);
painter->drawRect(graphRect);
painter->restore();
}
void myGauge6::drawMark(QPainter *painter)
{
painter->save();
// draw vertical line
painter->setPen(Qt::white);
QPointF topPot(width()-2*m_leftSpace,m_topSpace);
m_markX=topPot.x();
m_markY=topPot.y();
QPointF bottomPot(width()-2*m_leftSpace,height()-2*m_topSpace);
m_markBottom=bottomPot.y();
painter->drawLine(topPot,bottomPot);
int length=m_nMax-m_nMin;
qreal increment=(qreal)(height()-3*m_topSpace)/length;
int value=m_nMax;
QString strValue;
qreal startY=topPot.y();
for(int i=value;i>=m_nMin;i--)
{
if(i%10==0)
{
strValue=tr("%1").arg(value);
//qreal textWidth=fontMetrics().width(strValue);
qreal textHeight=fontMetrics().height();
QPointF rightPot(topPot.x(),startY);
QPointF leftPot(topPot.x()-m_long,startY);
painter->drawLine(rightPot,leftPot);
QPointF textPot(topPot.x()+m_long,startY+textHeight/2);
painter->drawText(textPot,strValue);
value-=10;
}
else if(i%5==0)
{
QPointF rightPot(topPot.x(),startY);
QPointF leftPot(topPot.x()-m_ok,startY);
painter->drawLine(rightPot,leftPot);
}
else
{
QPointF rightPot(topPot.x(),startY);
QPointF leftPot(topPot.x()-m_short,startY);
painter->drawLine(rightPot,leftPot);
}
startY+=increment;
}
painter->restore();
}
void myGauge6::drawUnderRect(QPainter *painter)
{
painter->save();
QLinearGradient rectGradient(m_underRect.topLeft(),m_underRect.topRight());
rectGradient.setColorAt(0.0,QColor(150,150,150));
rectGradient.setColorAt(0.1,QColor(100,100,100));
rectGradient.setColorAt(0.9,QColor(100,100,100));
rectGradient.setColorAt(1.0,QColor(150,150,150));
painter->setPen(Qt::NoPen);
painter->setBrush(rectGradient);
painter->drawRect(m_underRect);
painter->restore();
}
void myGauge6::drawTextRect(QPainter *painter)
{
painter->save();
QPointF topLeftPot(m_markX-3*m_long-m_leftSpace,m_markBottom+m_long);
QPointF bottomRightPot(m_markX-2*m_long,m_markBottom+m_leftSpace);
m_textRect=QRectF(topLeftPot,bottomRightPot);
painter->setPen(Qt::NoPen);
QLinearGradient textRectGradient(m_textRect.topLeft(),m_textRect.bottomLeft());
textRectGradient.setColorAt(0.0,QColor(180,180,180));
textRectGradient.setColorAt(0.2,QColor(100,100,100));
textRectGradient.setColorAt(0.8,QColor(100,100,100));
textRectGradient.setColorAt(1.0,QColor(180,180,180));
painter->setBrush(textRectGradient);
painter->drawRect(m_textRect);
painter->save();
painter->setPen(Qt::green);
painter->drawText(m_textRect,Qt::AlignVCenter|Qt::AlignHCenter,tr("%1").arg(m_currentValue));
painter->restore();
m_underRect=QRectF(QPointF(topLeftPot.x(),m_markY),QPointF(bottomRightPot.x(),m_markBottom));
m_gaugeTopLeftPot=QPointF(m_textRect.topLeft().x(),m_markY);
m_gaugeBottomRightPot=QPointF(m_textRect.bottomRight().x(),m_markBottom);
painter->restore();
}
void myGauge6::UpdateGraph()
{
if(m_bReverse)
{
m_currentValue-=0.5;
if(m_currentValue<=m_value)
{
updateTimer->stop();
}
}
else
{
m_currentValue+=0.5;
if(m_currentValue>=m_value)
{
updateTimer->stop();
}
}
update();
}
void myGauge6::setValue(qreal value)
{
if(value>m_value)
{
m_bReverse=false;
m_value=value;
}
else if(value<m_value)
{
m_bReverse=true;
m_value=value;
}
else
{
return ;
}
if(!m_bAnimate)
{
m_currentValue=value;
update();
return ;
}
updateTimer->start();
}
void myGauge6::setUseAntialiasing(bool use)
{
m_bUseAntialiasing=use;
update();
}
void myGauge6::setRange(int min, int max)
{
m_nMin=min;
m_nMax=max;
update();
}
void myGauge6::setAnimating(bool animate)
{
m_bAnimate=animate;
update();
}