提交 | 用户 | age
|
d3687b
|
1 |
#include "myHMIDisplay.h" |
Q |
2 |
#include <QPen> |
|
3 |
#include <QBrush> |
|
4 |
#include <QPainter> |
|
5 |
#include <QTimer> |
|
6 |
|
|
7 |
myHMIDisplay::myHMIDisplay(QWidget *parent) : QFrame(parent) |
|
8 |
{ |
|
9 |
QTimer* timer = new QTimer(this); |
|
10 |
timer->start(50); |
|
11 |
connect(timer,&QTimer::timeout,this,&myHMIDisplay::OnTimerd); |
|
12 |
// stEquipment equipment1 ={ "ABC",50,50,10,10,10,{3} }; |
|
13 |
// m_equipments.append(equipment1); |
|
14 |
m_equipments.append({1,u8"熔制区",8,20,20,5,5,{0}}); |
|
15 |
|
|
16 |
m_equipments.append({3,u8"龙门",40,15,40,3,5,{3}}); |
|
17 |
m_equipments.append({3,u8"车",25,25,6,4,2,{3}}); |
|
18 |
m_equipments.append({3,u8"车",35,25,6,4,2,{3}}); |
|
19 |
m_equipments.append({3,u8"车",45,25,6,4,2,{3}}); |
|
20 |
m_equipments.append({3,u8"车",55,25,6,4,2,{3}}); |
|
21 |
m_equipments.append({5,u8"龙门1",70,20,10,5,1,{3}}); |
|
22 |
m_equipments.append({5,u8"龙门2",80,20,10,5,1,{3}}); |
|
23 |
m_equipments.append({5,u8"车1",70,30,5,5,1,{3}}); |
|
24 |
m_equipments.append({5,u8"车2",75,30,5,5,1,{3}}); |
|
25 |
m_equipments.append({5,u8"车3",80,30,5,5,1,{3}}); |
|
26 |
m_equipments.append({5,u8"车4",85,30,5,5,1,{3}}); |
|
27 |
|
|
28 |
m_equipments.append({1,u8"冷车间",8,50,10,5,5,{3}}); |
|
29 |
m_equipments.append({7,u8"刷锅",20,50,10,5,5,{255}}); |
|
30 |
m_equipments.append({7,u8"磨锅",35,50,10,5,5,{3}}); |
|
31 |
m_equipments.append({7,u8"切割1",50,45,10,3,5,{3}}); |
|
32 |
m_equipments.append({7,u8"切割2",50,55,10,3,5,{3}}); |
|
33 |
m_equipments.append({7,u8"测量",65,50,10,5,5,{3}}); |
|
34 |
m_equipments.append({7,u8"酸洗",80,50,10,5,5,{3}}); |
|
35 |
|
|
36 |
m_equipments.append({1,u8"净化车间",8,80,20,5,5,{3}}); |
|
37 |
m_equipments.append({7,u8"烘烤",20,80,10,5,5,{3}}); |
|
38 |
m_equipments.append({7,u8"人工目检",40,80,10,5,5,{3}}); |
|
39 |
m_equipments.append({7,u8"8条直传",60,80,10,5,5,{3}}); |
|
40 |
m_equipments.append({7,u8"拆包",80,80,10,5,5,{3}}); |
|
41 |
} |
|
42 |
|
|
43 |
|
|
44 |
void myHMIDisplay::OnTimerd() |
|
45 |
{ |
|
46 |
nCount++; |
|
47 |
update(); |
|
48 |
} |
|
49 |
|
|
50 |
|
|
51 |
void myHMIDisplay::paintEvent(QPaintEvent *event) |
|
52 |
{ |
|
53 |
//控件的绘制工具,通过该工具我们可以进行常用的绘制了 |
|
54 |
QPainter painter(this); |
|
55 |
painter.setRenderHint(QPainter::Antialiasing); |
|
56 |
|
|
57 |
//取得画布大小和中心方形。 |
|
58 |
int w = this->rect().width(); |
|
59 |
int h = this->rect().height(); |
|
60 |
int s; |
|
61 |
int wmargin=0; |
|
62 |
int hmargin=0; |
|
63 |
if (w>h) { |
|
64 |
s= h; |
|
65 |
wmargin=(w-s)/2; |
|
66 |
|
|
67 |
}else{ |
|
68 |
s= w; |
|
69 |
hmargin=(h-s)/2; |
|
70 |
} |
|
71 |
//画边框 |
|
72 |
QPen pen0(QColor(0x4D9CF8)); |
|
73 |
painter.setPen(pen0); |
|
74 |
painter.drawRoundRect(this->rect(),5,5); |
|
75 |
|
|
76 |
int thick = s/20; //线宽 |
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
QPen pen1(QColor(80,80,80)); |
|
81 |
|
|
82 |
//绘制弧线的背景 |
|
83 |
pen1.setWidth(thick); |
|
84 |
pen1.setCapStyle(Qt::RoundCap); |
|
85 |
// pen.setColor(QColor("#4D9CF8")); |
|
86 |
painter.setPen(pen1); |
|
87 |
|
|
88 |
QRectF in_rectangle(wmargin+10, hmargin+s/4, s-20, s-20); |
|
89 |
int in_startAngle = (0) * 16; |
|
90 |
int in_spanAngle = (180)*16; |
|
91 |
painter.drawArc(in_rectangle,in_startAngle,in_spanAngle); |
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
QPen pen2(lineColor); |
|
96 |
QBrush brush1(QColor(0xbb,0xbb,0x66,0xff)); |
|
97 |
QBrush brush2(QColor(0x88,0x88,0x66,0xff)); |
|
98 |
QBrush brush3(QColor(0x88,0x88,0x66,0xff)); |
|
99 |
|
|
100 |
QBrush RunningBrush(RunningColor); |
|
101 |
QBrush StopedBrush(StopedColor); |
|
102 |
QBrush WarningBrush(WarningColor); |
|
103 |
|
|
104 |
QPen pen3(nameColor); |
|
105 |
//绘制弧线的背景 |
|
106 |
// pen2.setWidth(thick); |
|
107 |
// pen1.setCapStyle(Qt::RoundCap); |
|
108 |
// pen.setColor(QColor("#4D9CF8")); |
|
109 |
painter.setPen(pen2); |
|
110 |
// painter.setBrush(brush1); |
|
111 |
int n=m_equipments.count(); |
|
112 |
for (int i=0;i<n;i++){ |
|
113 |
stEquipment theequipment = m_equipments.at(i); |
|
114 |
theequipment.stat=0xff; |
|
115 |
theequipment.bRunning=1; |
|
116 |
theequipment.bStoped=1; |
|
117 |
theequipment.bWarning=1; |
|
118 |
int thetype = theequipment.nType; |
|
119 |
int ex = theequipment.x * w /100 ; |
|
120 |
int ey = theequipment.y * h /100 ; |
|
121 |
int el = theequipment.l * w /100 ; |
|
122 |
int ew = theequipment.w * w /100; |
|
123 |
int eh = theequipment.h/2 * h /100; |
|
124 |
QRect rect2(ex-el/2, ey-ew/2, el,ew); |
|
125 |
if (thetype & eqTypeSimple) { |
|
126 |
//draw Top |
|
127 |
painter.setPen(pen2); |
|
128 |
painter.setBrush(brush1); |
|
129 |
//painter.fillRect(rect2,brush1); |
|
130 |
painter.drawRect(rect2); |
|
131 |
//draw side; |
|
132 |
QPolygon polygon1; |
|
133 |
QPoint points[10]; |
|
134 |
points[0]=QPoint(ex-el/2,ey-ew/2); |
|
135 |
points[1]=QPoint(ex-el/2-eh,ey-ew/2+eh); |
|
136 |
points[2]=QPoint(ex-el/2-eh,ey+ew/2+eh); |
|
137 |
points[3]=QPoint(ex-el/2,ey+ew/2); |
|
138 |
|
|
139 |
painter.setBrush(brush2); |
|
140 |
painter.drawPolygon(points,4); |
|
141 |
painter.drawPolyline(points,4); |
|
142 |
|
|
143 |
points[0]=QPoint(ex-el/2,ey+ew/2); |
|
144 |
points[1]=QPoint(ex-el/2-eh,ey+ew/2+eh); |
|
145 |
points[2]=QPoint(ex+el/2-eh,ey+ew/2+eh); |
|
146 |
points[3]=QPoint(ex+el/2,ey+ew/2); |
|
147 |
painter.setBrush(brush3); |
|
148 |
painter.drawPolygon(points,4); |
|
149 |
painter.drawPolyline(points,4); |
|
150 |
} |
|
151 |
if (thetype & eqTypeText){ |
|
152 |
//draw Name |
|
153 |
QString sName = theequipment.sName; |
|
154 |
painter.setPen(pen3); |
|
155 |
|
|
156 |
painter.setFont(QFont("Microsoft YaHei",s/32)); |
|
157 |
//painter.drawText(this->rect(),s1); |
|
158 |
painter.drawText(rect2,Qt::AlignCenter,sName); |
|
159 |
} |
|
160 |
if (thetype & eqTypeIndicator ){ |
|
161 |
//draw indicator 指示器 |
|
162 |
painter.setBrush(brush3); |
|
163 |
QRect rect3(ex-el/4,ey-ew*7/8,el/2,ew/4); |
|
164 |
painter.fillRect(rect3,brush1); |
|
165 |
painter.drawRect(rect3); |
|
166 |
|
|
167 |
QRect rect31(ex-el/4,ey-ew*7/8,el/8,ew/4); |
|
168 |
if (theequipment.bRunning){ |
|
169 |
painter.fillRect(rect31,RunningBrush); |
|
170 |
} |
|
171 |
// painter.drawRect(rect31); |
|
172 |
|
|
173 |
QRect rect32(ex-el/16,ey-ew*7/8,el/8,ew/4); |
|
174 |
if (theequipment.bStoped){ |
|
175 |
//painter.fillRect(rect32,StopedBrush); |
|
176 |
painter.setBrush(StopedBrush); |
|
177 |
} |
|
178 |
|
|
179 |
painter.drawRect(rect32); |
|
180 |
|
|
181 |
QRect rect33(ex+2*el/16,ey-ew*7/8,el/8,ew/4); |
|
182 |
if (theequipment.bWarning){ |
|
183 |
painter.setBrush(WarningBrush); |
|
184 |
painter.fillRect(rect33,WarningBrush); |
|
185 |
} |
|
186 |
painter.drawRect(rect33); |
|
187 |
|
|
188 |
|
|
189 |
} |
|
190 |
} |
|
191 |
|
|
192 |
// in_startAngle = (nOffset + 90+270) * 16; |
|
193 |
// in_spanAngle = (45)*16; |
|
194 |
// painter.drawArc(in_rectangle,in_startAngle,in_spanAngle); |
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
return; |
|
199 |
// QRect rect2(wmargin + 20, hmargin +20, s-40,s-40); |
|
200 |
QString s1; |
|
201 |
/* |
|
202 |
//添加绘制的字体和字号 |
|
203 |
s1=QString::number(int(position)); |
|
204 |
int l = s1.length(); |
|
205 |
double fr = sqrt(l*l + 4); |
|
206 |
painter.setFont(QFont("Microsoft YaHei",(s-20)/fr)); |
|
207 |
// painter.drawText(this->rect(),s1); |
|
208 |
painter.drawText(rect2,Qt::AlignCenter,s1); |
|
209 |
*/ |
|
210 |
} |
|
211 |
|
|
212 |
void myHMIDisplay::SetRange(int n) |
|
213 |
{ |
|
214 |
range=n; |
|
215 |
} |
|
216 |
void myHMIDisplay::SetPosition(float n) |
|
217 |
{ |
|
218 |
position=n; |
|
219 |
update(); |
|
220 |
} |
|
221 |
void myHMIDisplay::SetTitle(QString title) |
|
222 |
{ |
|
223 |
sTitle = title; |
|
224 |
} |
|
225 |
void myHMIDisplay::SetUnit(QString unit) |
|
226 |
{ |
|
227 |
sUnit = unit; |
|
228 |
} |
|
229 |
void myHMIDisplay::SetColor(QColor Start,QColor End) |
|
230 |
{ |
|
231 |
color1 = Start; |
|
232 |
color2 = End; |
|
233 |
} |
|
234 |
|
|
235 |
|
|
236 |
void drawArc() |
|
237 |
{ |
|
238 |
int nCount=0; |
|
239 |
int position; |
|
240 |
int range=0; |
|
241 |
int thick=2; |
|
242 |
int wmargin=10; |
|
243 |
int hmargin=10; |
|
244 |
int s=2; |
|
245 |
|
|
246 |
QColor color1,color2; |
|
247 |
|
|
248 |
QPainter painter; |
|
249 |
QString sTitle="1122"; |
|
250 |
QString sUnit; |
|
251 |
|
|
252 |
int nOffset = nCount*5 % 180; |
|
253 |
|
|
254 |
position = nOffset *range /180; |
|
255 |
|
|
256 |
QPen pen2(QColor(0x4D9CF8)); |
|
257 |
pen2.setWidth(thick); |
|
258 |
pen2.setCapStyle(Qt::RoundCap); |
|
259 |
|
|
260 |
QRectF sec_rectangle(wmargin+10, hmargin+s/4, s-20, s-20); |
|
261 |
int sec_startAngle = 180 * 16; |
|
262 |
int sec_spanAngle = -1*((position / range)*180)*16; |
|
263 |
|
|
264 |
QConicalGradient gradient; |
|
265 |
gradient.setCenter(sec_rectangle.center()); |
|
266 |
gradient.setAngle(190); |
|
267 |
gradient.setColorAt(0, color2); |
|
268 |
gradient.setColorAt(0.5, color2); |
|
269 |
gradient.setColorAt(1, color1); |
|
270 |
|
|
271 |
|
|
272 |
// int arcLengthApproximation = m_width + m_width / 3; |
|
273 |
QPen pen3(QBrush(gradient), thick); |
|
274 |
pen3.setCapStyle(Qt::RoundCap); |
|
275 |
painter.setPen(pen3); |
|
276 |
painter.drawArc(sec_rectangle,sec_startAngle,sec_spanAngle); |
|
277 |
|
|
278 |
|
|
279 |
|
|
280 |
//绘制文本,绘制白色的文字 |
|
281 |
painter.setPen(QPen(QColor(255,255,255))); |
|
282 |
QString s1; |
|
283 |
s1=sTitle; |
|
284 |
//显示标题 |
|
285 |
QRect rect2(wmargin + 20, hmargin +10, s-40,s/8); |
|
286 |
painter.setFont(QFont("Microsoft YaHei",s/12)); |
|
287 |
// painter.drawText(this->rect(),s1); |
|
288 |
painter.drawText(rect2,Qt::AlignCenter,s1); |
|
289 |
//显示前后刻度 |
|
290 |
painter.setFont(QFont("Microsoft YaHei",s/18)); |
|
291 |
s1="0"; |
|
292 |
// painter.drawText(this->rect(),s1); |
|
293 |
rect2.setRect(wmargin,hmargin+s*3/4,20,s/16); |
|
294 |
painter.drawText(rect2,Qt::AlignCenter,s1); |
|
295 |
|
|
296 |
s1=QString::number(range); |
|
297 |
// painter.drawText(this->rect(),s1); |
|
298 |
rect2.setRect(wmargin+s-30,hmargin+s*3/4,40,s/16); |
|
299 |
painter.drawText(rect2,Qt::AlignCenter,s1); |
|
300 |
|
|
301 |
//显示当前数量 |
|
302 |
painter.setFont(QFont("Microsoft YaHei",s/8)); |
|
303 |
s1=QString::number(position); |
|
304 |
// painter.drawText(this->rect(),s1); |
|
305 |
rect2.setRect(wmargin+10,hmargin+10-s/20,s-20,s-20); |
|
306 |
painter.drawText(rect2,Qt::AlignCenter,s1); |
|
307 |
//显示单位 |
|
308 |
painter.setFont(QFont("Microsoft YaHei",s/16)); |
|
309 |
s1=sUnit; |
|
310 |
// painter.drawText(this->rect(),s1); |
|
311 |
rect2.setRect(wmargin+10,hmargin+10+s/10,s-20,s-20); |
|
312 |
painter.drawText(rect2,Qt::AlignCenter,s1); |
|
313 |
} |