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