JIoMapPage.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #ifndef JIOMAPPAGE_H
  2. #define JIOMAPPAGE_H
  3. #include "TreeViewManagerHead.h"
  4. #include "Src/common/JLogAllOutput.h"
  5. #include <QMouseEvent>
  6. class IoStateLab : public QLabel {
  7. Q_OBJECT
  8. public:
  9. IoStateLab(QWidget* parent = nullptr)
  10. : QLabel(parent)
  11. {
  12. setFixedSize(30, 30);
  13. }
  14. public:
  15. /**设置io 轴名称
  16. */
  17. void setIoName(const QString& strName)
  18. {
  19. m_strName = strName;
  20. UpDataIoStatus(true);
  21. }
  22. void UpDataIoStatus(bool isInit)
  23. {
  24. if (m_pCamera == nullptr)
  25. {
  26. m_pCamera = ns_module::CViewInterface::GetInstance();
  27. if (m_pCamera == nullptr)
  28. {
  29. // 为了调试准备的
  30. JLogAllOutput::cmd_error("io 轴设置失败");
  31. return;
  32. }
  33. }
  34. if (isInit)
  35. {
  36. // 信号量
  37. ns_db::DIGIT_IO_LEVEL nOutput;
  38. m_pCamera->GetViewMotion()->GetIoStatus(m_strName.toStdString(), nOutput);
  39. m_bClicked = nOutput;
  40. }
  41. else
  42. {
  43. m_pCamera->GetViewMotion()->SetIoStatus(m_strName.toStdString(), m_bClicked);
  44. }
  45. }
  46. protected:
  47. void paintEvent(QPaintEvent* event) override
  48. {
  49. QLabel::paintEvent(event);
  50. QPainter painter(this);
  51. painter.setRenderHint(QPainter::Antialiasing);
  52. int radius = qMin(width(), height()) / 2 - 5;
  53. int x = (width() - 2 * radius) / 2;
  54. int y = (height() - 2 * radius) / 2;
  55. painter.setPen(Qt::NoPen);
  56. painter.setBrush(m_bClicked ? Qt::green : Qt::red);
  57. painter.drawEllipse(x, y, 2 * radius, 2 * radius);
  58. }
  59. void mousePressEvent(QMouseEvent* event) override
  60. {
  61. if (event->button() == Qt::LeftButton)
  62. {
  63. m_bClicked = !m_bClicked;
  64. UpDataIoStatus(false);
  65. update();
  66. }
  67. QLabel::mousePressEvent(event);
  68. }
  69. private:
  70. bool m_bClicked; // 当前状态
  71. QString m_strName;
  72. ns_module::CViewInterface* m_pCamera = nullptr;
  73. };
  74. class JIoMapPage :public QObject
  75. {
  76. Q_OBJECT
  77. public:
  78. JIoMapPage();
  79. /**io 界面,先获取全部,后续可用,没用到的后续修改
  80. */
  81. static QWidget* CreateIoPage(const CONFIG_BASE_STRUCT& control);
  82. /**轴测试页面
  83. */
  84. static QWidget* CreateAxisTestPage(const CONFIG_BASE_STRUCT& control);
  85. };
  86. #include <QApplication>
  87. #include <QTableWidget>
  88. #include <QHeaderView>
  89. #include <QVBoxLayout>
  90. #include <QPushButton>
  91. #include <QWidget>
  92. #include <QMessageBox>
  93. class GPITableWidget : public QWidget {
  94. Q_OBJECT
  95. public:
  96. GPITableWidget(QWidget* parent = nullptr) : QWidget(parent) {
  97. // 创建主布局
  98. QVBoxLayout* mainLayout = new QVBoxLayout(this);
  99. // 创建表格控件
  100. tableWidget = new QTableWidget(this);
  101. // 设置表格属性
  102. tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
  103. tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
  104. tableWidget->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::EditKeyPressed);
  105. // 设置表头
  106. QStringList headers;
  107. headers << "GPI名称" << "卡号" << "卡类型" << "GPI号" << "有效电平" << "状态";
  108. tableWidget->setColumnCount(headers.size());
  109. tableWidget->setHorizontalHeaderLabels(headers);
  110. // 设置表头自适应
  111. tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  112. // 添加按钮
  113. QPushButton* addButton = new QPushButton("添加行", this);
  114. QPushButton* deleteButton = new QPushButton("删除行", this);
  115. QPushButton* saveButton = new QPushButton("保存数据", this);
  116. // 按钮布局
  117. QHBoxLayout* buttonLayout = new QHBoxLayout();
  118. buttonLayout->addWidget(addButton);
  119. buttonLayout->addWidget(deleteButton);
  120. buttonLayout->addWidget(saveButton);
  121. // 添加到主布局
  122. mainLayout->addWidget(tableWidget);
  123. mainLayout->addLayout(buttonLayout);
  124. // 连接信号槽
  125. connect(addButton, &QPushButton::clicked, this, &GPITableWidget::addRow);
  126. connect(deleteButton, &QPushButton::clicked, this, &GPITableWidget::deleteRow);
  127. connect(saveButton, &QPushButton::clicked, this, &GPITableWidget::saveData);
  128. // 添加初始行
  129. addRow();
  130. }
  131. private slots:
  132. void addRow() {
  133. int row = tableWidget->rowCount();
  134. tableWidget->insertRow(row);
  135. // 为每一列创建项目
  136. tableWidget->setItem(row, 0, new QTableWidgetItem("GPI_" + QString::number(row + 1)));
  137. tableWidget->setItem(row, 1, new QTableWidgetItem(QString::number(row + 1)));
  138. tableWidget->setItem(row, 2, new QTableWidgetItem("类型" + QString::number((row % 3) + 1)));
  139. tableWidget->setItem(row, 3, new QTableWidgetItem(QString::number(row + 101)));
  140. tableWidget->setItem(row, 4, new QTableWidgetItem(row % 2 ? "高电平" : "低电平"));
  141. tableWidget->setItem(row, 5, new QTableWidgetItem("未激活"));
  142. }
  143. void deleteRow() {
  144. int currentRow = tableWidget->currentRow();
  145. if (currentRow >= 0) {
  146. tableWidget->removeRow(currentRow);
  147. }
  148. else {
  149. QMessageBox::warning(this, "警告", "请先选择要删除的行");
  150. }
  151. }
  152. void saveData() {
  153. QString data;
  154. for (int row = 0; row < tableWidget->rowCount(); ++row) {
  155. for (int col = 0; col < tableWidget->columnCount(); ++col) {
  156. QTableWidgetItem* item = tableWidget->item(row, col);
  157. if (item) {
  158. data += item->text() + "\t";
  159. }
  160. }
  161. data += "\n";
  162. }
  163. QMessageBox::information(this, "表格数据", "保存的数据:\n" + data);
  164. // 实际应用中,这里可以将数据保存到文件或数据库
  165. }
  166. private:
  167. QTableWidget* tableWidget;
  168. };
  169. #endif // JIOMAPPAGE_H