]> git.sesse.net Git - kdenlive/blob - src/monitorscene.cpp
Fix issues with on monitor effects:
[kdenlive] / src / monitorscene.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include "monitorscene.h"
22 #include "renderer.h"
23 #include "kdenlivesettings.h"
24
25 #include <QGraphicsView>
26 #include <QGraphicsPixmapItem>
27 #include <QGraphicsSceneMouseEvent>
28
29 MonitorScene::MonitorScene(Render *renderer, QObject* parent) :
30         QGraphicsScene(parent),
31         m_renderer(renderer),
32         m_view(NULL),
33         m_selectedItem(NULL),
34         m_resizeMode(NoResize),
35         m_clickPoint(0, 0),
36         m_backgroundImage(QImage()),
37         m_enabled(true),
38         m_modified(false),
39         m_zoom(1.0)
40 {
41     setBackgroundBrush(QBrush(QColor(KdenliveSettings::window_background().name())));
42
43     QPen framepen(Qt::SolidLine);
44     framepen.setColor(Qt::red);
45
46     m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_renderer->frameRenderWidth(), m_renderer->renderHeight()));
47     m_frameBorder->setPen(framepen);
48     m_frameBorder->setZValue(-2);
49     m_frameBorder->setBrush(Qt::transparent);
50     m_frameBorder->setFlags(0);
51     addItem(m_frameBorder);
52
53     m_lastUpdate.start();
54     m_background = new QGraphicsPixmapItem();
55     m_background->setZValue(-1);
56     m_background->setFlags(0);
57     m_background->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
58     m_background->setTransformationMode(Qt::FastTransformation);
59     QPixmap bg(m_renderer->frameRenderWidth(), m_renderer->renderHeight());
60     bg.fill();
61     m_background->setPixmap(bg);
62     addItem(m_background);
63
64     //connect(m_renderer, SIGNAL(rendererPosition(int)), this, SLOT(slotUpdateBackground()));
65     connect(m_renderer, SIGNAL(frameUpdated(QImage)), this, SLOT(slotSetBackgroundImage(QImage)));
66 }
67
68 void MonitorScene::setUp()
69 {
70     if (views().count() > 0)
71         m_view = views().at(0);
72     else
73         m_view = NULL;
74
75     m_view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
76 }
77
78 void MonitorScene::resetProfile()
79 {
80     const QRectF border(0, 0, m_renderer->frameRenderWidth(), m_renderer->renderHeight());
81     m_frameBorder->setRect(border);
82 }
83
84 void MonitorScene::setEnabled(bool enabled)
85 {
86     m_enabled = enabled;
87 }
88
89 void MonitorScene::slotUpdateBackground()
90 {
91     if (m_view && m_view->isVisible()) {
92         if (m_lastUpdate.elapsed() > 100) {
93             m_background->setPixmap(QPixmap::fromImage(m_backgroundImage, Qt::ThresholdDither));
94             m_lastUpdate.start();
95         }
96     }
97 }
98
99 void MonitorScene::slotSetDirectUpdate(bool directUpdate)
100 {
101     KdenliveSettings::setMonitorscene_directupdate(directUpdate);
102 }
103
104 void MonitorScene::slotSetBackgroundImage(const QImage &image)
105 {
106     if (m_view && m_view->isVisible()) {
107         m_backgroundImage = image;
108         slotUpdateBackground();
109     }
110 }
111
112
113 void MonitorScene::slotZoom(int value)
114 {
115     if (m_view) {
116         m_zoom = value / 100.0;
117         m_view->resetTransform();
118         m_view->scale(m_renderer->renderWidth() * m_zoom / m_renderer->frameRenderWidth(), m_zoom);
119         emit zoomChanged(value);
120     }
121 }
122
123 void MonitorScene::slotZoomFit()
124 {
125     if (m_view) {
126         int xzoom = 100 * m_view->viewport()->height() / m_renderer->renderHeight();
127         int yzoom = 100 * m_view->viewport()->width() / m_renderer->renderWidth();
128         slotZoom(qMin(xzoom, yzoom));
129         m_view->centerOn(m_frameBorder);
130     }
131 }
132
133 void MonitorScene::slotZoomOriginal()
134 {
135     slotZoom(100);
136     if (m_view)
137         m_view->centerOn(m_frameBorder);
138 }
139
140 void MonitorScene::slotZoomOut()
141 {
142     slotZoom(qMax(0, (int)(m_zoom * 100 - 1)));
143 }
144
145 void MonitorScene::slotZoomIn()
146 {
147     int newzoom = (100 * m_zoom + 0.5);
148     newzoom++;
149     slotZoom(qMin(300, newzoom));
150 }
151
152
153 resizeModes MonitorScene::getResizeMode(QGraphicsRectItem *item, QPoint pos)
154 {
155     if (!m_view)
156         return NoResize;
157
158     QRectF rect = item->rect().normalized();
159     // Item mapped coordinates
160     QPolygon pol = item->deviceTransform(m_view->viewportTransform()).map(rect).toPolygon();
161     QPainterPath top(pol.point(0));
162     top.lineTo(pol.point(1));
163     QPainterPath bottom(pol.point(2));
164     bottom.lineTo(pol.point(3));
165     QPainterPath left(pol.point(0));
166     left.lineTo(pol.point(3));
167     QPainterPath right(pol.point(1));
168     right.lineTo(pol.point(2));
169
170     QPainterPath mouseArea;
171     mouseArea.addRect(pos.x() - 4, pos.y() - 4, 8, 8);
172
173     // Check for collisions between the mouse and the borders
174     if (mouseArea.contains(pol.point(0)))
175         return TopLeft;
176     else if (mouseArea.contains(pol.point(2)))
177         return BottomRight;
178     else if (mouseArea.contains(pol.point(1)))
179         return TopRight;
180     else if (mouseArea.contains(pol.point(3)))
181         return BottomLeft;
182     else if (top.intersects(mouseArea))
183         return Top;
184     else if (bottom.intersects(mouseArea))
185         return Bottom;
186     else if (right.intersects(mouseArea))
187         return Right;
188     else if (left.intersects(mouseArea))
189         return Left;
190     else
191         return NoResize;
192 }
193
194 void MonitorScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
195 {
196     if (!m_enabled)
197         return;
198
199     m_resizeMode = NoResize;
200     m_selectedItem = NULL;
201
202     m_clickPoint = event->scenePos();
203     QList<QGraphicsItem *> itemList = items(QRectF(m_clickPoint - QPoint(4, 4), QSizeF(4, 4)).toRect());
204
205     for (int i = 0; i < itemList.count(); ++i) {
206         if (itemList.at(i)->zValue() >= 0 && itemList.at(i)->flags() & QGraphicsItem::ItemIsMovable) {
207             m_selectedItem = itemList.at(i);
208             // Rect
209             if (itemList.at(i)->type() == 3) {
210                 m_resizeMode = getResizeMode((QGraphicsRectItem*)m_selectedItem, m_view->mapFromScene(m_clickPoint));
211                 break;
212             }
213         }
214     }
215
216     QGraphicsScene::mousePressEvent(event);
217 }
218
219 void MonitorScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
220 {
221     if (!m_enabled) {
222         if (m_view)
223             m_view->setCursor(Qt::ArrowCursor);
224         return;
225     }
226
227     /*if (event->buttons() != Qt::NoButton && (event->screenPos() - m_screenClickPoint).manhattanLength() < QApplication::startDragDistance()) {
228         event->accept();
229         return;
230     }*/
231
232     QPointF mousePos = event->scenePos();
233
234     if (m_selectedItem && event->buttons() & Qt::LeftButton) {
235         // Rect
236         if (m_selectedItem->type() == 3) {
237             QGraphicsRectItem *item = static_cast <QGraphicsRectItem *>(m_selectedItem);
238             QRectF rect = item->rect().normalized();
239             QPointF pos = item->pos();
240             QPointF mousePosInRect = item->mapFromScene(mousePos);
241             switch (m_resizeMode) {
242             case TopLeft:
243                 if (mousePos.x() < pos.x() + rect.height() && mousePos.y() < pos.y() + rect.height()) {
244                     item->setRect(rect.adjusted(0, 0, -mousePosInRect.x(), -mousePosInRect.y()));
245                     item->setPos(mousePos);
246                     m_modified = true;
247                 }
248                 break;
249             case Top:
250                 if (mousePos.y() < pos.y() + rect.height()) {
251                     rect.setBottom(rect.height() - mousePosInRect.y());
252                     item->setRect(rect);
253                     item->setPos(QPointF(pos.x(), mousePos.y()));
254                     m_modified = true;
255                 }
256                 break;
257             case TopRight:
258                 if (mousePos.x() > pos.x() && mousePos.y() < pos.y() + rect.height()) {
259                     rect.setBottomRight(QPointF(mousePosInRect.x(), rect.bottom() - mousePosInRect.y()));
260                     item->setRect(rect);
261                     item->setPos(QPointF(pos.x(), mousePos.y()));
262                     m_modified = true;
263                 }
264                 break;
265             case Left:
266                 if (mousePos.x() < pos.x() + rect.width()) {
267                     rect.setRight(rect.width() - mousePosInRect.x());
268                     item->setRect(rect);
269                     item->setPos(QPointF(mousePos.x(), pos.y()));
270                     m_modified = true;
271                 }
272                 break;
273             case Right:
274                 if (mousePos.x() > pos.x()) {
275                     rect.setRight(mousePosInRect.x());
276                     item->setRect(rect);
277                     m_modified = true;
278                 }
279                 break;
280             case BottomLeft:
281                 if (mousePos.x() < pos.x() + rect.width() && mousePos.y() > pos.y()) {
282                     rect.setBottomRight(QPointF(rect.width() - mousePosInRect.x(), mousePosInRect.y()));
283                     item->setRect(rect);
284                     item->setPos(QPointF(mousePos.x(), pos.y()));
285                     m_modified = true;
286                 }
287                 break;
288             case Bottom:
289                 if (mousePos.y() > pos.y()) {
290                     rect.setBottom(mousePosInRect.y());
291                     item->setRect(rect);
292                     m_modified = true;
293                 }
294                 break;
295             case BottomRight:
296                 if (mousePos.x() > pos.x() && mousePos.y() > pos.y()) {
297                     rect.setBottomRight(mousePosInRect);
298                     item->setRect(rect);
299                     m_modified = true;
300                 }
301                 break;
302             default:
303                 QPointF diff = mousePos - m_clickPoint;
304                 m_clickPoint = mousePos;
305                 item->moveBy(diff.x(), diff.y());
306                 m_modified = true;
307                 break;
308             }
309         }
310     } else {
311         mousePos -= QPoint(4, 4);
312         bool itemFound = false;
313         QList<QGraphicsItem *> itemList = items(QRectF(mousePos, QSizeF(4, 4)).toRect());
314
315         foreach(const QGraphicsItem* item, itemList) {
316             if (item->zValue() >= 0 && item->flags() &QGraphicsItem::ItemIsMovable) {
317                 // Rect
318                 if (item->type() == 3) {
319                     if (m_view == NULL)
320                         continue;
321
322                     itemFound = true;
323
324                     switch (getResizeMode((QGraphicsRectItem*)item, m_view->mapFromScene(event->scenePos()))) {
325                     case TopLeft:
326                     case BottomRight:
327                         m_view->setCursor(Qt::SizeFDiagCursor);
328                         break;
329                     case TopRight:
330                     case BottomLeft:
331                         m_view->setCursor(Qt::SizeBDiagCursor);
332                         break;
333                     case Top:
334                     case Bottom:
335                         m_view->setCursor(Qt::SizeVerCursor);
336                         break;
337                     case Left:
338                     case Right:
339                         m_view->setCursor(Qt::SizeHorCursor);
340                         break;
341                     default:
342                         m_view->setCursor(Qt::OpenHandCursor);
343                     }
344                     break;
345                 }
346             }
347         }
348
349         if (!itemFound && m_view)
350             m_view->setCursor(Qt::ArrowCursor);
351
352         QGraphicsScene::mouseMoveEvent(event);
353     }
354     if (m_modified && KdenliveSettings::monitorscene_directupdate()) {
355         emit actionFinished();
356         m_modified = false;
357     }
358 }
359
360 void MonitorScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
361 {
362     Q_UNUSED(event);
363
364     if (!m_enabled)
365         return;
366
367     if (m_modified) {
368         m_modified = false;
369         emit actionFinished();
370     }
371 }
372
373 void MonitorScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
374 {
375     Q_UNUSED(event);
376
377     if (!m_enabled)
378         emit addKeyframe();
379 }
380
381
382 #include "monitorscene.moc"