]> git.sesse.net Git - kdenlive/blob - src/onmonitoritems/onmonitorrectitem.cpp
Fix some compilation warnings
[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         QPointF diff = mousePos - m_clickPoint;
128         m_clickPoint = mousePos;
129         switch (m_mode) {
130         case ResizeTopLeft:
131             if (mousePos.x() < p.x() + r.height() && mousePos.y() < p.y() + r.height()) {
132                 setRect(r.adjusted(0, 0, -mousePosInRect.x(), -mousePosInRect.y()));
133                 setPos(mousePos);
134                 m_modified = true;
135             }
136             break;
137         case ResizeTop:
138             if (mousePos.y() < p.y() + r.height()) {
139                 r.setBottom(r.height() - mousePosInRect.y());
140                 setRect(r);
141                 setPos(QPointF(p.x(), mousePos.y()));
142                 m_modified = true;
143             }
144             break;
145         case ResizeTopRight:
146             if (mousePos.x() > p.x() && mousePos.y() < p.y() + r.height()) {
147                 r.setBottomRight(QPointF(mousePosInRect.x(), r.bottom() - mousePosInRect.y()));
148                 setRect(r);
149                 setPos(QPointF(p.x(), mousePos.y()));
150                 m_modified = true;
151             }
152             break;
153         case ResizeLeft:
154             if (mousePos.x() < p.x() + r.width()) {
155                 r.setRight(r.width() - mousePosInRect.x());
156                 setRect(r);
157                 setPos(QPointF(mousePos.x(), p.y()));
158                 m_modified = true;
159             }
160             break;
161         case ResizeRight:
162             if (mousePos.x() > p.x()) {
163                 r.setRight(mousePosInRect.x());
164                 setRect(r);
165                 m_modified = true;
166             }
167             break;
168         case ResizeBottomLeft:
169             if (mousePos.x() < p.x() + r.width() && mousePos.y() > p.y()) {
170                 r.setBottomRight(QPointF(r.width() - mousePosInRect.x(), mousePosInRect.y()));
171                 setRect(r);
172                 setPos(QPointF(mousePos.x(), p.y()));
173                 m_modified = true;
174             }
175             break;
176         case ResizeBottom:
177             if (mousePos.y() > p.y()) {
178                 r.setBottom(mousePosInRect.y());
179                 setRect(r);
180                 m_modified = true;
181             }
182             break;
183         case ResizeBottomRight:
184             if (mousePos.x() > p.x() && mousePos.y() > p.y()) {
185                 r.setBottomRight(mousePosInRect);
186                 setRect(r);
187                 m_modified = true;
188             }
189             break;
190         case Move:
191             moveBy(diff.x(), diff.y());
192             m_modified = true;
193             break;
194         default:
195             break;
196         }
197     } else {
198         switch (getMode(event->scenePos().toPoint())) {
199         case ResizeTopLeft:
200         case ResizeBottomRight:
201             emit setCursor(QCursor(Qt::SizeFDiagCursor));
202             break;
203         case ResizeTopRight:
204         case ResizeBottomLeft:
205             emit setCursor(QCursor(Qt::SizeBDiagCursor));
206             break;
207         case ResizeTop:
208         case ResizeBottom:
209             emit setCursor(QCursor(Qt::SizeVerCursor));
210             break;
211         case ResizeLeft:
212         case ResizeRight:
213             emit setCursor(QCursor(Qt::SizeHorCursor));
214             break;
215         case Move:
216             emit setCursor(QCursor(Qt::OpenHandCursor));
217             break;
218         default:
219             emit setCursor(QCursor(Qt::ArrowCursor));
220             break;
221         }
222     }
223     if (m_modified && KdenliveSettings::monitorscene_directupdate()) {
224         emit actionFinished();
225         m_modified = false;
226     }
227
228     event->accept();
229 }
230
231 void OnMonitorRectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
232 {
233     Q_UNUSED(widget);
234
235     painter->setPen(pen());
236     painter->drawRect(option->rect);
237
238     if (m_enabled) {
239         double handleSize = 6 / painter->matrix().m11();
240         double halfHandleSize = handleSize / 2;
241         painter->fillRect(-halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
242         painter->fillRect(option->rect.width() - halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
243         painter->fillRect(option->rect.width() - halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
244         painter->fillRect(-halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
245     }
246 }
247
248
249
250 #include "onmonitorrectitem.moc"