AbstractGraphicsMediaItem.cpp 8.14 KB
Newer Older
1 2 3
/*****************************************************************************
 * AbstractGraphicsMediaItem.h: Base class for media representation
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
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 <QMenu>
24
#include <QColorDialog>
25
#include "AbstractGraphicsMediaItem.h"
26
#include "TracksView.h"
27
#include "TracksScene.h"
28
#include "GraphicsTrack.h"
29

Ludovic Fauvet's avatar
Ludovic Fauvet committed
30
#include "Clip.h"
31
#include "Commands.h"
32

33
AbstractGraphicsMediaItem::AbstractGraphicsMediaItem() :
34
        oldTrackNumber( -1 ), oldPosition( -1 ), m_tracksView( NULL ),
35 36
        m_group( NULL ), m_width( 0 ), m_height( 0 ), m_resizeExpected( false ),
        m_muted( false )
37
{
38

39 40
}

41 42 43 44 45
AbstractGraphicsMediaItem::~AbstractGraphicsMediaItem()
{
    ungroup();
}

46 47 48 49 50
TracksScene* AbstractGraphicsMediaItem::scene()
{
    return qobject_cast<TracksScene*>( QGraphicsItem::scene() );
}

51 52 53 54
TracksView* AbstractGraphicsMediaItem::tracksView()
{
    return m_tracksView;
}
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
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;
}

73
quint32 AbstractGraphicsMediaItem::trackNumber()
74 75 76
{
    if ( parentItem() )
    {
77
        GraphicsTrack* graphicsTrack = qgraphicsitem_cast<GraphicsTrack*>( parentItem() );
78 79 80 81 82
        if ( graphicsTrack )
            return graphicsTrack->trackNumber();
    }
    return -1;
}
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

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;
}
104

105 106 107 108 109 110 111 112
void AbstractGraphicsMediaItem::contextMenuEvent( QGraphicsSceneContextMenuEvent* event )
{
    if ( !tracksView() )
        return;

    QMenu menu( tracksView() );

    QAction* removeAction = menu.addAction( "Remove" );
113 114 115
    QAction* muteAction = menu.addAction( "Mute" );
    muteAction->setCheckable( true );
    muteAction->setChecked( m_muted );
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

    QAction* linkAction = NULL;
    QAction* unlinkAction = NULL;

    if ( groupItem() )
        unlinkAction = menu.addAction( "Unlink" );
    else
    {
        QList<QGraphicsItem*> items = scene()->selectedItems();
        linkAction = menu.addAction( "Link" );

        if ( items.count() != 2 )
            linkAction->setEnabled( false );
    }

131 132
    menu.addSeparator();

133
    QAction* changeColorAction = menu.addAction( "Set color" );
134

135 136 137 138 139 140 141
    QAction* selectedAction = menu.exec( event->screenPos() );

    if ( !selectedAction )
        return;

    if ( selectedAction == removeAction )
        scene()->askRemoveSelectedItems();
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    else if ( selectedAction == muteAction )
    {

        if ( ( m_muted = muteAction->isChecked() ) )
        {
            tracksView()->m_mainWorkflow->muteClip( clip()->getUuid(),
                                                    trackNumber(),
                                                    mediaType() );
        }
        else
        {
            tracksView()->m_mainWorkflow->unmuteClip( clip()->getUuid(),
                                                    trackNumber(),
                                                    mediaType() );
        }
    }
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    else if ( selectedAction == linkAction )
    {
        QList<QGraphicsItem*> items = scene()->selectedItems();

        AbstractGraphicsMediaItem* item1;
        AbstractGraphicsMediaItem* item2;

        item1 = dynamic_cast<AbstractGraphicsMediaItem*>( items.at( 0 ) );
        item2 = dynamic_cast<AbstractGraphicsMediaItem*>( items.at( 1 ) );

        Q_ASSERT( item1 );
        Q_ASSERT( item2 );

        if ( item1->mediaType() != item2->mediaType() )
        {
            item1->group( item2 );
            tracksView()->moveMediaItem( item1, item1->trackNumber(), item1->startPos() );
        }
    }
    else if ( selectedAction == unlinkAction )
    {
        QList<QGraphicsItem*> items = scene()->selectedItems();

        AbstractGraphicsMediaItem* item;
        item = dynamic_cast<AbstractGraphicsMediaItem*>( items.at( 0 ) );

        item->ungroup();
    }
186
    else if ( selectedAction == changeColorAction )
187 188 189 190
    {
        m_itemColor = QColorDialog::getColor( m_itemColor, tracksView() );
        update();
    }
191 192 193

}

194 195 196 197 198 199 200 201 202
void AbstractGraphicsMediaItem::setStartPos( qint64 position )
{
    QGraphicsItem::setPos( (qreal)position, 0 );
}

qint64 AbstractGraphicsMediaItem::startPos()
{
    return qRound64( QGraphicsItem::pos().x() );
}
203 204 205 206 207

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

208
    if ( size < 1 )
Ludovic Fauvet's avatar
Ludovic Fauvet committed
209
        return;
210

211 212 213 214
    if ( clip()->getParent()->getFileType() != Media::Image )
        if ( size > clip()->getMaxEnd() )
            return;

215
    if ( from == BEGINNING )
216
    {
217 218 219
        if ( clip()->getParent()->getFileType() != Media::Image )
            if ( clip()->getBegin() + size > clip()->getMaxEnd() )
                return;
220 221 222
        //FIXME
//        tracksView()->getRenderer()->resizeClip( clip(), clip()->getBegin(), clip()->getBegin() + size, 0, //This parameter is unused in this case
//                                                 trackNumber(), mediaType() );
223
    }
224 225
    else
    {
226 227 228 229 230 231 232 233
        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();
234 235 236
            qint64  newStart = startPos() + ( oldLength - size );
            if ( newStart < 0 )
                return ;
237 238 239 240
//            tracksView()->getRenderer()->resizeClip( clip(), qMax( clip()->getEnd() - size, (qint64)0 ), clip()->getEnd(),
//                                                     newStart, trackNumber(), mediaType() );
//            setStartPos( newStart );
            //FIXME
241 242 243 244 245
        }
        else
        {
            m_resizeExpected = true;
            qint64 oldLength = clip()->getLength();
246 247 248 249
//            tracksView()->getRenderer()->resizeClip( clip(), 0, size, startPos() + ( oldLength - size ),
//                                                     trackNumber(), mediaType() );
//            setStartPos( startPos() + ( oldLength - size ) );
            //FIXME
250
        }
251 252 253 254 255 256 257
    }

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

void AbstractGraphicsMediaItem::adjustLength()
{
258 259 260 261 262
    if ( m_resizeExpected == true )
    {
        m_resizeExpected = false;
        return ;
    }
263 264 265
    Q_ASSERT( clip() );
    resize( clip()->getLength() );
}
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

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;
}
281 282 283 284 285

QColor AbstractGraphicsMediaItem::itemColor()
{
    return m_itemColor;
}