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)
36 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
38 QPen framepen(Qt::SolidLine);
39 framepen.setColor(Qt::yellow);
41 setBrush(Qt::transparent);
42 setAcceptHoverEvents(true);
45 rectActions OnMonitorRectItem::getMode(QPointF pos)
47 // Item mapped coordinates
48 QPolygonF pol(rect().normalized());
50 QPainterPath top(pol.at(0));
51 top.lineTo(pol.at(1));
52 QPainterPath bottom(pol.at(2));
53 bottom.lineTo(pol.at(3));
54 QPainterPath left(pol.at(0));
55 left.lineTo(pol.at(3));
56 QPainterPath right(pol.at(1));
57 right.lineTo(pol.at(2));
59 QPainterPath mouseArea;
63 xsize /= m_view->matrix().m11();
64 ysize /= m_view->matrix().m22();
66 mouseArea.addRect(pos.x() - xsize / 2, pos.y() - ysize / 2, xsize, ysize);
68 // Check for collisions between the mouse and the borders
69 if (mouseArea.contains(pol.at(0)))
71 else if (mouseArea.contains(pol.at(2)))
72 return ResizeBottomRight;
73 else if (mouseArea.contains(pol.at(1)))
74 return ResizeTopRight;
75 else if (mouseArea.contains(pol.at(3)))
76 return ResizeBottomLeft;
77 else if (top.intersects(mouseArea))
79 else if (bottom.intersects(mouseArea))
81 else if (right.intersects(mouseArea))
83 else if (left.intersects(mouseArea))
85 else if (rect().normalized().contains(pos))
91 void OnMonitorRectItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
95 m_lastPoint = event->scenePos();
96 m_oldRect = rect().normalized();
97 m_mode = getMode(event->pos());
100 void OnMonitorRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
102 /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
107 if (event->buttons() & Qt::LeftButton) {
108 QRectF r = rect().normalized();
110 QPointF mousePos = event->scenePos();
111 QPointF mousePosInRect = event->pos();
112 QPointF diff = mousePos - m_lastPoint;
113 m_lastPoint = mousePos;
117 if (mousePos.x() < p.x() + r.height() && mousePos.y() < p.y() + r.height()) {
118 r.adjust(0, 0, -mousePosInRect.x(), -mousePosInRect.y());
124 if (mousePos.y() < p.y() + r.height()) {
125 r.setBottom(r.height() - mousePosInRect.y());
126 setPos(QPointF(p.x(), mousePos.y()));
131 if (mousePos.x() > p.x() && mousePos.y() < p.y() + r.height()) {
132 r.setBottomRight(QPointF(mousePosInRect.x(), r.bottom() - mousePosInRect.y()));
133 setPos(QPointF(p.x(), mousePos.y()));
138 if (mousePos.x() < p.x() + r.width()) {
139 r.setRight(r.width() - mousePosInRect.x());
140 setPos(QPointF(mousePos.x(), p.y()));
145 if (mousePos.x() > p.x()) {
146 r.setRight(mousePosInRect.x());
150 case ResizeBottomLeft:
151 if (mousePos.x() < p.x() + r.width() && mousePos.y() > p.y()) {
152 r.setBottomRight(QPointF(r.width() - mousePosInRect.x(), mousePosInRect.y()));
153 setPos(QPointF(mousePos.x(), p.y()));
158 if (mousePos.y() > p.y()) {
159 r.setBottom(mousePosInRect.y());
163 case ResizeBottomRight:
164 if (mousePos.x() > p.x() && mousePos.y() > p.y()) {
165 r.setBottomRight(mousePosInRect);
170 moveBy(diff.x(), diff.y());
178 if (event->modifiers() == Qt::ControlModifier) {
179 // compare to rect during mouse press:
180 // if we subtract rect() we'll get a whole lot of flickering
181 // because of diffWidth > diffHeight changing all the time
182 int diffWidth = qAbs(r.width() - m_oldRect.width());
183 int diffHeight = qAbs(r.height() - m_oldRect.height());
185 if (diffHeight != 0 || diffWidth != 0) {
186 if (diffWidth > diffHeight)
187 r.setBottom(r.width() / m_dar);
189 r.setRight(r.height() * m_dar);
191 // the rect's position changed
192 if (p - pos() != QPointF()) {
193 if (diffWidth > diffHeight) {
194 if (m_mode != ResizeBottomLeft)
195 #if QT_VERSION >= 0x040600
196 setY(p.y() - r.height() + rect().normalized().height());
198 setPos(x(), p.y() - r.height() + rect().normalized().height());
201 if (m_mode != ResizeTopRight)
202 #if QT_VERSION >= 0x040600
203 setX(p.x() - r.width() + rect().normalized().width());
205 setPos(p.x() - r.width() + rect().normalized().width(), y());
217 if (KdenliveSettings::monitorscene_directupdate()) {
226 void OnMonitorRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
235 void OnMonitorRectItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
237 switch (getMode(event->pos())) {
239 case ResizeBottomRight:
240 setCursor(QCursor(Qt::SizeFDiagCursor));
243 case ResizeBottomLeft:
244 setCursor(QCursor(Qt::SizeBDiagCursor));
248 setCursor(QCursor(Qt::SizeVerCursor));
252 setCursor(QCursor(Qt::SizeHorCursor));
255 setCursor(QCursor(Qt::OpenHandCursor));
263 void OnMonitorRectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
267 painter->setPen(pen());
268 //painter->setClipRect(option->rect);
269 const QRectF r = rect();
270 painter->drawRect(r);
271 QRectF handle = painter->worldTransform().inverted().mapRect(QRectF(0, 0, 6, 6));
273 handle.moveTopLeft(r.topLeft());
274 painter->fillRect(handle, QColor(Qt::yellow));
275 handle.moveTopRight(r.topRight());
276 painter->fillRect(handle, QColor(Qt::yellow));
277 handle.moveBottomLeft(r.bottomLeft());
278 painter->fillRect(handle, QColor(Qt::yellow));
279 handle.moveBottomRight(r.bottomRight());
280 painter->fillRect(handle, QColor(Qt::yellow));
283 // Draw cross at center
284 QPointF center = r.center();
285 painter->drawLine(center + QPointF(-handle.width(), 0), center + QPointF(handle.width(), 0));
286 painter->drawLine(center + QPointF(0, handle.height()), center + QPointF(0, -handle.height()));
289 bool OnMonitorRectItem::getView()
294 if (scene() && scene()->views().count()) {
295 m_view = scene()->views()[0];
302 #include "onmonitorrectitem.moc"