class ItemResize : public QGraphicsItem{public: ItemResize(int num) : QGraphicsItem() { setFlag(QGraphicsItem::ItemIgnoresTransformations, true); setZValue(1000); numPoint = num; } void setNumPoint(int position); int getNumPoint() {return numPoint;}protected: int numPoint; QRectF boundingRect() const {return QRectF(-4, -4, 8, 8);} void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { painter->save(); painter->setBrush(Qt::red); painter->drawEllipse(-4, -4, 8, 8); painter->restore(); }// QPainterPath shape() const// {// QPainterPath path;// path.addEllipse(-4, -4, 8, 8);// return path;// }};
#ifndef QMOVABLEGRAPHICSPROXYWIDGET_H#define QMOVABLEGRAPHICSPROXYWIDGET_H#include <QDomDocument>#include <QDomElement>#include <QGraphicsProxyWidget>#include "crailobject.h"#include "cresizer.h"#include "qcommonrailitem.h"class QMovableGraphicsProxyWidget : public QGraphicsProxyWidget, public QCommonRailItem, public CResizer{ Q_OBJECT void onDrag(QPointF NewPosition); //при перетаскивании void onResize(); //при изменении размера с помощью маркера void updateAnchors(); //обновить изображение маркеровprotected: void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void resizeEvent(QGraphicsSceneResizeEvent *event); QPainterPath shape() const; void focusInEvent(QFocusEvent *event); void focusOutEvent(QFocusEvent *event);public: explicit QMovableGraphicsProxyWidget(QGraphicsItem * parent = 0, Qt::WindowFlags wFlags = 0); ~QMovableGraphicsProxyWidget(); void setWidget(QWidget *widget);public slots:};#endif // QMOVABLEGRAPHICSPROXYWIDGET_H
void QMovableGraphicsProxyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event){ if(work_state==wsEditing) { CResizer::mousePressEvent(event); } else if(event->button()==Qt::RightButton) { QGraphicsSceneContextMenuEvent cevent; cevent.setPos(event->pos()); contextMenuEvent(&cevent); }}void QMovableGraphicsProxyWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event){ if(work_state==wsEditing) { CResizer::mouseMoveEvent(event,pos()); } else QGraphicsProxyWidget::mouseMoveEvent(event);}QRectF QMovableGraphicsProxyWidget::boundingRect() const{ QRectF rect = QGraphicsProxyWidget::boundingRect(); if(isSelected()) rect.adjust(-6,-6,6,6); return rect;}void QMovableGraphicsProxyWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ QGraphicsProxyWidget::paint(painter,option,widget); if((option->state & QStyle::State_Selected) && work_state == wsEditing) { CResizer::paint(painter,option,widget); }}QPainterPath QMovableGraphicsProxyWidget::shape() const{ QPainterPath path; path.addRect(-6,-6,widget()->width()+12,widget()->height()+12); return path;}void QMovableGraphicsProxyWidget::resizeEvent(QGraphicsSceneResizeEvent * event){ CResizer::setSize(event->newSize());}void QMovableGraphicsProxyWidget::onDrag(QPointF NewPosition){ setPos(NewPosition);}void QMovableGraphicsProxyWidget::onResize(){ if(widget()) { widget()->resize(qCeil(CResizer::size().width()),qCeil(CResizer::size().height())); } //setSize(CResizer::size());}void QMovableGraphicsProxyWidget::updateAnchors(){ update();}void QMovableGraphicsProxyWidget::setWidget(QWidget *widget){ QGraphicsProxyWidget::setWidget(widget); if(widget) { CResizer::setSize(QSizeF(widget->size().width(),widget->size().height())); }}