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 "onmonitorrectitem.h"
21 #include "kdenlivesettings.h"
23 #include <QGraphicsSceneMouseEvent>
25 #include <QStyleOptionGraphicsItem>
28 OnMonitorRectItem::OnMonitorRectItem(const QRectF &rect, QGraphicsItem* parent) :
29 QGraphicsRectItem(rect, parent)
31 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
33 QPen framepen(Qt::SolidLine);
34 framepen.setColor(Qt::yellow);
36 setBrush(Qt::transparent);
39 rectActions OnMonitorRectItem::getMode(QPoint pos)
41 pos = mapFromScene(pos).toPoint();
42 // Item mapped coordinates
43 QPolygon pol(rect().normalized().toRect());
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));
54 QPainterPath mouseArea;
55 mouseArea.addRect(pos.x() - 4, pos.y() - 4, 8, 8);
57 // Check for collisions between the mouse and the borders
58 if (mouseArea.contains(pol.point(0)))
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))
68 else if (bottom.intersects(mouseArea))
70 else if (right.intersects(mouseArea))
72 else if (left.intersects(mouseArea))
74 else if (rect().normalized().contains(pos))
80 int OnMonitorRectItem::type() const
85 void OnMonitorRectItem::setEnabled(bool enabled)
90 void OnMonitorRectItem::slotMousePressed(QGraphicsSceneMouseEvent* event)
95 m_clickPoint = event->scenePos();
96 m_mode = getMode(m_clickPoint.toPoint());
99 void OnMonitorRectItem::slotMouseReleased(QGraphicsSceneMouseEvent* event)
103 emit actionFinished();
109 void OnMonitorRectItem::slotMouseMoved(QGraphicsSceneMouseEvent* event)
112 emit setCursor(QCursor(Qt::ArrowCursor));
116 /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
121 QPointF mousePos = event->scenePos();
123 if (event->buttons() & Qt::LeftButton) {
124 QRectF r = rect().normalized();
126 QPointF mousePosInRect = mapFromScene(mousePos);
129 if (mousePos.x() < p.x() + r.height() && mousePos.y() < p.y() + r.height()) {
130 setRect(r.adjusted(0, 0, -mousePosInRect.x(), -mousePosInRect.y()));
136 if (mousePos.y() < p.y() + r.height()) {
137 r.setBottom(r.height() - mousePosInRect.y());
139 setPos(QPointF(p.x(), mousePos.y()));
144 if (mousePos.x() > p.x() && mousePos.y() < p.y() + r.height()) {
145 r.setBottomRight(QPointF(mousePosInRect.x(), r.bottom() - mousePosInRect.y()));
147 setPos(QPointF(p.x(), mousePos.y()));
152 if (mousePos.x() < p.x() + r.width()) {
153 r.setRight(r.width() - mousePosInRect.x());
155 setPos(QPointF(mousePos.x(), p.y()));
160 if (mousePos.x() > p.x()) {
161 r.setRight(mousePosInRect.x());
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()));
170 setPos(QPointF(mousePos.x(), p.y()));
175 if (mousePos.y() > p.y()) {
176 r.setBottom(mousePosInRect.y());
181 case ResizeBottomRight:
182 if (mousePos.x() > p.x() && mousePos.y() > p.y()) {
183 r.setBottomRight(mousePosInRect);
189 QPointF diff = mousePos - m_clickPoint;
190 m_clickPoint = mousePos;
191 moveBy(diff.x(), diff.y());
196 switch (getMode(event->scenePos().toPoint())) {
198 case ResizeBottomRight:
199 emit setCursor(QCursor(Qt::SizeFDiagCursor));
202 case ResizeBottomLeft:
203 emit setCursor(QCursor(Qt::SizeBDiagCursor));
207 emit setCursor(QCursor(Qt::SizeVerCursor));
211 emit setCursor(QCursor(Qt::SizeHorCursor));
214 emit setCursor(QCursor(Qt::OpenHandCursor));
217 emit setCursor(QCursor(Qt::ArrowCursor));
221 if (m_modified && KdenliveSettings::monitorscene_directupdate()) {
222 emit actionFinished();
229 void OnMonitorRectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
231 painter->setPen(pen());
232 painter->drawRect(option->rect);
235 double handleSize = 6 / painter->matrix().m11();
236 double halfHandleSize = handleSize / 2;
237 painter->fillRect(-halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
238 painter->fillRect(option->rect.width() - halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
239 painter->fillRect(option->rect.width() - halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
240 painter->fillRect(-halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
246 #include "onmonitorrectitem.moc"