| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #include "GraphicsItem.h"
- GraphicsItem::GraphicsItem(QGraphicsItem *parent)
- :QAbstractGraphicsShapeItem(parent)
- {
- // QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
- // effect->setBlurRadius(8);
- // setGraphicsEffect(effect);
- }
- void GraphicsItem::updateGeometry()
- {
- const QRectF &geom = this->boundingRect();
- const int w = SELECTION_HANDLE_SIZE;
- const int h = SELECTION_HANDLE_SIZE;
- const Handles::iterator hend = m_handles.end();
- for (Handles::iterator it = m_handles.begin(); it != hend; ++it) {
- SizeHandleRect *hndl = *it;;
- switch (hndl->dir()) {
- case SizeHandleRect::LeftTop:
- hndl->move(geom.x() - w / 2, geom.y() - h / 2);
- break;
- case SizeHandleRect::Top:
- hndl->move(geom.x() + geom.width() / 2 - w / 2, geom.y() - h / 2);
- break;
- case SizeHandleRect::RightTop:
- hndl->move(geom.x() + geom.width() - w / 2, geom.y() - h / 2);
- break;
- case SizeHandleRect::Right:
- hndl->move(geom.x() + geom.width() - w / 2, geom.y() + geom.height() / 2 - h / 2);
- break;
- case SizeHandleRect::RightBottom:
- hndl->move(geom.x() + geom.width() - w / 2, geom.y() + geom.height() - h / 2);
- break;
- case SizeHandleRect::Bottom:
- hndl->move(geom.x() + geom.width() / 2 - w / 2, geom.y() + geom.height() - h / 2);
- break;
- case SizeHandleRect::LeftBottom:
- hndl->move(geom.x() - w / 2, geom.y() + geom.height() - h / 2);
- break;
- case SizeHandleRect::Left:
- hndl->move(geom.x() - w / 2, geom.y() + geom.height() / 2 - h / 2);
- break;
- case SizeHandleRect::Center:
- hndl->move(geom.center().x() - w / 2 , geom.center().y() - h / 2);
- break;
- default:
- break;
- }
- }
- }
- void GraphicsItem::setState(SelectionHandleState st)
- {
- const Handles::iterator hend = m_handles.end();
- for (Handles::iterator it = m_handles.begin(); it != hend; ++it)
- (*it)->setState(st);
- }
- SizeHandleRect::Direction GraphicsItem::hitTest(const QPointF &point) const
- {
- const Handles::const_iterator hend = m_handles.end();
- for (Handles::const_iterator it = m_handles.begin(); it != hend; ++it)
- {
- if ((*it)->hitTest(point) ){
- return (*it)->dir();
- }
- }
- return SizeHandleRect::None;
- }
- Qt::CursorShape GraphicsItem::getCursor(SizeHandleRect::Direction dir)
- {
- switch (dir) {
- case SizeHandleRect::Right:
- return Qt::SizeHorCursor;
- case SizeHandleRect::RightTop:
- return Qt::SizeBDiagCursor;
- case SizeHandleRect::RightBottom:
- return Qt::SizeFDiagCursor;
- case SizeHandleRect::LeftBottom:
- return Qt::SizeBDiagCursor;
- case SizeHandleRect::Bottom:
- return Qt::SizeVerCursor;
- case SizeHandleRect::LeftTop:
- return Qt::SizeFDiagCursor;
- case SizeHandleRect::Left:
- return Qt::SizeHorCursor;
- case SizeHandleRect::Top:
- return Qt::SizeVerCursor;
- default:
- break;
- }
- return Qt::ArrowCursor;
- }
- void GraphicsItem::resizeTo(SizeHandleRect::Direction dir, const QPointF &point)
- {
- }
- QVariant GraphicsItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
- {
- if ( change == QGraphicsItem::ItemSelectedHasChanged ) {
- qDebug()<<" Item Selected : " << value.toString();
- setState(value.toBool() ? SelectionHandleActive : SelectionHandleOff);
- }else if ( change == QGraphicsItem::ItemRotationHasChanged ){
- qDebug()<<"Item Rotation Changed:" << value.toString();
- }else if ( change == QGraphicsItem::ItemTransformOriginPointHasChanged ){
- qDebug()<<"ItemTransformOriginPointHasChanged:" << value.toPointF();
- }
- return value;
- }
|