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>
27 #include <QGraphicsView>
29 OnMonitorRectItem::OnMonitorRectItem(const QRectF &rect, double dar, QGraphicsItem* parent) :
30 QGraphicsRectItem(rect, parent),
35 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
37 QPen framepen(Qt::SolidLine);
38 framepen.setColor(Qt::yellow);
40 setBrush(Qt::transparent);
41 setAcceptHoverEvents(true);
44 rectActions OnMonitorRectItem::getMode(QPointF pos)
46 // Item mapped coordinates
47 QPolygonF pol(rect().normalized());
49 QPainterPath top(pol.at(0));
50 top.lineTo(pol.at(1));
51 QPainterPath bottom(pol.at(2));
52 bottom.lineTo(pol.at(3));
53 QPainterPath left(pol.at(0));
54 left.lineTo(pol.at(3));
55 QPainterPath right(pol.at(1));
56 right.lineTo(pol.at(2));
58 QPainterPath mouseArea;
61 size /= m_view->matrix().m11();
62 mouseArea.addRect(pos.x() - size / 2, pos.y() - size / 2, size, size);
64 // Check for collisions between the mouse and the borders
65 if (mouseArea.contains(pol.at(0)))
67 else if (mouseArea.contains(pol.at(2)))
68 return ResizeBottomRight;
69 else if (mouseArea.contains(pol.at(1)))
70 return ResizeTopRight;
71 else if (mouseArea.contains(pol.at(3)))
72 return ResizeBottomLeft;
73 else if (top.intersects(mouseArea))
75 else if (bottom.intersects(mouseArea))
77 else if (right.intersects(mouseArea))
79 else if (left.intersects(mouseArea))
81 else if (rect().normalized().contains(pos))
87 void OnMonitorRectItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
91 m_lastPoint = event->scenePos();
92 m_oldRect = rect().normalized();
93 m_mode = getMode(event->pos());
96 void OnMonitorRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
98 /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
103 if (event->buttons() & Qt::LeftButton) {
104 QRectF r = rect().normalized();
106 QPointF mousePos = event->scenePos();
107 QPointF mousePosInRect = event->pos();
108 QPointF diff = mousePos - m_lastPoint;
109 m_lastPoint = mousePos;
113 if (mousePos.x() < p.x() + r.height() && mousePos.y() < p.y() + r.height()) {
114 r.adjust(0, 0, -mousePosInRect.x(), -mousePosInRect.y());
120 if (mousePos.y() < p.y() + r.height()) {
121 r.setBottom(r.height() - mousePosInRect.y());
122 setPos(QPointF(p.x(), mousePos.y()));
127 if (mousePos.x() > p.x() && mousePos.y() < p.y() + r.height()) {
128 r.setBottomRight(QPointF(mousePosInRect.x(), r.bottom() - mousePosInRect.y()));
129 setPos(QPointF(p.x(), mousePos.y()));
134 if (mousePos.x() < p.x() + r.width()) {
135 r.setRight(r.width() - mousePosInRect.x());
136 setPos(QPointF(mousePos.x(), p.y()));
141 if (mousePos.x() > p.x()) {
142 r.setRight(mousePosInRect.x());
146 case ResizeBottomLeft:
147 if (mousePos.x() < p.x() + r.width() && mousePos.y() > p.y()) {
148 r.setBottomRight(QPointF(r.width() - mousePosInRect.x(), mousePosInRect.y()));
149 setPos(QPointF(mousePos.x(), p.y()));
154 if (mousePos.y() > p.y()) {
155 r.setBottom(mousePosInRect.y());
159 case ResizeBottomRight:
160 if (mousePos.x() > p.x() && mousePos.y() > p.y()) {
161 r.setBottomRight(mousePosInRect);
166 moveBy(diff.x(), diff.y());
174 if (event->modifiers() == Qt::ControlModifier) {
175 // compare to rect during mouse press:
176 // if we subtract rect() we'll get a whole lot of flickering
177 // because of diffWidth > diffHeight changing all the time
178 int diffWidth = qAbs(r.width() - m_oldRect.width());
179 int diffHeight = qAbs(r.height() - m_oldRect.height());
181 if (diffHeight != 0 || diffWidth != 0) {
182 if (diffWidth > diffHeight)
183 r.setBottom(r.width() / m_dar);
185 r.setRight(r.height() * m_dar);
187 // the rect's position changed
188 if (p - pos() != QPointF()) {
189 if (diffWidth > diffHeight) {
190 if (m_mode != ResizeBottomLeft)
191 #if QT_VERSION >= 0x040600
192 setY(p.y() - r.height() + rect().normalized().height());
194 setPos(x(), p.y() - r.height() + rect().normalized().height());
197 if (m_mode != ResizeTopRight)
198 #if QT_VERSION >= 0x040600
199 setX(p.x() - r.width() + rect().normalized().width());
201 setPos(p.x() - r.width() + rect().normalized().width(), y());
213 if (KdenliveSettings::monitorscene_directupdate()) {
222 void OnMonitorRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
231 void OnMonitorRectItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
233 switch (getMode(event->pos())) {
235 case ResizeBottomRight:
236 setCursor(QCursor(Qt::SizeFDiagCursor));
239 case ResizeBottomLeft:
240 setCursor(QCursor(Qt::SizeBDiagCursor));
244 setCursor(QCursor(Qt::SizeVerCursor));
248 setCursor(QCursor(Qt::SizeHorCursor));
251 setCursor(QCursor(Qt::OpenHandCursor));
259 void OnMonitorRectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
263 painter->setPen(pen());
264 painter->drawRect(option->rect);
267 double handleSize = 6 / painter->worldTransform().m11();
268 double halfHandleSize = handleSize / 2;
269 painter->fillRect(-halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
270 painter->fillRect(option->rect.width() - halfHandleSize, -halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
271 painter->fillRect(option->rect.width() - halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
272 painter->fillRect(-halfHandleSize, option->rect.height() - halfHandleSize, handleSize, handleSize, QColor(Qt::yellow));
276 bool OnMonitorRectItem::getView()
281 if (scene() && scene()->views().count()) {
282 m_view = scene()->views()[0];
289 #include "onmonitorrectitem.moc"