]> git.sesse.net Git - kdenlive/blob - src/graphicsscenerectmove.cpp
Start improving title creator
[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) {
76                 resizeMode = TopLeft;
77             } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6) {
78                 resizeMode = BottomLeft;
79             } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6) {
80                 resizeMode = TopRight;
81             } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6) {
82                 resizeMode = BottomRight;
83             } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3) {
84                 resizeMode = Left;
85             } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3) {
86                 resizeMode = Right;
87             } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3) {
88                 resizeMode = Up;
89             } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3) {
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.setTopLeft(newpoint);
118                 break;
119             case BottomLeft:
120                 newrect.setBottomLeft(newpoint);
121                 break;
122             case TopRight:
123                 newrect.setTopRight(newpoint);
124                 break;
125             case BottomRight:
126                 newrect.setBottomRight(newpoint);
127                 break;
128             case Left:
129                 newrect.setLeft(newpoint.x());
130                 break;
131             case Right:
132                 newrect.setRight(newpoint.x());
133                 break;
134             case Up:
135                 newrect.setTop(newpoint.y());
136                 break;
137             case Down:
138                 newrect.setBottom(newpoint.y());
139                 break;
140             default:
141                 QPointF diff = e->scenePos() - m_clickPoint;
142                 m_clickPoint = e->scenePos();
143                 gi->moveBy(diff.x(), diff.y());
144                 break;
145             }
146             gi->setRect(newrect);
147             //gi->setPos(m_selectedItem->scenePos());
148             /*if (resizeMode == NoResize) {
149                 QGraphicsScene::mouseMoveEvent(e);
150                 return;
151             }*/
152         } else if (m_selectedItem->type() == 8) {
153             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
154             if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
155                 QGraphicsScene::mouseMoveEvent(e);
156                 return;
157             }
158             QPointF diff = e->scenePos() - m_clickPoint;
159             m_clickPoint = e->scenePos();
160             m_selectedItem->moveBy(diff.x(), diff.y());
161         }
162     } else {
163
164         QPointF p = e->scenePos();
165         p += QPoint(-2, -2);
166         resizeMode = NoResize;
167
168         foreach(QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) {
169
170             if (g->type() == 3) {
171
172                 QGraphicsRectItem *gi = (QGraphicsRectItem*)g;
173                 QRectF r = gi->rect();
174                 r.translate(gi->scenePos());
175
176                 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6) {
177                     setCursor(QCursor(Qt::SizeFDiagCursor));
178                 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6) {
179                     setCursor(QCursor(Qt::SizeBDiagCursor));
180                 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6) {
181                     setCursor(QCursor(Qt::SizeBDiagCursor));
182                 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6) {
183                     setCursor(QCursor(Qt::SizeFDiagCursor));
184                 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3) {
185                     setCursor(Qt::SizeHorCursor);
186                 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3) {
187                     setCursor(Qt::SizeHorCursor);
188                 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3) {
189                     setCursor(Qt::SizeVerCursor);
190                 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3) {
191                     setCursor(Qt::SizeVerCursor);
192                 } else setCursor(QCursor(Qt::ArrowCursor));
193             }
194             break;
195         }
196         QGraphicsScene::mouseMoveEvent(e);
197     }
198 }
199
200 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) {
201     QList<QGraphicsView*> viewlist = views();
202     kDebug() << wheelEvent->delta() << " " << zoom;
203     double scale = 1.0;
204     if (viewlist.size() > 0) {
205         if (wheelEvent->delta() < 0 && zoom < 20.0) {
206             scale = 1.1;
207
208         } else if (wheelEvent->delta() > 0 && zoom > .05) {
209             scale = 1 / 1.1;
210         }
211         setScale(scale);
212         //viewlist[0]->resetTransform();
213         //viewlist[0]->scale(zoom, zoom);
214     }
215 }
216
217 void GraphicsSceneRectMove::setScale(double s) {
218     QList<QGraphicsView*> viewlist = views();
219     if (viewlist.size() > 0) {
220         viewlist[0]->scale(s, s);
221         zoom = zoom * s;
222     }
223 }
224
225 void GraphicsSceneRectMove::setCursor(QCursor c) {
226     QList<QGraphicsView*> l = views();
227     foreach(QGraphicsView* v, l) {
228         v->setCursor(c);
229     }
230 }