PreviewWidget.cpp 7.67 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
 *
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
7
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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 "PreviewWidget.h"
25
#include "PreviewRuler.h"
26
#include "RenderWidget.h"
27
#include "ui_PreviewWidget.h"
28
#include "ClipRenderer.h"
29
#include "Clip.h"
30

31
#include <QMessageBox>
32
#include <QDebug>
33

34
PreviewWidget::PreviewWidget( QWidget *parent ) :
35
    QWidget( parent ),
36
    m_ui( new Ui::PreviewWidget ),
37
    m_previewStopped( true )
38 39
{
    m_ui->setupUi( this );
40

41
    m_ui->rulerWidget->setFocusPolicy( Qt::NoFocus );
42

43 44 45 46 47 48
    // 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 );

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    setAcceptDrops( false );

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

    connect( m_ui->pushButtonMarkerStart, SIGNAL( clicked() ), this, SLOT( markerStartClicked() ) );
    connect( m_ui->pushButtonMarkerStop, SIGNAL( clicked() ), this, SLOT( markerStopClicked() ) );
    connect( m_ui->pushButtonCreateClip, SIGNAL( clicked() ), this, SLOT( createNewClipFromMarkers() ) );
}

PreviewWidget::~PreviewWidget()
{
    delete m_renderer;
    delete m_ui;
}

void
PreviewWidget::setRenderer( GenericRenderer *renderer )
{
    m_renderer = renderer;

70
    // Hide markers and createClip buttons if we are not using a ClipRenderer
71
    if ( qobject_cast<ClipRenderer*>( renderer ) == NULL )
72 73 74
    {
        m_ui->pushButtonMarkerStart->hide();
        m_ui->pushButtonMarkerStop->hide();
75
        m_ui->pushButtonCreateClip->hide();
76
    }
77 78 79
    // Give the renderer to the ruler
    m_ui->rulerWidget->setRenderer( m_renderer );

80
    m_renderer->setRenderWidget( m_ui->renderWidget );
81

82 83 84 85 86
#if defined ( Q_WS_MAC )
    /* Releases the NSView in the RenderWidget*/
    m_ui->renderWidget->release();
#endif

87 88 89
    connect( m_renderer,     SIGNAL( stopped() ),               this,       SLOT( videoStopped() ) );
    connect( m_renderer,     SIGNAL( paused() ),                this,       SLOT( videoPaused() ) );
    connect( m_renderer,     SIGNAL( playing() ),               this,       SLOT( videoPlaying() ) );
90 91
    connect( m_renderer,     SIGNAL( frameChanged(qint64, Vlmc::FrameChangedReason) ),
             this, SLOT( frameChanged(qint64, Vlmc::FrameChangedReason ) ) );
92
    connect( m_renderer,     SIGNAL( endReached() ),            this,       SLOT( endReached() ) );
93
    connect( m_ui->rulerWidget, SIGNAL( frameChanged(qint64, Vlmc::FrameChangedReason) ),
94
             m_renderer,       SLOT( previewWidgetCursorChanged(qint64) ) );
95 96 97 98 99 100 101
    connect( m_renderer,     SIGNAL( error() ),                 this,       SLOT( error() ) );
    connect( m_renderer,     SIGNAL( volumeChanged() ),         this,       SLOT( volumeChanged() ) );

    connect( m_ui->volumeSlider, SIGNAL( valueChanged ( int ) ),
             this, SLOT( updateVolume( int ) ) );
    connect( m_ui->volumeSlider, SIGNAL( sliderMoved( int ) ),
             this, SLOT( updateVolume( int ) ) );
102 103
}

104 105
void
PreviewWidget::changeEvent( QEvent *e )
106 107 108 109 110 111 112 113 114 115 116
{
    switch ( e->type() )
    {
    case QEvent::LanguageChange:
        m_ui->retranslateUi( this );
        break;
    default:
        break;
    }
}

117 118
void
PreviewWidget::frameChanged( qint64 currentFrame, Vlmc::FrameChangedReason reason )
119
{
120
    if ( m_previewStopped == false && reason != Vlmc::PreviewCursor )
121
    {
122
        m_ui->rulerWidget->setFrame( currentFrame, false );
123
    }
124
}
125

126 127
void
PreviewWidget::on_pushButtonStop_clicked()
128 129 130 131
{
    if ( m_previewStopped == false )
    {
        m_previewStopped = true;
132
        m_renderer->stop();
133 134 135
    }
}

136 137
void
PreviewWidget::on_pushButtonPlay_clicked()
138 139 140
{
    if ( m_previewStopped == true )
        m_previewStopped = false;
141
    m_renderer->togglePlayPause();
142 143
}

144 145
void
PreviewWidget::videoPaused()
146 147 148 149
{
    m_ui->pushButtonPlay->setIcon( QIcon( ":/images/play" ) );
}

150 151
void
PreviewWidget::videoStopped()
152 153 154 155
{
    m_ui->pushButtonPlay->setIcon( QIcon( ":/images/play" ) );
}

156 157
void
PreviewWidget::videoPlaying()
158 159 160 161
{
    m_ui->pushButtonPlay->setIcon( QIcon( ":/images/pause" ) );
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
void
PreviewWidget::volumeChanged()
{
    int volume = m_renderer->getVolume();
    m_ui->volumeSlider->setValue( volume );
}

void
PreviewWidget::updateVolume( int volume )
{
    // Returns 0 if the volume was set, -1 if it was out of range
    m_renderer->setVolume( volume );
}

void
PreviewWidget::endReached()
178 179 180 181
{
    m_previewStopped = true;

    m_ui->pushButtonPlay->setIcon( QIcon( ":/images/play" ) );
182 183 184

    // Set the black background
    m_ui->renderWidget->setPalette( m_videoPalette );
185
}
186

187 188
void
PreviewWidget::on_pushButtonNextFrame_clicked()
189
{
190
    if ( m_previewStopped == false )
191
        m_renderer->nextFrame();
192 193
}

194 195
void
PreviewWidget::on_pushButtonPreviousFrame_clicked()
196
{
197
    if ( m_previewStopped == false )
198
        m_renderer->previousFrame();
199
}
200

201 202
const GenericRenderer*
PreviewWidget::getGenericRenderer() const
203 204 205
{
    return m_renderer;
}
206

207 208
void
PreviewWidget::stop()
209 210 211 212
{
    //Ugly but avoid code dupplication.
    on_pushButtonStop_clicked();
}
213

214 215
void
PreviewWidget::markerStartClicked()
216
{
217
    m_ui->rulerWidget->setMarker( PreviewRuler::Start );
218

219 220
    qint64  beg = m_ui->rulerWidget->getMarker( PreviewRuler::Start );
    qint64  end = m_ui->rulerWidget->getMarker( PreviewRuler::Stop );
221 222
    if ( beg > end )
    {
223
        m_ui->rulerWidget->hideMarker( PreviewRuler::Stop );;
224
    }
225 226
}

227 228
void
PreviewWidget::markerStopClicked()
229
{
230 231 232
    m_ui->rulerWidget->setMarker( PreviewRuler::Stop );
    qint64  beg = m_ui->rulerWidget->getMarker( PreviewRuler::Start );
    qint64  end = m_ui->rulerWidget->getMarker( PreviewRuler::Stop );
233 234
    if ( beg > end )
    {
235
        m_ui->rulerWidget->hideMarker( PreviewRuler::Start );;
236
    }
237
}
238

239 240
void
PreviewWidget::createNewClipFromMarkers()
241
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
242 243 244 245 246
    ClipRenderer* clipRenderer = qobject_cast<ClipRenderer*>( m_renderer );
    Q_ASSERT( clipRenderer != NULL );

    Clip* clip = clipRenderer->getClip();
    if ( clip == NULL )
247
        return ;
248 249
    qint64  beg = m_ui->rulerWidget->getMarker( PreviewRuler::Start );
    qint64  end = m_ui->rulerWidget->getMarker( PreviewRuler::Stop );
250

251 252 253
    if ( beg == -1 && end == -1 )
        return ;

254 255
    if ( end < beg )
        return ;
256

257
    beg = beg < 0 ? 0 : beg;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
258
    Clip*   part = new Clip( clip, beg, end );
259 260

    //Adding the newly created clip to the media
261 262
    if ( clip->addSubclip( part ) == false )
        delete part;
263
}
264 265 266 267 268

void
PreviewWidget::error()
{
    QMessageBox::warning( this, tr( "Rendering error" ),
Nikoli's avatar
Nikoli committed
269
                          tr( "An error occurred while rendering.\nPlease check your VLC installation"
270 271
                              " before reporting the issue.") );
}