]> git.sesse.net Git - kdenlive/blob - src/onmonitoritems/onmonitorpathitem.cpp
Const'ref
[kdenlive] / src / onmonitoritems / onmonitorpathitem.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 "onmonitorpathitem.h"
21 #include "kdenlivesettings.h"
22
23 #include <QGraphicsSceneMouseEvent>
24 #include <QPainter>
25 #include <QStyleOptionGraphicsItem>
26 #include <QCursor>
27 #include <QGraphicsView>
28 #include <QApplication>
29
30 #include <mlt++/Mlt.h>
31
32 OnMonitorPathItem::OnMonitorPathItem(double dar, QGraphicsItem* parent) :
33     QGraphicsPathItem(parent),
34     m_dar(dar),
35     m_modified(false),
36     m_view(NULL),
37     m_activePoint(-1)
38 {
39     setFlags(QGraphicsItem::ItemIsMovable);
40
41     QPen framepen(Qt::SolidLine);
42     framepen.setColor(Qt::red);
43     setPen(framepen);
44     setBrush(Qt::transparent);
45     setAcceptHoverEvents(true);
46 }
47
48 void OnMonitorPathItem::setPoints(Mlt::Geometry *geometry)
49 {
50     m_points.clear();
51     QRectF r;
52     int pos = 0;
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;
58     }
59     rebuildShape();
60 }
61
62 void OnMonitorPathItem::rebuildShape()
63 {
64     if (m_activePoint > m_points.count()) m_activePoint = -1;
65     QPainterPath p;
66     QPainterPath shape;
67
68     if (!m_points.isEmpty()) {
69         QRectF r(0, 0, 20, 20);
70         r.moveCenter(m_points.at(0));
71         shape.addRect(r);
72
73         p.moveTo(m_points.at(0));
74         for (int i = 1; i < m_points.count(); ++i) {
75             p.lineTo(m_points.at(i));
76             r.moveCenter(m_points.at(i));
77             shape.addRect(r);
78         }
79     }
80     prepareGeometryChange();
81     m_shape = shape;
82     setPath(p);
83 }
84
85 void OnMonitorPathItem::getMode(const QPointF &pos)
86 {
87     double dist  = 8;
88     if (getView()) {
89         dist /= m_view->matrix().m11();
90     }
91     // Item mapped coordinates
92     for (int i = 0; i < m_points.count(); ++i) {
93         if ((pos - m_points.at(i)).manhattanLength() <= dist) {
94             m_activePoint = i;
95             return;
96         }
97     }
98     m_activePoint = -1;
99 }
100
101 void OnMonitorPathItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
102 {
103     /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
104      *   event->accept();
105      *   return;
106     }*/
107
108     if (m_activePoint >= 0 && event->buttons() & Qt::LeftButton) {
109         QPointF mousePos = event->pos();
110         m_points[m_activePoint] = mousePos;
111         rebuildShape();
112         m_modified = true;
113         update();
114     }
115
116     if (m_modified) {
117         event->accept();
118         if (KdenliveSettings::monitorscene_directupdate()) {
119             emit changed();
120             m_modified = false;
121         }
122     } else {
123         event->ignore();
124     }
125 }
126
127 QList <QPointF> OnMonitorPathItem::points() const
128 {
129     return m_points;
130 }
131
132 void OnMonitorPathItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
133 {
134     if (m_modified) {
135         m_modified = false;
136         emit changed();
137     }
138     event->accept();
139 }
140
141 QRectF OnMonitorPathItem::boundingRect () const
142 {
143     return shape().boundingRect();
144 }
145
146 QPainterPath OnMonitorPathItem::shape () const
147 {
148     return m_shape;
149 }
150
151 void OnMonitorPathItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
152 {
153     if (m_activePoint != -1) {
154         m_activePoint = -1;
155         update();
156     }
157     unsetCursor();
158 }
159
160 void OnMonitorPathItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
161 {
162     setCursor(QCursor(Qt::PointingHandCursor));
163 }
164
165 void OnMonitorPathItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
166 {
167     int currentPoint = m_activePoint;
168     getMode(event->pos());
169     if (currentPoint != m_activePoint) update();
170     setCursor(QCursor(Qt::PointingHandCursor));
171 }
172
173 void OnMonitorPathItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
174 {
175     //Q_UNUSED(widget)
176     QGraphicsPathItem::paint(painter, option, widget);
177
178     double w = 6;
179     double h = 6;
180     if (getView()) {
181         w /= m_view->matrix().m11();
182         h /= m_view->matrix().m22();
183     }
184
185     QRectF handle(0, 0, w, h);
186     for (int i = 0; i < m_points.count(); ++i) {
187         handle.moveCenter(m_points.at(i));
188         painter->fillRect(handle, m_activePoint == i ? Qt::blue : pen().color());
189     }
190 }
191
192 bool OnMonitorPathItem::getView()
193 {
194     if (m_view)
195         return true;
196
197     if (scene() && !scene()->views().isEmpty()) {
198         m_view = scene()->views().first();
199         return true;
200     } else {
201         return false;
202     }
203 }
204
205 #include "onmonitorpathitem.moc"