1 /***************************************************************************
2 * copyright (C) 2008 by Marco Gittler (g.marco@freenet.de) *
3 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "graphicsscenerectmove.h"
24 #include <QGraphicsSceneMouseEvent>
25 #include <QGraphicsRectItem>
26 #include <QGraphicsSvgItem>
27 #include <QGraphicsView>
29 #include <QTextCursor>
32 #include <QApplication>
36 GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent) :
37 QGraphicsScene(parent),
39 m_resizeMode(NoResize),
40 m_tool(TITLE_RECTANGLE)
44 setBackgroundBrush(QBrush(Qt::transparent));
48 void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item)
51 m_selectedItem = item;
52 item->setSelected(true);
56 TITLETOOL GraphicsSceneRectMove::tool()
61 void GraphicsSceneRectMove::setTool(TITLETOOL tool)
66 setCursor(Qt::CrossCursor);
69 setCursor(Qt::IBeamCursor);
72 setCursor(Qt::ArrowCursor);
76 void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent)
78 if (m_selectedItem == NULL || !(m_selectedItem->flags() & QGraphicsItem::ItemIsMovable)) {
79 QGraphicsScene::keyPressEvent(keyEvent);
83 if (m_selectedItem->type() == 8) {
84 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
85 if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
86 QGraphicsScene::keyPressEvent(keyEvent);
90 if (keyEvent->modifiers() & Qt::ControlModifier) diff = 10;
91 switch (keyEvent->key()) {
93 m_selectedItem->setPos(m_selectedItem->pos() - QPointF(diff, 0));
97 m_selectedItem->setPos(m_selectedItem->pos() + QPointF(diff, 0));
101 m_selectedItem->setPos(m_selectedItem->pos() - QPointF(0, diff));
105 m_selectedItem->setPos(m_selectedItem->pos() + QPointF(0, diff));
109 case Qt::Key_Backspace:
110 delete m_selectedItem;
111 m_selectedItem = NULL;
112 emit selectionChanged();
115 QGraphicsScene::keyPressEvent(keyEvent);
117 emit actionFinished();
120 void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e)
122 QPointF p = e->scenePos();
124 m_resizeMode = NoResize;
125 m_selectedItem = NULL;
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;
131 QGraphicsItem* g = i.at(0);
133 if (g->type() == 8) {
134 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
136 t->setTextInteractionFlags(Qt::TextEditorInteraction);
137 } else emit doubleClickEvent();
139 QGraphicsScene::mouseDoubleClickEvent(e);
142 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
144 if (m_tool == TITLE_RECTANGLE && m_selectedItem) setSelectedItem(m_selectedItem);
145 QGraphicsScene::mouseReleaseEvent(e);
146 emit actionFinished();
149 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e)
151 m_clickPoint = e->screenPos();
152 QPointF p = e->scenePos();
154 m_resizeMode = NoResize;
155 const QList <QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
156 QGraphicsItem *item = NULL;
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()) {
167 if (item == NULL || !(item->flags() & QGraphicsItem::ItemIsSelectable)) {
168 if (m_selectedItem && m_selectedItem->type() == 8) {
169 // disable text editing
170 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
171 t->textCursor().setPosition(0);
172 QTextBlock cur = t->textCursor().block();
173 t->setTextCursor(QTextCursor(cur));
174 t->setTextInteractionFlags(Qt::NoTextInteraction);
176 m_selectedItem = NULL;
177 foreach(QGraphicsItem* g, list) {
178 if (g->zValue() > -1000) {
184 if (item != NULL && item->flags() & QGraphicsItem::ItemIsMovable) {
185 m_sceneClickPoint = e->scenePos();
186 m_selectedItem = item;
187 kDebug() << "///////// ITEM TYPE: " << item->type();
188 if (item->type() == 8) {
189 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(item);
190 if (t->textInteractionFlags() == Qt::TextEditorInteraction) {
191 QGraphicsScene::mousePressEvent(e);
194 t->setTextInteractionFlags(Qt::NoTextInteraction);
195 setCursor(Qt::ClosedHandCursor);
196 } else if (item->type() == 3 || item->type() == 13 || item->type() == 7) {
198 if (m_selectedItem->type() == 3)
199 r1 = ((QGraphicsRectItem*)m_selectedItem)->rect().normalized();
201 r1 = m_selectedItem->boundingRect().normalized();
203 QList<QGraphicsView*> viewlist = views();
204 QGraphicsView *view = NULL;
205 if (viewlist.size() > 0) view = viewlist[0];
206 if (view == NULL) return;
207 // Item mapped coordinates
208 QPolygon r = m_selectedItem->deviceTransform(view->viewportTransform()).map(r1).toPolygon();
209 QPainterPath top(r.point(0));
210 top.lineTo(r.point(1));
211 QPainterPath bottom(r.point(2));
212 bottom.lineTo(r.point(3));
213 QPainterPath left(r.point(0));
214 left.lineTo(r.point(3));
215 QPainterPath right(r.point(1));
216 right.lineTo(r.point(2));
219 // The area interested by the mouse pointer
220 QPoint viewPos = view->mapFromScene(e->scenePos());
221 QPainterPath mouseArea;
222 mouseArea.addRect(viewPos.x() - 4, viewPos.y() - 4, 8, 8);
224 // Check for collisions between the mouse and the borders
225 if (mouseArea.contains(r.point(0))) m_resizeMode = TopLeft;
226 else if (mouseArea.contains(r.point(2))) m_resizeMode = BottomRight;
227 else if (mouseArea.contains(r.point(1))) m_resizeMode = TopRight;
228 else if (mouseArea.contains(r.point(3))) m_resizeMode = BottomLeft;
229 else if (top.intersects(mouseArea)) m_resizeMode = Up;
230 else if (bottom.intersects(mouseArea)) m_resizeMode = Down;
231 else if (right.intersects(mouseArea)) m_resizeMode = Right;
232 else if (left.intersects(mouseArea)) m_resizeMode = Left;
235 setCursor(Qt::ClosedHandCursor);
238 QGraphicsScene::mousePressEvent(e);
239 } else if (m_tool == TITLE_RECTANGLE) {
240 m_sceneClickPoint = e->scenePos();
241 m_selectedItem = NULL;
242 } else if (m_tool == TITLE_TEXT) {
243 m_selectedItem = addText(QString());
244 emit newText((QGraphicsTextItem *) m_selectedItem);
245 m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
246 ((QGraphicsTextItem *)m_selectedItem)->setTextInteractionFlags(Qt::TextEditorInteraction);
247 m_selectedItem->setPos(e->scenePos() - QPointF(0, (int)(m_fontSize / 2)));
248 QGraphicsScene::mousePressEvent(e);
251 kDebug() << "////// MOUSE CLICK, RESIZE MODE: " << m_resizeMode;
255 void GraphicsSceneRectMove::clearTextSelection()
257 if (m_selectedItem && m_selectedItem->type() == 8) {
258 // disable text editing
259 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
260 t->textCursor().setPosition(0);
261 QTextBlock cur = t->textCursor().block();
262 t->setTextCursor(QTextCursor(cur));
263 t->setTextInteractionFlags(Qt::NoTextInteraction);
265 m_selectedItem = NULL;
269 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
271 if (e->buttons() != Qt::NoButton && (e->screenPos() - m_clickPoint).manhattanLength() < QApplication::startDragDistance()) {
275 if (m_selectedItem && e->buttons() & Qt::LeftButton) {
276 if (m_selectedItem->type() == 3 || m_selectedItem->type() == 13 || m_selectedItem->type() == 7) {
278 if (m_selectedItem->type() == 3)
279 newrect = ((QGraphicsRectItem*)m_selectedItem)->rect();
281 newrect = m_selectedItem->boundingRect();
282 QPointF newpoint = e->scenePos();
284 * The vertices of the rectangle (check for matrix
285 * transformation); to be replaced by QTransform::map()?
287 QPointF itemOrigin = m_selectedItem->scenePos();
288 QTransform transform = m_selectedItem->transform();
289 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());
290 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());
291 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());
292 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());
293 // Convert the mouse coordinates applying inverted transformation
294 QPointF newPointRelative = newpoint - itemOrigin;
295 QPointF resizePoint(transform.inverted().m11() * newPointRelative.x() + transform.inverted().m21() * newPointRelative.y() + transform.inverted().m31(), transform.inverted().m22() * newPointRelative.y() + transform.inverted().m12() * newPointRelative.x() + transform.inverted().m32());
297 * Will check if the mouse is on the right of the limit lines with a
298 * determinant (it must be less than zero because the Y axis is
301 int determinantH, determinantV;
302 // Check whether to resize or to just move the item(s)
303 switch (m_resizeMode) {
305 determinantV = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
306 determinantH = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
307 if (determinantV < 0) {
308 if (determinantH < 0) {
309 // resizePoint is not working for some reason
310 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())));
311 m_selectedItem->setPos(resizePoint + itemOrigin);
313 m_resizeMode = BottomLeft;
315 if (determinantH < 0)
316 m_resizeMode = TopRight;
318 m_resizeMode = BottomRight;
322 determinantV = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
323 determinantH = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
324 if (determinantV < 0) {
325 if (determinantH < 0) {
326 newrect.setBottomRight(QPointF(newrect.width() - resizePoint.x(), resizePoint.y()));
327 m_selectedItem->setPos(QPointF(transform.m11() * resizePoint.x() + transform.m21() *(newrect.bottom() - resizePoint.y()) + transform.m31() + itemOrigin.x(), transform.m22() *(newrect.bottom() - resizePoint.y()) + transform.m12() * resizePoint.x() + transform.m32() + itemOrigin.y()));
329 m_resizeMode = TopLeft;
331 if (determinantH < 0)
332 m_resizeMode = BottomRight;
334 m_resizeMode = TopRight;
338 determinantV = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
339 determinantH = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
340 if (determinantV < 0) {
341 if (determinantH < 0) {
342 newrect.setBottomRight(QPointF(resizePoint.x(), newrect.bottom() - resizePoint.y()));
343 m_selectedItem->setPos(QPointF(transform.m11() *(newrect.width() - resizePoint.x()) + transform.m21() * resizePoint.y() + transform.m31() + itemOrigin.x(), transform.m22() * resizePoint.y() + transform.m12() *(newrect.width() - resizePoint.x()) + transform.m32() + itemOrigin.y()));
345 m_resizeMode = BottomRight;
347 if (determinantH < 0)
348 m_resizeMode = TopLeft;
350 m_resizeMode = BottomLeft;
354 determinantV = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
355 determinantH = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
356 if (determinantV < 0) {
357 if (determinantH < 0)
358 newrect.setBottomRight(resizePoint);
360 m_resizeMode = TopRight;
362 if (determinantH < 0)
363 m_resizeMode = BottomLeft;
365 m_resizeMode = TopLeft;
369 determinantV = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
370 if (determinantV < 0) {
371 newrect.setRight(newrect.width() - resizePoint.x());
372 m_selectedItem->setPos(QPointF(transform.m11() * resizePoint.x() + transform.m31() + itemOrigin.x(), transform.m12() * resizePoint.x() + transform.m32() + itemOrigin.y()));
374 m_resizeMode = Right;
377 determinantV = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
378 if (determinantV < 0)
379 newrect.setRight(resizePoint.x());
384 determinantH = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
385 if (determinantH < 0) {
386 newrect.setBottom(newrect.bottom() - resizePoint.y());
387 m_selectedItem->setPos(QPointF(transform.m21() * resizePoint.y() + transform.m31() + itemOrigin.x(), transform.m22() * resizePoint.y() + transform.m32() + itemOrigin.y()));
392 determinantH = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
393 if (determinantH < 0)
394 newrect.setBottom(resizePoint.y());
399 QPointF diff = e->scenePos() - m_sceneClickPoint;
400 m_sceneClickPoint = e->scenePos();
401 m_selectedItem->moveBy(diff.x(), diff.y());
404 if (m_selectedItem->type() == 3 && m_resizeMode != NoResize) {
405 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
406 // Resize using aspect ratio
407 if (!m_selectedItem->data(0).isNull()) {
408 // we want to keep aspect ratio
409 double hRatio = (double) newrect.width() / m_selectedItem->data(0).toInt();
410 double vRatio = (double) newrect.height() / m_selectedItem->data(1).toInt();
411 if (hRatio < vRatio) newrect.setHeight(m_selectedItem->data(1).toInt() * hRatio);
412 else newrect.setWidth(m_selectedItem->data(0).toInt() * vRatio);
415 gi->setRect(newrect);
419 if (resizeMode == Left || resizeMode == Right ) s = m_selectedItem->boundingRect().width() / newrect.width();
420 else s = m_selectedItem->boundingRect().height() / newrect.height();
421 m_selectedItem->scale( 1 / s, 1 / s );
422 kDebug()<<"/// SCALING SVG, RESIZE MODE: "<<resizeMode<<", RECT:"<<m_selectedItem->boundingRect();
424 //gi->setPos(m_selectedItem->scenePos());
425 /*if (resizeMode == NoResize) {
426 QGraphicsScene::mouseMoveEvent(e);
429 } else if (m_selectedItem->type() == 8) {
430 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
431 if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
432 QGraphicsScene::mouseMoveEvent(e);
435 QPointF diff = e->scenePos() - m_sceneClickPoint;
436 m_sceneClickPoint = e->scenePos();
437 m_selectedItem->moveBy(diff.x(), diff.y());
440 } else if (m_tool == TITLE_SELECT) {
441 QList<QGraphicsView*> viewlist = views();
442 QGraphicsView *view = NULL;
443 if (viewlist.size() > 0) view = viewlist[0];
445 QPointF p = e->scenePos();
447 m_resizeMode = NoResize;
448 bool itemFound = false;
449 QList<QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
450 foreach(const QGraphicsItem* g, list) {
451 if ((g->type() == 13 || g->type() == 7) && g->zValue() > -1000) {
453 setCursor(Qt::OpenHandCursor);
455 } else if (g->type() == 3 && g->zValue() > -1000) {
456 if (view == NULL) continue;
457 QRectF r1 = ((const QGraphicsRectItem*)g)->rect().normalized();
460 // Item mapped coordinates
461 QPolygon r = g->deviceTransform(view->viewportTransform()).map(r1).toPolygon();
462 QPainterPath top(r.point(0));
463 top.lineTo(r.point(1));
464 QPainterPath bottom(r.point(2));
465 bottom.lineTo(r.point(3));
466 QPainterPath left(r.point(0));
467 left.lineTo(r.point(3));
468 QPainterPath right(r.point(1));
469 right.lineTo(r.point(2));
471 // The area interested by the mouse pointer
472 QPoint viewPos = view->mapFromScene(e->scenePos());
473 QPainterPath mouseArea;
474 mouseArea.addRect(viewPos.x() - 4, viewPos.y() - 4, 8, 8);
476 // Check for collisions between the mouse and the borders
477 if (mouseArea.contains(r.point(0)) || mouseArea.contains(r.point(2))) setCursor(Qt::SizeFDiagCursor);
478 else if (mouseArea.contains(r.point(1)) || mouseArea.contains(r.point(3))) setCursor(Qt::SizeBDiagCursor);
479 else if (top.intersects(mouseArea) || bottom.intersects(mouseArea)) setCursor(Qt::SizeVerCursor);
480 else if (right.intersects(mouseArea) || left.intersects(mouseArea)) setCursor(Qt::SizeHorCursor);
482 setCursor(Qt::OpenHandCursor);
485 if (!itemFound) setCursor(Qt::ArrowCursor);
487 QGraphicsScene::mouseMoveEvent(e);
488 } else if (m_tool == TITLE_RECTANGLE && e->buttons() & Qt::LeftButton) {
489 if (m_selectedItem == NULL) {
490 // create new rect item
491 QRectF r(0, 0, e->scenePos().x() - m_sceneClickPoint.x(), e->scenePos().y() - m_sceneClickPoint.y());
493 m_selectedItem = addRect(QRectF(0, 0, r.width(), r.height()));
494 emit newRect((QGraphicsRectItem *) m_selectedItem);
495 m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
496 m_selectedItem->setPos(m_sceneClickPoint);
497 m_resizeMode = BottomRight;
498 QGraphicsScene::mouseMoveEvent(e);
503 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
505 if (wheelEvent->modifiers() == Qt::ControlModifier) {
506 QList<QGraphicsView*> viewlist = views();
507 //kDebug() << wheelEvent->delta() << " " << zoom;
508 if (viewlist.size() > 0) {
509 if (wheelEvent->delta() > 0) emit sceneZoom(true);
510 else emit sceneZoom(false);
512 } else wheelEvent->setAccepted(false);
515 void GraphicsSceneRectMove::setScale(double s)
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);
524 //kDebug()<<"////////// ZOOM: "<<zoom;
527 void GraphicsSceneRectMove::setZoom(double s)
529 QList<QGraphicsView*> viewlist = views();
530 if (viewlist.size() > 0) {
531 viewlist[0]->resetTransform();
532 viewlist[0]->scale(s, s);
536 //kDebug()<<"////////// ZOOM: "<<zoom;
539 void GraphicsSceneRectMove::setCursor(QCursor c)
541 const QList<QGraphicsView*> l = views();
542 foreach(QGraphicsView* v, l) {
547 void GraphicsSceneRectMove::setResizeCursor(qreal angle)
549 // % is not working...
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);
564 void GraphicsSceneRectMove::slotUpdateFontSize(int s)
569 #include "graphicsscenerectmove.moc"