AbstractGraphicsMediaItem.cpp 4.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*****************************************************************************
 * AbstractGraphicsMediaItem.h: Base class for media representation
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * 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.
 *****************************************************************************/

#include "AbstractGraphicsMediaItem.h"
24 25 26
#include "TracksView.h"

#include "Commands.h"
27

28
AbstractGraphicsMediaItem::AbstractGraphicsMediaItem() :
29 30
        oldTrackNumber( -1 ), oldPosition( -1 ), m_tracksView( NULL ),
        m_group( NULL ), m_width( 0 ), m_height( 0 )
31
{
32

33 34 35 36 37 38
}

TracksView* AbstractGraphicsMediaItem::tracksView()
{
    return m_tracksView;
}
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
QRectF AbstractGraphicsMediaItem::boundingRect() const
{
    return QRectF( 0, 0, (qreal)m_width, (qreal)m_height );
}

void AbstractGraphicsMediaItem::setWidth( qint64 width )
{
    prepareGeometryChange();
    m_width = width;
}

void AbstractGraphicsMediaItem::setHeight( qint64 height )
{
    prepareGeometryChange();
    m_height = height;
}

57
quint32 AbstractGraphicsMediaItem::trackNumber()
58 59 60
{
    if ( parentItem() )
    {
61
        GraphicsTrack* graphicsTrack = qgraphicsitem_cast<GraphicsTrack*>( parentItem() );
62 63 64 65 66
        if ( graphicsTrack )
            return graphicsTrack->trackNumber();
    }
    return -1;
}
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

void AbstractGraphicsMediaItem::group( AbstractGraphicsMediaItem* item )
{
    Q_ASSERT( item );
    if ( m_group )
        ungroup();
    item->m_group = this;
    m_group = item;
}

void AbstractGraphicsMediaItem::ungroup()
{
    if ( !m_group ) return;
    m_group->m_group = NULL;
    m_group = NULL;
}

AbstractGraphicsMediaItem* AbstractGraphicsMediaItem::groupItem()
{
    return m_group;
}
88 89 90 91 92 93 94 95 96 97

void AbstractGraphicsMediaItem::setStartPos( qint64 position )
{
    QGraphicsItem::setPos( (qreal)position, 0 );
}

qint64 AbstractGraphicsMediaItem::startPos()
{
    return qRound64( QGraphicsItem::pos().x() );
}
98 99 100 101 102

void AbstractGraphicsMediaItem::resize( qint64 size, From from )
{
    Q_ASSERT( clip() );

Ludovic Fauvet's avatar
Ludovic Fauvet committed
103
    if ( size > clip()->getParent()->getNbFrames() )
104 105
    {
        qWarning( "AbstractGraphicsMediaItem: Can't expand a clip beyond its original size" );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
106
        return;
107 108
    }

Ludovic Fauvet's avatar
Ludovic Fauvet committed
109 110
    if ( size < 0 ) return;

111
    if ( from == BEGINNING )
112 113 114 115 116
    {
//        clip()->setEnd( clip()->getBegin() + size );
        tracksView()->getRenderer()->resizeClip( clip(), clip()->getBegin(), clip()->getBegin() + size, boundingRect().x(),
                                                 trackNumber(), MainWorkflow::VideoTrack );
    }
117 118
    else
    {
Ludovic Fauvet's avatar
Ludovic Fauvet committed
119 120 121 122 123 124
        qint64 oldLength = clip()->getLength();
        if ( ( clip()->getEnd() - size ) < 0 )
        {
            qWarning( "Warning: resizing a region with a size below 0" );
            size += clip()->getEnd() - size;
        }
125 126 127
//        clip()->setBegin( qMax( clip()->getEnd() - size, (qint64)0 ) );
        tracksView()->getRenderer()->resizeClip( clip(), qMax( clip()->getEnd() - size, (qint64)0 ), clip()->getEnd(),
                                                 startPos() + ( oldLength - size ), trackNumber(), MainWorkflow::VideoTrack );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
128
        setStartPos( startPos() + ( oldLength - size ) );
129 130 131 132 133 134 135 136 137 138
    }

    setWidth( clip()->getLength() );
}

void AbstractGraphicsMediaItem::adjustLength()
{
    Q_ASSERT( clip() );
    resize( clip()->getLength() );
}