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