DiagnosisPage.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. #include "DiagnosisPage.h"
  2. #include "../common/JQCommon.h"
  3. #include "QPainter"
  4. #include "OriginalWnd/TreeViewManager/JIoMapPage.h"
  5. #include "QListWidget"
  6. DiagnosisPage::DiagnosisPage(QWidget *parent)
  7. : QWidget(parent)
  8. {
  9. ui.setupUi(this);
  10. Init();
  11. }
  12. DiagnosisPage::~DiagnosisPage()
  13. {}
  14. void DiagnosisPage::Init()
  15. {
  16. this->setAttribute(Qt::WA_DeleteOnClose);
  17. ui.axisSwitchTabPage->tabBar()->hide();
  18. SwitchTabPage(0);
  19. ui.retBut->setIcon(QIcon(":/images/home_openFile.png"));
  20. ui.axisListTree->setHeaderHidden(true);
  21. ui.axisListTree->setEditTriggers(QAbstractItemView::NoEditTriggers);
  22. ns_module::CViewInterface* pCViewInterface = ns_module::CViewInterface::GetInstance();
  23. if (pCViewInterface)
  24. {
  25. m_veCAxis = pCViewInterface->GetViewMotion()->GetAxisList();
  26. m_veCIO = pCViewInterface->GetViewMotion()->GetIoList();
  27. // 创建模型
  28. m_model = new QStandardItemModel(this);
  29. QString strModeA;
  30. QStandardItem* pModeType = nullptr;
  31. QStandardItem* rooto = m_model->invisibleRootItem();
  32. QStandardItem* root1 = new QStandardItem(tr("Diagnosis Page", "诊断页面"));
  33. rooto->appendRow(root1);
  34. root1->setFlags(root1->flags() | Qt::ItemIsEnabled);
  35. root1->setCheckState(Qt::Checked);
  36. for (auto a: m_veCAxis)
  37. {
  38. QString strMode = QString::fromLocal8Bit(a->GetModuleType().c_str());
  39. if (strModeA != strMode)
  40. {
  41. pModeType = new QStandardItem(strMode);
  42. pModeType->setCheckState(Qt::Checked);
  43. root1->appendRow(pModeType);
  44. strModeA = strMode;
  45. }
  46. // 添加二级节点
  47. QStandardItem* pAisType = new QStandardItem(QString::fromLocal8Bit(a->GetStringAxisType().c_str()));
  48. pModeType->appendRow(pAisType);
  49. pAisType->setCheckState(Qt::Checked);
  50. }
  51. ui.axisListTree->setModel(m_model);
  52. ui.axisListTree->expandAll();
  53. ui.axisListTree->viewport()->installEventFilter(this);
  54. }
  55. // 控件状态
  56. JQCommon::SetQLineEditLimit(ui.negLineEdit);
  57. JQCommon::SetQLineEditLimit(ui.posLineEdit);
  58. JQCommon::SetQLineEditLimit(ui.speedLineEdit);
  59. }
  60. void DiagnosisPage::SwitchOperationAxisPage(bool isSwitch)
  61. {
  62. if (isSwitch)
  63. {
  64. QString strMode = QString::fromLocal8Bit(m_pCurrOpenCAxis->GetAxisName().c_str());
  65. ui.titleLable->setText(strMode);
  66. // 设置状态
  67. //m_IsDriveEnabled = m_pCurrOpenCAxis->JIsDriveEnabled();
  68. //ui.xAxisEnabledRadioBut->setChecked(m_IsDriveEnabled);
  69. m_IsDriveEnabled = false;
  70. // 获取正负限位是否打开
  71. bool bEnabledNeg, bEnabledPos;
  72. m_pCurrOpenCAxis->JGetSoftLimitEnableStatus(bEnabledNeg, bEnabledPos);
  73. if (bEnabledNeg && bEnabledPos)
  74. {
  75. double dSoftLimitNeg, dSoftLimitPos, dSoftTravelRange;
  76. m_pCurrOpenCAxis->JGetSoftLimit(dSoftLimitNeg, dSoftLimitPos, dSoftTravelRange);
  77. ui.negLineEdit->setText(QString::number(dSoftLimitNeg,'f',2));
  78. ui.posLineEdit->setText(QString::number(dSoftLimitPos,'f',2));
  79. }
  80. double dMaxVel;
  81. m_pCurrOpenCAxis->JGetMaxVelocity(dMaxVel);
  82. ui.speedLineEdit->setText(QString::number(dMaxVel, 'f', 2));
  83. ui.speedMaxLineEdit->setText(QString::number(dMaxVel, 'f', 2));
  84. // 设置定时获取速度
  85. const int nRunTime = 300; //所有新开的定时器都用这个
  86. m_nTimerID = startTimer(nRunTime);
  87. }
  88. else
  89. {
  90. if (m_nTimerID > 0)
  91. {
  92. killTimer(m_nTimerID);
  93. m_nTimerID = -1;
  94. }
  95. SwitchTabPage(0);
  96. }
  97. }
  98. void DiagnosisPage::drawParentChildLine(const QModelIndex& childIndex, QPainter& painter)
  99. {
  100. QModelIndex parentIndex = childIndex.parent();
  101. if (!parentIndex.isValid())
  102. {
  103. // “顶层节点”定义一个固定的起点 (rootX, rootY)
  104. int indent = ui.axisListTree->indentation();
  105. int depth = 0; // 顶层节点深度为0
  106. int branchX = (depth + 1) * indent - indent / 2; // 计算 branchX 基于缩进和深度
  107. // 定义 rootY 为节点中心 Y
  108. QRect childRect = ui.axisListTree->visualRect(childIndex);
  109. if (!childRect.isValid())
  110. return;
  111. int rootY = childRect.center().y();
  112. // 定义横向偏移量
  113. const int hOffset = -20;
  114. // 绘制竖线
  115. painter.drawLine(QPoint(branchX, rootY),
  116. QPoint(branchX, childRect.center().y()));
  117. // 计算新的横线终点
  118. int newX = childRect.left() + hOffset;
  119. // 绘制横线
  120. painter.drawLine(QPoint(branchX, childRect.center().y()),
  121. QPoint(newX, childRect.center().y()));
  122. return;
  123. }
  124. QRect parentRect = ui.axisListTree->visualRect(parentIndex);
  125. QRect childRect = ui.axisListTree->visualRect(childIndex);
  126. if (!parentRect.isValid() || !childRect.isValid()) {
  127. // 父或子超出可视区域
  128. return;
  129. }
  130. int pMidY = parentRect.center().y();
  131. int cMidY = childRect.center().y();
  132. // 计算节点深度
  133. int depth = 0;
  134. QModelIndex p = parentIndex;
  135. while (p.isValid()) {
  136. depth++;
  137. p = p.parent();
  138. }
  139. int indent = ui.axisListTree->indentation();
  140. int branchX = depth * indent - indent / 2;
  141. // branchX 不超出视图范围
  142. branchX = qMax(branchX, 0);
  143. // 定义横向偏移量
  144. const int hOffset = -15;
  145. // 绘制竖线
  146. painter.drawLine(QPoint(branchX, pMidY), QPoint(branchX, cMidY));
  147. // 计算新的横线终点
  148. int newX = childRect.left() + hOffset;
  149. // 绘制横线
  150. painter.drawLine(QPoint(branchX, cMidY), QPoint(newX, cMidY));
  151. }
  152. void DiagnosisPage::drawSiblingLine(const QModelIndex& childIndex, QPainter& painter)
  153. {
  154. QModelIndex parentIndex = childIndex.parent();
  155. if (!parentIndex.isValid())
  156. {
  157. return; // 没有父节点
  158. }
  159. // 下一个兄弟
  160. int row = childIndex.row();
  161. int lastRow = m_model->rowCount(parentIndex) - 1;
  162. if (row >= lastRow)
  163. {
  164. return; // 说明是最后一个兄弟,不用画延伸线
  165. }
  166. QModelIndex nextSibling = m_model->index(row + 1, 0, parentIndex);
  167. QRect currRect = ui.axisListTree->visualRect(childIndex);
  168. QRect nextRect = ui.axisListTree->visualRect(nextSibling);
  169. if (!currRect.isValid() || !nextRect.isValid())
  170. {
  171. return;
  172. }
  173. // 计算节点深度
  174. int depth = 0;
  175. QModelIndex p = parentIndex;
  176. while (p.isValid())
  177. {
  178. depth++;
  179. p = p.parent();
  180. }
  181. int indent = ui.axisListTree->indentation();
  182. int branchX = depth * indent - indent / 2;
  183. // 确保 branchX 不超出视图范围
  184. branchX = qMax(branchX, 0);
  185. // 从当前节点底部向下延伸到下一个兄弟节点顶部
  186. int startY = currRect.bottom();
  187. int endY = nextRect.top();
  188. painter.drawLine(QPoint(branchX, startY), QPoint(branchX, endY));
  189. }
  190. void DiagnosisPage::paintAllBranches(const QModelIndex& parentIndex, QPainter& painter)
  191. {
  192. int rowCount = m_model->rowCount(parentIndex);
  193. for (int i = 0; i < rowCount; ++i)
  194. {
  195. // 当前子节点
  196. QModelIndex childIndex = m_model->index(i, 0, parentIndex);
  197. if (!childIndex.isValid()) continue;
  198. // 1) 父->子拐角线
  199. drawParentChildLine(childIndex, painter);
  200. // 2) 兄弟延续竖线(如果本节点不是最后一个兄弟,就在拐点列画条向下的线)
  201. if (i < rowCount - 1)
  202. {
  203. drawSiblingLine(childIndex, painter);
  204. }
  205. // 3) 递归处理子节点
  206. paintAllBranches(childIndex, painter);
  207. }
  208. }
  209. void DiagnosisPage::uncheckAllItems(const QModelIndex& parent /*= QModelIndex()*/)
  210. {
  211. QAbstractItemModel* model = ui.axisListTree->model();
  212. if (!model) return;
  213. for (int i = 0; i < model->rowCount(parent); ++i) {
  214. QModelIndex child = model->index(i, 0, parent);
  215. // 如果模型支持复选框
  216. if (model->flags(child) & Qt::ItemIsUserCheckable) {
  217. model->setData(child, Qt::Unchecked, Qt::CheckStateRole);
  218. }
  219. // 递归处理子项
  220. if (model->hasChildren(child)) {
  221. uncheckAllItems(child);
  222. }
  223. }
  224. }
  225. void DiagnosisPage::SwitchTabPage(int nSwitch)
  226. {
  227. ui.axisSwitchTabPage->setCurrentIndex(nSwitch);
  228. if (nSwitch == 0)
  229. {
  230. ui.AllHomeBut->show();
  231. ui.AllEnableBut->show();
  232. ui.AllIOPageBut->show();
  233. ui.retBut->hide();
  234. }
  235. else if (nSwitch == 1)
  236. {
  237. ui.AllHomeBut->hide();
  238. ui.AllEnableBut->hide();
  239. ui.AllIOPageBut->hide();
  240. ui.retBut->show();
  241. }
  242. else if (nSwitch == 2)
  243. {
  244. ui.AllHomeBut->hide();
  245. ui.AllEnableBut->hide();
  246. ui.AllIOPageBut->hide();
  247. ui.retBut->show();
  248. }
  249. m_nCurrIndex = nSwitch;
  250. }
  251. void DiagnosisPage::timerEvent(QTimerEvent* event)
  252. {
  253. if (m_nTimerID == event->timerId())
  254. {
  255. if (m_pCurrOpenCAxis)
  256. {
  257. double doPos = 0.0;
  258. m_pCurrOpenCAxis->GetActualPos(doPos);
  259. ui.xPositionLineEdit->setText(QString::number(doPos, 'f', 2));
  260. }
  261. }
  262. }
  263. bool DiagnosisPage::eventFilter(QObject* watched, QEvent* event)
  264. {
  265. if (watched == ui.axisListTree->viewport() && event->type() == QEvent::Paint)
  266. {
  267. // 进行默认绘制
  268. bool handled = QWidget::eventFilter(watched, event);
  269. // 使用 QPainter 叠加画“拐角线”
  270. QPainter painter(ui.axisListTree->viewport());
  271. if (!painter.isActive())
  272. {
  273. //qWarning() << "Painter not active";
  274. return handled;
  275. }
  276. painter.save();
  277. painter.setPen(QPen(Qt::gray, 1, Qt::DashLine)); // 灰色、1px 宽、虚线
  278. // 调用递归函数,绘制所有分支
  279. paintAllBranches(QModelIndex(), painter);
  280. painter.restore();
  281. return handled;
  282. }
  283. // 其余事件交给父类默认处理
  284. return QWidget::eventFilter(watched, event);
  285. }
  286. void DiagnosisPage::on_axisListTree_doubleClicked(const QModelIndex& index)
  287. {
  288. //uncheckAllItems();
  289. if (index.isValid())
  290. {
  291. QString itemName = index.data().toString();
  292. if (!m_model->hasChildren(index)) // 没有子节点
  293. {
  294. // 表示想要切换到轴控制
  295. QModelIndex parentIndex = index.parent();
  296. QString currentItem = parentIndex.data().toString();
  297. currentItem += "_";
  298. currentItem += itemName;
  299. // 给轴窗口
  300. SwitchTabPage(1);
  301. for (auto& a : m_veCAxis)
  302. {
  303. QString strMode = QString::fromLocal8Bit(a->GetAxisName().c_str());
  304. if (strMode == currentItem)
  305. {
  306. m_pCurrOpenCAxis = a;
  307. SwitchOperationAxisPage(true);
  308. break;
  309. }
  310. }
  311. }
  312. }
  313. }
  314. void DiagnosisPage::on_xAxisDisable_clicked()
  315. {
  316. QString strText = tr("Disable", "关使能");
  317. if (m_IsDriveEnabled)
  318. {
  319. m_pCurrOpenCAxis->AxisOff();
  320. strText = tr("Enable", "开使能");
  321. m_IsDriveEnabled = false;
  322. }
  323. else
  324. {
  325. m_pCurrOpenCAxis->AxisOn();
  326. m_IsDriveEnabled = true;
  327. }
  328. ui.xAxisDisable->setText(strText);
  329. ui.xAxisEnabledRadioBut->setChecked(m_IsDriveEnabled);
  330. }
  331. void DiagnosisPage::on_AllEnableBut_clicked()
  332. {
  333. for (auto a : m_veCAxis)
  334. {
  335. if (m_AllIsDriveEnabled)
  336. {
  337. a->AxisOn();
  338. }
  339. else
  340. {
  341. a->AxisOff();
  342. }
  343. }
  344. QString strText = tr("All Enable", "所有开使能");
  345. if (m_AllIsDriveEnabled)
  346. {
  347. strText = tr("All Disable", "所有关使能");
  348. }
  349. ui.AllEnableBut->setText(strText);
  350. m_AllIsDriveEnabled = !m_AllIsDriveEnabled;
  351. }
  352. void DiagnosisPage::on_AllIOPageBut_clicked()
  353. {
  354. SwitchTabPage(2);
  355. if (!m_isInitIo)
  356. {
  357. QScrollArea* scrollArea = new QScrollArea;
  358. scrollArea->setWidgetResizable(true);
  359. QWidget* contentWidget = new QWidget;
  360. QVBoxLayout* contentLayout = new QVBoxLayout(contentWidget);
  361. for (auto a : m_veCIO)
  362. {
  363. QString strMode = QString::fromLocal8Bit(a->GetName().c_str());
  364. contentLayout->addWidget(JIoMapPage::CreateIoPage(strMode));
  365. }
  366. scrollArea->setWidget(contentWidget);
  367. ui.ioUi->addWidget(scrollArea);
  368. m_isInitIo = true;
  369. }
  370. }
  371. void DiagnosisPage::on_xAxisHomeBut_clicked()
  372. {
  373. m_pCurrOpenCAxis->Home();
  374. }
  375. void DiagnosisPage::on_AllHomeBut_clicked()
  376. {
  377. ns_module::CViewInterface::GetInstance()->GetViewMotion()->ToHome();
  378. }
  379. void DiagnosisPage::on_setPosBut_clicked()
  380. {
  381. double xNeg = ui.negLineEdit->text().toDouble();
  382. double xPos = ui.posLineEdit->text().toDouble();
  383. m_pCurrOpenCAxis->JSetSoftLimit(xNeg, xPos);
  384. }
  385. void DiagnosisPage::on_setNegBut_clicked()
  386. {
  387. double xNeg = ui.negLineEdit->text().toDouble();
  388. double xPos = ui.posLineEdit->text().toDouble();
  389. m_pCurrOpenCAxis->JSetSoftLimit(xNeg, xPos);
  390. }
  391. void DiagnosisPage::on_setSpeedBut_clicked()
  392. {
  393. double doSpeed = ui.speedLineEdit->text().toDouble();
  394. m_pCurrOpenCAxis->JSetTargetVel(doSpeed);
  395. }
  396. void DiagnosisPage::on_setMaxSpeedBut_clicked()
  397. {
  398. double doSpeed = ui.speedMaxLineEdit->text().toDouble();
  399. m_pCurrOpenCAxis->JSetMaxVelocity(doSpeed);
  400. }
  401. void DiagnosisPage::on_retBut_clicked()
  402. {
  403. SwitchOperationAxisPage(false);
  404. }