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, MonitorScene *scene, QGraphicsItem* parent) :
29 AbstractOnMonitorItem(scene),
30 QGraphicsRectItem(rect, parent)
32 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
34 QPen framepen(Qt::SolidLine);
35 framepen.setColor(Qt::yellow);
37 setBrush(Qt::transparent);
40 rectActions OnMonitorRectItem::getMode(QPoint pos)
42 pos = mapFromScene(pos).toPoint();
43 // Item mapped coordinates
44 QPolygon pol(rect().normalized().toRect());
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));
55 QPainterPath mouseArea;
56 mouseArea.addRect(pos.x() - 4, pos.y() - 4, 8, 8);
58 // Check for collisions between the mouse and the borders
59 if (mouseArea.contains(pol.point(0)))
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))
69 else if (bottom.intersects(mouseArea))
71 else if (right.intersects(mouseArea))
73 else if (left.intersects(mouseArea))
75 else if (rect().normalized().contains(pos))
81 /*int OnMonitorRectItem::type() const
86 void OnMonitorRectItem::slotMousePressed(QGraphicsSceneMouseEvent* event)
93 m_clickPoint = event->scenePos();
94 m_mode = getMode(m_clickPoint.toPoint());
97 void OnMonitorRectItem::slotMouseMoved(QGraphicsSceneMouseEvent* event)
102 emit requestCursor(QCursor(Qt::ArrowCursor));
106 /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
111 QPointF mousePos = event->scenePos();
113 if (event->buttons() & Qt::LeftButton) {
114 QRectF r = rect().normalized();
116 QPointF mousePosInRect = mapFromScene(mousePos);
117 QPointF diff = mousePos - m_clickPoint;
118 m_clickPoint = mousePos;
121 if (mousePos.x() < p.x() + r.height() && mousePos.y() < p.y() + r.height()) {
122 setRect(r.adjusted(0, 0, -mousePosInRect.x(), -mousePosInRect.y()));
128 if (mousePos.y() < p.y() + r.height()) {
129 r.setBottom(r.height() - mousePosInRect.y());
131 setPos(QPointF(p.x(), mousePos.y()));
136 if (mousePos.x() > p.x() && mousePos.y() < p.y() + r.height()) {
137 r.setBottomRight(QPointF(mousePosInRect.x(), r.bottom() - mousePosInRect.y()));
139 setPos(QPointF(p.x(), mousePos.y()));
144 if (mousePos.x() < p.x() + r.width()) {
145 r.setRight(r.width() - mousePosInRect.x());
147 setPos(QPointF(mousePos.x(), p.y()));
152 if (mousePos.x() > p.x()) {
153 r.setRight(mousePosInRect.x());
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()));
162 setPos(QPointF(mousePos.x(), p.y()));
167 if (mousePos.y() > p.y()) {
168 r.setBottom(mousePosInRect.y());
173 case ResizeBottomRight:
174 if (mousePos.x() > p.x() && mousePos.y() > p.y()) {
175 r.setBottomRight(mousePosInRect);
181 moveBy(diff.x(), diff.y());
188 switch (getMode(event->scenePos().toPoint())) {
190 case ResizeBottomRight:
191 emit requestCursor(QCursor(Qt::SizeFDiagCursor));
194 case ResizeBottomLeft:
195 emit requestCursor(QCursor(Qt::SizeBDiagCursor));
199 emit requestCursor(QCursor(Qt::SizeVerCursor));
203 emit requestCursor(QCursor(Qt::SizeHorCursor));
206 emit requestCursor(QCursor(Qt::OpenHandCursor));
209 emit requestCursor(QCursor(Qt::ArrowCursor));
213 if (m_modified && KdenliveSettings::monitorscene_directupdate()) {
214 emit actionFinished();
219 void OnMonitorRectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
223 painter->setPen(pen());
224 painter->drawRect(option->rect);
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));
236 #include "onmonitorrectitem.moc"