]> git.sesse.net Git - kdenlive/blob - src/graphicsscenerectmove.cpp
zoom into scene
[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         zoom=1.0;
15 }
16
17 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e){
18         
19         if (selected && selected->type()==3 && e->buttons() & Qt::LeftButton){
20                 
21                 QGraphicsRectItem *gi=(QGraphicsRectItem*)selected;
22                 QRectF newrect=gi->rect();
23                 QPointF newpoint=e->scenePos();
24                 newpoint-=selected->scenePos();
25                 switch (resizeMode){
26                 case TopLeft:
27                         newrect.setTopLeft(newpoint);
28                         break;
29                 case BottomLeft:
30                         newrect.setBottomLeft(newpoint);
31                         break;
32                 case TopRight:
33                         newrect.setTopRight(newpoint);
34                         break;
35                 case BottomRight:
36                         newrect.setBottomRight(newpoint);
37                         break;
38                 case Left:
39                         newrect.setLeft(newpoint.x());
40                         break;
41                 case Right:
42                         newrect.setRight(newpoint.x());
43                         break;
44                 case Up:
45                         newrect.setTop(newpoint.y());
46                         break;
47                 case Down:
48                         newrect.setBottom(newpoint.y());
49                         break;
50                 }
51                 
52                 gi->setRect(newrect);
53                 gi->setPos(selected->scenePos());
54         }
55         
56         QPointF p=e->scenePos();
57         p+=QPoint(-2,-2);
58         resizeMode=NoResize;
59         selected=NULL;
60         foreach(QGraphicsItem* g, items( QRectF( p , QSizeF(4,4) ).toRect() ) ){
61
62                 if (g->type()==3 ){
63                         
64                         QGraphicsRectItem *gi=(QGraphicsRectItem*)g;
65                         QRectF r=gi->rect();
66                         r.translate(gi->scenePos());
67                         
68                         if ( (r.toRect().topLeft()-=e->scenePos().toPoint() ).manhattanLength()<3 ){
69                                 resizeMode=TopLeft;
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 ){
73                                 resizeMode=TopRight;    
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){
77                                 resizeMode=Left;        
78                         }else if ( qAbs(r.toRect().right()-e->scenePos().toPoint().x() ) <3){
79                                 resizeMode=Right;       
80                         }else if ( qAbs(r.toRect().top()-e->scenePos().toPoint().y() ) <3){
81                                 resizeMode=Up;  
82                         }else if ( qAbs(r.toRect().bottom()-e->scenePos().toPoint().y() ) <3){
83                                 resizeMode=Down;        
84                         }
85                         if (resizeMode!=NoResize)
86                                 selected=gi;
87                 }
88                 break;
89         }
90         switch (resizeMode){
91                 case TopLeft:
92                 case BottomRight:
93                         setCursor(QCursor(Qt::SizeFDiagCursor));
94                         break;
95                 case BottomLeft:
96                 case TopRight:
97                         setCursor(QCursor(Qt::SizeBDiagCursor));
98                         break;
99                 case Left:
100                 case Right:
101                         setCursor(Qt::SizeHorCursor);
102                         break;
103                 case Up:
104                 case Down:
105                         setCursor(Qt::SizeVerCursor);
106                         break;
107                 default:
108                         setCursor(QCursor(Qt::ArrowCursor));
109                         QGraphicsScene::mouseMoveEvent(e);
110         }
111
112         
113 }
114
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){
120                         zoom*=1.1;
121                         
122                 }else if (wheelEvent->delta()>0 && zoom>.05){
123                         zoom/=1.1;
124                 }
125                 
126                 viewlist[0]->resetTransform();
127                 viewlist[0]->scale(zoom,zoom);
128         }
129 }
130
131 void GraphicsSceneRectMove::setCursor(QCursor c){
132         QList<QGraphicsView*> l=views();
133         foreach(QGraphicsView* v, l){
134                 v->setCursor(c);
135         }
136 }