AbstractGraphicsMediaItem.cpp 5.15 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
#include "TracksView.h"

Ludovic Fauvet's avatar
Ludovic Fauvet committed
26
#include "Clip.h"
27
#include "Commands.h"
28

29
AbstractGraphicsMediaItem::AbstractGraphicsMediaItem() :
30
        oldTrackNumber( -1 ), oldPosition( -1 ), m_tracksView( NULL ),
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
31
        m_group( NULL ), m_width( 0 ), m_height( 0 ), m_resizeExpected( false )
32
{
33

34 35
}

36 37 38 39 40
AbstractGraphicsMediaItem::~AbstractGraphicsMediaItem()
{
    ungroup();
}

41 42 43 44
TracksView* AbstractGraphicsMediaItem::tracksView()
{
    return m_tracksView;
}
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
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;
}

63
quint32 AbstractGraphicsMediaItem::trackNumber()
64 65 66
{
    if ( parentItem() )
    {
67
        GraphicsTrack* graphicsTrack = qgraphicsitem_cast<GraphicsTrack*>( parentItem() );
68 69 70 71 72
        if ( graphicsTrack )
            return graphicsTrack->trackNumber();
    }
    return -1;
}
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

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;
}
94 95 96 97 98 99 100 101 102 103

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

qint64 AbstractGraphicsMediaItem::startPos()
{
    return qRound64( QGraphicsItem::pos().x() );
}
104 105 106 107 108

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

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

112 113 114 115
    if ( clip()->getParent()->getFileType() != Media::Image )
        if ( size > clip()->getMaxEnd() )
            return;

116
    if ( from == BEGINNING )
117
    {
118 119 120
        if ( clip()->getParent()->getFileType() != Media::Image )
            if ( clip()->getBegin() + size > clip()->getMaxEnd() )
                return;
121
        tracksView()->getRenderer()->resizeClip( clip(), clip()->getBegin(), clip()->getBegin() + size, 0, //This parameter is unused in this case
122
                                                 trackNumber(), mediaType() );
123
    }
124 125
    else
    {
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        if ( clip()->getParent()->getFileType() != Media::Image )
        {
            qint64 newBegin = qMax( clip()->getEnd() - size, (qint64)0 );
            if ( clip()->getMaxBegin() > newBegin )
                return;

            m_resizeExpected = true;
            qint64 oldLength = clip()->getLength();
            tracksView()->getRenderer()->resizeClip( clip(), qMax( clip()->getEnd() - size, (qint64)0 ), clip()->getEnd(),
                                                     startPos() + ( oldLength - size ), trackNumber(), mediaType() );
            setStartPos( startPos() + ( oldLength - size ) );
        }
        else
        {
            m_resizeExpected = true;
            qint64 oldLength = clip()->getLength();
            tracksView()->getRenderer()->resizeClip( clip(), 0, size, startPos() + ( oldLength - size ),
                                                     trackNumber(), mediaType() );
            setStartPos( startPos() + ( oldLength - size ) );
        }
146 147 148 149 150 151 152
    }

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

void AbstractGraphicsMediaItem::adjustLength()
{
153 154 155 156 157
    if ( m_resizeExpected == true )
    {
        m_resizeExpected = false;
        return ;
    }
158 159 160
    Q_ASSERT( clip() );
    resize( clip()->getLength() );
}
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

bool AbstractGraphicsMediaItem::resizeZone( const QPointF& position )
{
    // Get the current transformation of the view and invert it.
    QTransform transform = tracksView()->transform().inverted();
    // Map the RESIZE_ZONE distance from the view to the item coordinates.
    QLine line = transform.map( QLine( 0, 0, RESIZE_ZONE, 0 ) );

    if ( position.x() < line.x2() ||
         position.x() > ( boundingRect().width() - line.x2() ) )
    {
        return true;
    }
    return false;
}