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, double dar, MonitorScene *scene, QGraphicsItem* parent) :
29 AbstractOnMonitorItem(scene),
30 QGraphicsRectItem(rect, parent),
33 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
35 QPen framepen(Qt::SolidLine);
36 framepen.setColor(Qt::yellow);
38 setBrush(Qt::transparent);
41 rectActions OnMonitorRectItem::getMode(QPoint pos)
43 pos = mapFromScene(pos).toPoint();
44 // Item mapped coordinates
45 QPolygon pol(rect().normalized().toRect());
47 QPainterPath top(pol.point(0));
48 top.lineTo(pol.point(1));
49 QPainterPath bottom(pol.point(2));
50 bottom.lineTo(pol.point(3));
51 QPainterPath left(pol.point(0));
52 left.lineTo(pol.point(3));
53 QPainterPath right(pol.point(1));
54 right.lineTo(pol.point(2));
56 QPainterPath mouseArea;
57 mouseArea.addRect(pos.x() - 4, pos.y() - 4, 8, 8);
59 // Check for collisions between the mouse and the borders
60 if (mouseArea.contains(pol.point(0)))
62 else if (mouseArea.contains(pol.point(2)))
63 return ResizeBottomRight;
64 else if (mouseArea.contains(pol.point(1)))
65 return ResizeTopRight;
66 else if (mouseArea.contains(pol.point(3)))
67 return ResizeBottomLeft;
68 else if (top.intersects(mouseArea))
70 else if (bottom.intersects(mouseArea))
72 else if (right.intersects(mouseArea))
74 else if (left.intersects(mouseArea))
76 else if (rect().normalized().contains(pos))
82 /*int OnMonitorRectItem::type() const
87 void OnMonitorRectItem::slotMousePressed(QGraphicsSceneMouseEvent* event)
94 m_lastPoint = event->scenePos();
95 m_oldRect = rect().normalized();
96 m_mode = getMode(m_lastPoint.toPoint());
99 void OnMonitorRectItem::slotMouseMoved(QGraphicsSceneMouseEvent* event)
104 emit requestCursor(QCursor(Qt::ArrowCursor));
108 /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
113 if (event->buttons() & Qt::LeftButton) {
114 QRectF r = rect().normalized();
116 QPointF mousePos = event->scenePos();
117 QPointF mousePosInRect = mapFromScene(mousePos);
118 QPointF diff = mousePos - m_lastPoint;
119 m_lastPoint = mousePos;
123 if (mousePos.x() < p.x() + r.height() && mousePos.y() < p.y() + r.height()) {
124 r.adjust(0, 0, -mousePosInRect.x(), -mousePosInRect.y());
130 if (mousePos.y() < p.y() + r.height()) {
131 r.setBottom(r.height() - mousePosInRect.y());
132 setPos(QPointF(p.x(), mousePos.y()));
137 if (mousePos.x() > p.x() && mousePos.y() < p.y() + r.height()) {
138 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());
146 setPos(QPointF(mousePos.x(), p.y()));
151 if (mousePos.x() > p.x()) {
152 r.setRight(mousePosInRect.x());
156 case ResizeBottomLeft:
157 if (mousePos.x() < p.x() + r.width() && mousePos.y() > p.y()) {
158 r.setBottomRight(QPointF(r.width() - mousePosInRect.x(), mousePosInRect.y()));
159 setPos(QPointF(mousePos.x(), p.y()));
164 if (mousePos.y() > p.y()) {
165 r.setBottom(mousePosInRect.y());
169 case ResizeBottomRight:
170 if (mousePos.x() > p.x() && mousePos.y() > p.y()) {
171 r.setBottomRight(mousePosInRect);
176 moveBy(diff.x(), diff.y());
184 if (event->modifiers() == Qt::ControlModifier) {
185 // compare to rect during mouse press:
186 // if we subtract rect() we'll get a whole lot of flickering
187 // because of diffWidth > diffHeight changing all the time
188 int diffWidth = qAbs(r.width() - m_oldRect.width());
189 int diffHeight = qAbs(r.height() - m_oldRect.height());
191 if (diffHeight != 0 || diffWidth != 0) {
192 if (diffWidth > diffHeight)
193 r.setBottom(r.width() / m_dar);
195 r.setRight(r.height() * m_dar);
197 // the rect's position changed
198 if (p - pos() != QPointF()) {
199 if (diffWidth > diffHeight) {
200 if (m_mode != ResizeBottomLeft)
201 setY(p.y() - r.height() + rect().normalized().height());
203 if (m_mode != ResizeTopRight)
204 setX(p.x() - r.width() + rect().normalized().width());
212 switch (getMode(event->scenePos().toPoint())) {
214 case ResizeBottomRight:
215 emit requestCursor(QCursor(Qt::SizeFDiagCursor));
218 case ResizeBottomLeft:
219 emit requestCursor(QCursor(Qt::SizeBDiagCursor));
223 emit requestCursor(QCursor(Qt::SizeVerCursor));
227 emit requestCursor(QCursor(Qt::SizeHorCursor));
230 emit requestCursor(QCursor(Qt::OpenHandCursor));
233 emit requestCursor(QCursor(Qt::ArrowCursor));
238 if (m_modified && KdenliveSettings::monitorscene_directupdate()) {
239 emit actionFinished();
244 void OnMonitorRectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
248 painter->setPen(pen());
249 painter->drawRect(option->rect);
252 double handleSize = 6 / painter->matrix().m11();
253 double halfHandleSize = handleSize / 2;
254 painter->fillRect(-halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
255 painter->fillRect(option->rect.width() - halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
256 painter->fillRect(option->rect.width() - halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
257 painter->fillRect(-halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
261 #include "onmonitorrectitem.moc"