]> git.sesse.net Git - kdenlive/blob - src/graphicsscenerectmove.cpp
Reindent the codebase using 'linux' bracket placement.
[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): QGraphicsScene(parent), m_selectedItem(NULL), resizeMode(NoResize), m_tool(TITLE_RECTANGLE)
37 {
38     //grabMouse();
39     zoom = 1.0;
40     setBackgroundBrush(QBrush(Qt::transparent));
41 }
42
43 void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item)
44 {
45     clearSelection();
46     m_selectedItem = item;
47     item->setSelected(true);
48     update();
49 }
50
51 TITLETOOL GraphicsSceneRectMove::tool()
52 {
53     return m_tool;
54 }
55
56 void GraphicsSceneRectMove::setTool(TITLETOOL tool)
57 {
58     m_tool = tool;
59     switch (m_tool) {
60     case TITLE_RECTANGLE:
61         setCursor(Qt::CrossCursor);
62         break;
63     case TITLE_TEXT:
64         setCursor(Qt::IBeamCursor);
65         break;
66     default:
67         setCursor(Qt::ArrowCursor);
68     }
69 }
70
71 //virtual
72 void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent)
73 {
74     if (m_selectedItem == NULL) {
75         QGraphicsScene::keyPressEvent(keyEvent);
76         return;
77     }
78     int diff = 1;
79     if (m_selectedItem->type() == 8) {
80         QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
81         if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
82             QGraphicsScene::keyPressEvent(keyEvent);
83             return;
84         }
85     }
86     if (keyEvent->modifiers() & Qt::ControlModifier) diff = 10;
87     switch (keyEvent->key()) {
88     case Qt::Key_Left:
89         m_selectedItem->setPos(m_selectedItem->pos() - QPointF(diff, 0));
90         emit itemMoved();
91         break;
92     case Qt::Key_Right:
93         m_selectedItem->setPos(m_selectedItem->pos() + QPointF(diff, 0));
94         emit itemMoved();
95         break;
96     case Qt::Key_Up:
97         m_selectedItem->setPos(m_selectedItem->pos() - QPointF(0, diff));
98         emit itemMoved();
99         break;
100     case Qt::Key_Down:
101         m_selectedItem->setPos(m_selectedItem->pos() + QPointF(0, diff));
102         emit itemMoved();
103         break;
104     case Qt::Key_Delete:
105     case Qt::Key_Backspace:
106         delete m_selectedItem;
107         m_selectedItem = NULL;
108         emit selectionChanged();
109         break;
110     default:
111         QGraphicsScene::keyPressEvent(keyEvent);
112     }
113     emit actionFinished();
114 }
115
116 //virtual
117 void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e)
118 {
119     QPointF p = e->scenePos();
120     p += QPoint(-2, -2);
121     resizeMode = NoResize;
122     m_selectedItem = NULL;
123     QGraphicsItem* g = items(QRectF(p , QSizeF(4, 4)).toRect()).at(0);
124     if (g) {
125         if (g->type() == 8) {
126             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
127             m_selectedItem = g;
128             t->setTextInteractionFlags(Qt::TextEditorInteraction);
129         }
130     }
131     QGraphicsScene::mouseDoubleClickEvent(e);
132 }
133
134 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
135 {
136     if (m_tool == TITLE_RECTANGLE && m_selectedItem) setSelectedItem(m_selectedItem);
137     QGraphicsScene::mouseReleaseEvent(e);
138     emit actionFinished();
139 }
140
141 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e)
142 {
143     m_clickPoint = e->screenPos();
144     QPointF p = e->scenePos();
145     p += QPoint(-2, -2);
146     resizeMode = NoResize;
147     const QList <QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
148     QGraphicsItem *item = NULL;
149     bool hasSelected = false;
150
151     if (m_tool == TITLE_SELECT) {
152         foreach(QGraphicsItem *g, list) {
153             kDebug() << " - - CHECKING ITEM Z:" << g->zValue() << ", TYPE: " << g->type();
154             // check is there is a selected item in list
155             if (g->zValue() > -1000 && g->isSelected()) {
156                 hasSelected = true;
157                 item = g;
158                 break;
159             }
160         }
161         if (item == NULL) {
162             if (m_selectedItem && m_selectedItem->type() == 8) {
163                 // disable text editing
164                 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
165                 t->textCursor().setPosition(0);
166                 QTextBlock cur = t->textCursor().block();
167                 t->setTextCursor(QTextCursor(cur));
168                 t->setTextInteractionFlags(Qt::NoTextInteraction);
169             }
170             m_selectedItem = NULL;
171             foreach(QGraphicsItem* g, list) {
172                 if (g->zValue() > -1000) {
173                     item = g;
174                     break;
175                 }
176             }
177         }
178         if (item != NULL) {
179             m_sceneClickPoint = e->scenePos();
180             m_selectedItem = item;
181             kDebug() << "/////////  ITEM TYPE: " << item->type();
182             if (item->type() == 8) {
183                 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(item);
184                 if (t->textInteractionFlags() == Qt::TextEditorInteraction) {
185                     QGraphicsScene::mousePressEvent(e);
186                     return;
187                 }
188                 t->setTextInteractionFlags(Qt::NoTextInteraction);
189                 setCursor(Qt::ClosedHandCursor);
190             } else if (item->type() == 3 || item->type() == 13 || item->type() == 7) {
191                 QRectF r;
192                 if (m_selectedItem->type() == 3) {
193                     r = ((QGraphicsRectItem*)m_selectedItem)->rect();
194                 } else r = m_selectedItem->boundingRect();
195
196                 r.translate(item->scenePos());
197                 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
198                     resizeMode = TopLeft;
199                 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
200                     resizeMode = BottomLeft;
201                 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
202                     resizeMode = TopRight;
203                 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
204                     resizeMode = BottomRight;
205                 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
206                     resizeMode = Left;
207                 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
208                     resizeMode = Right;
209                 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
210                     resizeMode = Up;
211                 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
212                     resizeMode = Down;
213                 } else setCursor(Qt::ClosedHandCursor);
214             }
215         }
216         QGraphicsScene::mousePressEvent(e);
217     } else if (m_tool == TITLE_RECTANGLE) {
218         m_sceneClickPoint = e->scenePos();
219         m_selectedItem = NULL;
220     } else if (m_tool == TITLE_TEXT) {
221         m_selectedItem = addText(QString());
222         emit newText((QGraphicsTextItem *) m_selectedItem);
223         m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
224         ((QGraphicsTextItem *)m_selectedItem)->setTextInteractionFlags(Qt::TextEditorInteraction);
225         m_selectedItem->setPos(e->scenePos());
226         QGraphicsScene::mousePressEvent(e);
227     }
228
229     kDebug() << "//////  MOUSE CLICK, RESIZE MODE: " << resizeMode;
230
231 }
232
233 void GraphicsSceneRectMove::clearTextSelection()
234 {
235     if (m_selectedItem && m_selectedItem->type() == 8) {
236         // disable text editing
237         QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
238         t->textCursor().setPosition(0);
239         QTextBlock cur = t->textCursor().block();
240         t->setTextCursor(QTextCursor(cur));
241         t->setTextInteractionFlags(Qt::NoTextInteraction);
242     }
243     m_selectedItem = NULL;
244     clearSelection();
245 }
246
247 //virtual
248 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
249 {
250     if ((e->screenPos() - m_clickPoint).manhattanLength() < QApplication::startDragDistance()) {
251         e->accept();
252         return;
253     }
254     if (m_selectedItem && e->buttons() & Qt::LeftButton) {
255         if (m_selectedItem->type() == 3 || m_selectedItem->type() == 13 || m_selectedItem->type() == 7) {
256             QRectF newrect;
257             if (m_selectedItem->type() == 3) {
258                 newrect = ((QGraphicsRectItem*)m_selectedItem)->rect();
259             } else newrect = m_selectedItem->boundingRect();
260
261             QPointF newpoint = e->scenePos();
262             //newpoint -= m_selectedItem->scenePos();
263             switch (resizeMode) {
264             case TopLeft:
265                 newrect.setBottomRight(newrect.bottomRight() + m_selectedItem->pos() - newpoint);
266                 m_selectedItem->setPos(newpoint);
267                 break;
268             case BottomLeft:
269                 newrect.setBottomRight(QPointF(newrect.bottomRight().x() + m_selectedItem->pos().x() - newpoint.x(), newpoint.y() - m_selectedItem->pos().y()));
270                 m_selectedItem->setPos(QPointF(newpoint.x(), m_selectedItem->pos().y()));
271                 break;
272             case TopRight:
273                 newrect.setBottomRight(QPointF(newpoint.x() - m_selectedItem->pos().x(), newrect.bottom() + m_selectedItem->pos().y() - newpoint.y()));
274                 m_selectedItem->setPos(QPointF(m_selectedItem->pos().x(), newpoint.y()));
275                 break;
276             case BottomRight:
277                 newrect.setBottomRight(newpoint - m_selectedItem->pos());
278                 break;
279             case Left:
280                 newrect.setRight(m_selectedItem->pos().x() + newrect.width() - newpoint.x());
281                 m_selectedItem->setPos(QPointF(newpoint.x(), m_selectedItem->pos().y()));
282                 break;
283             case Right:
284                 newrect.setRight(newpoint.x() - m_selectedItem->pos().x());
285                 break;
286             case Up:
287                 newrect.setBottom(m_selectedItem->pos().y() + newrect.bottom() - newpoint.y());
288                 m_selectedItem->setPos(QPointF(m_selectedItem->pos().x(), newpoint.y()));
289                 break;
290             case Down:
291                 newrect.setBottom(newpoint.y() - m_selectedItem->pos().y());
292                 break;
293             default:
294                 QPointF diff = e->scenePos() - m_sceneClickPoint;
295                 m_sceneClickPoint = e->scenePos();
296                 m_selectedItem->moveBy(diff.x(), diff.y());
297                 break;
298             }
299             if (m_selectedItem->type() == 3 && resizeMode != NoResize) {
300                 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
301                 gi->setRect(newrect);
302             }
303             /*else {
304             qreal s;
305             if (resizeMode == Left || resizeMode == Right ) s = m_selectedItem->boundingRect().width() / newrect.width();
306             else s = m_selectedItem->boundingRect().height() / newrect.height();
307             m_selectedItem->scale( 1 / s, 1 / s );
308             kDebug()<<"/// SCALING SVG, RESIZE MODE: "<<resizeMode<<", RECT:"<<m_selectedItem->boundingRect();
309             }*/
310             //gi->setPos(m_selectedItem->scenePos());
311             /*if (resizeMode == NoResize) {
312                 QGraphicsScene::mouseMoveEvent(e);
313                 return;
314             }*/
315         } else if (m_selectedItem->type() == 8) {
316             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
317             if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
318                 QGraphicsScene::mouseMoveEvent(e);
319                 return;
320             }
321             QPointF diff = e->scenePos() - m_sceneClickPoint;
322             m_sceneClickPoint = e->scenePos();
323             m_selectedItem->moveBy(diff.x(), diff.y());
324         }
325         emit itemMoved();
326     } else if (m_tool == TITLE_SELECT) {
327         QPointF p = e->scenePos();
328         p += QPoint(-2, -2);
329         resizeMode = NoResize;
330         bool itemFound = false;
331         foreach(const QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) {
332             if ((g->type() == 13 || g->type() == 7) && g->zValue() > -1000) {
333                 // image or svg item
334                 setCursor(Qt::OpenHandCursor);
335                 break;
336             } else if (g->type() == 3 && g->zValue() > -1000) {
337                 QRectF r = ((const QGraphicsRectItem*)g)->rect();
338                 r.translate(g->scenePos());
339                 itemFound = true;
340                 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
341                     setCursor(QCursor(Qt::SizeFDiagCursor));
342                 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
343                     setCursor(QCursor(Qt::SizeBDiagCursor));
344                 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
345                     setCursor(QCursor(Qt::SizeBDiagCursor));
346                 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
347                     setCursor(QCursor(Qt::SizeFDiagCursor));
348                 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
349                     setCursor(Qt::SizeHorCursor);
350                 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
351                     setCursor(Qt::SizeHorCursor);
352                 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
353                     setCursor(Qt::SizeVerCursor);
354                 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
355                     setCursor(Qt::SizeVerCursor);
356                 } else setCursor(Qt::OpenHandCursor);
357                 break;
358             }
359             if (!itemFound) setCursor(Qt::ArrowCursor);
360         }
361         QGraphicsScene::mouseMoveEvent(e);
362     } else if (m_tool == TITLE_RECTANGLE && e->buttons() & Qt::LeftButton) {
363         if (m_selectedItem == NULL && (m_clickPoint - e->screenPos()).manhattanLength() >= QApplication::startDragDistance()) {
364             // create new rect item
365             m_selectedItem = addRect(0, 0, e->scenePos().x() - m_sceneClickPoint.x(), e->scenePos().y() - m_sceneClickPoint.y());
366             emit newRect((QGraphicsRectItem *) m_selectedItem);
367             m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
368             m_selectedItem->setPos(m_sceneClickPoint);
369             resizeMode = BottomRight;
370             QGraphicsScene::mouseMoveEvent(e);
371         }
372     }
373 }
374
375 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
376 {
377     QList<QGraphicsView*> viewlist = views();
378     //kDebug() << wheelEvent->delta() << " " << zoom;
379     if (viewlist.size() > 0) {
380         if (wheelEvent->delta() < 0) emit sceneZoom(true);
381         else emit sceneZoom(false);
382     }
383 }
384
385 void GraphicsSceneRectMove::setScale(double s)
386 {
387     if (zoom < 1.0 / 7.0 && s < 1.0) return;
388     else if (zoom > 10.0 / 7.9 && s > 1.0) return;
389     QList<QGraphicsView*> viewlist = views();
390     if (viewlist.size() > 0) {
391         viewlist[0]->scale(s, s);
392         zoom = zoom * s;
393     }
394     //kDebug()<<"//////////  ZOOM: "<<zoom;
395 }
396
397 void GraphicsSceneRectMove::setZoom(double s)
398 {
399     QList<QGraphicsView*> viewlist = views();
400     if (viewlist.size() > 0) {
401         viewlist[0]->resetTransform();
402         viewlist[0]->scale(s, s);
403         zoom = s;
404     }
405
406     //kDebug()<<"//////////  ZOOM: "<<zoom;
407 }
408
409 void GraphicsSceneRectMove::setCursor(QCursor c)
410 {
411     const QList<QGraphicsView*> l = views();
412     foreach(QGraphicsView* v, l) {
413         v->setCursor(c);
414     }
415 }