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 ***************************************************************************/
21 #include "monitorscene.h"
23 #include "kdenlivesettings.h"
26 #include <QGraphicsView>
27 #include <QGraphicsPixmapItem>
28 #include <QGraphicsSceneMouseEvent>
30 MonitorScene::MonitorScene(Render *renderer, QObject* parent) :
31 QGraphicsScene(parent),
35 m_resizeMode(NoResize),
38 setBackgroundBrush(QBrush(QColor(KdenliveSettings::window_background().name())));
40 QPen framepen(Qt::DotLine);
41 framepen.setColor(Qt::red);
43 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_renderer->renderWidth(), m_renderer->renderHeight()));
44 m_frameBorder->setPen(framepen);
45 m_frameBorder->setZValue(-9);
46 m_frameBorder->setBrush(Qt::transparent);
47 m_frameBorder->setFlags(0);
48 addItem(m_frameBorder);
51 m_background = new QGraphicsPixmapItem();
52 m_background->setZValue(-10);
53 m_background->setFlags(0);
54 QPixmap bg(m_renderer->renderWidth(), m_renderer->renderHeight());
56 m_background->setPixmap(bg);
57 addItem(m_background);
59 connect(m_renderer, SIGNAL(rendererPosition(int)), this, SLOT(slotUpdateBackground()));
60 connect(m_renderer, SIGNAL(frameUpdated(int)), this, SLOT(slotUpdateBackground()));
63 void MonitorScene::setUp()
65 if (views().count() > 0)
66 m_view = views().at(0);
69 slotUpdateBackground();
72 void MonitorScene::slotUpdateBackground()
74 if (m_view && m_view->isVisible()) {
75 if (m_lastUpdate.elapsed() > 200) {
76 m_background->setPixmap(QPixmap::fromImage(m_renderer->extractFrame(m_renderer->seekFramePosition())));
77 m_view->fitInView(m_frameBorder, Qt::KeepAspectRatio);
78 m_view->centerOn(m_frameBorder);
84 resizeModes MonitorScene::getResizeMode(QGraphicsRectItem *item, QPoint pos)
89 QRectF rect = item->rect().normalized();
90 // Item mapped coordinates
91 QPolygon pol = item->deviceTransform(m_view->viewportTransform()).map(rect).toPolygon();
92 QPainterPath top(pol.point(0));
93 top.lineTo(pol.point(1));
94 QPainterPath bottom(pol.point(2));
95 bottom.lineTo(pol.point(3));
96 QPainterPath left(pol.point(0));
97 left.lineTo(pol.point(3));
98 QPainterPath right(pol.point(1));
99 right.lineTo(pol.point(2));
101 QPainterPath mouseArea;
102 mouseArea.addRect(pos.x() - 4, pos.y() - 4, 8, 8);
104 // Check for collisions between the mouse and the borders
105 if (mouseArea.contains(pol.point(0)))
107 else if (mouseArea.contains(pol.point(2)))
109 else if (mouseArea.contains(pol.point(1)))
111 else if (mouseArea.contains(pol.point(3)))
113 else if (top.intersects(mouseArea))
115 else if (bottom.intersects(mouseArea))
117 else if (right.intersects(mouseArea))
119 else if (left.intersects(mouseArea))
125 void MonitorScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
127 m_resizeMode = NoResize;
128 m_selectedItem = NULL;
130 m_clickPoint = event->scenePos();
131 QList<QGraphicsItem *> itemList = items(QRectF(m_clickPoint - QPoint(4, 4), QSizeF(4, 4)).toRect());
133 for (int i = 0; i < itemList.count(); ++i) {
134 if (itemList.at(i)->zValue() >= 0 && itemList.at(i)->flags() & QGraphicsItem::ItemIsMovable) {
135 m_selectedItem = itemList.at(i);
137 if (itemList.at(i)->type() == 3) {
138 m_resizeMode = getResizeMode((QGraphicsRectItem*)m_selectedItem, m_view->mapFromScene(m_clickPoint));
144 QGraphicsScene::mousePressEvent(event);
147 void MonitorScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
149 QPointF mousePos = event->scenePos();
151 if (m_selectedItem && event->buttons() & Qt::LeftButton) {
153 if (m_selectedItem->type() == 3) {
154 QGraphicsRectItem *item = static_cast <QGraphicsRectItem *>(m_selectedItem);
155 QRectF rect = item->rect().normalized();
156 QPointF pos = item->pos();
157 QPointF mousePosInRect = item->mapFromScene(mousePos);
158 switch (m_resizeMode) {
160 if (mousePos.x() < pos.x() + rect.height() && mousePos.y() < pos.y() + rect.height()) {
161 item->setRect(rect.adjusted(0, 0, -mousePosInRect.x(), -mousePosInRect.y()));
162 item->setPos(mousePos);
166 if (mousePos.y() < pos.y() + rect.height()) {
167 rect.setBottom(rect.height() - mousePosInRect.y());
169 item->setPos(QPointF(pos.x(), mousePos.y()));
173 if (mousePos.x() > pos.x() && mousePos.y() < pos.y() + rect.height()) {
174 rect.setBottomRight(QPointF(mousePosInRect.x(), rect.bottom() - mousePosInRect.y()));
176 item->setPos(QPointF(pos.x(), mousePos.y()));
180 if (mousePos.x() < pos.x() + rect.width()) {
181 rect.setRight(rect.width() - mousePosInRect.x());
183 item->setPos(QPointF(mousePos.x(), pos.y()));
187 if (mousePos.x() > pos.x()) {
188 rect.setRight(mousePosInRect.x());
193 if (mousePos.x() < pos.x() + rect.width() && mousePos.y() > pos.y()) {
194 rect.setBottomRight(QPointF(rect.width() - mousePosInRect.x(), mousePosInRect.y()));
196 item->setPos(QPointF(mousePos.x(), pos.y()));
200 if (mousePos.y() > pos.y()) {
201 rect.setBottom(mousePosInRect.y());
206 if (mousePos.x() > pos.x() && mousePos.y() > pos.y()) {
207 rect.setBottomRight(mousePosInRect);
212 QPointF diff = mousePos - m_clickPoint;
213 m_clickPoint = mousePos;
214 item->moveBy(diff.x(), diff.y());
219 mousePos -= QPoint(4, 4);
220 bool itemFound = false;
221 QList<QGraphicsItem *> itemList = items(QRectF(mousePos, QSizeF(4, 4)).toRect());
223 foreach (const QGraphicsItem* item, itemList) {
224 if (item->zValue() >= 0 && item->flags() &QGraphicsItem::ItemIsMovable) {
226 if (item->type() == 3) {
232 switch (getResizeMode((QGraphicsRectItem*)item, m_view->mapFromScene(event->scenePos()))) {
235 m_view->setCursor(Qt::SizeFDiagCursor);
239 m_view->setCursor(Qt::SizeBDiagCursor);
243 m_view->setCursor(Qt::SizeVerCursor);
247 m_view->setCursor(Qt::SizeHorCursor);
250 m_view->setCursor(Qt::OpenHandCursor);
257 if (!itemFound && m_view != NULL)
258 m_view->setCursor(Qt::ArrowCursor);
260 QGraphicsScene::mouseMoveEvent(event);
264 void MonitorScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
266 QGraphicsScene::mouseReleaseEvent(event);
267 emit actionFinished();
270 #include "monitorscene.moc"