]> git.sesse.net Git - kdenlive/blob - src/graphicsscenerectmove.cpp
make the titler respect rectangles transformations when resizing or aligning:
[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
199                     r = m_selectedItem->boundingRect();
200                 /*
201                  * The vertices of the rectangle (check for matrix
202                  * transformation); i hope there is a shorter way to do this
203                  */
204                 QPointF itemOrigin = item->scenePos();
205                 QTransform transform = item->transform();
206                 QPointF topLeft(transform.m11() * r.toRect().left() + transform.m21() * r.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().top() + transform.m12() * r.toRect().left() + transform.m32() + itemOrigin.y());
207                 QPointF bottomLeft(transform.m11() * r.toRect().left() + transform.m21() * r.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().bottom() + transform.m12() * r.toRect().left() + transform.m32() + itemOrigin.y());
208                 QPointF topRight(transform.m11() * r.toRect().right() + transform.m21() * r.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().top() + transform.m12() * r.toRect().right() + transform.m32() + itemOrigin.y());
209                 QPointF bottomRight(transform.m11() * r.toRect().right() + transform.m21() * r.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().bottom() + transform.m12() * r.toRect().right() + transform.m32() + itemOrigin.y());
210                 // The borders (using the transformed coordinates)
211                 QGraphicsLineItem borderTop(topLeft.x(), topLeft.y(), topRight.x(), topRight.y());
212                 QGraphicsLineItem borderRight(topRight.x(), topRight.y(), bottomRight.x(), bottomRight.y());
213                 QGraphicsLineItem borderBottom(bottomRight.x(), bottomRight.y(), bottomLeft.x(), bottomLeft.y());
214                 QGraphicsLineItem borderLeft(bottomLeft.x(), bottomLeft.y(), topLeft.x(), topLeft.y());
215                 // The area interested by the mouse pointer
216                 QPainterPath mouseArea;
217                 mouseArea.addRect(e->scenePos().toPoint().x() - 3 / m_zoom, e->scenePos().toPoint().y() - 3 / m_zoom, 6 / m_zoom, 6 / m_zoom);
218                 // Check for collisions between the mouse and the borders
219                 if (borderLeft.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea))
220                     m_resizeMode = TopLeft;
221                 else if (borderLeft.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea))
222                     m_resizeMode = BottomLeft;
223                 else if (borderRight.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea))
224                     m_resizeMode = TopRight;
225                 else if (borderRight.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea))
226                     m_resizeMode = BottomRight;
227                 else if (borderLeft.collidesWithPath(mouseArea))
228                     m_resizeMode = Left;
229                 else if (borderRight.collidesWithPath(mouseArea))
230                     m_resizeMode = Right;
231                 else if (borderTop.collidesWithPath(mouseArea))
232                     m_resizeMode = Up;
233                 else if (borderBottom.collidesWithPath(mouseArea))
234                     m_resizeMode = Down;
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());
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() == 8) {
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 //virtual
271 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
272 {
273     if ((e->screenPos() - m_clickPoint).manhattanLength() < QApplication::startDragDistance()) {
274         e->accept();
275         return;
276     }
277     if (m_selectedItem && e->buttons() & Qt::LeftButton) {
278         if (m_selectedItem->type() == 3 || m_selectedItem->type() == 13 || m_selectedItem->type() == 7) {
279             QRectF newrect;
280             if (m_selectedItem->type() == 3)
281                 newrect = ((QGraphicsRectItem*)m_selectedItem)->rect();
282             else
283                 newrect = m_selectedItem->boundingRect();
284             QPointF newpoint = e->scenePos();
285             /*
286              * The vertices of the rectangle (check for matrix
287              * transformation); i hope there is a shorter way to do this
288              */
289             QPointF itemOrigin = m_selectedItem->scenePos();
290             QTransform transform = m_selectedItem->transform();
291             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());
292             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());
293             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());
294             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());
295             // Convert the mouse coordinates applying inverted transformation
296             QPointF newPointRelative = newpoint - itemOrigin;
297             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());
298             /*
299              * Will check if the mouse is on the right of the limit lines with a
300              * determinant (it must be less than zero because the Y axis is
301              * inverted)
302              */
303             int determinant;
304             switch (m_resizeMode) {
305                 case TopLeft:
306                     determinant = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
307                     if (determinant < 0) {
308                         determinant = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
309                         if (determinant < 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                         }
314                     }
315                     break;
316                 case BottomLeft:
317                     determinant = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
318                     if (determinant < 0) {
319                         determinant = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
320                         if (determinant < 0) {
321                             newrect.setBottomRight(QPointF(newrect.width() - resizePoint.x(), resizePoint.y()));
322                             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()));
323                         }
324                     }
325                     break;
326                 case TopRight:
327                     determinant = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
328                     if (determinant < 0) {
329                         determinant = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
330                         if (determinant < 0) {
331                             newrect.setBottomRight(QPointF(resizePoint.x(), newrect.bottom() - resizePoint.y()));
332                             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()));
333                         }
334                     }
335                     break;
336                 case BottomRight:
337                     determinant = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
338                     if (determinant < 0) {
339                         determinant = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
340                         if (determinant < 0)
341                             newrect.setBottomRight(resizePoint);
342                     }
343                     break;
344                 case Left:
345                     determinant = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
346                     if (determinant < 0) {
347                         newrect.setRight(newrect.width() - resizePoint.x());
348                         m_selectedItem->setPos(QPointF(transform.m11() * resizePoint.x() + transform.m31() + itemOrigin.x(), transform.m12() * resizePoint.x() + transform.m32() + itemOrigin.y()));
349                     }
350                     break;
351                 case Right:
352                     determinant = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
353                     if (determinant < 0)
354                         newrect.setRight(resizePoint.x());
355                     break;
356                 case Up:
357                     determinant = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
358                     if (determinant < 0) {
359                         newrect.setBottom(newrect.bottom() - resizePoint.y());
360                         m_selectedItem->setPos(QPointF(transform.m21() * resizePoint.y() + transform.m31() + itemOrigin.x(), transform.m22() * resizePoint.y() + transform.m32() + itemOrigin.y()));
361                     }
362                     break;
363                 case Down:
364                     determinant = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
365                     if (determinant < 0)
366                         newrect.setBottom(resizePoint.y());
367                     break;
368                 default:
369                     QPointF diff = e->scenePos() - m_sceneClickPoint;
370                     m_sceneClickPoint = e->scenePos();
371                     m_selectedItem->moveBy(diff.x(), diff.y());
372                     break;
373             }
374             if (m_selectedItem->type() == 3 && m_resizeMode != NoResize) {
375                 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
376                 gi->setRect(newrect);
377             }
378             /*else {
379             qreal s;
380             if (resizeMode == Left || resizeMode == Right ) s = m_selectedItem->boundingRect().width() / newrect.width();
381             else s = m_selectedItem->boundingRect().height() / newrect.height();
382             m_selectedItem->scale( 1 / s, 1 / s );
383             kDebug()<<"/// SCALING SVG, RESIZE MODE: "<<resizeMode<<", RECT:"<<m_selectedItem->boundingRect();
384             }*/
385             //gi->setPos(m_selectedItem->scenePos());
386             /*if (resizeMode == NoResize) {
387                 QGraphicsScene::mouseMoveEvent(e);
388                 return;
389             }*/
390         } else if (m_selectedItem->type() == 8) {
391             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
392             if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
393                 QGraphicsScene::mouseMoveEvent(e);
394                 return;
395             }
396             QPointF diff = e->scenePos() - m_sceneClickPoint;
397             m_sceneClickPoint = e->scenePos();
398             m_selectedItem->moveBy(diff.x(), diff.y());
399         }
400         emit itemMoved();
401     } else if (m_tool == TITLE_SELECT) {
402         QPointF p = e->scenePos();
403         p += QPoint(-2, -2);
404         m_resizeMode = NoResize;
405         bool itemFound = false;
406         foreach(const QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) {
407             if ((g->type() == 13 || g->type() == 7) && g->zValue() > -1000) {
408                 // image or svg item
409                 setCursor(Qt::OpenHandCursor);
410                 break;
411             } else if (g->type() == 3 && g->zValue() > -1000) {
412                 QRectF r = ((const QGraphicsRectItem*)g)->rect();
413                 itemFound = true;
414                 /*
415                  * The vertices of the rectangle (check for matrix
416                  * transformation); i hope there is a shorter way to do this
417                  */
418                 QPointF itemOrigin = g->scenePos();
419                 QTransform transform = g->transform();
420                 QPointF topLeft(transform.m11() * r.toRect().left() + transform.m21() * r.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().top() + transform.m12() * r.toRect().left() + transform.m32() + itemOrigin.y());
421                 QPointF bottomLeft(transform.m11() * r.toRect().left() + transform.m21() * r.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().bottom() + transform.m12() * r.toRect().left() + transform.m32() + itemOrigin.y());
422                 QPointF topRight(transform.m11() * r.toRect().right() + transform.m21() * r.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().top() + transform.m12() * r.toRect().right() + transform.m32() + itemOrigin.y());
423                 QPointF bottomRight(transform.m11() * r.toRect().right() + transform.m21() * r.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().bottom() + transform.m12() * r.toRect().right() + transform.m32() + itemOrigin.y());
424                 // The borders (using the transformed coordinates)
425                 QGraphicsLineItem borderTop(topLeft.x(), topLeft.y(), topRight.x(), topRight.y());
426                 QGraphicsLineItem borderRight(topRight.x(), topRight.y(), bottomRight.x(), bottomRight.y());
427                 QGraphicsLineItem borderBottom(bottomRight.x(), bottomRight.y(), bottomLeft.x(), bottomLeft.y());
428                 QGraphicsLineItem borderLeft(bottomLeft.x(), bottomLeft.y(), topLeft.x(), topLeft.y());
429                 // The area interested by the mouse pointer
430                 QPainterPath mouseArea;
431                 mouseArea.addRect(e->scenePos().toPoint().x() - 3 / m_zoom, e->scenePos().toPoint().y() - 3 / m_zoom, 6 / m_zoom, 6 / m_zoom);
432                 // Check for collisions between the mouse and the borders
433                 if (borderLeft.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea))
434                     setCursor(QCursor(Qt::SizeFDiagCursor));
435                 else if (borderLeft.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea))
436                     setCursor(QCursor(Qt::SizeBDiagCursor));
437                 else if (borderRight.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea))
438                     setCursor(QCursor(Qt::SizeBDiagCursor));
439                 else if (borderRight.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea))
440                     setCursor(QCursor(Qt::SizeFDiagCursor));
441                 else if (borderLeft.collidesWithPath(mouseArea))
442                     setCursor(Qt::SizeHorCursor);
443                 else if (borderRight.collidesWithPath(mouseArea))
444                     setCursor(Qt::SizeHorCursor);
445                 else if (borderTop.collidesWithPath(mouseArea))
446                     setCursor(Qt::SizeVerCursor);
447                 else if (borderBottom.collidesWithPath(mouseArea))
448                     setCursor(Qt::SizeVerCursor);
449                 else
450                     setCursor(Qt::OpenHandCursor);
451                 break;
452             }
453             if (!itemFound) setCursor(Qt::ArrowCursor);
454         }
455         QGraphicsScene::mouseMoveEvent(e);
456     } else if (m_tool == TITLE_RECTANGLE && e->buttons() & Qt::LeftButton) {
457         if (m_selectedItem == NULL && (m_clickPoint - e->screenPos()).manhattanLength() >= QApplication::startDragDistance()) {
458             // create new rect item
459             m_selectedItem = addRect(0, 0, e->scenePos().x() - m_sceneClickPoint.x(), e->scenePos().y() - m_sceneClickPoint.y());
460             emit newRect((QGraphicsRectItem *) m_selectedItem);
461             m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
462             m_selectedItem->setPos(m_sceneClickPoint);
463             m_resizeMode = BottomRight;
464             QGraphicsScene::mouseMoveEvent(e);
465         }
466     }
467 }
468
469 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
470 {
471     QList<QGraphicsView*> viewlist = views();
472     //kDebug() << wheelEvent->delta() << " " << zoom;
473     if (viewlist.size() > 0) {
474         if (wheelEvent->delta() < 0) emit sceneZoom(true);
475         else emit sceneZoom(false);
476     }
477 }
478
479 void GraphicsSceneRectMove::setScale(double s)
480 {
481     if (m_zoom < 1.0 / 7.0 && s < 1.0) return;
482     else if (m_zoom > 10.0 / 7.9 && s > 1.0) return;
483     QList<QGraphicsView*> viewlist = views();
484     if (viewlist.size() > 0) {
485         viewlist[0]->scale(s, s);
486         m_zoom = m_zoom * s;
487     }
488     //kDebug()<<"//////////  ZOOM: "<<zoom;
489 }
490
491 void GraphicsSceneRectMove::setZoom(double s)
492 {
493     QList<QGraphicsView*> viewlist = views();
494     if (viewlist.size() > 0) {
495         viewlist[0]->resetTransform();
496         viewlist[0]->scale(s, s);
497         m_zoom = s;
498     }
499
500     //kDebug()<<"//////////  ZOOM: "<<zoom;
501 }
502
503 void GraphicsSceneRectMove::setCursor(QCursor c)
504 {
505     const QList<QGraphicsView*> l = views();
506     foreach(QGraphicsView* v, l) {
507         v->setCursor(c);
508     }
509 }