PreviewWidget.cpp 6.63 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * PreviewWidget.cpp : Main widget for preview. Will dispatch on Clip or
 *                     Render preview
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
5
 * Copyright (C) 2008-2010 VideoLAN
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Authors: Hugo Beauzee-Luyssen <hugo@vlmc.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

24
#include <QtDebug>
25
#include "PreviewWidget.h"
26
#include "ui_PreviewWidget.h"
27
#include "ClipRenderer.h"
28
#include "Clip.h"
29

30
PreviewWidget::PreviewWidget( GenericRenderer* genericRenderer, QWidget *parent ) :
31
    QWidget( parent ),
32
    m_ui( new Ui::PreviewWidget ),
33 34
    m_renderer( genericRenderer ),
    m_previewStopped( true )
35 36
{
    m_ui->setupUi( this );
37

38
    m_ui->rulerWidget->setFocusPolicy( Qt::NoFocus );
39

40 41 42 43 44 45
    // Prepare and set the black background
    m_ui->renderWidget->setAutoFillBackground( true );
    m_videoPalette = m_ui->renderWidget->palette();
    m_videoPalette.setColor( QPalette::Window, QColor( Qt::black ) );
    m_ui->renderWidget->setPalette( m_videoPalette );

46
    // Hide markers and createClip buttons if we are not using a ClipRenderer
47 48 49 50
    if ( !qobject_cast<ClipRenderer*>( genericRenderer ) )
    {
        m_ui->pushButtonMarkerStart->hide();
        m_ui->pushButtonMarkerStop->hide();
51
        m_ui->pushButtonCreateClip->hide();
52 53
    }

54 55 56
    // Give the renderer to the ruler
    m_ui->rulerWidget->setRenderer( m_renderer );

57
    setAcceptDrops( false );
58

59 60
    connect( m_ui->rulerWidget, SIGNAL( timeChanged(int,int,int,int) ),
             m_ui->lcdNumber,   SLOT( setTime(int,int,int,int) ) );
61

62
    m_renderer->setRenderWidget( m_ui->renderWidget );
63

64 65 66 67 68 69 70 71
    connect( m_renderer,     SIGNAL( stopped() ),               this,       SLOT( videoStopped() ) );
    connect( m_renderer,     SIGNAL( paused() ),                this,       SLOT( videoPaused() ) );
    connect( m_renderer,     SIGNAL( playing() ),               this,       SLOT( videoPlaying() ) );
    connect( m_renderer,     SIGNAL( frameChanged(qint64, MainWorkflow::FrameChangedReason) ),
             this, SLOT( frameChanged(qint64, MainWorkflow::FrameChangedReason ) ) );
    connect( m_renderer,     SIGNAL( endReached() ),            this,       SLOT( endReached() ) );
    connect( m_ui->rulerWidget, SIGNAL( frameChanged(qint64, MainWorkflow::FrameChangedReason) ),
             m_renderer,       SLOT( previewWidgetCursorChanged(qint64) ) );
72 73 74

    connect( m_ui->pushButtonMarkerStart, SIGNAL( clicked() ), this, SLOT( markerStartClicked() ) );
    connect( m_ui->pushButtonMarkerStop, SIGNAL( clicked() ), this, SLOT( markerStopClicked() ) );
75
    connect( m_ui->pushButtonCreateClip, SIGNAL( clicked() ), this, SLOT( createNewClipFromMarkers() ) );
76 77 78 79
}

PreviewWidget::~PreviewWidget()
{
80
    delete m_renderer;
81
    delete m_ui;
82 83 84
}

void    PreviewWidget::changeEvent( QEvent *e )
85 86 87 88 89 90 91 92 93 94 95
{
    switch ( e->type() )
    {
    case QEvent::LanguageChange:
        m_ui->retranslateUi( this );
        break;
    default:
        break;
    }
}

96
void    PreviewWidget::frameChanged( qint64 currentFrame, MainWorkflow::FrameChangedReason reason )
97
{
98
    if ( m_previewStopped == false && reason != MainWorkflow::PreviewCursor )
99
    {
100
        m_ui->rulerWidget->setFrame( currentFrame, false );
101
    }
102
}
103

104 105 106 107 108
void    PreviewWidget::on_pushButtonStop_clicked()
{
    if ( m_previewStopped == false )
    {
        m_previewStopped = true;
109
        m_renderer->stop();
110 111 112 113
    }
}

void    PreviewWidget::on_pushButtonPlay_clicked()
114 115 116
{
    if ( m_previewStopped == true )
        m_previewStopped = false;
117
    m_renderer->togglePlayPause();
118 119
}

120
void    PreviewWidget::videoPaused()
121 122 123 124
{
    m_ui->pushButtonPlay->setIcon( QIcon( ":/images/play" ) );
}

125 126 127 128 129 130
void    PreviewWidget::videoStopped()
{
    m_ui->pushButtonPlay->setIcon( QIcon( ":/images/play" ) );
}

void    PreviewWidget::videoPlaying()
131 132 133 134 135 136 137 138 139
{
    m_ui->pushButtonPlay->setIcon( QIcon( ":/images/pause" ) );
}

void    PreviewWidget::endReached()
{
    m_previewStopped = true;

    m_ui->pushButtonPlay->setIcon( QIcon( ":/images/play" ) );
140 141 142

    // Set the black background
    m_ui->renderWidget->setPalette( m_videoPalette );
143
}
144

145 146
void        PreviewWidget::on_pushButtonNextFrame_clicked()
{
147
    if ( m_previewStopped == false )
148
        m_renderer->nextFrame();
149 150 151 152
}

void        PreviewWidget::on_pushButtonPreviousFrame_clicked()
{
153
    if ( m_previewStopped == false )
154
        m_renderer->previousFrame();
155
}
156

157
const GenericRenderer*      PreviewWidget::getGenericRenderer() const
158 159 160
{
    return m_renderer;
}
161 162 163 164 165 166

void                        PreviewWidget::stop()
{
    //Ugly but avoid code dupplication.
    on_pushButtonStop_clicked();
}
167 168 169

void                        PreviewWidget::markerStartClicked()
{
170
    m_ui->rulerWidget->setMarker( PreviewRuler::Start );
171

172 173
    qint64  beg = m_ui->rulerWidget->getMarker( PreviewRuler::Start );
    qint64  end = m_ui->rulerWidget->getMarker( PreviewRuler::Stop );
174 175
    if ( beg > end )
    {
176
        m_ui->rulerWidget->hideMarker( PreviewRuler::Stop );;
177
    }
178 179 180 181
}

void                        PreviewWidget::markerStopClicked()
{
182 183 184
    m_ui->rulerWidget->setMarker( PreviewRuler::Stop );
    qint64  beg = m_ui->rulerWidget->getMarker( PreviewRuler::Start );
    qint64  end = m_ui->rulerWidget->getMarker( PreviewRuler::Stop );
185 186
    if ( beg > end )
    {
187
        m_ui->rulerWidget->hideMarker( PreviewRuler::Start );;
188
    }
189
}
190 191 192

void        PreviewWidget::createNewClipFromMarkers()
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
193 194 195 196 197
    ClipRenderer* clipRenderer = qobject_cast<ClipRenderer*>( m_renderer );
    Q_ASSERT( clipRenderer != NULL );

    Clip* clip = clipRenderer->getClip();
    if ( clip == NULL )
198
        return ;
199 200
    qint64  beg = m_ui->rulerWidget->getMarker( PreviewRuler::Start );
    qint64  end = m_ui->rulerWidget->getMarker( PreviewRuler::Stop );
201

202 203 204
    if ( beg == -1 && end == -1 )
        return ;

205 206
    if ( end < beg )
        return ;
207

208
    beg = beg < 0 ? 0 : beg;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
209
    Clip*   part = new Clip( clip, beg, end );
210 211

    //Adding the newly created clip to the media
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
212
    clip->addSubclip( part );
213
}