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),
37 m_backgroundImage(QImage())
39 setBackgroundBrush(QBrush(QColor(KdenliveSettings::window_background().name())));
41 QPen framepen(Qt::DotLine);
42 framepen.setColor(Qt::red);
44 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_renderer->renderWidth(), m_renderer->renderHeight()));
45 m_frameBorder->setPen(framepen);
46 m_frameBorder->setZValue(-9);
47 m_frameBorder->setBrush(Qt::transparent);
48 m_frameBorder->setFlags(0);
49 addItem(m_frameBorder);
52 m_background = new QGraphicsPixmapItem();
53 m_background->setZValue(-10);
54 m_background->setFlags(0);
55 QPixmap bg(m_renderer->renderWidth(), m_renderer->renderHeight());
57 m_background->setPixmap(bg);
58 addItem(m_background);
60 //connect(m_renderer, SIGNAL(rendererPosition(int)), this, SLOT(slotUpdateBackground()));
61 connect(m_renderer, SIGNAL(frameUpdated(QImage)), this, SLOT(slotSetBackgroundImage(QImage)));
64 void MonitorScene::setUp()
66 if (views().count() > 0)
67 m_view = views().at(0);
70 slotUpdateBackground(true);
73 void MonitorScene::slotUpdateBackground(bool fit)
75 if (m_view && m_view->isVisible()) {
76 if (m_lastUpdate.elapsed() > 200) {
77 m_background->setPixmap(QPixmap::fromImage(m_backgroundImage));
79 m_view->fitInView(m_frameBorder, Qt::KeepAspectRatio);
80 m_view->centerOn(m_frameBorder);
87 void MonitorScene::slotSetBackgroundImage(QImage image)
89 m_backgroundImage = image;
90 slotUpdateBackground();
93 resizeModes MonitorScene::getResizeMode(QGraphicsRectItem *item, QPoint pos)
98 QRectF rect = item->rect().normalized();
99 // Item mapped coordinates
100 QPolygon pol = item->deviceTransform(m_view->viewportTransform()).map(rect).toPolygon();
101 QPainterPath top(pol.point(0));
102 top.lineTo(pol.point(1));
103 QPainterPath bottom(pol.point(2));
104 bottom.lineTo(pol.point(3));
105 QPainterPath left(pol.point(0));
106 left.lineTo(pol.point(3));
107 QPainterPath right(pol.point(1));
108 right.lineTo(pol.point(2));
110 QPainterPath mouseArea;
111 mouseArea.addRect(pos.x() - 4, pos.y() - 4, 8, 8);
113 // Check for collisions between the mouse and the borders
114 if (mouseArea.contains(pol.point(0)))
116 else if (mouseArea.contains(pol.point(2)))
118 else if (mouseArea.contains(pol.point(1)))
120 else if (mouseArea.contains(pol.point(3)))
122 else if (top.intersects(mouseArea))
124 else if (bottom.intersects(mouseArea))
126 else if (right.intersects(mouseArea))
128 else if (left.intersects(mouseArea))
134 void MonitorScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
136 m_resizeMode = NoResize;
137 m_selectedItem = NULL;
139 m_clickPoint = event->scenePos();
140 QList<QGraphicsItem *> itemList = items(QRectF(m_clickPoint - QPoint(4, 4), QSizeF(4, 4)).toRect());
142 for (int i = 0; i < itemList.count(); ++i) {
143 if (itemList.at(i)->zValue() >= 0 && itemList.at(i)->flags() & QGraphicsItem::ItemIsMovable) {
144 m_selectedItem = itemList.at(i);
146 if (itemList.at(i)->type() == 3) {
147 m_resizeMode = getResizeMode((QGraphicsRectItem*)m_selectedItem, m_view->mapFromScene(m_clickPoint));
153 QGraphicsScene::mousePressEvent(event);
156 void MonitorScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
158 QPointF mousePos = event->scenePos();
160 if (m_selectedItem && event->buttons() & Qt::LeftButton) {
162 if (m_selectedItem->type() == 3) {
163 QGraphicsRectItem *item = static_cast <QGraphicsRectItem *>(m_selectedItem);
164 QRectF rect = item->rect().normalized();
165 QPointF pos = item->pos();
166 QPointF mousePosInRect = item->mapFromScene(mousePos);
167 switch (m_resizeMode) {
169 if (mousePos.x() < pos.x() + rect.height() && mousePos.y() < pos.y() + rect.height()) {
170 item->setRect(rect.adjusted(0, 0, -mousePosInRect.x(), -mousePosInRect.y()));
171 item->setPos(mousePos);
175 if (mousePos.y() < pos.y() + rect.height()) {
176 rect.setBottom(rect.height() - mousePosInRect.y());
178 item->setPos(QPointF(pos.x(), mousePos.y()));
182 if (mousePos.x() > pos.x() && mousePos.y() < pos.y() + rect.height()) {
183 rect.setBottomRight(QPointF(mousePosInRect.x(), rect.bottom() - mousePosInRect.y()));
185 item->setPos(QPointF(pos.x(), mousePos.y()));
189 if (mousePos.x() < pos.x() + rect.width()) {
190 rect.setRight(rect.width() - mousePosInRect.x());
192 item->setPos(QPointF(mousePos.x(), pos.y()));
196 if (mousePos.x() > pos.x()) {
197 rect.setRight(mousePosInRect.x());
202 if (mousePos.x() < pos.x() + rect.width() && mousePos.y() > pos.y()) {
203 rect.setBottomRight(QPointF(rect.width() - mousePosInRect.x(), mousePosInRect.y()));
205 item->setPos(QPointF(mousePos.x(), pos.y()));
209 if (mousePos.y() > pos.y()) {
210 rect.setBottom(mousePosInRect.y());
215 if (mousePos.x() > pos.x() && mousePos.y() > pos.y()) {
216 rect.setBottomRight(mousePosInRect);
221 QPointF diff = mousePos - m_clickPoint;
222 m_clickPoint = mousePos;
223 item->moveBy(diff.x(), diff.y());
228 mousePos -= QPoint(4, 4);
229 bool itemFound = false;
230 QList<QGraphicsItem *> itemList = items(QRectF(mousePos, QSizeF(4, 4)).toRect());
232 foreach (const QGraphicsItem* item, itemList) {
233 if (item->zValue() >= 0 && item->flags() &QGraphicsItem::ItemIsMovable) {
235 if (item->type() == 3) {
241 switch (getResizeMode((QGraphicsRectItem*)item, m_view->mapFromScene(event->scenePos()))) {
244 m_view->setCursor(Qt::SizeFDiagCursor);
248 m_view->setCursor(Qt::SizeBDiagCursor);
252 m_view->setCursor(Qt::SizeVerCursor);
256 m_view->setCursor(Qt::SizeHorCursor);
259 m_view->setCursor(Qt::OpenHandCursor);
266 if (!itemFound && m_view != NULL)
267 m_view->setCursor(Qt::ArrowCursor);
269 QGraphicsScene::mouseMoveEvent(event);
273 void MonitorScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
275 QGraphicsScene::mouseReleaseEvent(event);
276 emit actionFinished();
279 #include "monitorscene.moc"