]> git.sesse.net Git - kdenlive/blob - src/monitorscene.cpp
Fix indent
[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 "onmonitoritems/rotoscoping/bpointitem.h"
24 #include "onmonitoritems/rotoscoping/splineitem.h"
25 #include "kdenlivesettings.h"
26
27 #include <QGraphicsView>
28 #include <QGraphicsPixmapItem>
29 #include <QGraphicsSceneMouseEvent>
30 #include <QScrollBar>
31
32
33 MonitorScene::MonitorScene(Render *renderer, QObject* parent) :
34         QGraphicsScene(parent),
35         m_renderer(renderer),
36         m_view(NULL),
37         m_backgroundImage(QImage()),
38         m_enabled(true),
39         m_zoom(1.0),
40         m_groupMove(false)
41 {
42     setBackgroundBrush(QBrush(QColor(KdenliveSettings::window_background().name())));
43
44     QPen framepen(Qt::DotLine);
45     framepen.setColor(Qt::red);
46
47     m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_renderer->frameRenderWidth(), m_renderer->renderHeight()));
48     m_frameBorder->setPen(framepen);
49     m_frameBorder->setZValue(-1);
50     m_frameBorder->setBrush(Qt::transparent);
51     m_frameBorder->setFlags(0);
52     addItem(m_frameBorder);
53
54     m_lastUpdate = QTime::currentTime();
55     m_background = new QGraphicsPixmapItem();
56     m_background->setZValue(-2);
57     m_background->setFlags(0);
58     m_background->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
59     m_background->setTransformationMode(Qt::FastTransformation);
60     QPixmap bg(m_renderer->frameRenderWidth(), m_renderer->renderHeight());
61     bg.fill();
62     m_background->setPixmap(bg);
63     addItem(m_background);
64
65     connect(m_renderer, SIGNAL(frameUpdated(QImage)), this, SLOT(slotSetBackgroundImage(QImage)));
66 }
67
68 void MonitorScene::centerView()
69 {
70     if (m_view) m_view->centerOn(m_frameBorder);
71 }
72
73 void MonitorScene::cleanup()
74 {
75     // Reset scene rect
76     setSceneRect(m_frameBorder->boundingRect());
77 }
78
79 void MonitorScene::setUp()
80 {
81     if (!views().isEmpty()) {
82         m_view = views().at(0);
83         m_view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
84     } else {
85         m_view = NULL;
86     }
87 }
88
89 void MonitorScene::resetProfile()
90 {
91     const QRectF border(0, 0, m_renderer->frameRenderWidth(), m_renderer->renderHeight());
92     m_frameBorder->setRect(border);
93 }
94
95 void MonitorScene::setEnabled(bool enabled)
96 {
97     m_enabled = enabled;
98 }
99
100 void MonitorScene::slotUpdateBackground()
101 {
102     if (m_view && m_view->isVisible()) {
103         if (m_lastUpdate.msecsTo(QTime::currentTime()) > 50) {
104             m_background->setPixmap(QPixmap::fromImage(m_backgroundImage));
105             m_lastUpdate = QTime::currentTime();
106         }
107     }
108 }
109
110 void MonitorScene::slotSetBackgroundImage(const QImage &image)
111 {
112     if (m_view && m_view->isVisible()) {
113         m_backgroundImage = image;
114         slotUpdateBackground();
115     }
116 }
117
118 void MonitorScene::slotZoom(int value)
119 {
120     if (m_view) {
121         m_zoom = value / 100.0;
122         m_view->resetTransform();
123         m_view->scale(m_renderer->renderWidth() * m_zoom / m_renderer->frameRenderWidth(), m_zoom);
124         emit zoomChanged(value);
125     }
126 }
127
128 void MonitorScene::slotZoomFit()
129 {
130     if (m_view) {
131         int xzoom = 100 * m_view->viewport()->height() / m_renderer->renderHeight();
132         int yzoom = 100 * m_view->viewport()->width() / m_renderer->renderWidth();
133         slotZoom(qMin(xzoom, yzoom));
134         m_view->centerOn(m_frameBorder);
135     }
136 }
137
138 void MonitorScene::slotZoomOriginal()
139 {
140     slotZoom(100);
141     if (m_view)
142         m_view->centerOn(m_frameBorder);
143 }
144
145 void MonitorScene::slotZoomOut(int by)
146 {
147     slotZoom(qMax(0, (int)(m_zoom * 100 - by)));
148 }
149
150 void MonitorScene::slotZoomIn(int by)
151 {
152     slotZoom(qMin(300, (int)(m_zoom * 100 + by + 0.5)));
153 }
154
155 void MonitorScene::mousePressEvent(QGraphicsSceneMouseEvent* event)
156 {
157     QList <QGraphicsItem *> selected = selectedItems();
158
159     QGraphicsScene::mousePressEvent(event);
160
161     if (selected.count() < selectedItems().count()) {
162         // mouse click on item not in selection group
163         // -> select only this item
164         foreach (QGraphicsItem *item, selected) {
165             if (item)
166                 item->setSelected(false);
167         }
168     }
169
170     if (event->isAccepted() && selectedItems().count() > 1) {
171         // multiple items selected + mouse pressed on an item
172         selected = selectedItems();
173         foreach (QGraphicsItem *item, selected) {
174             if (qgraphicsitem_cast<BPointItem*>(item)) {
175                 // works with rotoscoping only for now
176                 m_groupMove = true;
177                 m_lastPos = event->scenePos();
178                 return;
179             }
180         }
181     }
182
183     if (!event->isAccepted() && event->buttons() & Qt::LeftButton) {
184         if (event->modifiers() == Qt::ControlModifier)
185             m_view->setDragMode(QGraphicsView::ScrollHandDrag);
186         else if (event->modifiers() == Qt::ShiftModifier)
187             m_view->setDragMode(QGraphicsView::RubberBandDrag);
188     }
189 }
190
191 void MonitorScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
192 {
193     if (m_groupMove) {
194         // we want to move multiple items
195         // rotoscoping only for now
196         QPointF diff =  event->scenePos() - m_lastPos;
197         if (diff != QPointF(0, 0)) {
198             m_lastPos = event->scenePos();
199             QList <QGraphicsItem *> selected = selectedItems();
200             int first = -1;
201             int i = 0;
202             foreach (QGraphicsItem *item, selected) {
203                 BPointItem *bpoint = qgraphicsitem_cast<BPointItem *>(item);
204                 if (bpoint) {
205                     if (first < 0)
206                         first = i;
207                     BPoint p = bpoint->getPoint();
208                     p.setP(p.p + diff);
209                     bpoint->setPoint(p);
210                 }
211                 ++i;
212             }
213
214             if (first >= 0) {
215                 QGraphicsItem *item = selected.at(first);
216                 if (item->parentItem()) {
217                     SplineItem *parent = qgraphicsitem_cast<SplineItem*>(item->parentItem());
218                     if (parent)
219                         parent->updateSpline(true);
220                 }
221             }
222         }
223     } else {
224         QGraphicsScene::mouseMoveEvent(event);
225     }
226 }
227
228 void MonitorScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
229 {
230     if (m_groupMove) {
231         QList <QGraphicsItem *> selected = selectedItems();
232         foreach (QGraphicsItem *item, selected) {
233             if (qgraphicsitem_cast<BPointItem*>(item) && item->parentItem()) {
234                 SplineItem *parent = qgraphicsitem_cast<SplineItem*>(item->parentItem());
235                 if (parent) {
236                     parent->updateSpline(false);
237                     break;
238                 }
239             }
240         }
241         m_groupMove = false;
242     }
243     QGraphicsScene::mouseReleaseEvent(event);
244     m_view->setDragMode(QGraphicsView::NoDrag);
245 }
246 void MonitorScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
247 {
248     Q_UNUSED(event)
249
250     if (!m_enabled)
251         emit addKeyframe();
252 }
253
254 void MonitorScene::wheelEvent(QGraphicsSceneWheelEvent* event)
255 {
256     if (event->modifiers() == Qt::ControlModifier) {
257         if (event->delta() > 0) {
258             m_view->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
259             slotZoomIn(5);
260             m_view->setTransformationAnchor(QGraphicsView::AnchorViewCenter);
261         } else {
262             slotZoomOut(5);
263         }
264     } else {
265         QAbstractSlider::SliderAction action;
266         if (event->delta() > 0)
267             action = QAbstractSlider::SliderSingleStepSub;
268         else
269             action = QAbstractSlider::SliderSingleStepAdd;
270         if (event->orientation() == Qt::Horizontal)
271             m_view->horizontalScrollBar()->triggerAction(action);
272         else
273             m_view->verticalScrollBar()->triggerAction(action);
274     }
275
276     event->accept();
277 }
278
279 #include "monitorscene.moc"