]> git.sesse.net Git - kdenlive/blob - src/graphicsscenerectmove.cpp
text changes in other editor too
[kdenlive] / src / graphicsscenerectmove.cpp
1 #include "graphicsscenerectmove.h"
2 #include <KDebug>
3 #include <QGraphicsSceneMouseEvent>
4 #include <QGraphicsRectItem>
5 #include <QGraphicsView>
6 #include <QCursor>
7
8 QGraphicsItem* selected=NULL;
9 int button=0;
10 int resizeMode=-1;
11 enum resizeMode {NoResize,TopLeft,BottomLeft,TopRight,BottomRight,Left,Right,Up,Down};
12 GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent):QGraphicsScene(parent){
13         //grabMouse();  
14 }
15
16 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e){
17
18         if (selected && selected->type()==3 &&button==1){
19                 
20                 QGraphicsRectItem *gi=(QGraphicsRectItem*)selected;
21                 QRectF newrect=gi->rect();
22                 QPointF newpoint=e->scenePos();
23                 newpoint-=selected->scenePos();
24                 switch (resizeMode){
25                 case TopLeft:
26                         newrect.setTopLeft(newpoint);
27                         break;
28                 case BottomLeft:
29                         newrect.setBottomLeft(newpoint);
30                         break;
31                 case TopRight:
32                         newrect.setTopRight(newpoint);
33                         break;
34                 case BottomRight:
35                         newrect.setBottomRight(newpoint);
36                         break;
37                 case Left:
38                         newrect.setLeft(newpoint.x());
39                         break;
40                 case Right:
41                         newrect.setRight(newpoint.x());
42                         break;
43                 case Up:
44                         newrect.setTop(newpoint.y());
45                         break;
46                 case Down:
47                         newrect.setBottom(newpoint.y());
48                         break;
49                 }
50                 
51                 gi->setRect(newrect);
52                 gi->setPos(selected->scenePos());
53         }
54         
55         QPointF p=e->scenePos();
56         p+=QPoint(-2,-2);
57         resizeMode=NoResize;
58         selected=NULL;
59         foreach(QGraphicsItem* g, items( QRectF( p , QSizeF(4,4) ).toRect() ) ){
60
61                 if (g->type()==3 ){
62                         
63                         QGraphicsRectItem *gi=(QGraphicsRectItem*)g;
64                         QRectF r=gi->rect();
65                         r.translate(gi->scenePos());
66                         
67                         if ( (r.toRect().topLeft()-=e->scenePos().toPoint() ).manhattanLength()<3 ){
68                                 resizeMode=TopLeft;
69                         }else if ((r.toRect().bottomLeft()-=e->scenePos().toPoint() ).manhattanLength()<3 ){
70                                 resizeMode=BottomLeft;  
71                         }else if ((r.toRect().topRight()-=e->scenePos().toPoint() ).manhattanLength()<3 ){
72                                 resizeMode=TopRight;    
73                         }else if ((r.toRect().bottomRight()-=e->scenePos().toPoint() ).manhattanLength()<3 ){
74                                 resizeMode=BottomRight; 
75                         }else if ( qAbs(r.toRect().left()-e->scenePos().toPoint().x() ) <3){
76                                 resizeMode=Left;        
77                         }else if ( qAbs(r.toRect().right()-e->scenePos().toPoint().x() ) <3){
78                                 resizeMode=Right;       
79                         }else if ( qAbs(r.toRect().top()-e->scenePos().toPoint().y() ) <3){
80                                 resizeMode=Up;  
81                         }else if ( qAbs(r.toRect().bottom()-e->scenePos().toPoint().y() ) <3){
82                                 resizeMode=Down;        
83                         }
84                         if (resizeMode!=NoResize)
85                                 selected=gi;
86                 }
87         }
88         switch (resizeMode){
89                 case TopLeft:
90                 case BottomRight:
91                         setCursor(QCursor(Qt::SizeFDiagCursor));
92                         break;
93                 case BottomLeft:
94                 case TopRight:
95                         setCursor(QCursor(Qt::SizeBDiagCursor));
96                         break;
97                 case Left:
98                 case Right:
99                         setCursor(Qt::SizeHorCursor);
100                         break;
101                 case Up:
102                 case Down:
103                         setCursor(Qt::SizeVerCursor);
104                         break;
105                 default:
106                         setCursor(QCursor(Qt::ArrowCursor));
107                         QGraphicsScene::mouseMoveEvent(e);
108         }
109
110         
111 }
112
113 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e){
114         button=e->button();
115         if (!selected || selected->type()!=3)
116                 QGraphicsScene::mousePressEvent(e);
117 }
118 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent* e){
119         button=0;
120         if (!selected || selected->type()!=3)
121                 QGraphicsScene::mouseReleaseEvent(e);
122 }
123
124 void GraphicsSceneRectMove::setCursor(QCursor c){
125         QList<QGraphicsView*> l=views();
126         foreach(QGraphicsView* v, l){
127                 v->setCursor(c);
128         }
129 }