]> git.sesse.net Git - kdenlive/blob - src/onmonitoritems/rotoscoping/bpointitem.cpp
Fix compile with Qt < 4.6 (once again):
[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     setAcceptHoverEvents(true);
34
35     setPos(point.p);
36     m_point.h1 = mapFromScene(point.h1);
37     m_point.p = mapFromScene(point.p);
38     m_point.h2 = mapFromScene(point.h2);
39     m_point.handlesLinked = false;
40 }
41
42 BPoint BPointItem::getPoint()
43 {
44     return BPoint(mapToScene(m_point.h1), mapToScene(m_point.p), mapToScene(m_point.h2));
45 }
46
47 void BPointItem::setPoint(BPoint point)
48 {
49     setPos(point.p);
50     prepareGeometryChange();
51     m_point.h1 = mapFromScene(point.h1);
52     m_point.p = mapFromScene(point.p);
53     m_point.h2 = mapFromScene(point.h2);
54 }
55
56 int BPointItem::type() const
57 {
58     return Type;
59 }
60
61 QRectF BPointItem::boundingRect() const
62 {
63     QPolygonF p = QPolygonF() << m_point.h1 << m_point.p << m_point.h2;
64     return p.boundingRect().adjusted(-6, -6, 6, 6);
65 }
66
67 void BPointItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
68 {
69     Q_UNUSED(option);
70     Q_UNUSED(widget);
71
72     if (isEnabled()) {
73         painter->setPen(QPen(Qt::yellow, 1, Qt::SolidLine));
74         painter->setBrush(QBrush(isSelected() ? Qt::red : Qt::yellow));
75     } else {
76         painter->setPen(QPen(Qt::gray, 1, Qt::SolidLine));
77         painter->setBrush(QBrush(Qt::gray));
78     }
79     painter->setRenderHint(QPainter::Antialiasing);
80
81     double handleSize = 6 / painter->matrix().m11();
82     double handleSizeHalf = handleSize / 2;
83
84     QPolygonF handle = QPolygonF() << QPointF(0, -handleSizeHalf) << QPointF(handleSizeHalf, 0) << QPointF(0, handleSizeHalf) << QPointF(-handleSizeHalf, 0);
85
86     painter->drawLine(m_point.h1, m_point.p);
87     painter->drawLine(m_point.p, m_point.h2);
88
89 #if QT_VERSION >= 0x040600
90     painter->drawConvexPolygon(handle.translated(m_point.h1.x(), m_point.h1.y()));
91     painter->drawConvexPolygon(handle.translated(m_point.h2.x(), m_point.h2.y()));
92 #else
93     QPolygonF tmp;
94     tmp = handle;
95     tmp.translate(m_point.h1.x(), m_point.h1.y());
96     painter->drawConvexPolygon(tmp);
97     tmp = handle;
98     tmp.translate(m_point.h2.x(), m_point.h2.y());
99     painter->drawConvexPolygon(tmp);
100 #endif
101
102     painter->drawEllipse(QRectF(m_point.p.x() - handleSizeHalf,
103                                 m_point.p.y() - handleSizeHalf, handleSize, handleSize));
104 }
105
106 int BPointItem::getSelection(QPointF pos)
107 {
108     QRectF mouseArea(pos.x() - 6, pos.y() - 6, 12, 12);
109
110     if (mouseArea.contains(m_point.h1))
111         return 0;
112     else if (mouseArea.contains(m_point.p))
113         return 1;
114     else if (mouseArea.contains(m_point.h2))
115         return 2;
116
117     return -1;
118 }
119
120 void BPointItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
121 {
122     m_selection = getSelection(event->pos());
123
124     if (m_selection < 0) {
125         event->ignore();
126         setSelected(false);
127     } else {
128         if (event->button() == Qt::RightButton && m_selection == 1) {
129             SplineItem *parent = qgraphicsitem_cast<SplineItem*>(parentItem());
130             if (parent) {
131                 parent->removeChild(this);
132                 return;
133             }
134         }
135         setSelected(true);
136     }
137 }
138
139 void BPointItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
140 {
141     prepareGeometryChange();
142     switch (m_selection) {
143     case 0:
144         m_point.setH1(event->pos());
145         break;
146     case 1:
147         m_point.setP(event->pos());
148         break;
149     case 2:
150         m_point.setH2(event->pos());
151         break;
152     }
153
154     if (parentItem()) {
155         SplineItem *parent = qgraphicsitem_cast<SplineItem*>(parentItem());
156         if (parent)
157             parent->updateSpline(true);
158     }
159 }
160
161 void BPointItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
162 {
163     if (parentItem()) {
164         SplineItem *parent = qgraphicsitem_cast<SplineItem*>(parentItem());
165         if (parent)
166             parent->updateSpline(false);
167     }
168     QGraphicsItem::mouseReleaseEvent(event);
169 }
170
171 void BPointItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
172 {
173     if (getSelection(event->pos()) < 0)
174         unsetCursor();
175     else
176         setCursor(QCursor(Qt::PointingHandCursor));
177 }