+}
+
+void OnMonitorCornersItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
+{
+ painter->setPen(QPen(Qt::yellow, 1, Qt::SolidLine));
+
+ if (KdenliveSettings::onmonitoreffects_cornersshowlines())
+ QGraphicsPolygonItem::paint(painter, option, widget);
+
+ if (polygon().count() != 4)
+ return;
+
+ double baseSize = 1 / painter->worldTransform().m11();
+ painter->setRenderHint(QPainter::Antialiasing);
+ painter->setBrush(QBrush(isEnabled() ? Qt::yellow : Qt::red));
+ double handleSize = 4 * baseSize;
+ for (int i = 0; i < 4; ++i)
+ painter->drawEllipse(polygon().at(i), handleSize, handleSize);
+
+ if (KdenliveSettings::onmonitoreffects_cornersshowcontrols() && isEnabled()) {
+ painter->setPen(QPen(Qt::red, 2, Qt::SolidLine));
+ double toolSize = 6 * baseSize;
+ // move tool
+ QPointF c = getCentroid();
+ painter->drawLine(QLineF(c - QPointF(toolSize, toolSize), c + QPointF(toolSize, toolSize)));
+ painter->drawLine(QLineF(c - QPointF(-toolSize, toolSize), c + QPointF(-toolSize, toolSize)));
+
+ // move side tools (2 corners at once)
+ int j;
+ for (int i = 0; i < 4; ++i) {
+ j = (i + 1) % 4;
+ QPointF m = QLineF(polygon().at(i), polygon().at(j)).pointAt(.5);
+ painter->drawRect(QRectF(-toolSize / 2., -toolSize / 2., toolSize, toolSize).translated(m));
+ }
+ }
+}
+
+QPointF OnMonitorCornersItem::getCentroid()
+{
+ QList <QPointF> p = sortedClockwise();
+
+ /*
+ * See: http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
+ */
+
+ double A = 0;
+ int i, j;
+ for (i = 0; i < 4; ++i) {
+ j = (i + 1) % 4;
+ A += p[j].x() * p[i].y() - p[i].x() * p[j].y();
+ }
+ A /= 2.;
+ A = qAbs(A);
+
+ double x = 0, y = 0, f;
+ for (i = 0; i < 4; ++i) {
+ j = (i + 1) % 4;
+ f = (p[i].x() * p[j].y() - p[j].x() * p[i].y());
+ x += f * (p[i].x() + p[j].x());
+ y += f * (p[i].y() + p[j].y());
+ }
+ x /= 6*A;
+ y /= 6*A;
+ return QPointF(x, y);
+}
+
+QList <QPointF> OnMonitorCornersItem::sortedClockwise()
+{
+ QList <QPointF> points = polygon().toList();
+ QPointF& a = points[0];
+ QPointF& b = points[1];
+ QPointF& c = points[2];
+ QPointF& d = points[3];
+
+ /*
+ * http://stackoverflow.com/questions/242404/sort-four-points-in-clockwise-order
+ */
+
+ 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();
+ if (abc > 0) {
+ 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();
+ if (acd > 0) {
+ ;
+ } else {
+ 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();
+ if (abd > 0) {
+ std::swap(d, c);
+ } else{
+ std::swap(a, d);
+ }
+ }
+ } else {
+ 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();
+ if (acd > 0) {
+ 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();
+ if (abd > 0) {
+ std::swap(b, c);
+ } else {
+ std::swap(a, b);
+ }
+ } else {
+ std::swap(a, c);
+ }
+ }
+ return points;
+}
+
+bool OnMonitorCornersItem::getView()
+{
+ if (m_view)
+ return true;
+
+ if (scene() && !scene()->views().isEmpty()) {
+ m_view = scene()->views().first();
+ return true;
+ } else {
+ return false;
+ }
+}