123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #include "CRectItem.h"
- #include <QGraphicsSceneContextMenuEvent>
- #include <QMenu>
- #include <QDebug>
- CRectItem::CRectItem(QGraphicsItem *parent) : CRectItem(QRectF(), parent) { }
- CRectItem::CRectItem(const QRectF &rect, QGraphicsItem *parent) : CPathItem(parent), m_rect(rect)
- {
- updatePath();
- m_menu = new QMenu();
- m_select = m_menu->addAction("选中");
- m_subNode = m_menu->addAction("添加子节点");
- m_remove = m_menu->addAction("删除");
- }
- QRectF CRectItem::rect() const
- {
- return m_rect;
- }
- void CRectItem::setRect(const QRectF &rect)
- {
- m_rect = rect;
- updatePath();
- }
- 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)
- {
- qDebug() << __FUNCTION__ << __LINE__ << endl;
- const QString txt = highlighted() ? "取消选中" : "选中";
- m_select->setText(txt);
- m_menu->exec(event->screenPos());
- }
- void CRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
- {
- qDebug() << __FUNCTION__ << __LINE__ << endl;
- }
- QAction *CRectItem::selectAction() const
- {
- return m_select;
- }
- QAction *CRectItem::subNodeAction() const
- {
- return m_subNode;
- }
- QAction *CRectItem::removeAction() const
- {
- return m_remove;
- }
- void CRectItem::slotSelect()
- {
- qDebug() << __FUNCTION__ << __LINE__ << endl;
- }
|