]> git.sesse.net Git - kdenlive/blob - src/monitorscene.cpp
Show progress while loading project
[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/onmonitorrectitem.h"
24 #include "kdenlivesettings.h"
25
26 #include <QGraphicsView>
27 #include <QGraphicsPixmapItem>
28 #include <QGraphicsSceneMouseEvent>
29
30 MonitorScene::MonitorScene(Render *renderer, QObject* parent) :
31         QGraphicsScene(parent),
32         m_renderer(renderer),
33         m_view(NULL),
34         m_backgroundImage(QImage()),
35         m_enabled(true),
36         m_zoom(1.0)
37 {
38     setBackgroundBrush(QBrush(QColor(KdenliveSettings::window_background().name())));
39
40     QPen framepen(Qt::DotLine);
41     framepen.setColor(Qt::red);
42
43     m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_renderer->frameRenderWidth(), m_renderer->renderHeight()));
44     m_frameBorder->setPen(framepen);
45     m_frameBorder->setZValue(-1);
46     m_frameBorder->setBrush(Qt::transparent);
47     m_frameBorder->setFlags(0);
48     addItem(m_frameBorder);
49
50     m_lastUpdate = QTime::currentTime();
51     m_background = new QGraphicsPixmapItem();
52     m_background->setZValue(-2);
53     m_background->setFlags(0);
54     m_background->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
55     m_background->setTransformationMode(Qt::FastTransformation);
56     QPixmap bg(m_renderer->frameRenderWidth(), m_renderer->renderHeight());
57     bg.fill();
58     m_background->setPixmap(bg);
59     addItem(m_background);
60
61     connect(m_renderer, SIGNAL(frameUpdated(QImage)), this, SLOT(slotSetBackgroundImage(QImage)));
62 }
63
64 void MonitorScene::setUp()
65 {
66     if (views().count() > 0) {
67         m_view = views().at(0);
68         m_view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
69     } else {
70         m_view = NULL;
71     }
72 }
73
74 void MonitorScene::resetProfile()
75 {
76     const QRectF border(0, 0, m_renderer->frameRenderWidth(), m_renderer->renderHeight());
77     m_frameBorder->setRect(border);
78 }
79
80 void MonitorScene::setEnabled(bool enabled)
81 {
82     m_enabled = enabled;
83 }
84
85 void MonitorScene::slotUpdateBackground()
86 {
87     if (m_view && m_view->isVisible()) {
88         if (m_lastUpdate.msecsTo(QTime::currentTime()) > 100) {
89             m_background->setPixmap(QPixmap::fromImage(m_backgroundImage));
90             m_lastUpdate = QTime::currentTime();
91         }
92     }
93 }
94
95 void MonitorScene::slotSetDirectUpdate(bool directUpdate)
96 {
97     KdenliveSettings::setMonitorscene_directupdate(directUpdate);
98 }
99
100 void MonitorScene::slotSetBackgroundImage(const QImage &image)
101 {
102     if (m_view && m_view->isVisible()) {
103         m_backgroundImage = image;
104         slotUpdateBackground();
105     }
106 }
107
108 void MonitorScene::slotZoom(int value)
109 {
110     if (m_view) {
111         m_zoom = value / 100.0;
112         m_view->resetTransform();
113         m_view->scale(m_renderer->renderWidth() * m_zoom / m_renderer->frameRenderWidth(), m_zoom);
114         emit zoomChanged(value);
115     }
116 }
117
118 void MonitorScene::slotZoomFit()
119 {
120     if (m_view) {
121         int xzoom = 100 * m_view->viewport()->height() / m_renderer->renderHeight();
122         int yzoom = 100 * m_view->viewport()->width() / m_renderer->renderWidth();
123         slotZoom(qMin(xzoom, yzoom));
124         m_view->centerOn(m_frameBorder);
125     }
126 }
127
128 void MonitorScene::slotZoomOriginal()
129 {
130     slotZoom(100);
131     if (m_view)
132         m_view->centerOn(m_frameBorder);
133 }
134
135 void MonitorScene::slotZoomOut()
136 {
137     slotZoom(qMax(0, (int)(m_zoom * 100 - 1)));
138 }
139
140 void MonitorScene::slotZoomIn()
141 {
142     int newzoom = (100 * m_zoom + 0.5);
143     newzoom++;
144     slotZoom(qMin(300, newzoom));
145 }
146
147 void MonitorScene::addItem(QGraphicsItem* item)
148 {
149     QGraphicsScene::addItem(item);
150
151     OnMonitorRectItem *rect = qgraphicsitem_cast<OnMonitorRectItem*>(item);
152     if (rect) {
153         connect(this, SIGNAL(mousePressed(QGraphicsSceneMouseEvent*)), rect, SLOT(slotMousePressed(QGraphicsSceneMouseEvent*)));
154         connect(this, SIGNAL(mouseReleased(QGraphicsSceneMouseEvent*)), rect, SLOT(slotMouseReleased(QGraphicsSceneMouseEvent*)));
155         connect(this, SIGNAL(mouseMoved(QGraphicsSceneMouseEvent*)), rect, SLOT(slotMouseMoved(QGraphicsSceneMouseEvent*)));
156         connect(rect, SIGNAL(actionFinished()), this, SIGNAL(actionFinished()));
157         connect(rect, SIGNAL(setCursor(const QCursor &)), this, SLOT(slotSetCursor(const QCursor &)));
158     }
159 }
160
161 void MonitorScene::slotSetCursor(const QCursor &cursor)
162 {
163     if (m_view)
164         m_view->setCursor(cursor);
165 }
166
167
168 void MonitorScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
169 {
170     emit mousePressed(event);
171
172     if (!event->isAccepted() && m_enabled)
173         QGraphicsScene::mousePressEvent(event);
174 }
175
176 void MonitorScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
177 {
178     emit mouseMoved(event);
179
180     if (!event->isAccepted() && m_enabled)
181         QGraphicsScene::mouseMoveEvent(event);
182 }
183
184 void MonitorScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
185 {
186     emit mouseReleased(event);
187
188     if (!event->isAccepted() && m_enabled)
189         QGraphicsScene::mouseReleaseEvent(event);
190 }
191
192 void MonitorScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
193 {
194     Q_UNUSED(event);
195
196     if (!m_enabled)
197         emit addKeyframe();
198 }
199
200
201 #include "monitorscene.moc"