]> git.sesse.net Git - kdenlive/blob - src/graphicsscenerectmove.cpp
[PATCH 2/2] Ensure that all member variables have an m_ prefix
[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         }
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                 newrect.setBottomRight(newrect.bottomRight() + m_selectedItem->pos() - newpoint);
270                 m_selectedItem->setPos(newpoint);
271                 break;
272             case BottomLeft:
273                 newrect.setBottomRight(QPointF(newrect.bottomRight().x() + m_selectedItem->pos().x() - newpoint.x(), newpoint.y() - m_selectedItem->pos().y()));
274                 m_selectedItem->setPos(QPointF(newpoint.x(), m_selectedItem->pos().y()));
275                 break;
276             case TopRight:
277                 newrect.setBottomRight(QPointF(newpoint.x() - m_selectedItem->pos().x(), newrect.bottom() + m_selectedItem->pos().y() - newpoint.y()));
278                 m_selectedItem->setPos(QPointF(m_selectedItem->pos().x(), newpoint.y()));
279                 break;
280             case BottomRight:
281                 newrect.setBottomRight(newpoint - m_selectedItem->pos());
282                 break;
283             case Left:
284                 newrect.setRight(m_selectedItem->pos().x() + newrect.width() - newpoint.x());
285                 m_selectedItem->setPos(QPointF(newpoint.x(), m_selectedItem->pos().y()));
286                 break;
287             case Right:
288                 newrect.setRight(newpoint.x() - m_selectedItem->pos().x());
289                 break;
290             case Up:
291                 newrect.setBottom(m_selectedItem->pos().y() + newrect.bottom() - newpoint.y());
292                 m_selectedItem->setPos(QPointF(m_selectedItem->pos().x(), newpoint.y()));
293                 break;
294             case Down:
295                 newrect.setBottom(newpoint.y() - m_selectedItem->pos().y());
296                 break;
297             default:
298                 QPointF diff = e->scenePos() - m_sceneClickPoint;
299                 m_sceneClickPoint = e->scenePos();
300                 m_selectedItem->moveBy(diff.x(), diff.y());
301                 break;
302             }
303             if (m_selectedItem->type() == 3 && m_resizeMode != NoResize) {
304                 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
305                 gi->setRect(newrect);
306             }
307             /*else {
308             qreal s;
309             if (resizeMode == Left || resizeMode == Right ) s = m_selectedItem->boundingRect().width() / newrect.width();
310             else s = m_selectedItem->boundingRect().height() / newrect.height();
311             m_selectedItem->scale( 1 / s, 1 / s );
312             kDebug()<<"/// SCALING SVG, RESIZE MODE: "<<resizeMode<<", RECT:"<<m_selectedItem->boundingRect();
313             }*/
314             //gi->setPos(m_selectedItem->scenePos());
315             /*if (resizeMode == NoResize) {
316                 QGraphicsScene::mouseMoveEvent(e);
317                 return;
318             }*/
319         } else if (m_selectedItem->type() == 8) {
320             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
321             if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
322                 QGraphicsScene::mouseMoveEvent(e);
323                 return;
324             }
325             QPointF diff = e->scenePos() - m_sceneClickPoint;
326             m_sceneClickPoint = e->scenePos();
327             m_selectedItem->moveBy(diff.x(), diff.y());
328         }
329         emit itemMoved();
330     } else if (m_tool == TITLE_SELECT) {
331         QPointF p = e->scenePos();
332         p += QPoint(-2, -2);
333         m_resizeMode = NoResize;
334         bool itemFound = false;
335         foreach(const QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) {
336             if ((g->type() == 13 || g->type() == 7) && g->zValue() > -1000) {
337                 // image or svg item
338                 setCursor(Qt::OpenHandCursor);
339                 break;
340             } else if (g->type() == 3 && g->zValue() > -1000) {
341                 QRectF r = ((const QGraphicsRectItem*)g)->rect();
342                 r.translate(g->scenePos());
343                 itemFound = true;
344                 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
345                     setCursor(QCursor(Qt::SizeFDiagCursor));
346                 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
347                     setCursor(QCursor(Qt::SizeBDiagCursor));
348                 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
349                     setCursor(QCursor(Qt::SizeBDiagCursor));
350                 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
351                     setCursor(QCursor(Qt::SizeFDiagCursor));
352                 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / m_zoom) {
353                     setCursor(Qt::SizeHorCursor);
354                 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / m_zoom) {
355                     setCursor(Qt::SizeHorCursor);
356                 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / m_zoom) {
357                     setCursor(Qt::SizeVerCursor);
358                 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / m_zoom) {
359                     setCursor(Qt::SizeVerCursor);
360                 } else setCursor(Qt::OpenHandCursor);
361                 break;
362             }
363             if (!itemFound) setCursor(Qt::ArrowCursor);
364         }
365         QGraphicsScene::mouseMoveEvent(e);
366     } else if (m_tool == TITLE_RECTANGLE && e->buttons() & Qt::LeftButton) {
367         if (m_selectedItem == NULL && (m_clickPoint - e->screenPos()).manhattanLength() >= QApplication::startDragDistance()) {
368             // create new rect item
369             m_selectedItem = addRect(0, 0, e->scenePos().x() - m_sceneClickPoint.x(), e->scenePos().y() - m_sceneClickPoint.y());
370             emit newRect((QGraphicsRectItem *) m_selectedItem);
371             m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
372             m_selectedItem->setPos(m_sceneClickPoint);
373             m_resizeMode = BottomRight;
374             QGraphicsScene::mouseMoveEvent(e);
375         }
376     }
377 }
378
379 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
380 {
381     QList<QGraphicsView*> viewlist = views();
382     //kDebug() << wheelEvent->delta() << " " << zoom;
383     if (viewlist.size() > 0) {
384         if (wheelEvent->delta() < 0) emit sceneZoom(true);
385         else emit sceneZoom(false);
386     }
387 }
388
389 void GraphicsSceneRectMove::setScale(double s)
390 {
391     if (m_zoom < 1.0 / 7.0 && s < 1.0) return;
392     else if (m_zoom > 10.0 / 7.9 && s > 1.0) return;
393     QList<QGraphicsView*> viewlist = views();
394     if (viewlist.size() > 0) {
395         viewlist[0]->scale(s, s);
396         m_zoom = m_zoom * s;
397     }
398     //kDebug()<<"//////////  ZOOM: "<<zoom;
399 }
400
401 void GraphicsSceneRectMove::setZoom(double s)
402 {
403     QList<QGraphicsView*> viewlist = views();
404     if (viewlist.size() > 0) {
405         viewlist[0]->resetTransform();
406         viewlist[0]->scale(s, s);
407         m_zoom = s;
408     }
409
410     //kDebug()<<"//////////  ZOOM: "<<zoom;
411 }
412
413 void GraphicsSceneRectMove::setCursor(QCursor c)
414 {
415     const QList<QGraphicsView*> l = views();
416     foreach(QGraphicsView* v, l) {
417         v->setCursor(c);
418     }
419 }