Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

77 rindas
2.1 KiB

  1. #include "item.h"
  2. #include "connection.h"
  3. #include <QPainter>
  4. #include <QStyleOptionGraphicsItem>
  5. Item::Item(const QString &type, QGraphicsItem *parent)
  6. : QGraphicsObject(parent), type_(type)
  7. {
  8. if (type == "常开") color_ = Qt::blue;
  9. else if (type == "常闭") color_ = Qt::red;
  10. else if (type == "比较指令") color_ = Qt::green;
  11. else color_ = Qt::darkYellow;
  12. setFlags(QGraphicsItem::ItemIsMovable |
  13. QGraphicsItem::ItemIsSelectable |
  14. QGraphicsItem::ItemSendsGeometryChanges);
  15. }
  16. QRectF Item::boundingRect() const
  17. {
  18. return QRectF(-5, -5, 90, 50);
  19. }
  20. void Item::paint(QPainter *painter,
  21. const QStyleOptionGraphicsItem *,
  22. QWidget *)
  23. {
  24. QRectF r = boundingRect().adjusted(5, 5, -5, -5); // 图元主体
  25. painter->setBrush(color_.lighter(150));
  26. painter->setPen(QPen(color_, 2));
  27. painter->drawRoundedRect(r, 5, 5);
  28. // 画锚点
  29. painter->setBrush(Qt::darkGray);
  30. painter->setPen(Qt::NoPen);
  31. painter->drawEllipse(QPointF(r.left(), r.center().y()), 5, 5); // 左锚点
  32. painter->drawEllipse(QPointF(r.right(), r.center().y()), 5, 5); // 右锚点
  33. painter->setPen(Qt::black);
  34. painter->drawText(r, Qt::AlignCenter, type_);
  35. }
  36. QPointF Item::anchorPos(AnchorType anc) const
  37. {
  38. QRectF r = boundingRect();
  39. switch (anc) {
  40. case Left: return mapToScene(QPointF(r.left(), r.center().y()));
  41. case Right: return mapToScene(QPointF(r.right(), r.center().y()));
  42. }
  43. return QPointF();
  44. }
  45. void Item::addConnection(Connection* conn)
  46. {
  47. if (!connections_.contains(conn))
  48. connections_.append(conn);
  49. }
  50. void Item::removeConnection(Connection* conn)
  51. {
  52. connections_.removeAll(conn);
  53. }
  54. QList<Connection *> Item::connections()
  55. {
  56. return connections_;
  57. }
  58. QVariant Item::itemChange(GraphicsItemChange change, const QVariant &value)
  59. {
  60. if (change == QGraphicsItem::ItemPositionChange) {
  61. for (Connection* conn : connections_)
  62. conn->updatePosition();
  63. }
  64. return QGraphicsObject::itemChange(change, value);
  65. }