#include "CRectItem.h" #include #include #include #include #include CRectItem::CRectItem(QGraphicsItem *parent) : CRectItem(QRectF(), parent) { } CRectItem::CRectItem(const QRectF &rect, QGraphicsItem *parent) : CPathItem(parent), m_rect(rect) { setHandlesChildEvents(false); updatePath(); m_menu = new RoundMenu("menu"); m_subNode = new QAction("添加子节点"); m_select = new QAction("选中"); m_remove = new QAction("删除"); m_edit = new QAction("编辑节点"); m_menu->addAction(m_edit); m_menu->addAction(m_subNode); // m_menu->addAction(m_select); m_menu->addAction(m_remove); } QRectF CRectItem::rect() const { return m_rect; } void CRectItem::setRect(const QRectF &rect) { m_rect = rect; updatePath(); } QPointF CRectItem::centerLeft() const { return QPointF(m_rect.center() - QPointF(m_rect.width() / 2, 0)); } QPointF CRectItem::centerRight() const { return QPointF(m_rect.center() + QPointF(m_rect.width() / 2, 0)); } int CRectItem::cornerRadius() const { return m_cornerRadius; } void CRectItem::setCornerRadius(qreal radius) { m_cornerRadius = radius; updatePath(); } void CRectItem::updatePath() { QPainterPath path; path.addRoundedRect(m_rect, m_cornerRadius, m_cornerRadius); setPath(path); } void CRectItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { if (!m_allowEdit) { return; } const QString txt = highlighted() ? "取消选中" : "选中"; m_select->setText(txt); m_menu->exec(event->screenPos() + QPoint(-40, -20)); } void CRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { Q_UNUSED(event) } QAction *CRectItem::selectAction() const { return m_select; } QAction *CRectItem::subNodeAction() const { return m_subNode; } QAction *CRectItem::removeAction() const { return m_remove; } QAction *CRectItem::editAction() const { return m_edit; } QRectF CRectItem::boundingRect() const { return m_rect; } void CRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { painter->setPen(pen()); painter->setBrush(Qt::blue); QGraphicsPathItem::paint(painter, option, widget); } void CRectItem::setAllowEdit(bool allow) { m_allowEdit = allow; } void CRectItem::slotSelect() { qDebug() << __FUNCTION__ << __LINE__ << endl; }