JIoMapPage.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "JIoMapPage.h"
  2. #include <Machine.h>
  3. #include <QJsonObject>
  4. JIoMapPage::JIoMapPage() {}
  5. QWidget* JIoMapPage::CreateIoPage(const CONFIG_BASE_STRUCT& control)
  6. {
  7. // 1.用到表中的变量
  8. QString strName = control.strName.c_str();
  9. // 2. 创建垂直布局
  10. QHBoxLayout* pHLayout = new QHBoxLayout();
  11. IoStateLab* pIoState = new IoStateLab();
  12. pIoState->setIoName(strName);
  13. QLabel* pAxisNmae = new QLabel();
  14. QString strAxisName = tr("AxisNmae:", "轴名称:");
  15. strAxisName += strName;
  16. pAxisNmae->setText(strAxisName);
  17. //QLabel* pLow = new QLabel();
  18. //pLow->setText(tr("Voltage status", "电压状态"));
  19. //QComboBox* pComBox = new QComboBox();
  20. //pComBox->addItem(tr("Low", "低电平"));
  21. //pComBox->addItem(tr("High","高电平"));
  22. //pComBox->setCurrentIndex(1);
  23. //// 连接信号和槽
  24. //connect(pComBox, &QComboBox::currentTextChanged, []() {
  25. // });
  26. // 轴名称
  27. pHLayout->addWidget(pAxisNmae);
  28. //// 电平组合
  29. //pHLayout->addWidget(pLow);
  30. //pHLayout->addWidget(pComBox);
  31. // 改变状态标记
  32. pHLayout->addWidget(pIoState);
  33. QWidget* pWidget = new QWidget;
  34. pWidget->setLayout(pHLayout);
  35. return pWidget;
  36. }
  37. QWidget* JIoMapPage::CreateAxisTestPage(const CONFIG_BASE_STRUCT& control)
  38. {
  39. // 1.用到表中的变量
  40. QString strName = control.strName.c_str();
  41. QHBoxLayout* pHLayout = new QHBoxLayout();
  42. QLabel* pAxisNmae = new QLabel();
  43. QString strAxisName = tr("AxisNmae:", "轴名称:");
  44. strAxisName += strName;
  45. pAxisNmae->setText(strAxisName);
  46. pHLayout->addWidget(pAxisNmae);
  47. QLineEdit* pCardNumberType = new QLineEdit(tr("Card Number Type", "卡号类型"));
  48. pCardNumberType->setText("0");
  49. pHLayout->addWidget(pCardNumberType);
  50. QLineEdit* pCardNumber = new QLineEdit(tr("Card Number", "卡号"));
  51. pCardNumber->setText("0");
  52. pHLayout->addWidget(pCardNumberType);
  53. QPushButton* pTest = new QPushButton(tr("Axis Test","轴测试"));
  54. QObject::connect(pTest, &QPushButton::clicked, [strName]()
  55. {
  56. ns_module::CViewMotion::GetInstance()->ShowMotionAdjustPage(strName.toStdString());
  57. });
  58. pHLayout->addWidget(pTest);
  59. QWidget* pWidget = new QWidget;
  60. pWidget->setLayout(pHLayout);
  61. return pWidget;
  62. }
  63. void JIoMapPage::JCreateMenu(QWidget* pWidget)
  64. {
  65. // 设置上下文菜单策略
  66. pWidget->setContextMenuPolicy(Qt::CustomContextMenu);
  67. // 连接信号到槽函数
  68. connect(pWidget, &QWidget::customContextMenuRequested, [=](const QPoint& pos)
  69. {
  70. QMenu menu;
  71. QAction* pGetAct = menu.addAction(tr("Get","获取"));
  72. QAction* pSetUpAct = menu.addAction(tr("set up","设置"));
  73. QAction* pDefValUpAct = menu.addAction(tr("default value", "默认值"));
  74. // 连接动作的触发信号
  75. connect(pGetAct, &QAction::triggered, []() { qDebug() << "Get"; });
  76. connect(pSetUpAct, &QAction::triggered, []() { qDebug() << "set up"; });
  77. connect(pDefValUpAct, &QAction::triggered, []() { qDebug() << "default value"; });
  78. // 显示菜单
  79. menu.exec(pWidget->mapToGlobal(pos));
  80. });
  81. }
  82. void JIoMapPage::JCreateMenu(QWidget* pWidget, ST_DEFAULT_VAL defaultVal)
  83. {
  84. pWidget->setContextMenuPolicy(Qt::CustomContextMenu);
  85. connect(pWidget, &QWidget::customContextMenuRequested, [=](const QPoint& pos)
  86. {
  87. QMenu menu;
  88. QAction* pDefValUpAct = menu.addAction(tr("default value", "默认值"));
  89. connect(pDefValUpAct, &QAction::triggered, [defaultVal]()
  90. {
  91. if (!defaultVal.fieldDefult.isEmpty())
  92. {
  93. defaultVal.lineEdit->setText(defaultVal.fieldDefult);
  94. }
  95. });
  96. menu.exec(pWidget->mapToGlobal(pos));
  97. });
  98. }
  99. QLabel* JIoMapPage::JCreateName0(const QString& fieldDescribe, const QString& fieldDescribe_Eng, int nlanguage)
  100. {
  101. QLabel* pNameLable = new QLabel();
  102. pNameLable->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  103. pNameLable->setFixedHeight(24);
  104. // 自定义大小
  105. auto AdaptFun = [](QLabel* pNameLable, const QString name)
  106. {
  107. QFontMetrics metrics(pNameLable->font());
  108. QString elidedText = metrics.elidedText(name, Qt::ElideRight, pNameLable->width());//180
  109. pNameLable->setText(elidedText);
  110. };
  111. if (nlanguage == 0)
  112. {
  113. if (fieldDescribe_Eng.isEmpty())
  114. {
  115. AdaptFun(pNameLable, fieldDescribe);
  116. }
  117. else
  118. {
  119. AdaptFun(pNameLable, fieldDescribe_Eng);
  120. }
  121. }
  122. else
  123. {
  124. if (fieldDescribe.isEmpty())
  125. {
  126. AdaptFun(pNameLable, fieldDescribe_Eng);
  127. }
  128. else
  129. {
  130. AdaptFun(pNameLable, fieldDescribe);
  131. }
  132. }
  133. return pNameLable;
  134. }
  135. AutoResizeLabel* JIoMapPage::JCreateJText(const QString& name)
  136. {
  137. AutoResizeLabel* pLabel = new AutoResizeLabel();
  138. pLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  139. pLabel->setFixedHeight(24);
  140. //pLabel->setFixedWidth(300);
  141. QFontMetrics metrics(pLabel->font());
  142. QString elidedText = metrics.elidedText(name, Qt::ElideRight, pLabel->width()-330);
  143. pLabel->setText(elidedText);
  144. return pLabel;
  145. }
  146. QList<ST_DATA_ITEM> JIoMapPage::parseJsonToDataList(const QString& jsonString)
  147. {
  148. QList<ST_DATA_ITEM> dataList;
  149. QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());
  150. if (jsonDoc.isNull() || !jsonDoc.isArray())
  151. {
  152. qWarning() << "解析失败或JSON不是数组";
  153. return dataList;
  154. }
  155. QJsonArray jsonArray = jsonDoc.array();
  156. // 遍历数组
  157. for (const QJsonValue& val : jsonArray)
  158. {
  159. if (val.isObject())
  160. {
  161. QJsonObject obj = val.toObject();
  162. ST_DATA_ITEM item;
  163. item.key = obj.value("key").toString();
  164. item.value = obj.value("value").toString();
  165. dataList.append(item);
  166. }
  167. }
  168. return dataList;
  169. }