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"
25 #include <QGraphicsView>
26 #include <QGraphicsPixmapItem>
27 #include <QGraphicsSceneMouseEvent>
29 MonitorScene::MonitorScene(Render *renderer, QObject* parent) :
30 QGraphicsScene(parent),
34 m_resizeMode(NoResize),
36 m_backgroundImage(QImage()),
39 setBackgroundBrush(QBrush(QColor(KdenliveSettings::window_background().name())));
41 QPen framepen(Qt::SolidLine);
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(-2);
47 m_frameBorder->setBrush(Qt::transparent);
48 m_frameBorder->setFlags(0);
49 addItem(m_frameBorder);
52 m_background = new QGraphicsPixmapItem();
53 m_background->setZValue(-1);
54 m_background->setFlags(0);
55 m_background->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
56 m_background->setTransformationMode(Qt::FastTransformation);
57 QPixmap bg(m_renderer->renderWidth(), m_renderer->renderHeight());
59 m_background->setPixmap(bg);
60 addItem(m_background);
62 //connect(m_renderer, SIGNAL(rendererPosition(int)), this, SLOT(slotUpdateBackground()));
63 connect(m_renderer, SIGNAL(frameUpdated(QImage)), this, SLOT(slotSetBackgroundImage(QImage)));
66 void MonitorScene::setUp()
68 if (views().count() > 0)
69 m_view = views().at(0);
73 m_view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
75 slotUpdateBackground(true);
78 void MonitorScene::setEnabled(bool enabled)
83 void MonitorScene::slotUpdateBackground(bool fit)
85 if (m_view && m_view->isVisible()) {
86 if (m_lastUpdate.elapsed() > 200) {
87 m_background->setPixmap(QPixmap::fromImage(m_backgroundImage, Qt::ThresholdDither));
89 m_view->fitInView(m_frameBorder, Qt::KeepAspectRatio);
90 m_view->centerOn(m_frameBorder);
97 void MonitorScene::slotSetBackgroundImage(QImage image)
99 m_backgroundImage = image;
100 slotUpdateBackground();
103 resizeModes MonitorScene::getResizeMode(QGraphicsRectItem *item, QPoint pos)
108 QRectF rect = item->rect().normalized();
109 // Item mapped coordinates
110 QPolygon pol = item->deviceTransform(m_view->viewportTransform()).map(rect).toPolygon();
111 QPainterPath top(pol.point(0));
112 top.lineTo(pol.point(1));
113 QPainterPath bottom(pol.point(2));
114 bottom.lineTo(pol.point(3));
115 QPainterPath left(pol.point(0));
116 left.lineTo(pol.point(3));
117 QPainterPath right(pol.point(1));
118 right.lineTo(pol.point(2));
120 QPainterPath mouseArea;
121 mouseArea.addRect(pos.x() - 4, pos.y() - 4, 8, 8);
123 // Check for collisions between the mouse and the borders
124 if (mouseArea.contains(pol.point(0)))
126 else if (mouseArea.contains(pol.point(2)))
128 else if (mouseArea.contains(pol.point(1)))
130 else if (mouseArea.contains(pol.point(3)))
132 else if (top.intersects(mouseArea))
134 else if (bottom.intersects(mouseArea))
136 else if (right.intersects(mouseArea))
138 else if (left.intersects(mouseArea))
144 void MonitorScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
149 m_resizeMode = NoResize;
150 m_selectedItem = NULL;
152 m_clickPoint = event->scenePos();
153 QList<QGraphicsItem *> itemList = items(QRectF(m_clickPoint - QPoint(4, 4), QSizeF(4, 4)).toRect());
155 for (int i = 0; i < itemList.count(); ++i) {
156 if (itemList.at(i)->zValue() >= 0 && itemList.at(i)->flags() & QGraphicsItem::ItemIsMovable) {
157 m_selectedItem = itemList.at(i);
159 if (itemList.at(i)->type() == 3) {
160 m_resizeMode = getResizeMode((QGraphicsRectItem*)m_selectedItem, m_view->mapFromScene(m_clickPoint));
166 QGraphicsScene::mousePressEvent(event);
169 void MonitorScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
174 QPointF mousePos = event->scenePos();
176 if (m_selectedItem && event->buttons() & Qt::LeftButton) {
178 if (m_selectedItem->type() == 3) {
179 QGraphicsRectItem *item = static_cast <QGraphicsRectItem *>(m_selectedItem);
180 QRectF rect = item->rect().normalized();
181 QPointF pos = item->pos();
182 QPointF mousePosInRect = item->mapFromScene(mousePos);
183 switch (m_resizeMode) {
185 if (mousePos.x() < pos.x() + rect.height() && mousePos.y() < pos.y() + rect.height()) {
186 item->setRect(rect.adjusted(0, 0, -mousePosInRect.x(), -mousePosInRect.y()));
187 item->setPos(mousePos);
191 if (mousePos.y() < pos.y() + rect.height()) {
192 rect.setBottom(rect.height() - mousePosInRect.y());
194 item->setPos(QPointF(pos.x(), mousePos.y()));
198 if (mousePos.x() > pos.x() && mousePos.y() < pos.y() + rect.height()) {
199 rect.setBottomRight(QPointF(mousePosInRect.x(), rect.bottom() - mousePosInRect.y()));
201 item->setPos(QPointF(pos.x(), mousePos.y()));
205 if (mousePos.x() < pos.x() + rect.width()) {
206 rect.setRight(rect.width() - mousePosInRect.x());
208 item->setPos(QPointF(mousePos.x(), pos.y()));
212 if (mousePos.x() > pos.x()) {
213 rect.setRight(mousePosInRect.x());
218 if (mousePos.x() < pos.x() + rect.width() && mousePos.y() > pos.y()) {
219 rect.setBottomRight(QPointF(rect.width() - mousePosInRect.x(), mousePosInRect.y()));
221 item->setPos(QPointF(mousePos.x(), pos.y()));
225 if (mousePos.y() > pos.y()) {
226 rect.setBottom(mousePosInRect.y());
231 if (mousePos.x() > pos.x() && mousePos.y() > pos.y()) {
232 rect.setBottomRight(mousePosInRect);
237 QPointF diff = mousePos - m_clickPoint;
238 m_clickPoint = mousePos;
239 item->moveBy(diff.x(), diff.y());
244 mousePos -= QPoint(4, 4);
245 bool itemFound = false;
246 QList<QGraphicsItem *> itemList = items(QRectF(mousePos, QSizeF(4, 4)).toRect());
248 foreach (const QGraphicsItem* item, itemList) {
249 if (item->zValue() >= 0 && item->flags() &QGraphicsItem::ItemIsMovable) {
251 if (item->type() == 3) {
257 switch (getResizeMode((QGraphicsRectItem*)item, m_view->mapFromScene(event->scenePos()))) {
260 m_view->setCursor(Qt::SizeFDiagCursor);
264 m_view->setCursor(Qt::SizeBDiagCursor);
268 m_view->setCursor(Qt::SizeVerCursor);
272 m_view->setCursor(Qt::SizeHorCursor);
275 m_view->setCursor(Qt::OpenHandCursor);
282 if (!itemFound && m_view != NULL)
283 m_view->setCursor(Qt::ArrowCursor);
285 QGraphicsScene::mouseMoveEvent(event);
289 void MonitorScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
294 QGraphicsScene::mouseReleaseEvent(event);
295 emit actionFinished();
298 #include "monitorscene.moc"