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