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): QGraphicsScene(parent), m_selectedItem(NULL), resizeMode(NoResize), m_tool(TITLE_RECTANGLE)
40 setBackgroundBrush(QBrush(Qt::transparent));
43 void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item)
46 m_selectedItem = item;
47 item->setSelected(true);
51 TITLETOOL GraphicsSceneRectMove::tool()
56 void GraphicsSceneRectMove::setTool(TITLETOOL tool)
61 setCursor(Qt::CrossCursor);
64 setCursor(Qt::IBeamCursor);
67 setCursor(Qt::ArrowCursor);
72 void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent)
74 if (m_selectedItem == NULL) {
75 QGraphicsScene::keyPressEvent(keyEvent);
79 if (m_selectedItem->type() == 8) {
80 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
81 if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
82 QGraphicsScene::keyPressEvent(keyEvent);
86 if (keyEvent->modifiers() & Qt::ControlModifier) diff = 10;
87 switch (keyEvent->key()) {
89 m_selectedItem->setPos(m_selectedItem->pos() - QPointF(diff, 0));
93 m_selectedItem->setPos(m_selectedItem->pos() + QPointF(diff, 0));
97 m_selectedItem->setPos(m_selectedItem->pos() - QPointF(0, diff));
101 m_selectedItem->setPos(m_selectedItem->pos() + QPointF(0, diff));
105 case Qt::Key_Backspace:
106 delete m_selectedItem;
107 m_selectedItem = NULL;
108 emit selectionChanged();
111 QGraphicsScene::keyPressEvent(keyEvent);
113 emit actionFinished();
117 void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e)
119 QPointF p = e->scenePos();
121 resizeMode = NoResize;
122 m_selectedItem = NULL;
123 QGraphicsItem* g = items(QRectF(p , QSizeF(4, 4)).toRect()).at(0);
125 if (g->type() == 8) {
126 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
128 t->setTextInteractionFlags(Qt::TextEditorInteraction);
131 QGraphicsScene::mouseDoubleClickEvent(e);
134 void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
136 if (m_tool == TITLE_RECTANGLE && m_selectedItem) setSelectedItem(m_selectedItem);
137 QGraphicsScene::mouseReleaseEvent(e);
138 emit actionFinished();
141 void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e)
143 m_clickPoint = e->screenPos();
144 QPointF p = e->scenePos();
146 resizeMode = NoResize;
147 const QList <QGraphicsItem *> list = items(QRectF(p , QSizeF(4, 4)).toRect());
148 QGraphicsItem *item = NULL;
149 bool hasSelected = false;
151 if (m_tool == TITLE_SELECT) {
152 foreach(QGraphicsItem *g, list) {
153 kDebug() << " - - CHECKING ITEM Z:" << g->zValue() << ", TYPE: " << g->type();
154 // check is there is a selected item in list
155 if (g->zValue() > -1000 && g->isSelected()) {
162 if (m_selectedItem && m_selectedItem->type() == 8) {
163 // disable text editing
164 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
165 t->textCursor().setPosition(0);
166 QTextBlock cur = t->textCursor().block();
167 t->setTextCursor(QTextCursor(cur));
168 t->setTextInteractionFlags(Qt::NoTextInteraction);
170 m_selectedItem = NULL;
171 foreach(QGraphicsItem* g, list) {
172 if (g->zValue() > -1000) {
179 m_sceneClickPoint = e->scenePos();
180 m_selectedItem = item;
181 kDebug() << "///////// ITEM TYPE: " << item->type();
182 if (item->type() == 8) {
183 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(item);
184 if (t->textInteractionFlags() == Qt::TextEditorInteraction) {
185 QGraphicsScene::mousePressEvent(e);
188 t->setTextInteractionFlags(Qt::NoTextInteraction);
189 setCursor(Qt::ClosedHandCursor);
190 } else if (item->type() == 3 || item->type() == 13 || item->type() == 7) {
192 if (m_selectedItem->type() == 3) {
193 r = ((QGraphicsRectItem*)m_selectedItem)->rect();
194 } else r = m_selectedItem->boundingRect();
196 r.translate(item->scenePos());
197 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
198 resizeMode = TopLeft;
199 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
200 resizeMode = BottomLeft;
201 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
202 resizeMode = TopRight;
203 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
204 resizeMode = BottomRight;
205 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
207 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
209 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
211 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
213 } else setCursor(Qt::ClosedHandCursor);
216 QGraphicsScene::mousePressEvent(e);
217 } else if (m_tool == TITLE_RECTANGLE) {
218 m_sceneClickPoint = e->scenePos();
219 m_selectedItem = NULL;
220 } else if (m_tool == TITLE_TEXT) {
221 m_selectedItem = addText(QString());
222 emit newText((QGraphicsTextItem *) m_selectedItem);
223 m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
224 ((QGraphicsTextItem *)m_selectedItem)->setTextInteractionFlags(Qt::TextEditorInteraction);
225 m_selectedItem->setPos(e->scenePos());
226 QGraphicsScene::mousePressEvent(e);
229 kDebug() << "////// MOUSE CLICK, RESIZE MODE: " << resizeMode;
233 void GraphicsSceneRectMove::clearTextSelection()
235 if (m_selectedItem && m_selectedItem->type() == 8) {
236 // disable text editing
237 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
238 t->textCursor().setPosition(0);
239 QTextBlock cur = t->textCursor().block();
240 t->setTextCursor(QTextCursor(cur));
241 t->setTextInteractionFlags(Qt::NoTextInteraction);
243 m_selectedItem = NULL;
248 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
250 if ((e->screenPos() - m_clickPoint).manhattanLength() < QApplication::startDragDistance()) {
254 if (m_selectedItem && e->buttons() & Qt::LeftButton) {
255 if (m_selectedItem->type() == 3 || m_selectedItem->type() == 13 || m_selectedItem->type() == 7) {
257 if (m_selectedItem->type() == 3) {
258 newrect = ((QGraphicsRectItem*)m_selectedItem)->rect();
259 } else newrect = m_selectedItem->boundingRect();
261 QPointF newpoint = e->scenePos();
262 //newpoint -= m_selectedItem->scenePos();
263 switch (resizeMode) {
265 newrect.setBottomRight(newrect.bottomRight() + m_selectedItem->pos() - newpoint);
266 m_selectedItem->setPos(newpoint);
269 newrect.setBottomRight(QPointF(newrect.bottomRight().x() + m_selectedItem->pos().x() - newpoint.x(), newpoint.y() - m_selectedItem->pos().y()));
270 m_selectedItem->setPos(QPointF(newpoint.x(), m_selectedItem->pos().y()));
273 newrect.setBottomRight(QPointF(newpoint.x() - m_selectedItem->pos().x(), newrect.bottom() + m_selectedItem->pos().y() - newpoint.y()));
274 m_selectedItem->setPos(QPointF(m_selectedItem->pos().x(), newpoint.y()));
277 newrect.setBottomRight(newpoint - m_selectedItem->pos());
280 newrect.setRight(m_selectedItem->pos().x() + newrect.width() - newpoint.x());
281 m_selectedItem->setPos(QPointF(newpoint.x(), m_selectedItem->pos().y()));
284 newrect.setRight(newpoint.x() - m_selectedItem->pos().x());
287 newrect.setBottom(m_selectedItem->pos().y() + newrect.bottom() - newpoint.y());
288 m_selectedItem->setPos(QPointF(m_selectedItem->pos().x(), newpoint.y()));
291 newrect.setBottom(newpoint.y() - m_selectedItem->pos().y());
294 QPointF diff = e->scenePos() - m_sceneClickPoint;
295 m_sceneClickPoint = e->scenePos();
296 m_selectedItem->moveBy(diff.x(), diff.y());
299 if (m_selectedItem->type() == 3 && resizeMode != NoResize) {
300 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
301 gi->setRect(newrect);
305 if (resizeMode == Left || resizeMode == Right ) s = m_selectedItem->boundingRect().width() / newrect.width();
306 else s = m_selectedItem->boundingRect().height() / newrect.height();
307 m_selectedItem->scale( 1 / s, 1 / s );
308 kDebug()<<"/// SCALING SVG, RESIZE MODE: "<<resizeMode<<", RECT:"<<m_selectedItem->boundingRect();
310 //gi->setPos(m_selectedItem->scenePos());
311 /*if (resizeMode == NoResize) {
312 QGraphicsScene::mouseMoveEvent(e);
315 } else if (m_selectedItem->type() == 8) {
316 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
317 if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
318 QGraphicsScene::mouseMoveEvent(e);
321 QPointF diff = e->scenePos() - m_sceneClickPoint;
322 m_sceneClickPoint = e->scenePos();
323 m_selectedItem->moveBy(diff.x(), diff.y());
326 } else if (m_tool == TITLE_SELECT) {
327 QPointF p = e->scenePos();
329 resizeMode = NoResize;
330 bool itemFound = false;
331 foreach(const QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) {
332 if ((g->type() == 13 || g->type() == 7) && g->zValue() > -1000) {
334 setCursor(Qt::OpenHandCursor);
336 } else if (g->type() == 3 && g->zValue() > -1000) {
337 QRectF r = ((const QGraphicsRectItem*)g)->rect();
338 r.translate(g->scenePos());
340 if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
341 setCursor(QCursor(Qt::SizeFDiagCursor));
342 } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
343 setCursor(QCursor(Qt::SizeBDiagCursor));
344 } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
345 setCursor(QCursor(Qt::SizeBDiagCursor));
346 } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / zoom) {
347 setCursor(QCursor(Qt::SizeFDiagCursor));
348 } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / zoom) {
349 setCursor(Qt::SizeHorCursor);
350 } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / zoom) {
351 setCursor(Qt::SizeHorCursor);
352 } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / zoom) {
353 setCursor(Qt::SizeVerCursor);
354 } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / zoom) {
355 setCursor(Qt::SizeVerCursor);
356 } else setCursor(Qt::OpenHandCursor);
359 if (!itemFound) setCursor(Qt::ArrowCursor);
361 QGraphicsScene::mouseMoveEvent(e);
362 } else if (m_tool == TITLE_RECTANGLE && e->buttons() & Qt::LeftButton) {
363 if (m_selectedItem == NULL && (m_clickPoint - e->screenPos()).manhattanLength() >= QApplication::startDragDistance()) {
364 // create new rect item
365 m_selectedItem = addRect(0, 0, e->scenePos().x() - m_sceneClickPoint.x(), e->scenePos().y() - m_sceneClickPoint.y());
366 emit newRect((QGraphicsRectItem *) m_selectedItem);
367 m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
368 m_selectedItem->setPos(m_sceneClickPoint);
369 resizeMode = BottomRight;
370 QGraphicsScene::mouseMoveEvent(e);
375 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
377 QList<QGraphicsView*> viewlist = views();
378 //kDebug() << wheelEvent->delta() << " " << zoom;
379 if (viewlist.size() > 0) {
380 if (wheelEvent->delta() < 0) emit sceneZoom(true);
381 else emit sceneZoom(false);
385 void GraphicsSceneRectMove::setScale(double s)
387 if (zoom < 1.0 / 7.0 && s < 1.0) return;
388 else if (zoom > 10.0 / 7.9 && s > 1.0) return;
389 QList<QGraphicsView*> viewlist = views();
390 if (viewlist.size() > 0) {
391 viewlist[0]->scale(s, s);
394 //kDebug()<<"////////// ZOOM: "<<zoom;
397 void GraphicsSceneRectMove::setZoom(double s)
399 QList<QGraphicsView*> viewlist = views();
400 if (viewlist.size() > 0) {
401 viewlist[0]->resetTransform();
402 viewlist[0]->scale(s, s);
406 //kDebug()<<"////////// ZOOM: "<<zoom;
409 void GraphicsSceneRectMove::setCursor(QCursor c)
411 const QList<QGraphicsView*> l = views();
412 foreach(QGraphicsView* v, l) {