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