123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- #include "CRectItem.h"
- #include <Widgets/Menu.h>
- #include <QGraphicsSceneContextMenuEvent>
- #include <QMenu>
- #include <QPainter>
- #include <QDebug>
- 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;
- }
|