1 /***************************************************************************
2 * Copyright (C) 2011 by Till Theato (root@ttill.de) *
3 * This file is part of Kdenlive (www.kdenlive.org). *
5 * Kdenlive 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 * Kdenlive 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 Kdenlive. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
19 #include "bpointitem.h"
20 #include "splineitem.h"
23 #include <QGraphicsSceneMouseEvent>
27 BPointItem::BPointItem(BPoint point, QGraphicsItem* parent) :
28 QAbstractGraphicsShapeItem(parent),
31 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
33 QPen framepen(Qt::SolidLine);
34 framepen.setColor(Qt::yellow);
36 setBrush(Qt::NoBrush);
37 setAcceptHoverEvents(true);
40 m_point.h1 = mapFromScene(point.h1);
41 m_point.p = mapFromScene(point.p);
42 m_point.h2 = mapFromScene(point.h2);
43 m_point.handlesLinked = false;
46 BPoint BPointItem::getPoint()
48 return BPoint(mapToScene(m_point.h1), mapToScene(m_point.p), mapToScene(m_point.h2));
51 void BPointItem::setPoint(BPoint point)
54 prepareGeometryChange();
55 m_point.h1 = mapFromScene(point.h1);
56 m_point.p = mapFromScene(point.p);
57 m_point.h2 = mapFromScene(point.h2);
60 int BPointItem::type() const
65 QRectF BPointItem::boundingRect() const
67 QPolygonF p = QPolygonF() << m_point.h1 << m_point.p << m_point.h2;
68 return p.boundingRect().adjusted(-6, -6, 6, 6);
71 void BPointItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
77 painter->setPen(QPen(Qt::yellow, 1, Qt::SolidLine));
78 painter->setBrush(QBrush(isSelected() ? Qt::red : Qt::yellow));
80 painter->setPen(QPen(Qt::gray, 1, Qt::SolidLine));
81 painter->setBrush(QBrush(Qt::gray));
83 painter->setRenderHint(QPainter::Antialiasing);
85 double handleSize = 6 / painter->matrix().m11();
86 double handleSizeHalf = handleSize / 2;
88 QPolygonF handle = QPolygonF() << QPointF(0, -handleSizeHalf) << QPointF(handleSizeHalf, 0) << QPointF(0, handleSizeHalf) << QPointF(-handleSizeHalf, 0);
90 painter->drawLine(m_point.h1, m_point.p);
91 painter->drawLine(m_point.p, m_point.h2);
93 #if QT_VERSION >= 0x040600
94 painter->drawConvexPolygon(handle.translated(m_point.h1.x(), m_point.h1.y()));
95 painter->drawConvexPolygon(handle.translated(m_point.h2.x(), m_point.h2.y()));
98 tmp.translate(m_point.h1.x(), m_point.h1.y());
99 p.drawConvexPolygon(tmp);
100 tmp.translate(m_point.h2.x(), m_point.h2.y());
101 p.drawConvexPolygon(tmp);
104 painter->drawEllipse(QRectF(m_point.p.x() - handleSizeHalf,
105 m_point.p.y() - handleSizeHalf, handleSize, handleSize));
108 int BPointItem::getSelection(QPointF pos)
110 QPainterPath mouseArea;
111 mouseArea.addRect(pos.x() - 6, pos.y() - 6, 12, 12);
113 if (mouseArea.contains(m_point.h1))
115 else if (mouseArea.contains(m_point.p))
117 else if (mouseArea.contains(m_point.h2))
123 void BPointItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
125 m_selection = getSelection(event->pos());
127 if (m_selection < 0) {
131 if (event->button() == Qt::RightButton && m_selection == 1) {
132 SplineItem *parent = qgraphicsitem_cast<SplineItem*>(parentItem());
134 parent->removeChild(this);
142 void BPointItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
144 prepareGeometryChange();
145 switch (m_selection) {
147 m_point.setH1(event->pos());
150 m_point.setP(event->pos());
153 m_point.setH2(event->pos());
158 SplineItem *parent = qgraphicsitem_cast<SplineItem*>(parentItem());
160 parent->updateSpline(true);
164 void BPointItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
167 SplineItem *parent = qgraphicsitem_cast<SplineItem*>(parentItem());
169 parent->updateSpline(false);
171 QGraphicsItem::mouseReleaseEvent(event);
174 void BPointItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
176 if (getSelection(event->pos()) < 0)
179 setCursor(QCursor(Qt::PointingHandCursor));