]> git.sesse.net Git - kdenlive/blob - src/monitorscene.cpp
Update monitorscene rect when changing profile
[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_directUpdate(false)
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->renderWidth(), 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->renderWidth(), 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     slotUpdateBackground(true);
78 }
79
80 void MonitorScene::resetProfile()
81 {
82     const QRectF border(0, 0, m_renderer->renderWidth(), m_renderer->renderHeight());
83     m_frameBorder->setRect(border);
84 }
85
86 void MonitorScene::setEnabled(bool enabled)
87 {
88     m_enabled = enabled;
89 }
90
91 void MonitorScene::slotUpdateBackground(bool fit)
92 {
93     if (m_view && m_view->isVisible()) {
94         if (m_lastUpdate.elapsed() > 200) {
95             m_background->setPixmap(QPixmap::fromImage(m_backgroundImage, Qt::ThresholdDither));
96             if (fit) {
97                 m_view->fitInView(m_frameBorder, Qt::KeepAspectRatio);
98                 m_view->centerOn(m_frameBorder);
99             }
100             m_lastUpdate.start();
101         }
102     }
103 }
104
105 void MonitorScene::slotSetDirectUpdate(bool directUpdate)
106 {
107     m_directUpdate = directUpdate;
108 }
109
110 bool MonitorScene::getDirectUpdate()
111 {
112     return m_directUpdate;
113 }
114
115 void MonitorScene::slotSetBackgroundImage(QImage image)
116 {
117     m_backgroundImage = image;
118     slotUpdateBackground();
119 }
120
121 resizeModes MonitorScene::getResizeMode(QGraphicsRectItem *item, QPoint pos)
122 {
123     if (!m_view)
124         return NoResize;
125
126     QRectF rect = item->rect().normalized();
127     // Item mapped coordinates
128     QPolygon pol = item->deviceTransform(m_view->viewportTransform()).map(rect).toPolygon();
129     QPainterPath top(pol.point(0));
130     top.lineTo(pol.point(1));
131     QPainterPath bottom(pol.point(2));
132     bottom.lineTo(pol.point(3));
133     QPainterPath left(pol.point(0));
134     left.lineTo(pol.point(3));
135     QPainterPath right(pol.point(1));
136     right.lineTo(pol.point(2));
137
138     QPainterPath mouseArea;
139     mouseArea.addRect(pos.x() - 4, pos.y() - 4, 8, 8);
140
141     // Check for collisions between the mouse and the borders
142     if (mouseArea.contains(pol.point(0)))
143         return TopLeft;
144     else if (mouseArea.contains(pol.point(2)))
145         return BottomRight;
146     else if (mouseArea.contains(pol.point(1)))
147         return TopRight;
148     else if (mouseArea.contains(pol.point(3)))
149         return BottomLeft;
150     else if (top.intersects(mouseArea))
151         return Top;
152     else if (bottom.intersects(mouseArea))
153         return Bottom;
154     else if (right.intersects(mouseArea))
155         return Right;
156     else if (left.intersects(mouseArea))
157         return Left;
158     else
159         return NoResize;
160 }
161
162 void MonitorScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
163 {
164     if (!m_enabled)
165         return;
166
167     m_resizeMode = NoResize;
168     m_selectedItem = NULL;
169
170     m_clickPoint = event->scenePos();
171     QList<QGraphicsItem *> itemList = items(QRectF(m_clickPoint - QPoint(4, 4), QSizeF(4, 4)).toRect());
172
173     for (int i = 0; i < itemList.count(); ++i) {
174         if (itemList.at(i)->zValue() >= 0 && itemList.at(i)->flags() & QGraphicsItem::ItemIsMovable) {
175             m_selectedItem = itemList.at(i);
176             // Rect
177             if (itemList.at(i)->type() == 3) {
178                 m_resizeMode = getResizeMode((QGraphicsRectItem*)m_selectedItem, m_view->mapFromScene(m_clickPoint));
179                 break;
180             }
181         }
182     }
183
184     QGraphicsScene::mousePressEvent(event);
185 }
186
187 void MonitorScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
188 {
189     if (!m_enabled)
190         return;
191
192     QPointF mousePos = event->scenePos();
193
194     if (m_selectedItem && event->buttons() & Qt::LeftButton) {
195         // Rect
196         if (m_selectedItem->type() == 3) {
197             QGraphicsRectItem *item = static_cast <QGraphicsRectItem *>(m_selectedItem);
198             QRectF rect = item->rect().normalized();
199             QPointF pos = item->pos();
200             QPointF mousePosInRect = item->mapFromScene(mousePos);
201             switch (m_resizeMode) {
202             case TopLeft:
203                 if (mousePos.x() < pos.x() + rect.height() && mousePos.y() < pos.y() + rect.height()) {
204                     item->setRect(rect.adjusted(0, 0, -mousePosInRect.x(), -mousePosInRect.y()));
205                     item->setPos(mousePos);
206                     m_modified = true;
207                 }
208                 break;
209             case Top:
210                 if (mousePos.y() < pos.y() + rect.height()) {
211                     rect.setBottom(rect.height() - mousePosInRect.y());
212                     item->setRect(rect);
213                     item->setPos(QPointF(pos.x(), mousePos.y()));
214                     m_modified = true;
215                 }
216                 break;
217             case TopRight:
218                 if (mousePos.x() > pos.x() && mousePos.y() < pos.y() + rect.height()) {
219                     rect.setBottomRight(QPointF(mousePosInRect.x(), rect.bottom() - mousePosInRect.y()));
220                     item->setRect(rect);
221                     item->setPos(QPointF(pos.x(), mousePos.y()));
222                     m_modified = true;
223                 }
224                 break;
225             case Left:
226                 if (mousePos.x() < pos.x() + rect.width()) {
227                     rect.setRight(rect.width() - mousePosInRect.x());
228                     item->setRect(rect);
229                     item->setPos(QPointF(mousePos.x(), pos.y()));
230                     m_modified = true;
231                 }
232                 break;
233             case Right:
234                 if (mousePos.x() > pos.x()) {
235                     rect.setRight(mousePosInRect.x());
236                     item->setRect(rect);
237                     m_modified = true;
238                 }
239                 break;
240             case BottomLeft:
241                 if (mousePos.x() < pos.x() + rect.width() && mousePos.y() > pos.y()) {
242                     rect.setBottomRight(QPointF(rect.width() - mousePosInRect.x(), mousePosInRect.y()));
243                     item->setRect(rect);
244                     item->setPos(QPointF(mousePos.x(), pos.y()));
245                     m_modified = true;
246                 }
247                 break;
248             case Bottom:
249                 if (mousePos.y() > pos.y()) {
250                     rect.setBottom(mousePosInRect.y());
251                     item->setRect(rect);
252                     m_modified = true;
253                 }
254                 break;
255             case BottomRight:
256                 if (mousePos.x() > pos.x() && mousePos.y() > pos.y()) {
257                     rect.setBottomRight(mousePosInRect);
258                     item->setRect(rect);
259                     m_modified = true;
260                 }
261                 break;
262             default:
263                 QPointF diff = mousePos - m_clickPoint;
264                 m_clickPoint = mousePos;
265                 item->moveBy(diff.x(), diff.y());
266                 m_modified = true;
267                 break;
268             }
269         }
270     } else {
271         mousePos -= QPoint(4, 4);
272         bool itemFound = false;
273         QList<QGraphicsItem *> itemList = items(QRectF(mousePos, QSizeF(4, 4)).toRect());
274
275         foreach(const QGraphicsItem* item, itemList) {
276             if (item->zValue() >= 0 && item->flags() &QGraphicsItem::ItemIsMovable) {
277                 // Rect
278                 if (item->type() == 3) {
279                     if (m_view == NULL)
280                         continue;
281
282                     itemFound = true;
283
284                     switch (getResizeMode((QGraphicsRectItem*)item, m_view->mapFromScene(event->scenePos()))) {
285                     case TopLeft:
286                     case BottomRight:
287                         m_view->setCursor(Qt::SizeFDiagCursor);
288                         break;
289                     case TopRight:
290                     case BottomLeft:
291                         m_view->setCursor(Qt::SizeBDiagCursor);
292                         break;
293                     case Top:
294                     case Bottom:
295                         m_view->setCursor(Qt::SizeVerCursor);
296                         break;
297                     case Left:
298                     case Right:
299                         m_view->setCursor(Qt::SizeHorCursor);
300                         break;
301                     default:
302                         m_view->setCursor(Qt::OpenHandCursor);
303                     }
304                     break;
305                 }
306             }
307         }
308
309         if (!itemFound && m_view)
310             m_view->setCursor(Qt::ArrowCursor);
311
312         QGraphicsScene::mouseMoveEvent(event);
313     }
314     if (m_modified && m_directUpdate) {
315         emit actionFinished();
316         m_modified = false;
317     }
318 }
319
320 void MonitorScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
321 {
322     if (!m_enabled)
323         return;
324
325     QGraphicsScene::mouseReleaseEvent(event);
326     if (m_modified) {
327         m_modified = false;
328         emit actionFinished();
329     }
330 }
331
332 #include "monitorscene.moc"