]> git.sesse.net Git - kdenlive/blob - src/onmonitoritems/onmonitorrectitem.cpp
Add OnMonitorRectItem to separate rect move and resize code from MonitorScene
[kdenlive] / src / onmonitoritems / onmonitorrectitem.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 "onmonitorrectitem.h"
21 #include "kdenlivesettings.h"
22
23 #include <QGraphicsSceneMouseEvent>
24 #include <QPainter>
25 #include <QStyleOptionGraphicsItem>
26 #include <QCursor>
27
28 OnMonitorRectItem::OnMonitorRectItem(const QRectF &rect, QGraphicsItem* parent) :
29         QGraphicsRectItem(rect, parent)
30 {
31     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
32
33     QPen framepen(Qt::SolidLine);
34     framepen.setColor(Qt::yellow);
35     setPen(framepen);
36     setBrush(Qt::transparent);
37 }
38
39 rectActions OnMonitorRectItem::getMode(QPoint pos)
40 {
41     pos = mapFromScene(pos).toPoint();
42     // Item mapped coordinates
43     QPolygon pol(rect().normalized().toRect());
44
45     QPainterPath top(pol.point(0));
46     top.lineTo(pol.point(1));
47     QPainterPath bottom(pol.point(2));
48     bottom.lineTo(pol.point(3));
49     QPainterPath left(pol.point(0));
50     left.lineTo(pol.point(3));
51     QPainterPath right(pol.point(1));
52     right.lineTo(pol.point(2));
53
54     QPainterPath mouseArea;
55     mouseArea.addRect(pos.x() - 4, pos.y() - 4, 8, 8);
56
57     // Check for collisions between the mouse and the borders
58     if (mouseArea.contains(pol.point(0)))
59         return ResizeTopLeft;
60     else if (mouseArea.contains(pol.point(2)))
61         return ResizeBottomRight;
62     else if (mouseArea.contains(pol.point(1)))
63         return ResizeTopRight;
64     else if (mouseArea.contains(pol.point(3)))
65         return ResizeBottomLeft;
66     else if (top.intersects(mouseArea))
67         return ResizeTop;
68     else if (bottom.intersects(mouseArea))
69         return ResizeBottom;
70     else if (right.intersects(mouseArea))
71         return ResizeRight;
72     else if (left.intersects(mouseArea))
73         return ResizeLeft;
74     else if (rect().normalized().contains(pos))
75         return Move;
76     else
77         return NoAction;
78 }
79
80 int OnMonitorRectItem::type() const
81 {
82     return Type;
83 }
84
85 void OnMonitorRectItem::setEnabled(bool enabled)
86 {
87     m_enabled = enabled;
88 }
89
90 void OnMonitorRectItem::slotMousePressed(QGraphicsSceneMouseEvent* event)
91 {
92     if (!m_enabled)
93         return;
94
95     m_clickPoint = event->scenePos();
96     m_mode = getMode(m_clickPoint.toPoint());
97 }
98
99 void OnMonitorRectItem::slotMouseReleased(QGraphicsSceneMouseEvent* event)
100 {
101     if (m_modified) {
102         m_modified = false;
103         emit actionFinished();
104     }
105
106     event->accept();
107 }
108
109 void OnMonitorRectItem::slotMouseMoved(QGraphicsSceneMouseEvent* event)
110 {
111     if (!m_enabled) {
112         emit setCursor(QCursor(Qt::ArrowCursor));
113         return;
114     }
115
116     /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
117      *   event->accept();
118      *   return;
119     }*/
120
121     QPointF mousePos = event->scenePos();
122
123     if (event->buttons() & Qt::LeftButton) {
124         QRectF r = rect().normalized();
125         QPointF p = pos();
126         QPointF mousePosInRect = mapFromScene(mousePos);
127         switch (m_mode) {
128         case ResizeTopLeft:
129             if (mousePos.x() < p.x() + r.height() && mousePos.y() < p.y() + r.height()) {
130                 setRect(r.adjusted(0, 0, -mousePosInRect.x(), -mousePosInRect.y()));
131                 setPos(mousePos);
132                 m_modified = true;
133             }
134             break;
135         case ResizeTop:
136             if (mousePos.y() < p.y() + r.height()) {
137                 r.setBottom(r.height() - mousePosInRect.y());
138                 setRect(r);
139                 setPos(QPointF(p.x(), mousePos.y()));
140                 m_modified = true;
141             }
142             break;
143         case ResizeTopRight:
144             if (mousePos.x() > p.x() && mousePos.y() < p.y() + r.height()) {
145                 r.setBottomRight(QPointF(mousePosInRect.x(), r.bottom() - mousePosInRect.y()));
146                 setRect(r);
147                 setPos(QPointF(p.x(), mousePos.y()));
148                 m_modified = true;
149             }
150             break;
151         case ResizeLeft:
152             if (mousePos.x() < p.x() + r.width()) {
153                 r.setRight(r.width() - mousePosInRect.x());
154                 setRect(r);
155                 setPos(QPointF(mousePos.x(), p.y()));
156                 m_modified = true;
157             }
158             break;
159         case ResizeRight:
160             if (mousePos.x() > p.x()) {
161                 r.setRight(mousePosInRect.x());
162                 setRect(r);
163                 m_modified = true;
164             }
165             break;
166         case ResizeBottomLeft:
167             if (mousePos.x() < p.x() + r.width() && mousePos.y() > p.y()) {
168                 r.setBottomRight(QPointF(r.width() - mousePosInRect.x(), mousePosInRect.y()));
169                 setRect(r);
170                 setPos(QPointF(mousePos.x(), p.y()));
171                 m_modified = true;
172             }
173             break;
174         case ResizeBottom:
175             if (mousePos.y() > p.y()) {
176                 r.setBottom(mousePosInRect.y());
177                 setRect(r);
178                 m_modified = true;
179             }
180             break;
181         case ResizeBottomRight:
182             if (mousePos.x() > p.x() && mousePos.y() > p.y()) {
183                 r.setBottomRight(mousePosInRect);
184                 setRect(r);
185                 m_modified = true;
186             }
187             break;
188         case Move:
189             QPointF diff = mousePos - m_clickPoint;
190             m_clickPoint = mousePos;
191             moveBy(diff.x(), diff.y());
192             m_modified = true;
193             break;
194         }
195     } else {
196         switch (getMode(event->scenePos().toPoint())) {
197         case ResizeTopLeft:
198         case ResizeBottomRight:
199             emit setCursor(QCursor(Qt::SizeFDiagCursor));
200             break;
201         case ResizeTopRight:
202         case ResizeBottomLeft:
203             emit setCursor(QCursor(Qt::SizeBDiagCursor));
204             break;
205         case ResizeTop:
206         case ResizeBottom:
207             emit setCursor(QCursor(Qt::SizeVerCursor));
208             break;
209         case ResizeLeft:
210         case ResizeRight:
211             emit setCursor(QCursor(Qt::SizeHorCursor));
212             break;
213         case Move:
214             emit setCursor(QCursor(Qt::OpenHandCursor));
215             break;
216         default:
217             emit setCursor(QCursor(Qt::ArrowCursor));
218             break;
219         }
220     }
221     if (m_modified && KdenliveSettings::monitorscene_directupdate()) {
222         emit actionFinished();
223         m_modified = false;
224     }
225
226     event->accept();
227 }
228
229 void OnMonitorRectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
230 {
231     QGraphicsRectItem::paint(painter, option, widget);
232
233     if (m_enabled) {
234         double handleSize = 6 / painter->matrix().m11();
235         double halfHandleSize = handleSize / 2;
236         painter->fillRect(-halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
237         painter->fillRect(option->rect.width() - halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
238         painter->fillRect(option->rect.width() - halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
239         painter->fillRect(-halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
240     }
241 }
242
243
244
245 #include "onmonitorrectitem.moc"