QuakeGod
2023-05-30 acd576fbf6ca6086455d7f82140dd495237c6803
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#include "dialogscence3.h"
#include "ui_dialogscence3.h"
 
#include <QChart>
//#include <QtCharts>
//#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QLineSeries>
#include <QPieSeries>
#include <QAreaSeries>
#include <QSplineSeries>
#include <QScatterSeries>
#include <QBarCategoryAxis>
#include <QValueAxis>
#include <QLegendMarker>
 
#include <QDateTime>
#include <QtDebug>
#include <QDialog>
#include <QKeyEvent>
#include <QTimer>
#include <QTimerEvent>
#include <QDateTime>
 
#include "mainwindow.h"
 
DialogScence3::DialogScence3(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogScence3)
{
    ui->setupUi(this);
    // 产生一个定时器
    QTimer *timer = new QTimer(this);
 
    //连接这个定时器的信号和槽,利用定时器的timeout(),从而触发clear()槽函数
    connect(timer, &QTimer::timeout, this, &DialogScence3::timerProc);
 
    //开始定时器,并设定定时周期,每隔5秒就会重启定时器,会重复触发定时,除非你利用stop()将定时器关掉
    timer->start(100);
 
    // 仅仅启动定时器一次
    //  timer->setSingleShot(true);
    InitDisplay();
 
    //label->setPixmap(QPixmap("./pic.jpg"));
    //  ui->label_27->setPixmap(QPixmap(":/Image/JPEG.webp"));
    ShowParams();
    //  ui->centralwidget->installEventFilter(this);//在label上安装事件过滤器,this指针指定当事件发生时调用当前类中的事件过滤器进行处理
}
 
DialogScence3::~DialogScence3()
{
    delete ui;
}
 
void DialogScence3::myDraw(QWidget *widget)
{
    QPainter painter(widget);
    painter.setRenderHint(QPainter::Antialiasing);
 
    //取得画布大小和中心方形。
    int w = this->rect().width();
    int h = this->rect().height();
    int s;
    int wmargin=10;
    int hmargin=10;
    int ButtomMgn = 32;
    QRect rect1(wmargin,hmargin,w-wmargin*2,h-hmargin*2 -ButtomMgn);
    if (w>h) {
        s= h;
        wmargin=(w-s)/2;
 
    }else{
        s= w;
        hmargin=(h-s)/2;
    }
    //画边框
    QColor color1(0xFF6060);
    QColor color2(0x60F080);
    QColor color3(0x6080FF);
    QBrush brush1(QColor(0xFFB060));
 
    QConicalGradient gradient;
    gradient.setCenter(rect1.center());
    gradient.setAngle(360*16 - nPos*4);
 
    gradient.setColorAt(0, color1);
    gradient.setColorAt(0.3, color2);
    gradient.setColorAt(0.6, color3);
    gradient.setColorAt(1, color1);
 
    QPen pen0(gradient,8);
    painter.setPen(pen0);
 
    painter.drawRoundRect(rect1,5,5);
}
 
bool DialogScence3::eventFilter(QObject * obj, QEvent * eve)
{
    /*
    if(obj == ui->centralwidget && eve->type() == QEvent::Paint)//发生绘图事件,且是在label上发生的
    {
        if(flag == 1)//标志位为1才在label上绘图,否者不绘图
        {
            myDraw(ui->centralwidget);
            return true;
        }
        else
            return false;
    }
    else
*/
    return QDialog::eventFilter(obj,eve);//其它绘图事件交给父类处理
}
 
 
void DialogScence3::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_Enter:
        break;
    case Qt::Key_F11:
        if (bfull==0)
        {
            oldsize = this->size();
            //     showMaximized();
            //      setWindowFlags ( Qt::FramelessWindowHint);
            showFullScreen();
            bfull = 1;
        }
        break;
    case Qt::Key_Escape:
        if (bfull == 1){
            //   setWindowFlags (Qt::Window );
            showNormal();
            //   this->resize(oldsize);
            bfull=0;
        }
        else {QDialog::keyPressEvent(event);}
        break;
    default:
        QDialog::keyPressEvent(event);
    }
}
 
void DialogScence3::InitDisplay()
{
    qDebug() << "Scence2 Show Charts";
 
    ///*
//    ui->frame_8->SetValue(55);
//    ui->frame_8->SetTarget(120);
    auto charview1 = new QChartView;
    ui->horizontalLayout_13->addWidget(charview1);
    auto chart1 = CreateBarChart();
    charview1->setChart(chart1);
    charview1->setStyleSheet("background: transparent");
    chart1->setBackgroundVisible(false);
 
    auto charview2 = new QChartView;
    ui->horizontalLayout_17->addWidget(charview2);
    auto chart2 = createPieChart();
    charview2->setChart(chart2);
    charview2->setStyleSheet("background: transparent");
    chart2->setBackgroundVisible(false);
 //   auto charview3 = new QChartView;
//    ui->horizontalLayout_15->addWidget(charview3);
//    auto chart3 = createAreaChart();
//    charview3->setChart(chart3);
 
    auto charview4 = new QChartView;
    ui->horizontalLayout_19->addWidget(charview4);
    auto chart4 = createLineChart();
    charview4->setChart(chart4);
    charview4->setStyleSheet("background: transparent");
    chart4->setBackgroundVisible(false);
 
    auto charview5 = new QChartView;
//    ui->horizontalLayout_20->addWidget(charview5);
    auto chart5 = createSpLineChart();
    charview5->setChart(chart5);
    charview5->setStyleSheet("background: transparent");
    chart5->setBackgroundVisible(false);
 
//*/
    /*
    auto charview6 = new QChartView;
    ui->horizontalLayout_11->addWidget(charview6);
    auto chart6 = createScatterChart();
    charview6->setChart(chart6);
*/
    ui->frame_heat->SetRange(350);
    ui->frame_heat->SetTitle(u8"坩埚温度");
    ui->frame_heat->SetValue(237);
    ui->frame_heat->SetTarget(250);
    ui->frame_heat->SetUnit(u8"℃");
    ui->frame_heat->SetColor(QColor(200,200,100),QColor(200,100,100));
 
    qDebug() << "Scence2 Show Charts Done";
 
}
 
void DialogScence3::timerProc()
{
    QString s1;
    s1 = QDateTime::currentDateTime().toString(u8"M月dd ddd h:mm:ss"); //"yyyy-MM-dd HH:mm:ss"
    ui->label_Time_2->setText(s1);
    nPos+=1;
    //  ui->centralwidget->repaint();
    this->repaint();
}
 
void DialogScence3::paintEvent(QPaintEvent *event)
{
 
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
 
    //取得画布大小和中心方形。
    int w = this->rect().width();
    int h = this->rect().height();
    int s;
    int wmargin=10;
    int hmargin=10;
    int ButtomMgn = 0;
    QRect rect1(wmargin,hmargin,w-wmargin*2,h-hmargin*2 -ButtomMgn);
    if (w>h) {
        s= h;
        wmargin=(w-s)/2;
 
    }else{
        s= w;
        hmargin=(h-s)/2;
    }
    //画边框
    //    QPen pen0(QColor("#FFFFFF"));
    //    painter.setPen(pen0);
    //   painter.drawRoundRect(this->rect(),5,5);
 
    QColor color1(0xFF6060);
    QColor color2(0x60F080);
    QColor color3(0x6080FF);
    QBrush brush1(QColor(0xFFB060));
 
    QConicalGradient gradient;
    gradient.setCenter(rect1.center());
    gradient.setAngle(360*16 - nPos*4);
 
    gradient.setColorAt(0, color1);
    gradient.setColorAt(0.3, color2);
    gradient.setColorAt(0.6, color3);
    gradient.setColorAt(1, color1);
 
    QPen pen0(gradient,8);
    painter.setPen(pen0);
 
    painter.drawRoundRect(rect1,5,5);
 
 
 
}
 
void DialogScence3::resizeLabelFont(QLabel * label1,int lines, const QString & color)
{
    QString s1;
    double height = label1->height();
    height = label1->geometry().height();
    //    s1=QString::number(height);
    //    label1->setText(s1);
    //auto font1=label1->font();
    //font1.setPointSize(height/5);
    //label1->setFont(font1);
    QString colorseg;
    if (!color.isEmpty()) {colorseg= QString("color:%1;").arg(color);}
    int sheight=height/1.0/(lines+0.5);
    if (sheight>80) {sheight=80;}
    qDebug()<<"height" << height << "lines" << lines << "sHeight" << sheight;
    s1=QString("QLabel{font:%1px;%2}").arg(sheight).arg(colorseg);
    label1->setStyleSheet(s1);
    //label1->setScaledContents(true);
}
 
void DialogScence3::resizeCtrls()
{
    /*
    *{
    background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(40, 60, 100, 255), stop:1 rgba(20, 40, 80, 255));
        font-size:18pt;
        font-style:MingLiU-ExtB;
    }
 
    QFrame{
        background-color: rgb(0,30,80,160);
        border-radius:10px;
    border:1px solid rgb(100,169,139);
    }
QLabel{ background:rgba(85,170,255,0);   color:white; font-style:MingLiU-ExtB;  font-size:22pt; }
QLabel:hover{  background:rgba(85,170,255,0);  color:white; font-style:MingLiU-ExtB;   font-size:22pt;  }
QLabel:TimeDisplay{ background:rgba(85,170,255,0);  color:white;  font-style:MingLiU-ExtB;  font-size:22pt; }
 */
    resizeLabelFont(ui->label_4,8);
 
    resizeLabelFont(ui->label_5,12);
    resizeLabelFont(ui->label_32,1);
    resizeLabelFont(ui->label_2,2);
    resizeLabelFont(ui->label_3,2);
    resizeLabelFont(ui->label_Time_2,3);
 
 
}
 
void DialogScence3::resizeEvent(QResizeEvent *event)
{
    QDialog::resizeEvent(event);
 
    resizeCtrls();
}
void DialogScence3::ShowParams()
{
 
}
 
void DialogScence3::setChartAttribute(QChart *chart, QString title)
{
    // 创建默认坐标轴
    chart->createDefaultAxes();
    // 设置标题
    chart->setTitle(title);
    // 设置data,在设置标题时使用
    chart->setData(Qt::UserRole, title);
}
 
QChart * DialogScence3::CreateBarChart()
{
    // 创建QChart对象
    QChart *chart = new QChart();
    // 创建饼图系列对象
    QBarSeries *series = new QBarSeries(chart);
 
    qsrand(QDateTime::currentMSecsSinceEpoch() % 20000);
 
    // 添加图表值
    for(int count = 0; count < 1; ++count)
    {
        // 创建曲线图系列对象
        QBarSet *set = new QBarSet(QString(65 + count));
        for(int index = 0; index < 3; ++index)
        {
            set->append(33 + rand() % 50);
        }
        series->append(set);
    }
    series->setLabelsPosition(QAbstractBarSeries::LabelsOutsideEnd);
    series->setLabelsVisible(true);
    //series->setLabelsPrecision(2);
    chart->setTheme(QChart::ChartThemeBlueCerulean);
    chart->setAnimationOptions(QChart::SeriesAnimations);
    // 将series添加到QChart对象中
    chart->addSeries(series);
    //坐标轴//
    QStringList categories;
    categories <<u8"A班"<<u8"B班"<<u8"C班";
    QBarCategoryAxis * axisX = new QBarCategoryAxis();
    axisX->append(categories);
    chart->addAxis(axisX,Qt::AlignBottom);
    series->attachAxis(axisX);
    QValueAxis *axisY = new QValueAxis();
    axisY->setRange(0,100);
    axisY->setLabelsVisible(false);
    chart->addAxis(axisY,Qt::AlignLeft);
    series->attachAxis(axisY);
 
    // 设置通用属性
//    setChartAttribute(chart, u8"班组生产数据");
    chart->setTitle(u8"班组生产数据");
    QFont font1;
    QFont font2;
    font1.setPixelSize(32);
    font2.setPixelSize(24);
    //chart->setFont(QFont("宋体",32));
    chart->setTitleFont(font1);
    chart->axisX()->setLabelsFont(font2);
    chart->axisY()->setLabelsFont(font2);
    chart->legend()->setVisible(false);
    // 返回QChart对象
    return chart;
}
 
QChart *DialogScence3::createPieChart()
{
    // 创建QChart对象
    QChart *chart = new QChart();
    // 创建饼图系列对象
    QPieSeries *series = new QPieSeries(chart);
    // 遍历假数据容器将数据添加到series对象中
 
    qsrand(QDateTime::currentMSecsSinceEpoch() % 20000);
 
    // 添加图表值
/*
    for(int index = 0; index < 2; ++index)
    {
        series->append(QString(65 + index), rand() % 100);
    }
*/
 
    //定义标签
    static const QStringList list_pie_label = {
        u8"设备故障率  6%",u8"设备开动率 94%",
    };
 
    series->append(u8"设备故障率  6%",6);
    series->append(u8"设备开动率 94%",94);
    series->setPieSize(0.8);
    series->setHoleSize(0.66);
    series->setLabelsPosition(QPieSlice::LabelOutside);
    series->setLabelsVisible(true);
 
    // 将series添加到QChart对象中
    series->slices().at(0)->setColor(QColor(234,144,92));
    series->slices().at(1)->setColor(QColor(155,217,254));
    series->slices().at(0)->setLabel(u8"6%");
    series->slices().at(1)->setLabel(u8"94%");
    series->slices().at(1)->setColor(QColor(155,217,254));
 
    series->slices().at(0)->setLabelColor(QColor(180,220,255));
    series->slices().at(1)->setLabelColor(QColor(180,220,255));
    //series->slices().at(0)->setLabel("10%");
    QFont font1(u8"黑体");
    font1.setPixelSize(24);
    series->slices().at(0)->setLabelFont(font1);
    series->slices().at(1)->setLabelFont(font1);
    series->setPieStartAngle(30);
    series->setPieEndAngle(30+360);
    chart->addSeries(series);
    chart->setAnimationOptions(QChart::SeriesAnimations);
 
    //设置图例标签
    QList<QLegendMarker*> list_markers = chart->legend()->markers(series);
    for (int i = 0; i < list_markers.size(); i++) {
        list_markers[i]->setLabel(list_pie_label[i]);
    }
 
    // 设置通用属性
//    setChartAttribute(chart, u8"饼图");
    QFont font2;
    font2.setPixelSize(24);
    chart->legend()->setFont(font2);
    chart->legend()->setAlignment(Qt::AlignRight);
    chart->legend()->setLabelColor(QColor(180,220,255));
    //chart->legend()->detachFromChart();
    // 返回QChart对象
    return chart;
}
 
QChart *DialogScence3::createAreaChart()
{
    // 创建QChart对象
    QChart *chart = new QChart();
 
    // 添加图表值
    //! 创建区域图系列对象
    //! 区域图的系列对象
    QLineSeries *lowerSeries = nullptr;
    for(int count = 1; count != 3; ++count)
    {
        // 创建折线图系列对象
        QLineSeries *upperSeries = new QLineSeries(chart);
        for(int index = 1; index != 5; ++index)
        {
            upperSeries->append(index , index * count * 5 + rand() % 5);
        }
        // 将series添加到QChart对象中
        QAreaSeries *series = new QAreaSeries(upperSeries, lowerSeries);
        lowerSeries = upperSeries;
        chart->addSeries(series);
    }
 
    // 设置通用属性
    setChartAttribute(chart, u8"面积图 ");
    // 返回QChart对象
    return chart;
}
 
QChart *DialogScence3::createLineChart()
{
    // 创建QChart对象
    QChart *chart = new QChart();
 
    // 添加图表值
    for(int count = 0; count < 1; ++count)
    {
        // 创建折线图系列对象
        QLineSeries *series = new QLineSeries(chart);
 
        series->append(0, 0);
        for(int index = 1; index < 15; ++index)
        {
            series->append(index, 100 - (rand() % 30));
        }
 
        // 将series添加到QChart对象中
        series->setPointsVisible(true);
        series->setPointLabelsVisible(true);
        series->setPointLabelsColor(QColor(255,255,255));
        chart->addSeries(series);
    }
    //坐标轴//
    /*
    QStringList categories;
    categories <<u8"A班"<<u8"B班"<<u8"C班";
    QBarCategoryAxis * axisX = new QBarCategoryAxis();
    axisX->append(categories);
    chart->addAxis(axisX,Qt::AlignBottom);
    series->attachAxis(axisX);
    QValueAxis *axisY = new QValueAxis();
    axisY->setRange(0,100);
    axisY->setLabelsVisible(false);
    chart->addAxis(axisY,Qt::AlignLeft);
    series->attachAxis(axisY);
*/
    chart->setAnimationOptions(QChart::SeriesAnimations);
    chart->createDefaultAxes();
    chart->setTitle(u8"月份质量数据");
    QFont font1(u8"黑体");
    font1.setPixelSize(32);
    chart->setTitleFont(font1);
    chart->setTitleBrush(QColor(255,255,255));
    // 设置通用属性
//    setChartAttribute(chart, u8"月份质量数据 ");
    // 返回QChart对象
    return chart;
}
 
QChart *DialogScence3::createSpLineChart()
{
    // 创建QChart对象
    QChart *chart = new QChart();
 
    // 添加图表值
    for(int count = 0; count != 3; ++count)
    {
        // 创建曲线图系列对象
        QSplineSeries *series = new QSplineSeries(chart);
        for(int index = 0; index != 5; ++index)
        {
            series->append(index * count, index * count - rand() % 8);
        }
        // 将series添加到QChart对象中
        chart->addSeries(series);
    }
 
    // 设置通用属性
    setChartAttribute(chart, u8"曲线图 ");
    chart->setAnimationOptions(QChart::SeriesAnimations);
    // 返回QChart对象
    return chart;
}
 
QChart *DialogScence3::createScatterChart()
{
    // 创建QChart对象
    QChart *chart = new QChart();
 
    // 添加图表值
    for(int count = 0; count != 3; ++count)
    {
        // 创建曲线图系列对象
        QScatterSeries *series = new QScatterSeries(chart);
        for(int index = 0; index != 5; ++index)
        {
            series->append(index , index * count * 5 + rand() % 5);
        }
        // 将series添加到QChart对象中
        chart->addSeries(series);
    }
    chart->setAnimationOptions(QChart::SeriesAnimations);
    // 设置通用属性
    setChartAttribute(chart, u8"散点图 ");
    // 返回QChart对象
    return chart;
}
 
/*
void DialogScence3::on_comboBox_currentIndexChanged(int index)
{
    // 拿到chart容器中相同索引位置的QChart对象,并将其设置到view对象上
    QChart *chart = m_chartVector[index];
    m_chartView->setChart(chart);
}
 
void DialogScence3::on_legendGroupButtonClicked(QAbstractButton *button)
{
    // 拿到发送信号的单选按钮
    QRadioButton *btn = dynamic_cast<QRadioButton *>(button);
    // 遍历图表容器,将legend的aign属性设为当前按钮对应的属性状态
    foreach(QChart *chart, m_chartVector)
    {
        //! 拿到当前单选按钮的属性值并强转为Qt::Alignment类型
        //!
        Qt::Alignment align = static_cast<Qt::Alignment>(btn->property("LegendValue").toUInt());
        // 将类型值设置到当前图表上
        chart->legend()->setAlignment(align);
    }
}
 
void DialogScence3::on_animationGroup_buttonClicked(QAbstractButton *button)
{
    // 拿到发送信号的单选按钮
    QRadioButton *btn = dynamic_cast<QRadioButton *>(button);
    // 遍历图表容器,将chart的动画设为当前按钮对应的属性状态
    foreach(QChart *chart, m_chartVector)
    {
        //! 拿到当前单选按钮的属性值并强转为Qt::AnimationOption
        //! 其属性值为对应动画的int值,可通过帮助查看具体值
        QChart::AnimationOption animation = static_cast<QChart::AnimationOption>(btn->property("AnimationValue").toUInt());
        // 将类型值设置到当前图表上
        chart->setAnimationOptions(animation);
    }
 
}
 
void DialogScence3::on_otherGroup_buttonClicked(QAbstractButton *button)
{
    // 拿到发送信号的复选按钮
    QCheckBox *btn = dynamic_cast<QCheckBox *>(button);
    // 判断所点击按钮的勾选状态
    bool checked = btn->isChecked();
    // 通过按钮动态属性判断按钮操作
    switch(btn->property("btnType").toInt())
    {
    case 0:
    {
        // 设置view的防锯齿,其还包含其他防锯齿选项,可查看帮助了解
        m_chartView->setRenderHint(QPainter::Antialiasing, checked);
        break;
    }
    case 1:
    {
        foreach(QChart *chart, m_chartVector)
        {
            // 通过三目运算符判断标题是否获取data中的值
            QString title = checked? chart->data(Qt::UserRole).toString(): "";
            // 设置标题
            chart->setTitle(title);
        }
        break;
    }
    default:
        break;
    }
 
}
 
*/