]> git.sesse.net Git - kdenlive/blob - src/graphicsscenerectmove.cpp
New option in composite transition: double click in rectangle to manually adjust...
[kdenlive] / src / graphicsscenerectmove.cpp
1 /***************************************************************************
2  *   copyright (C) 2008 by Marco Gittler (g.marco@freenet.de)                                 *
3  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
19  ***************************************************************************/
20
21 #include "graphicsscenerectmove.h"
22
23 #include <KDebug>
24 #include <QGraphicsSceneMouseEvent>
25 #include <QGraphicsRectItem>
26 #include <QGraphicsSvgItem>
27 #include <QGraphicsView>
28 #include <QCursor>
29 #include <QTextCursor>
30 #include <QList>
31 #include <QKeyEvent>
32 #include <QApplication>
33 #include <QTextBlock>
34
35
36 GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent) :
37         QGraphicsScene(parent),
38         m_selectedItem(NULL),
39         m_resizeMode(NoResize),
40         m_tool(TITLE_RECTANGLE)
41 {
42     //grabMouse();
43     m_zoom = 1.0;
44     setBackgroundBrush(QBrush(Qt::transparent));
45 }
46
47 void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item)
48 {
49     clearSelection();
50     m_selectedItem = item;
51     item->setSelected(true);
52     update();
53 }
54
55 TITLETOOL GraphicsSceneRectMove::tool()
56 {
57     return m_tool;
58 }
59
60 void GraphicsSceneRectMove::setTool(TITLETOOL tool)
61 {
62     m_tool = tool;
63     switch (m_tool) {
64     case TITLE_RECTANGLE:
65         setCursor(Qt::CrossCursor);
66         break;
67     case TITLE_TEXT:
68         setCursor(Qt::IBeamCursor);
69         break;
70     default:
71         setCursor(Qt::ArrowCursor);
72     }
73 }
74
75 //virtual
76 void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent)
77 {
78     if (m_selectedItem == NULL) {
79         QGraphicsScene::keyPressEvent(keyEvent);
80         return;
81     }
82     int diff = 1;
83     if (m_selectedItem->type() == 8) {
84         QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
85         if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
86             QGraphicsScene::keyPressEvent(keyEvent);
87             return;
88         }
89     }
90     if (keyEvent->modifiers() & Qt::ControlModifier) diff = 10;
91     switch (keyEvent->key()) {
92     case Qt::Key_Left:
93         m_selectedItem->setPos(m_selectedItem->pos() - QPointF(diff, 0));
94         emit itemMoved();
95         break;
96     case Qt::Key_Right:
97         m_selectedItem->setPos(m_selectedItem->pos() + QPointF(diff, 0));
98         emit itemMoved();
99         break;
100     case Qt::Key_Up:
101         m_selectedItem->setPos(m_selectedItem->pos() - QPointF(0, diff));
102         emit itemMoved();
103         break;
104     case Qt::Key_Down:
105         m_selectedItem->setPos(m_selectedItem->pos() + QPointF(0, diff));
106         emit itemMoved();
107         break;
108     case Qt::Key_Delete:
109     case Qt::Key_Backspace:
110         delete m_selectedItem;
111         m_selectedItem = NULL;
112         emit selectionChanged();
113         break;
114     default:
115         QGraphicsScene::keyPressEvent(keyEvent);
116     }
117     emit actionFinished();
118 }
119
120 //virtual
121 void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e)
122 {
123     QPointF p = e->scenePos();
124     p += QPoint(-2, -2);
125     m_resizeMode = NoResize;
126     m_selectedItem = NULL;
127     QGraphicsItem* g = items(QRectF(p , QSizeF(4, 4)).toRect()).at(0);
128     if (g) {
129         if (g->type() == 8) {
130             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
131             m_selectedItem = g;
132             t->setTextInteractionFlags(Qt::TextEditorInteraction);
133         } else emit doubleClickEvent();
134     }
135     QGraphicsScene::mouseDoubleClickEvent(e);
136 }
137
138 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
139 {
140     if (m_tool == TITLE_RECTANGLE && m_selectedItem) setSelectedItem(m_selectedItem);
141     QGraphicsScene::mouseReleaseEvent(e);
142     emit actionFinished();
143 }
144
145 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e)
146 {
147     m_clickPoint = e->screenPos();
148     QPointF p = e->scenePos();
149     p += QPoint(-2, -2);
150     m_resizeMode = NoResize;
151     const QList <QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
152     QGraphicsItem *item = NULL;
153     bool hasSelected = false;
154
155     if (m_tool == TITLE_SELECT) {
156         foreach(QGraphicsItem *g, list) {
157             kDebug() << " - - CHECKING ITEM Z:" << g->zValue() << ", TYPE: " << g->type();
158             // check is there is a selected item in list
159             if (g->zValue() > -1000 && g->isSelected()) {
160                 hasSelected = true;
161                 item = g;
162                 break;
163             }
164         }
165         if (item == NULL) {
166             if (m_selectedItem && m_selectedItem->type() == 8) {
167                 // disable text editing
168                 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
169                 t->textCursor().setPosition(0);
170                 QTextBlock cur = t->textCursor().block();
171                 t->setTextCursor(QTextCursor(cur));
172                 t->setTextInteractionFlags(Qt::NoTextInteraction);
173             }
174             m_selectedItem = NULL;
175             foreach(QGraphicsItem* g, list) {
176                 if (g->zValue() > -1000) {
177                     item = g;
178                     break;
179                 }
180             }
181         }
182         if (item != NULL) {
183             m_sceneClickPoint = e->scenePos();
184             m_selectedItem = item;
185             kDebug() << "/////////  ITEM TYPE: " << item->type();
186             if (item->type() == 8) {
187                 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(item);
188                 if (t->textInteractionFlags() == Qt::TextEditorInteraction) {
189                     QGraphicsScene::mousePressEvent(e);
190                     return;
191                 }
192                 t->setTextInteractionFlags(Qt::NoTextInteraction);
193                 setCursor(Qt::ClosedHandCursor);
194             } else if (item->type() == 3 || item->type() == 13 || item->type() == 7) {
195                 QRectF r;
196                 if (m_selectedItem->type() == 3) {
197                     r = ((QGraphicsRectItem*)m_selectedItem)->rect();
198                 } else r = m_selectedItem->boundingRect();
199
200                 r.translate(item->scenePos());
201                 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
202                     m_resizeMode = TopLeft;
203                 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
204                     m_resizeMode = BottomLeft;
205                 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
206                     m_resizeMode = TopRight;
207                 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
208                     m_resizeMode = BottomRight;
209                 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / m_zoom) {
210                     m_resizeMode = Left;
211                 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / m_zoom) {
212                     m_resizeMode = Right;
213                 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / m_zoom) {
214                     m_resizeMode = Up;
215                 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / m_zoom) {
216                     m_resizeMode = Down;
217                 } else setCursor(Qt::ClosedHandCursor);
218             }
219         }
220         QGraphicsScene::mousePressEvent(e);
221     } else if (m_tool == TITLE_RECTANGLE) {
222         m_sceneClickPoint = e->scenePos();
223         m_selectedItem = NULL;
224     } else if (m_tool == TITLE_TEXT) {
225         m_selectedItem = addText(QString());
226         emit newText((QGraphicsTextItem *) m_selectedItem);
227         m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
228         ((QGraphicsTextItem *)m_selectedItem)->setTextInteractionFlags(Qt::TextEditorInteraction);
229         m_selectedItem->setPos(e->scenePos());
230         QGraphicsScene::mousePressEvent(e);
231     }
232
233     kDebug() << "//////  MOUSE CLICK, RESIZE MODE: " << m_resizeMode;
234
235 }
236
237 void GraphicsSceneRectMove::clearTextSelection()
238 {
239     if (m_selectedItem && m_selectedItem->type() == 8) {
240         // disable text editing
241         QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
242         t->textCursor().setPosition(0);
243         QTextBlock cur = t->textCursor().block();
244         t->setTextCursor(QTextCursor(cur));
245         t->setTextInteractionFlags(Qt::NoTextInteraction);
246     }
247     m_selectedItem = NULL;
248     clearSelection();
249 }
250
251 //virtual
252 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
253 {
254     if ((e->screenPos() - m_clickPoint).manhattanLength() < QApplication::startDragDistance()) {
255         e->accept();
256         return;
257     }
258     if (m_selectedItem && e->buttons() & Qt::LeftButton) {
259         if (m_selectedItem->type() == 3 || m_selectedItem->type() == 13 || m_selectedItem->type() == 7) {
260             QRectF newrect;
261             if (m_selectedItem->type() == 3) {
262                 newrect = ((QGraphicsRectItem*)m_selectedItem)->rect();
263             } else newrect = m_selectedItem->boundingRect();
264
265             QPointF newpoint = e->scenePos();
266             //newpoint -= m_selectedItem->scenePos();
267             switch (m_resizeMode) {
268             case TopLeft:
269                 if (newpoint.x() < newrect.right() + m_selectedItem->pos().x() && newpoint.y() < newrect.bottom() + m_selectedItem->pos().y()) {
270                     newrect.setBottomRight(newrect.bottomRight() + m_selectedItem->pos() - newpoint);
271                     m_selectedItem->setPos(newpoint);
272                 }
273                 break;
274             case BottomLeft:
275                 if (newpoint.x() < newrect.right() + m_selectedItem->pos().x() && newpoint.y() > m_selectedItem->pos().y()) {
276                     newrect.setBottomRight(QPointF(newrect.bottomRight().x() + m_selectedItem->pos().x() - newpoint.x(), newpoint.y() - m_selectedItem->pos().y()));
277                     m_selectedItem->setPos(QPointF(newpoint.x(), m_selectedItem->pos().y()));
278                 }
279                 break;
280             case TopRight:
281                 if (newpoint.x() > m_selectedItem->pos().x() && newpoint.y() < newrect.bottom() + m_selectedItem->pos().y()) {
282                     newrect.setBottomRight(QPointF(newpoint.x() - m_selectedItem->pos().x(), newrect.bottom() + m_selectedItem->pos().y() - newpoint.y()));
283                     m_selectedItem->setPos(QPointF(m_selectedItem->pos().x(), newpoint.y()));
284                 }
285                 break;
286             case BottomRight:
287                 if (newpoint.x() > m_selectedItem->pos().x() && newpoint.y() > m_selectedItem->pos().y()) {
288                     newrect.setBottomRight(newpoint - m_selectedItem->pos());
289                 }
290                 break;
291             case Left:
292                 if (newpoint.x() < newrect.right() + m_selectedItem->pos().x()) {
293                     newrect.setRight(m_selectedItem->pos().x() + newrect.width() - newpoint.x());
294                     m_selectedItem->setPos(QPointF(newpoint.x(), m_selectedItem->pos().y()));
295                 }
296                 break;
297             case Right:
298                 if (newpoint.x() > m_selectedItem->pos().x()) {
299                     newrect.setRight(newpoint.x() - m_selectedItem->pos().x());
300                 }
301                 break;
302             case Up:
303                 if (newpoint.y() < newrect.bottom() + m_selectedItem->pos().y()) {
304                     newrect.setBottom(m_selectedItem->pos().y() + newrect.bottom() - newpoint.y());
305                     m_selectedItem->setPos(QPointF(m_selectedItem->pos().x(), newpoint.y()));
306                 }
307                 break;
308             case Down:
309                 if (newpoint.y() > m_selectedItem->pos().y()) {
310                     newrect.setBottom(newpoint.y() - m_selectedItem->pos().y());
311                 }
312                 break;
313             default:
314                 QPointF diff = e->scenePos() - m_sceneClickPoint;
315                 m_sceneClickPoint = e->scenePos();
316                 m_selectedItem->moveBy(diff.x(), diff.y());
317                 break;
318             }
319             if (m_selectedItem->type() == 3 && m_resizeMode != NoResize) {
320                 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
321                 gi->setRect(newrect);
322             }
323             /*else {
324             qreal s;
325             if (resizeMode == Left || resizeMode == Right ) s = m_selectedItem->boundingRect().width() / newrect.width();
326             else s = m_selectedItem->boundingRect().height() / newrect.height();
327             m_selectedItem->scale( 1 / s, 1 / s );
328             kDebug()<<"/// SCALING SVG, RESIZE MODE: "<<resizeMode<<", RECT:"<<m_selectedItem->boundingRect();
329             }*/
330             //gi->setPos(m_selectedItem->scenePos());
331             /*if (resizeMode == NoResize) {
332                 QGraphicsScene::mouseMoveEvent(e);
333                 return;
334             }*/
335         } else if (m_selectedItem->type() == 8) {
336             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
337             if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
338                 QGraphicsScene::mouseMoveEvent(e);
339                 return;
340             }
341             QPointF diff = e->scenePos() - m_sceneClickPoint;
342             m_sceneClickPoint = e->scenePos();
343             m_selectedItem->moveBy(diff.x(), diff.y());
344         }
345         emit itemMoved();
346     } else if (m_tool == TITLE_SELECT) {
347         QPointF p = e->scenePos();
348         p += QPoint(-2, -2);
349         m_resizeMode = NoResize;
350         bool itemFound = false;
351         foreach(const QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) {
352             if ((g->type() == 13 || g->type() == 7) && g->zValue() > -1000) {
353                 // image or svg item
354                 setCursor(Qt::OpenHandCursor);
355                 break;
356             } else if (g->type() == 3 && g->zValue() > -1000) {
357                 QRectF r = ((const QGraphicsRectItem*)g)->rect();
358                 r.translate(g->scenePos());
359                 itemFound = true;
360                 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
361                     setCursor(QCursor(Qt::SizeFDiagCursor));
362                 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
363                     setCursor(QCursor(Qt::SizeBDiagCursor));
364                 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
365                     setCursor(QCursor(Qt::SizeBDiagCursor));
366                 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
367                     setCursor(QCursor(Qt::SizeFDiagCursor));
368                 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / m_zoom) {
369                     setCursor(Qt::SizeHorCursor);
370                 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / m_zoom) {
371                     setCursor(Qt::SizeHorCursor);
372                 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / m_zoom) {
373                     setCursor(Qt::SizeVerCursor);
374                 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / m_zoom) {
375                     setCursor(Qt::SizeVerCursor);
376                 } else setCursor(Qt::OpenHandCursor);
377                 break;
378             }
379             if (!itemFound) setCursor(Qt::ArrowCursor);
380         }
381         QGraphicsScene::mouseMoveEvent(e);
382     } else if (m_tool == TITLE_RECTANGLE && e->buttons() & Qt::LeftButton) {
383         if (m_selectedItem == NULL && (m_clickPoint - e->screenPos()).manhattanLength() >= QApplication::startDragDistance()) {
384             // create new rect item
385             m_selectedItem = addRect(0, 0, e->scenePos().x() - m_sceneClickPoint.x(), e->scenePos().y() - m_sceneClickPoint.y());
386             emit newRect((QGraphicsRectItem *) m_selectedItem);
387             m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
388             m_selectedItem->setPos(m_sceneClickPoint);
389             m_resizeMode = BottomRight;
390             QGraphicsScene::mouseMoveEvent(e);
391         }
392     }
393 }
394
395 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
396 {
397     QList<QGraphicsView*> viewlist = views();
398     //kDebug() << wheelEvent->delta() << " " << zoom;
399     if (viewlist.size() > 0) {
400         if (wheelEvent->delta() < 0) emit sceneZoom(true);
401         else emit sceneZoom(false);
402     }
403 }
404
405 void GraphicsSceneRectMove::setScale(double s)
406 {
407     if (m_zoom < 1.0 / 7.0 && s < 1.0) return;
408     else if (m_zoom > 10.0 / 7.9 && s > 1.0) return;
409     QList<QGraphicsView*> viewlist = views();
410     if (viewlist.size() > 0) {
411         viewlist[0]->scale(s, s);
412         m_zoom = m_zoom * s;
413     }
414     //kDebug()<<"//////////  ZOOM: "<<zoom;
415 }
416
417 void GraphicsSceneRectMove::setZoom(double s)
418 {
419     QList<QGraphicsView*> viewlist = views();
420     if (viewlist.size() > 0) {
421         viewlist[0]->resetTransform();
422         viewlist[0]->scale(s, s);
423         m_zoom = s;
424     }
425
426     //kDebug()<<"//////////  ZOOM: "<<zoom;
427 }
428
429 void GraphicsSceneRectMove::setCursor(QCursor c)
430 {
431     const QList<QGraphicsView*> l = views();
432     foreach(QGraphicsView* v, l) {
433         v->setCursor(c);
434     }
435 }