1 #include "graphicsscenerectmove.h"
3 #include <QGraphicsSceneMouseEvent>
4 #include <QGraphicsRectItem>
5 #include <QGraphicsView>
8 QGraphicsItem* selected=NULL;
11 enum resizeMode {NoResize,TopLeft,BottomLeft,TopRight,BottomRight,Left,Right,Up,Down};
12 GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent):QGraphicsScene(parent){
17 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e){
19 if (selected && selected->type()==3 && e->buttons() & Qt::LeftButton){
21 QGraphicsRectItem *gi=(QGraphicsRectItem*)selected;
22 QRectF newrect=gi->rect();
23 QPointF newpoint=e->scenePos();
24 newpoint-=selected->scenePos();
27 newrect.setTopLeft(newpoint);
30 newrect.setBottomLeft(newpoint);
33 newrect.setTopRight(newpoint);
36 newrect.setBottomRight(newpoint);
39 newrect.setLeft(newpoint.x());
42 newrect.setRight(newpoint.x());
45 newrect.setTop(newpoint.y());
48 newrect.setBottom(newpoint.y());
53 gi->setPos(selected->scenePos());
56 QPointF p=e->scenePos();
60 foreach(QGraphicsItem* g, items( QRectF( p , QSizeF(4,4) ).toRect() ) ){
64 QGraphicsRectItem *gi=(QGraphicsRectItem*)g;
66 r.translate(gi->scenePos());
68 if ( (r.toRect().topLeft()-=e->scenePos().toPoint() ).manhattanLength()<3 ){
70 }else if ((r.toRect().bottomLeft()-=e->scenePos().toPoint() ).manhattanLength()<3 ){
71 resizeMode=BottomLeft;
72 }else if ((r.toRect().topRight()-=e->scenePos().toPoint() ).manhattanLength()<3 ){
74 }else if ((r.toRect().bottomRight()-=e->scenePos().toPoint() ).manhattanLength()<3 ){
75 resizeMode=BottomRight;
76 }else if ( qAbs(r.toRect().left()-e->scenePos().toPoint().x() ) <3){
78 }else if ( qAbs(r.toRect().right()-e->scenePos().toPoint().x() ) <3){
80 }else if ( qAbs(r.toRect().top()-e->scenePos().toPoint().y() ) <3){
82 }else if ( qAbs(r.toRect().bottom()-e->scenePos().toPoint().y() ) <3){
85 if (resizeMode!=NoResize)
93 setCursor(QCursor(Qt::SizeFDiagCursor));
97 setCursor(QCursor(Qt::SizeBDiagCursor));
101 setCursor(Qt::SizeHorCursor);
105 setCursor(Qt::SizeVerCursor);
108 setCursor(QCursor(Qt::ArrowCursor));
109 QGraphicsScene::mouseMoveEvent(e);
115 void GraphicsSceneRectMove::wheelEvent ( QGraphicsSceneWheelEvent * wheelEvent ){
116 QList<QGraphicsView*> viewlist=views();
117 kDebug() << wheelEvent->delta() << " " << zoom;
118 if (viewlist.size()>0){
119 if (wheelEvent->delta()<0 && zoom<20.0){
122 }else if (wheelEvent->delta()>0 && zoom>.05){
126 viewlist[0]->resetTransform();
127 viewlist[0]->scale(zoom,zoom);
131 void GraphicsSceneRectMove::setCursor(QCursor c){
132 QList<QGraphicsView*> l=views();
133 foreach(QGraphicsView* v, l){