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;
62 xsize /= m_view->matrix().m11();
63 ysize /= m_view->matrix().m22();
65 mouseArea.addRect(pos.x() - xsize / 2, pos.y() - ysize / 2, xsize, ysize);
67 // Check for collisions between the mouse and the borders
68 if (mouseArea.contains(pol.at(0)))
70 else if (mouseArea.contains(pol.at(2)))
71 return ResizeBottomRight;
72 else if (mouseArea.contains(pol.at(1)))
73 return ResizeTopRight;
74 else if (mouseArea.contains(pol.at(3)))
75 return ResizeBottomLeft;
76 else if (top.intersects(mouseArea))
78 else if (bottom.intersects(mouseArea))
80 else if (right.intersects(mouseArea))
82 else if (left.intersects(mouseArea))
84 else if (rect().normalized().contains(pos))
90 void OnMonitorRectItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
94 m_lastPoint = event->scenePos();
95 m_oldRect = rect().normalized();
96 m_mode = getMode(event->pos());
99 void OnMonitorRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
101 /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
106 if (event->buttons() & Qt::LeftButton) {
107 QRectF r = rect().normalized();
109 QPointF mousePos = event->scenePos();
110 QPointF mousePosInRect = event->pos();
111 QPointF diff = mousePos - m_lastPoint;
112 m_lastPoint = mousePos;
116 if (mousePos.x() < p.x() + r.height() && mousePos.y() < p.y() + r.height()) {
117 r.adjust(0, 0, -mousePosInRect.x(), -mousePosInRect.y());
123 if (mousePos.y() < p.y() + r.height()) {
124 r.setBottom(r.height() - mousePosInRect.y());
125 setPos(QPointF(p.x(), mousePos.y()));
130 if (mousePos.x() > p.x() && mousePos.y() < p.y() + r.height()) {
131 r.setBottomRight(QPointF(mousePosInRect.x(), r.bottom() - mousePosInRect.y()));
132 setPos(QPointF(p.x(), mousePos.y()));
137 if (mousePos.x() < p.x() + r.width()) {
138 r.setRight(r.width() - mousePosInRect.x());
139 setPos(QPointF(mousePos.x(), p.y()));
144 if (mousePos.x() > p.x()) {
145 r.setRight(mousePosInRect.x());
149 case ResizeBottomLeft:
150 if (mousePos.x() < p.x() + r.width() && mousePos.y() > p.y()) {
151 r.setBottomRight(QPointF(r.width() - mousePosInRect.x(), mousePosInRect.y()));
152 setPos(QPointF(mousePos.x(), p.y()));
157 if (mousePos.y() > p.y()) {
158 r.setBottom(mousePosInRect.y());
162 case ResizeBottomRight:
163 if (mousePos.x() > p.x() && mousePos.y() > p.y()) {
164 r.setBottomRight(mousePosInRect);
169 moveBy(diff.x(), diff.y());
177 if (event->modifiers() == Qt::ControlModifier) {
178 // compare to rect during mouse press:
179 // if we subtract rect() we'll get a whole lot of flickering
180 // because of diffWidth > diffHeight changing all the time
181 int diffWidth = qAbs(r.width() - m_oldRect.width());
182 int diffHeight = qAbs(r.height() - m_oldRect.height());
184 if (diffHeight != 0 || diffWidth != 0) {
185 if (diffWidth > diffHeight)
186 r.setBottom(r.width() / m_dar);
188 r.setRight(r.height() * m_dar);
190 // the rect's position changed
191 if (p - pos() != QPointF()) {
192 if (diffWidth > diffHeight) {
193 if (m_mode != ResizeBottomLeft)
194 #if QT_VERSION >= 0x040600
195 setY(p.y() - r.height() + rect().normalized().height());
197 setPos(x(), p.y() - r.height() + rect().normalized().height());
200 if (m_mode != ResizeTopRight)
201 #if QT_VERSION >= 0x040600
202 setX(p.x() - r.width() + rect().normalized().width());
204 setPos(p.x() - r.width() + rect().normalized().width(), y());
216 if (KdenliveSettings::monitorscene_directupdate()) {
225 void OnMonitorRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
234 void OnMonitorRectItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
236 switch (getMode(event->pos())) {
238 case ResizeBottomRight:
239 setCursor(QCursor(Qt::SizeFDiagCursor));
242 case ResizeBottomLeft:
243 setCursor(QCursor(Qt::SizeBDiagCursor));
247 setCursor(QCursor(Qt::SizeVerCursor));
251 setCursor(QCursor(Qt::SizeHorCursor));
254 setCursor(QCursor(Qt::OpenHandCursor));
262 void OnMonitorRectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
266 painter->setPen(pen());
267 //painter->setClipRect(option->rect);
268 const QRectF r = rect();
269 painter->drawRect(r);
270 QRectF handle = painter->worldTransform().inverted().mapRect(QRectF(0, 0, 6, 6));
272 handle.moveTopLeft(r.topLeft());
273 painter->fillRect(handle, QColor(Qt::yellow));
274 handle.moveTopRight(r.topRight());
275 painter->fillRect(handle, QColor(Qt::yellow));
276 handle.moveBottomLeft(r.bottomLeft());
277 painter->fillRect(handle, QColor(Qt::yellow));
278 handle.moveBottomRight(r.bottomRight());
279 painter->fillRect(handle, QColor(Qt::yellow));
282 // Draw cross at center
283 QPointF center = r.center();
284 painter->drawLine(center + QPointF(-handle.width(), 0), center + QPointF(handle.width(), 0));
285 painter->drawLine(center + QPointF(0, handle.height()), center + QPointF(0, -handle.height()));
288 bool OnMonitorRectItem::getView()
293 if (scene() && scene()->views().count()) {
294 m_view = scene()->views()[0];
301 #include "onmonitorrectitem.moc"