Timeline.cpp 8.17 KB
Newer Older
Ludovic Fauvet's avatar
Ludovic Fauvet committed
1 2 3
/*****************************************************************************
 * Timeline.cpp: Widget that handle the tracks
 *****************************************************************************
4
 * Copyright (C) 2008-2014 VideoLAN
Ludovic Fauvet's avatar
Ludovic Fauvet committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Authors: Ludovic Fauvet <etix@l0cal.com>
 *
 * 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.
 *****************************************************************************/

23
#include "Timeline.h"
24

25
#include "Project/Project.h"
26
#include "Main/Core.h"
27 28
#include "Media/Clip.h"
#include "Workflow/ClipHelper.h"
29 30
#include "TracksView.h"
#include "TracksScene.h"
Ludovic Fauvet's avatar
Ludovic Fauvet committed
31 32
#include "TracksControls.h"
#include "TracksRuler.h"
33 34
#include "Tools/VlmcDebug.h"
#include "Renderer/WorkflowRenderer.h"
35

36
#include <QHBoxLayout>
37
#include <QDomElement>
38 39
#include <QScrollBar>

40 41
Timeline*   Timeline::m_instance = NULL;

42 43 44
Timeline::Timeline( QWidget *parent )
    : QWidget( parent )
    , m_scale( 1.0 )
45
{
46 47
    Q_ASSERT( m_instance == NULL );
    m_instance = this;
48
    m_ui.setupUi( this );
49

50
    m_mainWorkflow = Project::getInstance()->workflow();
51

52
    m_tracksScene = new TracksScene( this );
53
    m_tracksView = new TracksView( m_tracksScene, m_mainWorkflow, m_renderer, m_ui.tracksFrame );
54 55 56
    m_tracksView->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
    m_tracksView->scale(1, 1);

57 58 59 60
    QHBoxLayout* tracksViewLayout = new QHBoxLayout();
    tracksViewLayout->setContentsMargins( 0, 0, 0, 0 );
    m_ui.tracksFrame->setLayout( tracksViewLayout );
    tracksViewLayout->addWidget( m_tracksView );
61

62
    m_tracksRuler = new TracksRuler( tracksView(), this );
63 64 65 66 67
    QHBoxLayout* tracksRulerLayout = new QHBoxLayout();
    tracksRulerLayout->setContentsMargins( 0, 0, 0, 0 );
    m_ui.rulerFrame->setLayout( tracksRulerLayout );
    tracksRulerLayout->addWidget( m_tracksRuler );

68 69 70 71 72 73
    m_tracksControls = new TracksControls( this );
    QHBoxLayout* tracksControlsLayout = new QHBoxLayout();
    tracksControlsLayout->setContentsMargins( 0, 0, 0, 0 );
    m_ui.controlsFrame->setLayout( tracksControlsLayout );
    tracksControlsLayout->addWidget( m_tracksControls );

74
    changeZoom( 10 );
75
    setDuration( 0 );
76 77

    // Scroll
78 79 80 81
    connect( m_tracksView->horizontalScrollBar(), SIGNAL( valueChanged(int) ),
             m_tracksRuler, SLOT( moveRuler(int) ) );
    connect( m_tracksView->verticalScrollBar(), SIGNAL( valueChanged(int) ),
             m_tracksControls->verticalScrollBar(), SLOT( setValue(int) ) );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
82 83
    connect( m_tracksControls->verticalScrollBar(), SIGNAL( valueChanged(int) ),
             m_tracksView->verticalScrollBar(), SLOT( setValue(int) ) );
84 85

    // Project duration change
86
    connect( m_tracksView, SIGNAL( durationChanged(int) ), this, SLOT( setDuration(int) ) );
87 88

    // Clear / reset
89
    connect( m_mainWorkflow, SIGNAL( cleared() ), m_tracksControls, SLOT( clear() ) );
90
    connect( m_mainWorkflow, SIGNAL( cleared() ), tracksView(), SLOT( clear() ) );
91

92
    // Tracks controls
93 94 95 96
    connect( m_tracksView, SIGNAL( videoTrackAdded(GraphicsTrack*) ),
             m_tracksControls, SLOT( addVideoTrack(GraphicsTrack*) ) );
    connect( m_tracksView, SIGNAL( audioTrackAdded(GraphicsTrack*) ),
             m_tracksControls, SLOT( addAudioTrack(GraphicsTrack*) ) );
97 98 99 100
    connect( m_tracksView, SIGNAL( videoTrackRemoved() ),
             m_tracksControls, SLOT( removeVideoTrack() ) );
    connect( m_tracksView, SIGNAL( audioTrackRemoved() ),
             m_tracksControls, SLOT( removeAudioTrack() ) );
101

102 103 104 105
    // Cursor position updates
    connect( m_tracksView->tracksCursor(), SIGNAL( cursorPositionChanged( qint64 ) ),
             m_renderer, SLOT( timelineCursorChanged(qint64) ) );

106
    m_tracksView->createLayout();
107 108 109

    connect( Core::getInstance(), SIGNAL( projectLoading( Project* ) ),
             this, SLOT( projectLoading( Project* ) ), Qt::DirectConnection );
110 111
}

112 113 114 115
Timeline::~Timeline()
{
}

116 117
void
Timeline::changeEvent( QEvent *e )
118
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
119 120
    switch ( e->type() )
    {
121
    case QEvent::LanguageChange:
Ludovic Fauvet's avatar
Ludovic Fauvet committed
122
        m_ui.retranslateUi( this );
123 124 125 126
        break;
    default:
        break;
    }
127
}
128

129 130
void
Timeline::clear()
131 132 133 134 135
{
    // The main workflow will ask the GUI to clear itself.
    m_mainWorkflow->clear();
}

136 137
void
Timeline::changeZoom( int factor )
138 139 140 141 142
{
    m_tracksRuler->setPixelPerMark( factor );
    m_scale = (double) FRAME_SIZE / m_tracksRuler->comboScale[factor];
    m_tracksView->setScale( m_scale );
}
143

144 145
void
Timeline::setDuration( int duration )
146 147 148 149
{
    m_tracksView->setDuration( duration );
    m_tracksRuler->setDuration( duration );
}
150

151 152
void
Timeline::setTool( ToolButtons button )
153 154 155 156
{
    tracksView()->setTool( button );
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170
void
Timeline::projectLoading( Project* project )
{
    m_renderer = project->workflowRenderer();
    // Frames updates
    connect( m_renderer, SIGNAL( frameChanged(qint64, Vlmc::FrameChangedReason) ),
             m_tracksView->tracksCursor(), SLOT( frameChanged( qint64, Vlmc::FrameChangedReason ) ),
             Qt::QueuedConnection );
    connect( m_renderer, SIGNAL( frameChanged(qint64,Vlmc::FrameChangedReason) ),
             m_tracksRuler, SLOT( update() ) );
    connect( m_tracksRuler, SIGNAL( frameChanged(qint64,Vlmc::FrameChangedReason) ),
             m_renderer, SLOT( rulerCursorChanged(qint64)) );
}

171 172 173 174 175 176 177 178 179 180 181 182 183
void
Timeline::save( QXmlStreamWriter &project ) const
{
    project.writeStartElement( "timeline" );
    for ( int i = 0; i < tracksView()->m_scene->items().size(); ++i )
    {
        AbstractGraphicsMediaItem* item =
                dynamic_cast<AbstractGraphicsMediaItem*>( tracksView()->m_scene->items().at( i ) );

        if ( item == NULL )
            continue ;
        project.writeStartElement( "item" );
        project.writeAttribute( "uuid", item->clipHelper()->uuid().toString() );
184 185
        if ( item->itemColor().isValid() == true )
            project.writeAttribute( "color", item->itemColor().name() );
186 187 188 189
        if ( item->groupItem() != NULL )
        {
            project.writeStartElement( "linkedTo" );
                project.writeStartElement( "item" );
190
                    project.writeAttribute( "uuid", item->groupItem()->uuid().toString() );
191 192 193
                project.writeEndElement();
            project.writeEndElement();
        }
194 195 196 197
        project.writeEndElement();
    }
    project.writeEndDocument();
}
198 199 200 201 202 203 204

void
Timeline::load( const QDomElement &root )
{
    QDomElement     project = root.firstChildElement( "timeline" );
    if ( project.isNull() == true )
    {
205
        vlmcCritical() << "No timeline node in the project file";
206 207 208
        return ;
    }

209
    QDomElement elem = project.firstChildElement();
210 211 212 213 214 215 216
    while ( elem.isNull() == false )
    {
        QString uuid = elem.attribute( "uuid" );

        AbstractGraphicsMediaItem   *item = tracksView()->item( uuid );
        if ( item != NULL )
        {
217 218 219 220 221
            if ( elem.hasAttribute( "color" ) == true )
            {
                QString     color = elem.attribute( "color" );
                item->setColor( color );
            }
222 223 224
            QDomElement links = elem.firstChildElement( "linkedTo" );
            if ( links.isNull() == false )
            {
225
                QDomElement linkedItem = links.firstChildElement();
226 227 228 229 230 231
                while ( linkedItem.isNull() == false )
                {
                    QString     linkedUuid = linkedItem.attribute( "uuid" );
                    AbstractGraphicsMediaItem   *li = tracksView()->item( linkedUuid );
                    if ( li != NULL )
                        item->group( li );
232
                    linkedItem = linkedItem.nextSiblingElement();
233 234
                }
            }
235 236
        }
        else
237
            vlmcWarning() << "No such timeline item:" << uuid;
238
        elem = elem.nextSiblingElement();
239 240
    }
}