]> git.sesse.net Git - kdenlive/blob - src/onmonitoritems/onmonitorcornersitem.cpp
Make c0rners keyframable
[kdenlive] / src / onmonitoritems / onmonitorcornersitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20 #include "onmonitorcornersitem.h"
21 #include "kdenlivesettings.h"
22
23 #include <algorithm>
24
25 #include <QGraphicsSceneMouseEvent>
26 #include <QPainter>
27 #include <QStyleOptionGraphicsItem>
28 #include <QCursor>
29
30 OnMonitorCornersItem::OnMonitorCornersItem(MonitorScene* scene, QGraphicsItem* parent) :
31         AbstractOnMonitorItem(scene),
32         QGraphicsPolygonItem(parent),
33         m_selectedCorner(-1)
34 {
35     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
36
37     QPen framepen(Qt::SolidLine);
38     framepen.setColor(Qt::yellow);
39     setPen(framepen);
40     setBrush(Qt::NoBrush);
41 }
42
43 OnMonitorCornersItem::cornersActions OnMonitorCornersItem::getMode(QPointF pos, int *corner)
44 {
45     *corner = -1;
46     if (polygon().count() != 4)
47         return NoAction;
48
49     QPainterPath mouseArea;
50     pos = mapFromScene(pos);
51     mouseArea.addRect(pos.x() - 6, pos.y() - 6, 12, 12);
52     for (int i = 0; i < 4; ++i) {
53         if (mouseArea.contains(polygon().at(i))) {
54             *corner = i;
55             return Corner;
56         }
57     }
58     if (KdenliveSettings::onmonitoreffects_cornersshowcontrols()) {
59         if (mouseArea.contains(getCentroid()))
60             return Move;
61
62         int j;
63         for (int i = 0; i < 4; ++i) {
64             j = (i + 1) % 4;
65             if (mouseArea.contains(QLineF(polygon().at(i), polygon().at(j)).pointAt(.5))) {
66                 *corner = i;
67                 return MoveSide;
68             }
69         }
70     }
71
72     return NoAction;
73 }
74
75 void OnMonitorCornersItem::slotMousePressed(QGraphicsSceneMouseEvent* event)
76 {
77     event->accept();
78
79     if (!isEnabled())
80         return;
81
82     m_mode = getMode(event->scenePos(), &m_selectedCorner);
83     m_lastPoint = event->scenePos();
84 }
85
86 void OnMonitorCornersItem::slotMouseMoved(QGraphicsSceneMouseEvent* event)
87 {
88     event->accept();
89
90     if (!isEnabled()) {
91         emit requestCursor(QCursor(Qt::ArrowCursor));
92         return;
93     }
94
95     /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
96      *   event->accept();
97      *   return;
98     }*/
99
100     if (event->buttons() & Qt::LeftButton) {
101         QPointF mousePos = mapFromScene(event->scenePos());
102         QPolygonF p = polygon();
103         switch (m_mode) {
104         case Corner:
105             p.replace(m_selectedCorner, mousePos);
106             m_modified = true;
107             break;
108         case Move:
109             p.translate(mousePos - m_lastPoint);
110             m_modified = true;
111             break;
112         case MoveSide:
113             p[m_selectedCorner] += mousePos - m_lastPoint;
114             p[(m_selectedCorner + 1) % 4] += mousePos - m_lastPoint;
115             m_modified = true;
116             break;
117         default:
118             break;
119         }
120         m_lastPoint = mousePos;
121         setPolygon(p);
122     } else {
123         int corner;
124         switch (getMode(event->scenePos(), &corner)) {
125         case NoAction:
126             emit requestCursor(QCursor(Qt::ArrowCursor));
127             break;
128         case Move:
129             emit requestCursor(QCursor(Qt::SizeAllCursor));
130             break;
131         default:
132             emit requestCursor(QCursor(Qt::OpenHandCursor));
133             break;
134         }
135     }
136     if (m_modified && KdenliveSettings::monitorscene_directupdate()) {
137         emit actionFinished();
138         m_modified = false;
139     }
140 }
141
142 void OnMonitorCornersItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
143 {
144     painter->setPen(QPen(Qt::yellow, 1, Qt::SolidLine));
145
146     if (KdenliveSettings::onmonitoreffects_cornersshowlines())
147         QGraphicsPolygonItem::paint(painter, option, widget);
148
149     if (polygon().count() != 4)
150         return;
151
152     double baseSize = 1 / painter->matrix().m11();
153     painter->setRenderHint(QPainter::Antialiasing);
154     painter->setBrush(QBrush(Qt::yellow));
155     double handleSize = 4  * baseSize;
156     for (int i = 0; i < 4; ++i)
157         painter->drawEllipse(polygon().at(i), handleSize, handleSize);
158
159     if (KdenliveSettings::onmonitoreffects_cornersshowcontrols()) {
160         painter->setPen(QPen(Qt::red, 2, Qt::SolidLine));
161         double toolSize = 6 * baseSize;
162         // move tool
163         QPointF c = getCentroid();
164         painter->drawLine(QLineF(c - QPointF(toolSize, toolSize), c + QPointF(toolSize, toolSize)));
165         painter->drawLine(QLineF(c - QPointF(-toolSize, toolSize), c + QPointF(-toolSize, toolSize)));
166
167         // move side tools (2 corners at once)
168         int j;
169         for (int i = 0; i < 4; ++i) {
170             j = (i + 1) % 4;
171             QPointF m = QLineF(polygon().at(i), polygon().at(j)).pointAt(.5);
172             painter->drawRect(QRectF(-toolSize / 2., -toolSize / 2., toolSize, toolSize).translated(m));
173         }
174     }
175 }
176
177 QPointF OnMonitorCornersItem::getCentroid()
178 {
179     QList <QPointF> p = sortedClockwise();
180
181     /*
182      * See: http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
183      */
184
185     double A = 0;
186     int i, j;
187     for (i = 0; i < 4; ++i) {
188         j = (i + 1) % 4;
189         A += p[j].x() * p[i].y() - p[i].x() * p[j].y();
190     }
191     A /= 2.;
192     A = qAbs(A);
193
194     double x = 0, y = 0, f;
195     for (i = 0; i < 4; ++i) {
196         j = (i + 1) % 4;
197         f = (p[i].x() * p[j].y() - p[j].x() * p[i].y());
198         x += f * (p[i].x() + p[j].x());
199         y += f * (p[i].y() + p[j].y());
200     }
201     x /= 6*A;
202     y /= 6*A;
203     return QPointF(x, y);
204 }
205
206 QList <QPointF> OnMonitorCornersItem::sortedClockwise()
207 {
208     QList <QPointF> points = polygon().toList();
209     QPointF& a = points[0];
210     QPointF& b = points[1];
211     QPointF& c = points[2];
212     QPointF& d = points[3];
213
214     /*
215      * http://stackoverflow.com/questions/242404/sort-four-points-in-clockwise-order
216      */
217
218     double abc = a.x() * b.y() - a.y() * b.x() + b.x() * c.y() - b.y() * c.x() + c.x() * a.y() - c.y() * a.x();
219     if (abc > 0) {
220         double acd = a.x() * c.y() - a.y() * c.x() + c.x() * d.y() - c.y() * d.x() + d.x() * a.y() - d.y() * a.x();
221         if (acd > 0) {
222             ;
223         } else {
224             double abd = a.x() * b.y() - a.y() * b.x() + b.x() * d.y() - b.y() * d.x() + d.x() * a.y() - d.y() * a.x();
225             if (abd > 0) {
226                 std::swap(d, c);
227             } else{
228                 std::swap(a, d);
229             }
230         }
231     } else {
232         double acd = a.x() * c.y() - a.y() * c.x() + c.x() * d.y() - c.y() * d.x() + d.x() * a.y() - d.y() * a.x();
233         if (acd > 0) {
234             double abd = a.x() * b.y() - a.y() * b.x() + b.x() * d.y() - b.y() * d.x() + d.x() * a.y() - d.y() * a.x();
235             if (abd > 0) {
236                 std::swap(b, c);
237             } else {
238                 std::swap(a, b);
239             }
240         } else {
241             std::swap(a, c);
242         }
243     }
244     return points;
245 }
246
247 #include "onmonitorcornersitem.moc"