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 "onmonitoritems/rotoscoping/bpointitem.h"
24 #include "onmonitoritems/rotoscoping/splineitem.h"
25 #include "kdenlivesettings.h"
27 #include <QGraphicsView>
28 #include <QGraphicsPixmapItem>
29 #include <QGraphicsSceneMouseEvent>
33 MonitorScene::MonitorScene(Render *renderer, QObject* parent) :
34 QGraphicsScene(parent),
37 m_backgroundImage(QImage()),
42 setBackgroundBrush(QBrush(QColor(KdenliveSettings::window_background().name())));
44 QPen framepen(Qt::DotLine);
45 framepen.setColor(Qt::red);
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);
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());
62 m_background->setPixmap(bg);
63 addItem(m_background);
65 connect(m_renderer, SIGNAL(frameUpdated(QImage)), this, SLOT(slotSetBackgroundImage(QImage)));
68 void MonitorScene::setUp()
70 if (views().count() > 0) {
71 m_view = views().at(0);
72 m_view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
78 void MonitorScene::resetProfile()
80 const QRectF border(0, 0, m_renderer->frameRenderWidth(), m_renderer->renderHeight());
81 m_frameBorder->setRect(border);
84 void MonitorScene::setEnabled(bool enabled)
89 void MonitorScene::slotUpdateBackground()
91 if (m_view && m_view->isVisible()) {
92 if (m_lastUpdate.msecsTo(QTime::currentTime()) > 50) {
93 m_background->setPixmap(QPixmap::fromImage(m_backgroundImage));
94 m_lastUpdate = QTime::currentTime();
99 void MonitorScene::slotSetBackgroundImage(const QImage &image)
101 if (m_view && m_view->isVisible()) {
102 m_backgroundImage = image;
103 slotUpdateBackground();
107 void MonitorScene::slotZoom(int value)
110 m_zoom = value / 100.0;
111 m_view->resetTransform();
112 m_view->scale(m_renderer->renderWidth() * m_zoom / m_renderer->frameRenderWidth(), m_zoom);
113 emit zoomChanged(value);
117 void MonitorScene::slotZoomFit()
120 int xzoom = 100 * m_view->viewport()->height() / m_renderer->renderHeight();
121 int yzoom = 100 * m_view->viewport()->width() / m_renderer->renderWidth();
122 slotZoom(qMin(xzoom, yzoom));
123 m_view->centerOn(m_frameBorder);
127 void MonitorScene::slotZoomOriginal()
131 m_view->centerOn(m_frameBorder);
134 void MonitorScene::slotZoomOut(int by)
136 slotZoom(qMax(0, (int)(m_zoom * 100 - by)));
139 void MonitorScene::slotZoomIn(int by)
141 slotZoom(qMin(300, (int)(m_zoom * 100 + by + 0.5)));
144 void MonitorScene::mousePressEvent(QGraphicsSceneMouseEvent* event)
146 QList <QGraphicsItem *> selected = selectedItems();
148 QGraphicsScene::mousePressEvent(event);
150 if (selected.count() < selectedItems().count()) {
151 // mouse click on item not in selection group
152 // -> select only this item
153 foreach (QGraphicsItem *item, selected) {
155 item->setSelected(false);
159 if (event->isAccepted() && selectedItems().count() > 1) {
160 // multiple items selected + mouse pressed on an item
161 selected = selectedItems();
162 foreach (QGraphicsItem *item, selected) {
163 if (qgraphicsitem_cast<BPointItem*>(item)) {
164 // works with rotoscoping only for now
166 m_lastPos = event->scenePos();
172 if (!event->isAccepted() && event->buttons() & Qt::LeftButton) {
173 if (event->modifiers() == Qt::ControlModifier)
174 m_view->setDragMode(QGraphicsView::ScrollHandDrag);
175 else if (event->modifiers() == Qt::ShiftModifier)
176 m_view->setDragMode(QGraphicsView::RubberBandDrag);
180 void MonitorScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
183 // we want to move multiple items
184 // rotoscoping only for now
185 QPointF diff = event->scenePos() - m_lastPos;
186 if (diff != QPointF(0, 0)) {
187 m_lastPos = event->scenePos();
188 QList <QGraphicsItem *> selected = selectedItems();
191 foreach (QGraphicsItem *item, selected) {
192 BPointItem *bpoint = qgraphicsitem_cast<BPointItem *>(item);
196 BPoint p = bpoint->getPoint();
204 QGraphicsItem *item = selected.at(first);
205 if (item->parentItem()) {
206 SplineItem *parent = qgraphicsitem_cast<SplineItem*>(item->parentItem());
208 parent->updateSpline(true);
213 QGraphicsScene::mouseMoveEvent(event);
217 void MonitorScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
220 QList <QGraphicsItem *> selected = selectedItems();
221 foreach (QGraphicsItem *item, selected) {
222 if (qgraphicsitem_cast<BPointItem*>(item) && item->parentItem()) {
223 SplineItem *parent = qgraphicsitem_cast<SplineItem*>(item->parentItem());
225 parent->updateSpline(false);
232 QGraphicsScene::mouseReleaseEvent(event);
233 m_view->setDragMode(QGraphicsView::NoDrag);
235 void MonitorScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
243 void MonitorScene::wheelEvent(QGraphicsSceneWheelEvent* event)
245 if (event->modifiers() == Qt::ControlModifier) {
246 if (event->delta() > 0) {
247 m_view->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
249 m_view->setTransformationAnchor(QGraphicsView::AnchorViewCenter);
254 QAbstractSlider::SliderAction action;
255 if (event->delta() > 0)
256 action = QAbstractSlider::SliderSingleStepSub;
258 action = QAbstractSlider::SliderSingleStepAdd;
259 if (event->orientation() == Qt::Horizontal)
260 m_view->horizontalScrollBar()->triggerAction(action);
262 m_view->verticalScrollBar()->triggerAction(action);
268 #include "monitorscene.moc"