]> git.sesse.net Git - kdenlive/blob - src/onmonitoritems/rotoscoping/bpointitem.cpp
First work on rotoscoping GUI
[kdenlive] / src / onmonitoritems / rotoscoping / bpointitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 by Till Theato (root@ttill.de)                     *
3  *   This file is part of Kdenlive (www.kdenlive.org).                     *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
18
19 #include "bpointitem.h"
20 #include "splineitem.h"
21
22 #include <QPainter>
23 #include <QGraphicsSceneMouseEvent>
24 #include <QCursor>
25
26
27 BPointItem::BPointItem(BPoint point, QGraphicsItem* parent) :
28         QAbstractGraphicsShapeItem(parent),
29         m_selection(-1)
30 {
31     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
32
33     QPen framepen(Qt::SolidLine);
34     framepen.setColor(Qt::yellow);
35     setPen(framepen);
36     setBrush(Qt::NoBrush);
37     setAcceptHoverEvents(true);
38
39     setPos(point.p);
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;
44 }
45
46 BPoint BPointItem::getPoint()
47 {
48     return BPoint(mapToScene(m_point.h1), mapToScene(m_point.p), mapToScene(m_point.h2));
49 }
50
51 int BPointItem::type() const
52 {
53     return Type;
54 }
55
56 QRectF BPointItem::boundingRect() const
57 {
58     QPolygonF p = QPolygonF() << m_point.h1 << m_point.p << m_point.h2;
59     return p.boundingRect().adjusted(-6, -6, 6, 6);
60 }
61
62 void BPointItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
63 {
64     Q_UNUSED(option);
65     Q_UNUSED(widget);
66
67     painter->setPen(QPen(Qt::yellow, 1, Qt::SolidLine));
68     painter->setBrush(QBrush(isSelected() ? Qt::red : Qt::yellow));
69     painter->setRenderHint(QPainter::Antialiasing);
70
71     double handleSize = 6 / painter->matrix().m11();
72     double handleSizeHalf = handleSize / 2;
73
74     QPolygonF handle = QPolygonF() << QPointF(0, -handleSizeHalf) << QPointF(handleSizeHalf, 0) << QPointF(0, handleSizeHalf) << QPointF(-handleSizeHalf, 0);
75
76     painter->drawLine(m_point.h1, m_point.p);
77     painter->drawLine(m_point.p, m_point.h2);
78
79 #if QT_VERSION >= 0x040600
80     painter->drawConvexPolygon(handle.translated(m_point.h1.x(), m_point.h1.y()));
81     painter->drawConvexPolygon(handle.translated(m_point.h2.x(), m_point.h2.y()));
82 #else
83     tmp = handle;
84     tmp.translate(m_point.h1.x(), m_point.h1.y());
85     p.drawConvexPolygon(tmp);
86     tmp.translate(m_point.h2.x(), m_point.h2.y());
87     p.drawConvexPolygon(tmp);
88 #endif
89
90     painter->drawEllipse(QRectF(m_point.p.x() - handleSizeHalf,
91                                 m_point.p.y() - handleSizeHalf, handleSize, handleSize));
92 }
93
94 int BPointItem::getSelection(QPointF pos)
95 {
96     QPainterPath mouseArea;
97     mouseArea.addRect(pos.x() - 6, pos.y() - 6, 12, 12);
98
99     if (mouseArea.contains(m_point.p))
100         return 1;
101     else if (mouseArea.contains(m_point.h1))
102         return 0;
103     else if (mouseArea.contains(m_point.h2))
104         return 2;
105
106     return -1;
107 }
108
109 void BPointItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
110 {
111     m_selection = getSelection(event->pos());
112
113     if (m_selection < 0) {
114         event->ignore();
115         setSelected(false);
116     } else {
117         setSelected(true);
118     }
119 }
120
121 void BPointItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
122 {
123     prepareGeometryChange();
124     switch (m_selection) {
125     case 0:
126         m_point.setH1(event->pos());
127         break;
128     case 1:
129         m_point.setP(event->pos());
130         break;
131     case 2:
132         m_point.setH2(event->pos());
133         break;
134     }
135
136     if (parentItem()) {
137         SplineItem *parent = qgraphicsitem_cast<SplineItem*>(parentItem());
138         if (parent)
139             parent->updateSpline();
140     }
141 }
142
143 void BPointItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
144 {
145 QGraphicsItem::mouseReleaseEvent(event);
146 }
147
148 void BPointItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
149 {
150     if (getSelection(event->pos()) < 0)
151         unsetCursor();
152     else
153         setCursor(QCursor(Qt::PointingHandCursor));
154 }