]> git.sesse.net Git - kdenlive/blob - src/graphicsscenerectmove.cpp
Allow deleting & moving multiple selected items
[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     m_fontSize = 0;
46 }
47
48 void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item)
49 {
50     clearSelection();
51     m_selectedItem = item;
52     item->setSelected(true);
53     update();
54 }
55
56 TITLETOOL GraphicsSceneRectMove::tool()
57 {
58     return m_tool;
59 }
60
61 void GraphicsSceneRectMove::setTool(TITLETOOL tool)
62 {
63     m_tool = tool;
64     switch (m_tool) {
65     case TITLE_RECTANGLE:
66         setCursor(Qt::CrossCursor);
67         break;
68     case TITLE_TEXT:
69         setCursor(Qt::IBeamCursor);
70         break;
71     default:
72         setCursor(Qt::ArrowCursor);
73     }
74 }
75
76 void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent)
77 {
78     if (m_selectedItem == NULL || !(m_selectedItem->flags() & QGraphicsItem::ItemIsMovable)) {
79         QGraphicsScene::keyPressEvent(keyEvent);
80         return;
81     }
82     if (m_selectedItem->type() == QGraphicsTextItem::Type) {
83         QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
84         if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
85             QGraphicsScene::keyPressEvent(keyEvent);
86             return;
87         }
88     }
89     int diff = 1;
90     if (keyEvent->modifiers() & Qt::ControlModifier) diff = 10;
91     switch (keyEvent->key()) {
92     case Qt::Key_Left:
93         foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(-diff,0); }
94         emit itemMoved();
95         break;
96     case Qt::Key_Right:
97         foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy( diff,0); }
98         emit itemMoved();
99         break;
100     case Qt::Key_Up:
101         foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(0,-diff); }
102         emit itemMoved();
103         break;
104     case Qt::Key_Down:
105         foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(0, diff); }
106         emit itemMoved();
107         break;
108     case Qt::Key_Delete:
109     case Qt::Key_Backspace:
110         foreach (QGraphicsItem *qgi, selectedItems()) {
111             removeItem(qgi);
112             delete qgi;
113         }
114         m_selectedItem = NULL;
115         emit selectionChanged();
116         break;
117     default:
118         QGraphicsScene::keyPressEvent(keyEvent);
119     }
120     emit actionFinished();
121 }
122
123 void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e)
124 {
125     QPointF p = e->scenePos();
126     p += QPoint(-2, -2);
127     m_resizeMode = NoResize;
128     m_selectedItem = NULL;
129
130     // http://www.kdenlive.org/mantis/view.php?id=1035
131     QList<QGraphicsItem*> i = items(QRectF(p , QSizeF(4, 4)).toRect());
132     if (i.isEmpty()) return;
133
134     QGraphicsItem* g = i.first();
135     if (g->type() == QGraphicsTextItem::Type) {
136         m_selectedItem = g;
137         QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
138         t->setTextInteractionFlags(Qt::TextEditorInteraction);
139     } else emit doubleClickEvent();
140     QGraphicsScene::mouseDoubleClickEvent(e);
141 }
142
143 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
144 {
145     if (m_tool == TITLE_RECTANGLE && m_selectedItem) setSelectedItem(m_selectedItem);
146     QGraphicsScene::mouseReleaseEvent(e);
147     emit actionFinished();
148 }
149
150 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e)
151 {
152     m_clickPoint = e->screenPos();
153     QPointF p = e->scenePos();
154     p += QPoint(-2, -2);
155     m_resizeMode = NoResize;
156     const QList <QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
157     QGraphicsItem *item = NULL;
158
159     if (m_tool == TITLE_SELECT) {
160         foreach(QGraphicsItem *g, list) {
161             kDebug() << " - - CHECKING ITEM Z:" << g->zValue() << ", TYPE: " << g->type();
162             // check is there is a selected item in list
163             if (g->zValue() > -1000 && g->isSelected()) {
164                 item = g;
165                 break;
166             }
167         }
168         if (item == NULL  || !(item->flags() & QGraphicsItem::ItemIsSelectable)) {
169             if (m_selectedItem && m_selectedItem->type() == QGraphicsTextItem::Type) {
170                 // disable text editing
171                 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
172                 t->textCursor().setPosition(0);
173                 QTextBlock cur = t->textCursor().block();
174                 t->setTextCursor(QTextCursor(cur));
175                 t->setTextInteractionFlags(Qt::NoTextInteraction);
176             }
177             m_selectedItem = NULL;
178             foreach(QGraphicsItem* g, list) {
179                 if (g->zValue() > -1000) {
180                     item = g;
181                     break;
182                 }
183             }
184         }
185         if (item != NULL && item->flags() & QGraphicsItem::ItemIsMovable) {
186             m_sceneClickPoint = e->scenePos();
187             m_selectedItem = item;
188             kDebug() << "/////////  ITEM TYPE: " << item->type();
189             if (item->type() == QGraphicsTextItem::Type) {
190                 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(item);
191                 if (t->textInteractionFlags() == Qt::TextEditorInteraction) {
192                     QGraphicsScene::mousePressEvent(e);
193                     return;
194                 }
195                 t->setTextInteractionFlags(Qt::NoTextInteraction);
196                 setCursor(Qt::ClosedHandCursor);
197             } else if (item->type() == QGraphicsRectItem::Type || item->type() == QGraphicsSvgItem::Type || item->type() == QGraphicsPixmapItem::Type) {
198                 QRectF r1;
199                 if (m_selectedItem->type() == QGraphicsRectItem::Type)
200                     r1 = ((QGraphicsRectItem*)m_selectedItem)->rect().normalized();
201                 else
202                     r1 = m_selectedItem->boundingRect().normalized();
203
204                 QList<QGraphicsView*> viewlist = views();
205                 QGraphicsView *view = NULL;
206                 if (viewlist.size() > 0) view = viewlist[0];
207                 if (view == NULL) return;
208                 // Item mapped coordinates
209                 QPolygon r = m_selectedItem->deviceTransform(view->viewportTransform()).map(r1).toPolygon();
210                 QPainterPath top(r.point(0));
211                 top.lineTo(r.point(1));
212                 QPainterPath bottom(r.point(2));
213                 bottom.lineTo(r.point(3));
214                 QPainterPath left(r.point(0));
215                 left.lineTo(r.point(3));
216                 QPainterPath right(r.point(1));
217                 right.lineTo(r.point(2));
218
219
220                 // The area interested by the mouse pointer
221                 QPoint viewPos = view->mapFromScene(e->scenePos());
222                 QPainterPath mouseArea;
223                 mouseArea.addRect(viewPos.x() - 4, viewPos.y() - 4, 8, 8);
224
225                 // Check for collisions between the mouse and the borders
226                 if (mouseArea.contains(r.point(0))) m_resizeMode = TopLeft;
227                 else if (mouseArea.contains(r.point(2))) m_resizeMode = BottomRight;
228                 else if (mouseArea.contains(r.point(1))) m_resizeMode = TopRight;
229                 else if (mouseArea.contains(r.point(3))) m_resizeMode = BottomLeft;
230                 else if (top.intersects(mouseArea)) m_resizeMode = Up;
231                 else if (bottom.intersects(mouseArea)) m_resizeMode = Down;
232                 else if (right.intersects(mouseArea)) m_resizeMode = Right;
233                 else if (left.intersects(mouseArea)) m_resizeMode = Left;
234
235                 else
236                     setCursor(Qt::ClosedHandCursor);
237             }
238         }
239         QGraphicsScene::mousePressEvent(e);
240     } else if (m_tool == TITLE_RECTANGLE) {
241         m_sceneClickPoint = e->scenePos();
242         m_selectedItem = NULL;
243     } else if (m_tool == TITLE_TEXT) {
244         m_selectedItem = addText(QString());
245         emit newText((QGraphicsTextItem *) m_selectedItem);
246         m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
247         ((QGraphicsTextItem *)m_selectedItem)->setTextInteractionFlags(Qt::TextEditorInteraction);
248         m_selectedItem->setPos(e->scenePos() - QPointF(0, (int)(m_fontSize / 2)));
249         QGraphicsScene::mousePressEvent(e);
250     }
251
252     kDebug() << "//////  MOUSE CLICK, RESIZE MODE: " << m_resizeMode;
253
254 }
255
256 void GraphicsSceneRectMove::clearTextSelection()
257 {
258     if (m_selectedItem && m_selectedItem->type() == QGraphicsTextItem::Type) {
259         // disable text editing
260         QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
261         t->textCursor().setPosition(0);
262         QTextBlock cur = t->textCursor().block();
263         t->setTextCursor(QTextCursor(cur));
264         t->setTextInteractionFlags(Qt::NoTextInteraction);
265     }
266     m_selectedItem = NULL;
267     clearSelection();
268 }
269
270 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
271 {
272     if (e->buttons() != Qt::NoButton && (e->screenPos() - m_clickPoint).manhattanLength() < QApplication::startDragDistance()) {
273         e->accept();
274         return;
275     }
276     if (m_selectedItem && e->buttons() & Qt::LeftButton) {
277         if (m_selectedItem->type() == QGraphicsRectItem::Type || m_selectedItem->type() == QGraphicsSvgItem::Type || m_selectedItem->type() == QGraphicsPixmapItem::Type) {
278             QRectF newrect;
279             if (m_selectedItem->type() == QGraphicsRectItem::Type)
280                 newrect = ((QGraphicsRectItem*)m_selectedItem)->rect();
281             else
282                 newrect = m_selectedItem->boundingRect();
283             QPointF newpoint = e->scenePos();
284             /*
285              * The vertices of the rectangle (check for matrix
286              * transformation); to be replaced by QTransform::map()?
287              */
288             QPointF itemOrigin = m_selectedItem->scenePos();
289             QTransform transform = m_selectedItem->transform();
290             QPointF topLeft(transform.m11() * newrect.toRect().left() + transform.m21() * newrect.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * newrect.toRect().top() + transform.m12() * newrect.toRect().left() + transform.m32() + itemOrigin.y());
291             QPointF bottomLeft(transform.m11() * newrect.toRect().left() + transform.m21() * newrect.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * newrect.toRect().bottom() + transform.m12() * newrect.toRect().left() + transform.m32() + itemOrigin.y());
292             QPointF topRight(transform.m11() * newrect.toRect().right() + transform.m21() * newrect.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * newrect.toRect().top() + transform.m12() * newrect.toRect().right() + transform.m32() + itemOrigin.y());
293             QPointF bottomRight(transform.m11() * newrect.toRect().right() + transform.m21() * newrect.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * newrect.toRect().bottom() + transform.m12() * newrect.toRect().right() + transform.m32() + itemOrigin.y());
294             // Convert the mouse coordinates applying inverted transformation
295             QPointF newPointRelative = newpoint - itemOrigin;
296             QPointF resizePoint(transform.inverted().m11() * newPointRelative.x() + transform.inverted().m21() * newPointRelative.y() + transform.inverted().m31(), transform.inverted().m22() * newPointRelative.y() + transform.inverted().m12() * newPointRelative.x() + transform.inverted().m32());
297             /*
298              * Will check if the mouse is on the right of the limit lines with a
299              * determinant (it must be less than zero because the Y axis is
300              * inverted)
301              */
302             int determinantH, determinantV;
303             // Check whether to resize or to just move the item(s)
304             switch (m_resizeMode) {
305             case TopLeft:
306                 determinantV = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
307                 determinantH = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
308                 if (determinantV < 0) {
309                     if (determinantH < 0) {
310                         // resizePoint is not working for some reason
311                         newrect.setBottomRight(QPointF(newrect.width() - (transform.inverted().m11() * resizePoint.x() + transform.inverted().m21() * resizePoint.y() + transform.inverted().m31()), newrect.bottom() - (transform.inverted().m22() * resizePoint.y() + transform.inverted().m12() * resizePoint.x() + transform.inverted().m32())));
312                         m_selectedItem->setPos(resizePoint + itemOrigin);
313                     } else
314                         m_resizeMode = BottomLeft;
315                 } else {
316                     if (determinantH < 0)
317                         m_resizeMode = TopRight;
318                     else
319                         m_resizeMode = BottomRight;
320                 }
321                 break;
322             case BottomLeft:
323                 determinantV = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
324                 determinantH = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
325                 if (determinantV < 0) {
326                     if (determinantH < 0) {
327                         newrect.setBottomRight(QPointF(newrect.width() - resizePoint.x(), resizePoint.y()));
328                         m_selectedItem->setPos(QPointF(transform.m11() * resizePoint.x() + transform.m21() *(newrect.bottom() - resizePoint.y()) + transform.m31() + itemOrigin.x(), transform.m22() *(newrect.bottom() - resizePoint.y()) + transform.m12() * resizePoint.x() + transform.m32() + itemOrigin.y()));
329                     } else
330                         m_resizeMode = TopLeft;
331                 } else {
332                     if (determinantH < 0)
333                         m_resizeMode = BottomRight;
334                     else
335                         m_resizeMode = TopRight;
336                 }
337                 break;
338             case TopRight:
339                 determinantV = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
340                 determinantH = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
341                 if (determinantV < 0) {
342                     if (determinantH < 0) {
343                         newrect.setBottomRight(QPointF(resizePoint.x(), newrect.bottom() - resizePoint.y()));
344                         m_selectedItem->setPos(QPointF(transform.m11() *(newrect.width() - resizePoint.x()) + transform.m21() * resizePoint.y() + transform.m31() + itemOrigin.x(), transform.m22() * resizePoint.y() + transform.m12() *(newrect.width() - resizePoint.x()) + transform.m32() + itemOrigin.y()));
345                     } else
346                         m_resizeMode = BottomRight;
347                 } else {
348                     if (determinantH < 0)
349                         m_resizeMode = TopLeft;
350                     else
351                         m_resizeMode = BottomLeft;
352                 }
353                 break;
354             case BottomRight:
355                 determinantV = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
356                 determinantH = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
357                 if (determinantV < 0) {
358                     if (determinantH < 0)
359                         newrect.setBottomRight(resizePoint);
360                     else
361                         m_resizeMode = TopRight;
362                 } else {
363                     if (determinantH < 0)
364                         m_resizeMode = BottomLeft;
365                     else
366                         m_resizeMode = TopLeft;
367                 }
368                 break;
369             case Left:
370                 determinantV = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
371                 if (determinantV < 0) {
372                     newrect.setRight(newrect.width() - resizePoint.x());
373                     m_selectedItem->setPos(QPointF(transform.m11() * resizePoint.x() + transform.m31() + itemOrigin.x(), transform.m12() * resizePoint.x() + transform.m32() + itemOrigin.y()));
374                 } else
375                     m_resizeMode = Right;
376                 break;
377             case Right:
378                 determinantV = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
379                 if (determinantV < 0)
380                     newrect.setRight(resizePoint.x());
381                 else
382                     m_resizeMode = Left;
383                 break;
384             case Up:
385                 determinantH = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
386                 if (determinantH < 0) {
387                     newrect.setBottom(newrect.bottom() - resizePoint.y());
388                     m_selectedItem->setPos(QPointF(transform.m21() * resizePoint.y() + transform.m31() + itemOrigin.x(), transform.m22() * resizePoint.y() + transform.m32() + itemOrigin.y()));
389                 } else
390                     m_resizeMode = Down;
391                 break;
392             case Down:
393                 determinantH = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
394                 if (determinantH < 0)
395                     newrect.setBottom(resizePoint.y());
396                 else
397                     m_resizeMode = Up;
398                 break;
399             default:
400                 QPointF diff = e->scenePos() - m_sceneClickPoint;
401                 m_sceneClickPoint = e->scenePos();
402                 foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(diff.x(), diff.y()); }
403                 break;
404             }
405             if (m_selectedItem->type() == QGraphicsRectItem::Type && m_resizeMode != NoResize) {
406                 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
407                 // Resize using aspect ratio
408                 if (!m_selectedItem->data(0).isNull()) {
409                     // we want to keep aspect ratio
410                     double hRatio = (double) newrect.width() / m_selectedItem->data(0).toInt();
411                     double vRatio = (double) newrect.height() / m_selectedItem->data(1).toInt();
412                     if (hRatio < vRatio) newrect.setHeight(m_selectedItem->data(1).toInt() * hRatio);
413                     else newrect.setWidth(m_selectedItem->data(0).toInt() * vRatio);
414                 }
415
416                 gi->setRect(newrect);
417             }
418             /*else {
419             qreal s;
420             if (resizeMode == Left || resizeMode == Right ) s = m_selectedItem->boundingRect().width() / newrect.width();
421             else s = m_selectedItem->boundingRect().height() / newrect.height();
422             m_selectedItem->scale( 1 / s, 1 / s );
423             kDebug()<<"/// SCALING SVG, RESIZE MODE: "<<resizeMode<<", RECT:"<<m_selectedItem->boundingRect();
424             }*/
425             //gi->setPos(m_selectedItem->scenePos());
426             /*if (resizeMode == NoResize) {
427                 QGraphicsScene::mouseMoveEvent(e);
428                 return;
429             }*/
430         } else if (m_selectedItem->type() == QGraphicsTextItem::Type) {
431             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
432             if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
433                 QGraphicsScene::mouseMoveEvent(e);
434                 return;
435             }
436             QPointF diff = e->scenePos() - m_sceneClickPoint;
437             m_sceneClickPoint = e->scenePos();
438             foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(diff.x(), diff.y()); }
439         }
440         emit itemMoved();
441     } else if (m_tool == TITLE_SELECT) {
442         QList<QGraphicsView*> viewlist = views();
443         QGraphicsView *view = NULL;
444         if (viewlist.size() > 0) view = viewlist[0];
445
446         QPointF p = e->scenePos();
447         p += QPoint(-2, -2);
448         m_resizeMode = NoResize;
449         bool itemFound = false;
450         QList<QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
451         foreach(const QGraphicsItem* g, list) {
452             if ((g->type() == QGraphicsSvgItem::Type || g->type() == QGraphicsPixmapItem::Type) && g->zValue() > -1000) {
453                 // image or svg item
454                 setCursor(Qt::OpenHandCursor);
455                 break;
456             } else if (g->type() == QGraphicsRectItem::Type && g->zValue() > -1000) {
457                 if (view == NULL) continue;
458                 QRectF r1 = ((const QGraphicsRectItem*)g)->rect().normalized();
459                 itemFound = true;
460
461                 // Item mapped coordinates
462                 QPolygon r = g->deviceTransform(view->viewportTransform()).map(r1).toPolygon();
463                 QPainterPath top(r.point(0));
464                 top.lineTo(r.point(1));
465                 QPainterPath bottom(r.point(2));
466                 bottom.lineTo(r.point(3));
467                 QPainterPath left(r.point(0));
468                 left.lineTo(r.point(3));
469                 QPainterPath right(r.point(1));
470                 right.lineTo(r.point(2));
471
472                 // The area interested by the mouse pointer
473                 QPoint viewPos = view->mapFromScene(e->scenePos());
474                 QPainterPath mouseArea;
475                 mouseArea.addRect(viewPos.x() - 4, viewPos.y() - 4, 8, 8);
476
477                 // Check for collisions between the mouse and the borders
478                 if (mouseArea.contains(r.point(0)) || mouseArea.contains(r.point(2))) setCursor(Qt::SizeFDiagCursor);
479                 else if (mouseArea.contains(r.point(1)) || mouseArea.contains(r.point(3))) setCursor(Qt::SizeBDiagCursor);
480                 else if (top.intersects(mouseArea) || bottom.intersects(mouseArea)) setCursor(Qt::SizeVerCursor);
481                 else if (right.intersects(mouseArea) || left.intersects(mouseArea)) setCursor(Qt::SizeHorCursor);
482                 else
483                     setCursor(Qt::OpenHandCursor);
484                 break;
485             }
486             if (!itemFound) setCursor(Qt::ArrowCursor);
487         }
488         QGraphicsScene::mouseMoveEvent(e);
489     } else if (m_tool == TITLE_RECTANGLE && e->buttons() & Qt::LeftButton) {
490         if (m_selectedItem == NULL) {
491             // create new rect item
492             QRectF r(0, 0, e->scenePos().x() - m_sceneClickPoint.x(), e->scenePos().y() - m_sceneClickPoint.y());
493             r = r.normalized();
494             m_selectedItem = addRect(QRectF(0, 0, r.width(), r.height()));
495             emit newRect((QGraphicsRectItem *) m_selectedItem);
496             m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
497             m_selectedItem->setPos(m_sceneClickPoint);
498             m_resizeMode = BottomRight;
499             QGraphicsScene::mouseMoveEvent(e);
500         }
501     }
502 }
503
504 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
505 {
506     if (wheelEvent->modifiers() == Qt::ControlModifier) {
507         QList<QGraphicsView*> viewlist = views();
508         //kDebug() << wheelEvent->delta() << " " << zoom;
509         if (viewlist.size() > 0) {
510             if (wheelEvent->delta() > 0) emit sceneZoom(true);
511             else emit sceneZoom(false);
512         }
513     } else wheelEvent->setAccepted(false);
514 }
515
516 void GraphicsSceneRectMove::setScale(double s)
517 {
518     if (m_zoom < 1.0 / 7.0 && s < 1.0) return;
519     else if (m_zoom > 10.0 / 7.9 && s > 1.0) return;
520     QList<QGraphicsView*> viewlist = views();
521     if (viewlist.size() > 0) {
522         viewlist[0]->scale(s, s);
523         m_zoom = m_zoom * s;
524     }
525     //kDebug()<<"//////////  ZOOM: "<<zoom;
526 }
527
528 void GraphicsSceneRectMove::setZoom(double s)
529 {
530     QList<QGraphicsView*> viewlist = views();
531     if (viewlist.size() > 0) {
532         viewlist[0]->resetTransform();
533         viewlist[0]->scale(s, s);
534         m_zoom = s;
535     }
536
537     //kDebug()<<"//////////  ZOOM: "<<zoom;
538 }
539
540 void GraphicsSceneRectMove::setCursor(QCursor c)
541 {
542     const QList<QGraphicsView*> l = views();
543     foreach(QGraphicsView* v, l) {
544         v->setCursor(c);
545     }
546 }
547
548 void GraphicsSceneRectMove::setResizeCursor(qreal angle)
549 {
550     // % is not working...
551     while (angle < 0)
552         angle += 180;
553     while (angle >= 180)
554         angle -= 180;
555     if (angle > 157.5 || angle <= 22.5)
556         setCursor(Qt::SizeVerCursor);
557     else if (angle > 22.5 && angle <= 67.5)
558         setCursor(Qt::SizeFDiagCursor);
559     else if (angle > 67.5 && angle <= 112.5)
560         setCursor(Qt::SizeHorCursor);
561     else if (angle > 112.5 && angle <= 157.5)
562         setCursor(Qt::SizeBDiagCursor);
563 }
564
565 void GraphicsSceneRectMove::slotUpdateFontSize(int s)
566 {
567     m_fontSize = s;
568 }
569
570 #include "graphicsscenerectmove.moc"