]> git.sesse.net Git - kdenlive/blob - src/graphicsscenerectmove.cpp
title widget: small UI fixes and one crash fix
[kdenlive] / src / graphicsscenerectmove.cpp
1
2 #include <KDebug>
3 #include <QGraphicsSceneMouseEvent>
4 #include <QGraphicsRectItem>
5 #include <QGraphicsView>
6 #include <QCursor>
7 #include <QList>
8 #include <QKeyEvent>
9 #include <QApplication>
10
11 #include "graphicsscenerectmove.h"
12
13 GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent): QGraphicsScene(parent), m_selectedItem(NULL), resizeMode(NoResize), m_tool(TITLE_RECTANGLE) {
14     //grabMouse();
15     zoom = 1.0;
16     setBackgroundBrush(QBrush(QColor(0, 0, 0, 0)));
17 }
18
19 void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item) {
20     clearSelection();
21     m_selectedItem = item;
22     item->setSelected(true);
23     update();
24 }
25
26 TITLETOOL GraphicsSceneRectMove::tool() {
27     return m_tool;
28 }
29
30 void GraphicsSceneRectMove::setTool(TITLETOOL tool) {
31     m_tool = tool;
32     switch (m_tool) {
33     case TITLE_RECTANGLE:
34         setCursor(Qt::CrossCursor);
35         break;
36     case TITLE_TEXT:
37         setCursor(Qt::IBeamCursor);
38         break;
39     default:
40         setCursor(Qt::ArrowCursor);
41     }
42 }
43
44 //virtual
45 void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent) {
46     if (m_selectedItem == NULL) {
47         QGraphicsScene::keyPressEvent(keyEvent);
48         return;
49     }
50     int diff = 1;
51     if (keyEvent->modifiers() & Qt::ControlModifier) diff = 10;
52     switch (keyEvent->key()) {
53     case Qt::Key_Left:
54         m_selectedItem->setPos(m_selectedItem->pos() - QPointF(diff, 0));
55         emit itemMoved();
56         break;
57     case Qt::Key_Right:
58         m_selectedItem->setPos(m_selectedItem->pos() + QPointF(diff, 0));
59         emit itemMoved();
60         break;
61     case Qt::Key_Up:
62         m_selectedItem->setPos(m_selectedItem->pos() - QPointF(0, diff));
63         emit itemMoved();
64         break;
65     case Qt::Key_Down:
66         m_selectedItem->setPos(m_selectedItem->pos() + QPointF(0, diff));
67         emit itemMoved();
68         break;
69     case Qt::Key_Delete:
70     case Qt::Key_Backspace:
71         delete m_selectedItem;
72         m_selectedItem = NULL;
73         emit selectionChanged();
74         break;
75     default:
76         QGraphicsScene::keyPressEvent(keyEvent);
77     }
78 }
79
80 //virtual
81 void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e) {
82     QPointF p = e->scenePos();
83     p += QPoint(-2, -2);
84     resizeMode = NoResize;
85     m_selectedItem = NULL;
86     QGraphicsItem* g = items(QRectF(p , QSizeF(4, 4)).toRect()).at(0);
87     if (g) {
88         if (g->type() == 8) {
89             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
90             t->setTextInteractionFlags(Qt::TextEditorInteraction);
91         }
92     }
93     QGraphicsScene::mouseDoubleClickEvent(e);
94 }
95
96 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
97     if (m_tool == TITLE_RECTANGLE && m_selectedItem) setSelectedItem(m_selectedItem);
98     emit actionFinished();
99     QGraphicsScene::mouseReleaseEvent(e);
100 }
101
102 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) {
103     QPointF p = e->scenePos();
104     p += QPoint(-2, -2);
105     resizeMode = NoResize;
106     QList <QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
107     QGraphicsItem *item = NULL;
108     bool hasSelected = false;
109
110     if (m_tool == TITLE_SELECT) {
111         foreach(QGraphicsItem* g, list) {
112             kDebug() << " - - CHECKING ITEM Z:" << g->zValue() << ", TYPE: " << g->type();
113             // check is there is a selected item in list
114             if (g->zValue() > -1000 && g->isSelected()) {
115                 hasSelected = true;
116                 item = g;
117                 break;
118             }
119         }
120         if (item == NULL) {
121             if (m_selectedItem && m_selectedItem->type() == 8) {
122                 // disable text editing
123                 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
124                 t->setTextInteractionFlags(Qt::NoTextInteraction);
125             }
126             m_selectedItem = NULL;
127             foreach(QGraphicsItem* g, list) {
128                 if (g->zValue() > -1000) {
129                     item = g;
130                     break;
131                 }
132             }
133         }
134         if (item != NULL) {
135             m_clickPoint = e->scenePos();
136             m_selectedItem = item;
137             kDebug() << "/////////  ITEM TYPE: " << item->type();
138             if (item->type() == 8) {
139                 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(item);
140                 if (t->textInteractionFlags() == Qt::TextEditorInteraction) {
141                     QGraphicsScene::mousePressEvent(e);
142                     return;
143                 }
144                 t->setTextInteractionFlags(Qt::NoTextInteraction);
145                 setCursor(Qt::ClosedHandCursor);
146             } else if (item->type() == 3) {
147                 QGraphicsRectItem *gi = (QGraphicsRectItem*)item;
148                 QRectF r = gi->rect();
149                 r.translate(gi->scenePos());
150                 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
151                     resizeMode = TopLeft;
152                 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
153                     resizeMode = BottomLeft;
154                 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
155                     resizeMode = TopRight;
156                 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
157                     resizeMode = BottomRight;
158                 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
159                     resizeMode = Left;
160                 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
161                     resizeMode = Right;
162                 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
163                     resizeMode = Up;
164                 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
165                     resizeMode = Down;
166                 } else setCursor(Qt::ClosedHandCursor);
167             }
168         }
169         QGraphicsScene::mousePressEvent(e);
170     } else if (m_tool == TITLE_RECTANGLE) {
171         m_clickPoint = e->scenePos();
172         m_selectedItem = NULL;
173     } else if (m_tool == TITLE_TEXT) {
174         m_selectedItem = addText(QString());
175         emit newText((QGraphicsTextItem *) m_selectedItem);
176         m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
177         ((QGraphicsTextItem *)m_selectedItem)->setTextInteractionFlags(Qt::TextEditorInteraction);
178         m_selectedItem->setPos(e->scenePos());
179         QGraphicsScene::mousePressEvent(e);
180     }
181
182     kDebug() << "//////  MOUSE CLICK, RESIZE MODE: " << resizeMode;
183
184 }
185
186
187 //virtual
188 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) {
189
190     if (m_selectedItem && e->buttons() & Qt::LeftButton) {
191         if (m_selectedItem->type() == 3) {
192             QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
193             QRectF newrect = gi->rect();
194             QPointF newpoint = e->scenePos();
195             //newpoint -= m_selectedItem->scenePos();
196             switch (resizeMode) {
197             case TopLeft:
198                 newrect.setBottomRight(newrect.bottomRight() + gi->pos() - newpoint);
199                 gi->setPos(newpoint);
200                 break;
201             case BottomLeft:
202                 newrect.setBottomRight(QPointF(newrect.bottomRight().x() + gi->pos().x() - newpoint.x(), newpoint.y() - gi->pos().y()));
203                 gi->setPos(QPointF(newpoint.x(), gi->pos().y()));
204                 break;
205             case TopRight:
206                 newrect.setBottomRight(QPointF(newpoint.x() - gi->pos().x(), newrect.bottom() + gi->pos().y() - newpoint.y()));
207                 gi->setPos(QPointF(gi->pos().x(), newpoint.y()));
208                 break;
209             case BottomRight:
210                 newrect.setBottomRight(newpoint - gi->pos());
211                 break;
212             case Left:
213                 newrect.setRight(gi->pos().x() + newrect.width() - newpoint.x());
214                 gi->setPos(QPointF(newpoint.x(), gi->pos().y()));
215                 break;
216             case Right:
217                 newrect.setRight(newpoint.x() - gi->pos().x());
218                 break;
219             case Up:
220                 newrect.setBottom(gi->pos().y() + newrect.bottom() - newpoint.y());
221                 gi->setPos(QPointF(gi->pos().x(), newpoint.y()));
222                 break;
223             case Down:
224                 newrect.setBottom(newpoint.y() - gi->pos().y());
225                 break;
226             default:
227                 QPointF diff = e->scenePos() - m_clickPoint;
228                 m_clickPoint = e->scenePos();
229                 gi->moveBy(diff.x(), diff.y());
230                 break;
231             }
232             gi->setRect(newrect);
233             //gi->setPos(m_selectedItem->scenePos());
234             /*if (resizeMode == NoResize) {
235                 QGraphicsScene::mouseMoveEvent(e);
236                 return;
237             }*/
238         } else if (m_selectedItem->type() == 8) {
239             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
240             if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
241                 QGraphicsScene::mouseMoveEvent(e);
242                 return;
243             }
244             QPointF diff = e->scenePos() - m_clickPoint;
245             m_clickPoint = e->scenePos();
246             m_selectedItem->moveBy(diff.x(), diff.y());
247         }
248         emit itemMoved();
249     } else if (m_tool == TITLE_SELECT) {
250
251         QPointF p = e->scenePos();
252         p += QPoint(-2, -2);
253         resizeMode = NoResize;
254         bool itemFound = false;
255         foreach(QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) {
256             if (g->type() == 3 && g->zValue() > -1000) {
257                 QGraphicsRectItem *gi = (QGraphicsRectItem*)g;
258                 QRectF r = gi->rect();
259                 r.translate(gi->scenePos());
260                 itemFound = true;
261                 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
262                     setCursor(QCursor(Qt::SizeFDiagCursor));
263                 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
264                     setCursor(QCursor(Qt::SizeBDiagCursor));
265                 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
266                     setCursor(QCursor(Qt::SizeBDiagCursor));
267                 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
268                     setCursor(QCursor(Qt::SizeFDiagCursor));
269                 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
270                     setCursor(Qt::SizeHorCursor);
271                 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
272                     setCursor(Qt::SizeHorCursor);
273                 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
274                     setCursor(Qt::SizeVerCursor);
275                 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
276                     setCursor(Qt::SizeVerCursor);
277                 } else setCursor(Qt::OpenHandCursor);
278                 break;
279             }
280             if (!itemFound) setCursor(Qt::ArrowCursor);
281         }
282         QGraphicsScene::mouseMoveEvent(e);
283     } else if (m_tool == TITLE_RECTANGLE && e->buttons() & Qt::LeftButton) {
284         if (m_selectedItem == NULL && (m_clickPoint.toPoint() - e->scenePos().toPoint()).manhattanLength() >= QApplication::startDragDistance()) {
285             // create new rect item
286             m_selectedItem = addRect(0, 0, e->scenePos().x() - m_clickPoint.x(), e->scenePos().y() - m_clickPoint.y());
287             emit newRect((QGraphicsRectItem *) m_selectedItem);
288             m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
289             m_selectedItem->setPos(m_clickPoint);
290             resizeMode = BottomRight;
291             QGraphicsScene::mouseMoveEvent(e);
292         }
293     }
294 }
295
296 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) {
297     QList<QGraphicsView*> viewlist = views();
298     //kDebug() << wheelEvent->delta() << " " << zoom;
299     double scale = 1.0;
300     if (viewlist.size() > 0) {
301         if (wheelEvent->delta() < 0) emit sceneZoom(true);
302         else emit sceneZoom(false);
303     }
304 }
305
306 void GraphicsSceneRectMove::setScale(double s) {
307     if (zoom < 1.0 / 7.0 && s < 1.0) return;
308     else if (zoom > 10.0 / 7.9 && s > 1.0) return;
309     QList<QGraphicsView*> viewlist = views();
310     if (viewlist.size() > 0) {
311         viewlist[0]->scale(s, s);
312         zoom = zoom * s;
313     }
314     //kDebug()<<"//////////  ZOOM: "<<zoom;
315 }
316
317 void GraphicsSceneRectMove::setZoom(double s) {
318     QList<QGraphicsView*> viewlist = views();
319     if (viewlist.size() > 0) {
320         viewlist[0]->resetTransform();
321         viewlist[0]->scale(s, s);
322         zoom = s;
323     }
324
325     //kDebug()<<"//////////  ZOOM: "<<zoom;
326 }
327
328 void GraphicsSceneRectMove::setCursor(QCursor c) {
329     QList<QGraphicsView*> l = views();
330     foreach(QGraphicsView* v, l) {
331         v->setCursor(c);
332     }
333 }