You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.9 KiB

  1. #include "hmi.h"
  2. #include "ui_hmi.h"
  3. #include <QListWidgetItem>
  4. #include <QMimeData>
  5. #include <QDrag>
  6. #include <QPainter>
  7. #include <QMouseEvent>
  8. #include <QDropEvent>
  9. #include <QGraphicsView>
  10. #include <QMessageBox>
  11. #include "creatitem.h"
  12. HMI::HMI(QWidget *parent) :
  13. QWidget(parent),
  14. ui(new Ui::HMI)
  15. {
  16. ui->setupUi(this);
  17. /* 1. 场景 */
  18. hmi_scene_ = new QGraphicsScene(this);
  19. ui->graphicsView->setScene(hmi_scene_);
  20. ui->graphicsView->setSceneRect(0, 0, 800, 600);
  21. ui->graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
  22. /* 2. 列表 */
  23. ui->listWidget->setViewMode(QListView::IconMode);
  24. ui->listWidget->setIconSize(QSize(60, 30));
  25. ui->listWidget->setDragEnabled(false);
  26. ui->listWidget->viewport()->installEventFilter(this);
  27. /* 3. 填充列表 */
  28. createComponents();
  29. connect(ui->listWidget,&QListWidget::currentTextChanged,this,&HMI::onListwidgetCurrenttextchanged);
  30. }
  31. HMI::~HMI()
  32. {
  33. delete ui;
  34. }
  35. bool HMI::eventFilter(QObject *obj, QEvent *event)
  36. {
  37. if (obj == ui->listWidget->viewport()) {
  38. static QListWidgetItem *dragItem = nullptr;
  39. static QPoint startPos;
  40. if (event->type() == QEvent::MouseButtonPress) {
  41. auto *me = static_cast<QMouseEvent*>(event);
  42. if (me->button() == Qt::LeftButton) {
  43. dragItem = ui->listWidget->itemAt(me->pos());
  44. startPos = me->pos();
  45. }
  46. } else if (event->type() == QEvent::MouseMove && dragItem) {
  47. auto *me = static_cast<QMouseEvent*>(event);
  48. if ((me->pos() - startPos).manhattanLength()
  49. >= QApplication::startDragDistance()) {
  50. QString type = dragItem->data(Qt::UserRole).toString();
  51. QMimeData *mime = new QMimeData;
  52. mime->setData("application/x-component", type.toUtf8());
  53. QDrag *drag = new QDrag(this);
  54. drag->setMimeData(mime);
  55. drag->setPixmap(dragItem->icon().pixmap(ui->listWidget->iconSize()));
  56. drag->setHotSpot(drag->pixmap().rect().center());
  57. drag->exec(Qt::CopyAction);
  58. dragItem = nullptr;
  59. }
  60. } else if (event->type() == QEvent::MouseButtonRelease) {
  61. dragItem = nullptr;
  62. }
  63. }
  64. return QWidget::eventFilter(obj, event);
  65. }
  66. void HMI::createComponents()
  67. {
  68. struct Comp { QString name; QColor color; };
  69. const QVector<Comp> comps = {
  70. {"按钮", Qt::blue},
  71. {"指示灯", Qt::red}
  72. };
  73. for (const Comp &c : comps) {
  74. QListWidgetItem *it = new QListWidgetItem(c.name, ui->listWidget);
  75. QPixmap pix(60, 30);
  76. pix.fill(Qt::white);
  77. QPainter p(&pix);
  78. p.setRenderHint(QPainter::Antialiasing);
  79. p.setBrush(c.color.lighter(150));
  80. p.setPen(QPen(c.color, 2));
  81. p.drawRoundedRect(QRect(5, 5, 50, 20), 3, 3);
  82. p.setPen(Qt::black);
  83. p.drawText(QRect(5, 5, 50, 20), Qt::AlignCenter, c.name);
  84. it->setIcon(QIcon(pix));
  85. it->setData(Qt::UserRole, c.name);
  86. }
  87. }
  88. void HMI::clearScene()
  89. {
  90. hmi_scene_->clear();
  91. }
  92. void HMI::applyProjectToScene(const Project& proj)
  93. {
  94. clearScene();
  95. QVector<Item*> itemObjs;
  96. for (const auto& d : proj.items_) {
  97. Item* item = creatItem(d.type);
  98. if (!item) continue;
  99. item->setPos(d.x, d.y);
  100. item->setRegisterId(d.registerId);
  101. connect(item, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy);
  102. connect(item, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete);
  103. connect(item, &Item::requestBindRegister, ui->graphicsView, &MyGraphicsView::onItemRequestBindRegister);
  104. hmi_scene_->addItem(item);
  105. itemObjs.append(item);
  106. }
  107. }
  108. void HMI::extractSceneToProject(Project& proj)
  109. {
  110. proj.clear();
  111. QList<Item*> items;
  112. for (QGraphicsItem* gi : hmi_scene_->items()) {
  113. if (Item* it = dynamic_cast<Item*>(gi)) {
  114. items.append(it);
  115. }
  116. }
  117. for (Item* it : items) {
  118. Project::ItemData d;
  119. d.type = it->itemType();
  120. d.x = it->pos().x();
  121. d.y = it->pos().y();
  122. d.registerId = it->registerId();
  123. proj.items_.append(d);
  124. }
  125. for (QGraphicsItem* gi : hmi_scene_->items()) {
  126. if (Connection* conn = dynamic_cast<Connection*>(gi)) {
  127. int fromIdx = items.indexOf(conn->from_);
  128. int toIdx = items.indexOf(conn->to_);
  129. if (fromIdx >= 0 && toIdx >= 0) {
  130. Project::ConnectionData c;
  131. c.from = fromIdx;
  132. c.to = toIdx;
  133. c.fromType = static_cast<int>(conn->fromType_);
  134. c.toType = static_cast<int>(conn->toType_);
  135. proj.connections_.append(c);
  136. }
  137. }
  138. }
  139. }
  140. void HMI::onListwidgetCurrenttextchanged(const QString &text)
  141. {
  142. selectedComponentType = text;
  143. }