]> git.sesse.net Git - kdenlive/blob - src/graphicsscenerectmove.cpp
d6bd318893dd7601905702e5c1d38d70b99a539e
[kdenlive] / src / graphicsscenerectmove.cpp
1
2 #include <KDebug>
3 #include <QGraphicsSceneMouseEvent>
4 #include <QGraphicsRectItem>
5 #include <QGraphicsView>
6 #include <QCursor>
7 #include <QList>
8
9 #include "graphicsscenerectmove.h"
10
11 GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent): QGraphicsScene(parent), m_selectedItem(NULL), resizeMode(NoResize) {
12     //grabMouse();
13     zoom = 1.0;
14 }
15
16 //virtual
17 void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e) {
18     QPointF p = e->scenePos();
19     p += QPoint(-2, -2);
20     resizeMode = NoResize;
21     m_selectedItem = NULL;
22     QGraphicsItem* g = items(QRectF(p , QSizeF(4, 4)).toRect()).at(0);
23     if (g) {
24         if (g->type() == 8) {
25             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
26             t->setTextInteractionFlags(Qt::TextEditorInteraction);
27         }
28     }
29     QGraphicsScene::mouseDoubleClickEvent(e);
30 }
31
32 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) {
33     QPointF p = e->scenePos();
34     p += QPoint(-2, -2);
35     resizeMode = NoResize;
36     QList <QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
37     QGraphicsItem *item = NULL;
38     bool hasSelected = false;
39     foreach(QGraphicsItem* g, list) {
40         // check is there is a selected item in list
41         if (g->zValue() > -1000 && g->isSelected()) {
42             hasSelected = true;
43             item = g;
44             break;
45         }
46     }
47     if (item == NULL) {
48         if (m_selectedItem && m_selectedItem->type() == 8) {
49             // disable text editing
50             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
51             t->setTextInteractionFlags(Qt::NoTextInteraction);
52         }
53         m_selectedItem = NULL;
54         foreach(QGraphicsItem* g, list) {
55             if (g->zValue() > -1000) {
56                 item = g;
57                 break;
58             }
59         }
60     }
61     if (item != NULL) {
62         m_clickPoint = e->scenePos();
63         m_selectedItem = item;
64         if (item->type() == 8) {
65             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(item);
66             if (t->textInteractionFlags() == Qt::TextEditorInteraction) {
67                 QGraphicsScene::mousePressEvent(e);
68                 return;
69             }
70             t->setTextInteractionFlags(Qt::NoTextInteraction);
71         } else if (item->type() == 3) {
72             QGraphicsRectItem *gi = (QGraphicsRectItem*)item;
73             QRectF r = gi->rect();
74             r.translate(gi->scenePos());
75             if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
76                 resizeMode = TopLeft;
77             } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
78                 resizeMode = BottomLeft;
79             } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
80                 resizeMode = TopRight;
81             } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
82                 resizeMode = BottomRight;
83             } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
84                 resizeMode = Left;
85             } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
86                 resizeMode = Right;
87             } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
88                 resizeMode = Up;
89             } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
90                 resizeMode = Down;
91             }
92         }
93     }
94     if (!hasSelected) QGraphicsScene::mousePressEvent(e);
95
96     kDebug() << "//////  MOUSE CLICK, RESIZE MODE: " << resizeMode;
97
98 }
99
100 //virtual
101 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent* e) {
102     //m_selectedItem = NULL;
103 }
104
105
106 //virtual
107 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) {
108
109     if (m_selectedItem && e->buttons() & Qt::LeftButton) {
110         if (m_selectedItem->type() == 3) {
111             QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
112             QRectF newrect = gi->rect();
113             QPointF newpoint = e->scenePos();
114             //newpoint -= m_selectedItem->scenePos();
115             switch (resizeMode) {
116             case TopLeft:
117                 newrect.setBottomRight(newrect.bottomRight() + gi->pos() - newpoint);
118                 gi->setPos(newpoint);
119                 break;
120             case BottomLeft:
121                 newrect.setBottomRight(QPointF(newrect.bottomRight().x() + gi->pos().x() - newpoint.x(), newpoint.y() - gi->pos().y()));
122                 gi->setPos(QPointF(newpoint.x(), gi->pos().y()));
123                 break;
124             case TopRight:
125                 newrect.setBottomRight(QPointF(newpoint.x() - gi->pos().x(), newrect.bottom() + gi->pos().y() - newpoint.y()));
126                 gi->setPos(QPointF(gi->pos().x(), newpoint.y()));
127                 break;
128             case BottomRight:
129                 newrect.setBottomRight(newpoint - gi->pos());
130                 break;
131             case Left:
132                 newrect.setRight(gi->pos().x() + newrect.width() - newpoint.x());
133                 gi->setPos(QPointF(newpoint.x(), gi->pos().y()));
134                 break;
135             case Right:
136                 newrect.setRight(newpoint.x() - gi->pos().x());
137                 break;
138             case Up:
139                 newrect.setBottom(gi->pos().y() + newrect.bottom() - newpoint.y());
140                 gi->setPos(QPointF(gi->pos().x(), newpoint.y()));
141                 break;
142             case Down:
143                 newrect.setBottom(newpoint.y() - gi->pos().y());
144                 break;
145             default:
146                 QPointF diff = e->scenePos() - m_clickPoint;
147                 m_clickPoint = e->scenePos();
148                 gi->moveBy(diff.x(), diff.y());
149                 break;
150             }
151             gi->setRect(newrect);
152             //gi->setPos(m_selectedItem->scenePos());
153             /*if (resizeMode == NoResize) {
154                 QGraphicsScene::mouseMoveEvent(e);
155                 return;
156             }*/
157         } else if (m_selectedItem->type() == 8) {
158             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
159             if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
160                 QGraphicsScene::mouseMoveEvent(e);
161                 return;
162             }
163             QPointF diff = e->scenePos() - m_clickPoint;
164             m_clickPoint = e->scenePos();
165             m_selectedItem->moveBy(diff.x(), diff.y());
166         }
167         emit itemMoved();
168     } else {
169
170         QPointF p = e->scenePos();
171         p += QPoint(-2, -2);
172         resizeMode = NoResize;
173
174         foreach(QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) {
175
176             if (g->type() == 3) {
177
178                 QGraphicsRectItem *gi = (QGraphicsRectItem*)g;
179                 QRectF r = gi->rect();
180                 r.translate(gi->scenePos());
181
182                 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
183                     setCursor(QCursor(Qt::SizeFDiagCursor));
184                 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
185                     setCursor(QCursor(Qt::SizeBDiagCursor));
186                 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
187                     setCursor(QCursor(Qt::SizeBDiagCursor));
188                 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
189                     setCursor(QCursor(Qt::SizeFDiagCursor));
190                 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
191                     setCursor(Qt::SizeHorCursor);
192                 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
193                     setCursor(Qt::SizeHorCursor);
194                 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
195                     setCursor(Qt::SizeVerCursor);
196                 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
197                     setCursor(Qt::SizeVerCursor);
198                 } else setCursor(QCursor(Qt::ArrowCursor));
199             }
200             break;
201         }
202         QGraphicsScene::mouseMoveEvent(e);
203     }
204 }
205
206 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) {
207     QList<QGraphicsView*> viewlist = views();
208     //kDebug() << wheelEvent->delta() << " " << zoom;
209     double scale = 1.0;
210     if (viewlist.size() > 0) {
211         if (wheelEvent->delta() < 0) emit sceneZoom(true);
212         else emit sceneZoom(false);
213     }
214 }
215
216 void GraphicsSceneRectMove::setScale(double s) {
217     if (zoom < 1.0 / 7.0 && s < 1.0) return;
218     else if (zoom > 10.0 / 7.9 && s > 1.0) return;
219     QList<QGraphicsView*> viewlist = views();
220     if (viewlist.size() > 0) {
221         viewlist[0]->scale(s, s);
222         zoom = zoom * s;
223     }
224     //kDebug()<<"//////////  ZOOM: "<<zoom;
225 }
226
227 void GraphicsSceneRectMove::setZoom(double s) {
228     QList<QGraphicsView*> viewlist = views();
229     if (viewlist.size() > 0) {
230         viewlist[0]->resetTransform();
231         viewlist[0]->scale(s, s);
232         zoom = s;
233     }
234
235     //kDebug()<<"//////////  ZOOM: "<<zoom;
236 }
237
238 void GraphicsSceneRectMove::setCursor(QCursor c) {
239     QList<QGraphicsView*> l = views();
240     foreach(QGraphicsView* v, l) {
241         v->setCursor(c);
242     }
243 }