etCameraTest.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "etCameraTest.h"
  2. #include "ui_etCameraTest.h"
  3. #include <QScrollBar>
  4. #include <QHBoxLayout>
  5. #include <QLabel>
  6. #include <QPalette>
  7. #include "CAppInfo.h"
  8. #include "awMsgBox.h"
  9. #include "logproc.h"
  10. #include "CCommonTools.h"
  11. etCameraTest::etCameraTest(QWidget *parent) :
  12. QWidget(parent),
  13. ui(new Ui::etCameraTest)
  14. {
  15. ui->setupUi(this);
  16. setStyleSheet(g_appInfoPtr->m_sQssStr);
  17. m_nCameraOpenStatus = 2;
  18. m_pinitTimer = std::make_shared<QTimer>();
  19. m_pinitTimer->setInterval(1000);
  20. connect(m_pinitTimer.get(), &QTimer::timeout, this, [&](){
  21. m_pinitTimer->stop();
  22. bool bOpened = true;
  23. if(!m_cam.open(0))
  24. {
  25. ShowMsg(QString::fromLocal8Bit("打开摄像头失败"), this);
  26. bOpened = false;
  27. }
  28. int inWidth = ui->widget_etct_camera->width();
  29. m_cam.set(CV_CAP_PROP_FRAME_WIDTH, inWidth);
  30. int inHeight = ui->widget_etct_camera->height();
  31. m_cam.set(CV_CAP_PROP_FRAME_HEIGHT, inHeight);
  32. m_cam.set(CV_CAP_PROP_FPS, 5);
  33. m_pVideoTimer->start();
  34. ui->tablewt_envTest->item(0, 0)->setText(QString::fromLocal8Bit("摄像头正常启用"));
  35. QWidget *widget = new QWidget;
  36. QHBoxLayout *layout = new QHBoxLayout;
  37. widget->setLayout(layout);
  38. QLabel *icon = new QLabel;
  39. if(bOpened)
  40. {
  41. ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("正常"));
  42. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  43. icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  44. }
  45. else
  46. {
  47. ui->tablewt_envTest->item(0, 1)->setText(QString::fromLocal8Bit("异常"));
  48. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  49. icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  50. }
  51. layout->addWidget(icon);
  52. layout->addStretch();
  53. ui->tablewt_envTest->setCellWidget(0, 2, widget);
  54. });
  55. m_pVideoTimer = std::make_shared<QTimer>();
  56. m_pVideoTimer->setInterval(200);
  57. connect(m_pVideoTimer.get(), &QTimer::timeout, this, [&](){
  58. cv::Mat frame;
  59. m_cam>>frame;
  60. if(frame.empty())
  61. {
  62. qDebug()<<"widgetCameraTest frame is empty";
  63. return;
  64. }
  65. QImage img = CCommonTools::Mat2QImage(frame);
  66. ui->widget_etct_camera->setAutoFillBackground(true);
  67. QPalette palette;
  68. palette.setBrush(QPalette::Window, QBrush(img.scaled(ui->widget_etct_camera->width(), ui->widget_etct_camera->height())));
  69. ui->widget_etct_camera->setPalette(palette);
  70. });
  71. }
  72. etCameraTest::~etCameraTest()
  73. {
  74. m_pVideoTimer->stop();
  75. m_cam.release();
  76. delete ui;
  77. }
  78. int etCameraTest::setUI(const int nLeft, const int nTop, const int nWidth)
  79. {
  80. setGeometry(nLeft, nTop, nWidth, g_appInfoPtr->m_fRate*507);
  81. ui->widget_etct_camera->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*30,
  82. g_appInfoPtr->m_fRate*536, g_appInfoPtr->m_fRate*300);
  83. ui->btn_IsNotSelf->setGeometry(ui->widget_etct_camera->x() + ui->widget_etct_camera->width() + g_appInfoPtr->m_fRate*20,
  84. ui->widget_etct_camera->y(), g_appInfoPtr->m_fRate*184, g_appInfoPtr->m_fRate*140);
  85. ui->btn_IsNotSelf->setIconSize(QSize(g_appInfoPtr->m_fRate*32, g_appInfoPtr->m_fRate*32));
  86. ui->btn_IsNotSelf->setIcon(QIcon(":/images/icon-is-not-self.png"));
  87. ui->btn_IsSelf->setGeometry(ui->btn_IsNotSelf->x(), ui->btn_IsNotSelf->y() + ui->btn_IsNotSelf->height() + g_appInfoPtr->m_fRate*20,
  88. ui->btn_IsNotSelf->width(), ui->btn_IsNotSelf->height());
  89. ui->btn_IsSelf->setIconSize(QSize(g_appInfoPtr->m_fRate*32, g_appInfoPtr->m_fRate*32));
  90. ui->btn_IsSelf->setIcon(QIcon(":/images/icon-is-self.png"));
  91. ui->label_HLine->setGeometry(ui->widget_etct_camera->x(), ui->widget_etct_camera->y() + ui->widget_etct_camera->height() + g_appInfoPtr->m_fRate*30,
  92. width() - g_appInfoPtr->m_fRate*30*2, g_appInfoPtr->m_fRate*1);
  93. ui->tablewt_envTest->setGeometry(ui->widget_etct_camera->x(), ui->label_HLine->y() + ui->label_HLine->height(),
  94. width() - g_appInfoPtr->m_fRate*30*2, height() - ui->label_HLine->y() - ui->label_HLine->height());
  95. ui->tablewt_envTest->setEditTriggers(QAbstractItemView::NoEditTriggers);
  96. //整行选中的方式
  97. ui->tablewt_envTest->setSelectionBehavior(QAbstractItemView::SelectRows);
  98. //设置为只能选中一行
  99. ui->tablewt_envTest->setSelectionMode(QAbstractItemView::SingleSelection);
  100. //隐藏列表头
  101. ui->tablewt_envTest->verticalHeader()->setVisible(false);
  102. //隐藏边框
  103. ui->tablewt_envTest->setShowGrid(false);
  104. //表头不高亮显示
  105. ui->tablewt_envTest->horizontalHeader()->setHighlightSections(false);
  106. //设置行数
  107. ui->tablewt_envTest->setRowCount(2);
  108. //设置列数
  109. ui->tablewt_envTest->setColumnCount(3);
  110. ui->tablewt_envTest->setHorizontalScrollMode(QTableWidget::ScrollPerPixel);
  111. ui->tablewt_envTest->horizontalScrollBar()->setSingleStep(g_appInfoPtr->m_fRate*5);
  112. QStringList sHeadStr;
  113. sHeadStr << QString::fromLocal8Bit("检测项") <<
  114. QString::fromLocal8Bit("值") <<
  115. QString::fromLocal8Bit("状态");
  116. ui->tablewt_envTest->setHorizontalHeaderLabels(sHeadStr);
  117. ui->tablewt_envTest->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  118. ui->tablewt_envTest->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
  119. ui->tablewt_envTest->verticalHeader()->setDefaultSectionSize(g_appInfoPtr->m_fRate*48);
  120. ui->tablewt_envTest->setColumnWidth(0, g_appInfoPtr->m_fRate*525);
  121. ui->tablewt_envTest->setColumnWidth(1, g_appInfoPtr->m_fRate*170);
  122. ui->tablewt_envTest->setColumnWidth(2, g_appInfoPtr->m_fRate*24);
  123. ui->tablewt_envTest->horizontalHeader()->setStretchLastSection(true);
  124. for(int i = 0; i < 2; i++)
  125. {
  126. ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("")));
  127. ui->tablewt_envTest->setItem(i, 1, new QTableWidgetItem(QString::fromLocal8Bit("")));
  128. /* QWidget *widget = new QWidget;
  129. QHBoxLayout *layout = new QHBoxLayout;
  130. widget->setLayout(layout);
  131. QLabel *icon = new QLabel;
  132. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
  133. icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
  134. layout->addWidget(icon);
  135. layout->addStretch();
  136. ui->tablewt_envTest->setCellWidget(i, 2, widget);*/
  137. }
  138. m_pinitTimer->start();
  139. return height();
  140. }
  141. int etCameraTest::widgetHeight()
  142. {
  143. return height();
  144. }
  145. int etCameraTest::getCheckStatus()
  146. {
  147. return m_nCameraOpenStatus;
  148. }
  149. void etCameraTest::on_btn_IsSelf_clicked()
  150. {
  151. ui->tablewt_envTest->item(1, 0)->setText(QString::fromLocal8Bit("视频显示的是电脑操作者本人"));
  152. ui->tablewt_envTest->item(1, 1)->setText(QString::fromLocal8Bit("正常"));
  153. QWidget *widget = new QWidget;
  154. QHBoxLayout *layout = new QHBoxLayout;
  155. widget->setLayout(layout);
  156. QLabel *icon = new QLabel;
  157. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  158. icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate * 12, g_appInfoPtr->m_fRate * 9));
  159. layout->addWidget(icon);
  160. layout->addStretch();
  161. ui->tablewt_envTest->setCellWidget(1, 2, widget);
  162. m_nCameraOpenStatus = 1;
  163. }
  164. void etCameraTest::on_btn_IsNotSelf_clicked()
  165. {
  166. ui->tablewt_envTest->item(1, 0)->setText(QString::fromLocal8Bit("视频显示的是电脑操作者本人"));
  167. ui->tablewt_envTest->item(1, 1)->setText(QString::fromLocal8Bit("请检查摄像头"));
  168. QWidget *widget = new QWidget;
  169. QHBoxLayout *layout = new QHBoxLayout;
  170. widget->setLayout(layout);
  171. QLabel *icon = new QLabel;
  172. icon->setFixedSize(QSize(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  173. icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate * 8, g_appInfoPtr->m_fRate * 8));
  174. layout->addWidget(icon);
  175. layout->addStretch();
  176. ui->tablewt_envTest->setCellWidget(1, 2, widget);
  177. m_nCameraOpenStatus = 0;
  178. }