CRectItem.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "CRectItem.h"
  2. #include <Widgets/Menu.h>
  3. #include <QGraphicsSceneContextMenuEvent>
  4. #include <QMenu>
  5. #include <QDebug>
  6. CRectItem::CRectItem(QGraphicsItem *parent) : CRectItem(QRectF(), parent) { }
  7. CRectItem::CRectItem(const QRectF &rect, QGraphicsItem *parent) : CPathItem(parent), m_rect(rect)
  8. {
  9. setHandlesChildEvents(false);
  10. updatePath();
  11. m_menu = new RoundMenu("menu");
  12. m_subNode = new QAction("添加子节点");
  13. m_select = new QAction("选中");
  14. m_remove = new QAction("删除");
  15. m_menu->addAction(m_subNode);
  16. m_menu->addAction(m_select);
  17. m_menu->addAction(m_remove);
  18. }
  19. QRectF CRectItem::rect() const
  20. {
  21. return m_rect;
  22. }
  23. void CRectItem::setRect(const QRectF &rect)
  24. {
  25. m_rect = rect;
  26. updatePath();
  27. }
  28. QPointF CRectItem::centerLeft() const
  29. {
  30. return QPointF(m_rect.center() - QPointF(m_rect.width() / 2, 0));
  31. }
  32. QPointF CRectItem::centerRight() const
  33. {
  34. return QPointF(m_rect.center() + QPointF(m_rect.width() / 2, 0));
  35. }
  36. int CRectItem::cornerRadius() const
  37. {
  38. return m_cornerRadius;
  39. }
  40. void CRectItem::setCornerRadius(qreal radius)
  41. {
  42. m_cornerRadius = radius;
  43. updatePath();
  44. }
  45. void CRectItem::updatePath()
  46. {
  47. QPainterPath path;
  48. path.addRoundedRect(m_rect, m_cornerRadius, m_cornerRadius);
  49. setPath(path);
  50. }
  51. void CRectItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
  52. {
  53. const QString txt = highlighted() ? "取消选中" : "选中";
  54. m_select->setText(txt);
  55. m_menu->exec(event->screenPos() + QPoint(-40, -20));
  56. }
  57. void CRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { }
  58. QAction *CRectItem::selectAction() const
  59. {
  60. return m_select;
  61. }
  62. QAction *CRectItem::subNodeAction() const
  63. {
  64. return m_subNode;
  65. }
  66. QAction *CRectItem::removeAction() const
  67. {
  68. return m_remove;
  69. }
  70. void CRectItem::slotSelect()
  71. {
  72. qDebug() << __FUNCTION__ << __LINE__ << endl;
  73. }