1 /***************************************************************************
2 * Copyright (C) 2010 by Till Theato (root@ttill.de) *
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. *
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. *
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 ***************************************************************************/
20 #include "onmonitorpathitem.h"
21 #include "kdenlivesettings.h"
23 #include <QGraphicsSceneMouseEvent>
25 #include <QStyleOptionGraphicsItem>
27 #include <QGraphicsView>
28 #include <QApplication>
30 #include <mlt++/Mlt.h>
32 OnMonitorPathItem::OnMonitorPathItem(double dar, QGraphicsItem* parent) :
33 QGraphicsPathItem(parent),
39 setFlags(QGraphicsItem::ItemIsMovable);
41 QPen framepen(Qt::SolidLine);
42 framepen.setColor(Qt::red);
44 setBrush(Qt::transparent);
45 setAcceptHoverEvents(true);
48 void OnMonitorPathItem::setPoints(Mlt::Geometry *geometry)
53 Mlt::GeometryItem item;
54 while (!geometry->next_key(&item, pos)) {
55 r = QRectF(item.x(), item.y(), item.w(), item.h());
56 m_points << r.center();
57 pos = item.frame() + 1;
62 void OnMonitorPathItem::rebuildShape() {
63 if (m_activePoint > m_points.count()) m_activePoint = -1;
67 if (!m_points.isEmpty()) {
68 QRectF r(0, 0, 20, 20);
69 r.moveCenter(m_points.at(0));
72 p.moveTo(m_points.at(0));
73 for (int i = 1; i < m_points.count(); i++) {
74 p.lineTo(m_points.at(i));
75 r.moveCenter(m_points.at(i));
79 prepareGeometryChange();
84 void OnMonitorPathItem::getMode(QPointF pos)
88 dist /= m_view->matrix().m11();
90 // Item mapped coordinates
91 for (int i = 0; i < m_points.count(); i++) {
92 if ((pos - m_points.at(i)).manhattanLength() <= dist) {
100 void OnMonitorPathItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
102 /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
107 if (m_activePoint >= 0 && event->buttons() & Qt::LeftButton) {
108 QPointF mousePos = event->pos();
109 m_points[m_activePoint] = mousePos;
117 if (KdenliveSettings::monitorscene_directupdate()) {
126 QList <QPointF> OnMonitorPathItem::points() const
131 void OnMonitorPathItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
140 QRectF OnMonitorPathItem::boundingRect () const
142 return shape().boundingRect();
145 QPainterPath OnMonitorPathItem::shape () const
150 void OnMonitorPathItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
152 if (m_activePoint != -1) {
159 void OnMonitorPathItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
161 setCursor(QCursor(Qt::PointingHandCursor));
164 void OnMonitorPathItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
166 int currentPoint = m_activePoint;
167 getMode(event->pos());
168 if (currentPoint != m_activePoint) update();
169 setCursor(QCursor(Qt::PointingHandCursor));
172 void OnMonitorPathItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
175 QGraphicsPathItem::paint(painter, option, widget);
180 w /= m_view->matrix().m11();
181 h /= m_view->matrix().m22();
184 QRectF handle(0, 0, w, h);
185 for (int i = 0; i < m_points.count(); i++) {
186 handle.moveCenter(m_points.at(i));
187 painter->fillRect(handle, m_activePoint == i ? Qt::blue : pen().color());
191 bool OnMonitorPathItem::getView()
196 if (scene() && scene()->views().count()) {
197 m_view = scene()->views()[0];
204 #include "onmonitorpathitem.moc"