AbstractGraphicsMediaItem.cpp 8.5 KB
Newer Older
1 2 3
/*****************************************************************************
 * AbstractGraphicsMediaItem.h: Base class for media representation
 *****************************************************************************
4
 * Copyright (C) 2008-2014 VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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
#include "TracksView.h"
25
#include "TracksScene.h"
26
#include "Workflow/TrackWorkflow.h"
27
#include "GraphicsTrack.h"
28

29 30 31 32
#include "Media/Clip.h"
#include "Workflow/ClipHelper.h"
#include "Commands/Commands.h"
#include "Media/Media.h"
33

34 35 36 37
#include <QMenu>
#include <QColorDialog>
#include <QGraphicsSceneContextMenuEvent>

38 39
#include <QCoreApplication>

40
AbstractGraphicsMediaItem::AbstractGraphicsMediaItem( Clip* clip ) :
41
        m_muted( false )
42
{
43 44
    m_clipHelper = new ClipHelper( clip );
    // Adjust the width
45
    setWidth( clip->nbFrames() );
46 47
    // Automatically adjust future changes
    connect( m_clipHelper, SIGNAL( lengthUpdated() ), this, SLOT( adjustLength() ) );
48 49
    connect( clip, SIGNAL( unloaded( Clip* ) ),
             this, SLOT( clipDestroyed( Clip* ) ), Qt::DirectConnection );
50 51
}

52
AbstractGraphicsMediaItem::AbstractGraphicsMediaItem( ClipHelper* ch ) :
53
        m_clipHelper( ch ),
54
        m_muted( false )
55 56 57 58 59 60 61 62 63
{
    // Adjust the width
    setWidth( ch->length() );
    // Automatically adjust future changes
    connect( ch, SIGNAL( lengthUpdated() ), this, SLOT( adjustLength() ) );
    connect( ch->clip(), SIGNAL( unloaded( Clip* ) ),
             this, SLOT( clipDestroyed( Clip* ) ), Qt::DirectConnection );
}

64 65 66 67 68
AbstractGraphicsMediaItem::~AbstractGraphicsMediaItem()
{
    ungroup();
}

69 70
void
AbstractGraphicsMediaItem::contextMenuEvent( QGraphicsSceneContextMenuEvent* event )
71 72 73 74 75 76
{
    if ( !tracksView() )
        return;

    QMenu menu( tracksView() );

77 78
    QAction* removeAction = menu.addAction( tr( "Remove" ) );
    QAction* muteAction = menu.addAction( tr( "Mute" ) );
79 80
    muteAction->setCheckable( true );
    muteAction->setChecked( m_muted );
81 82 83 84 85

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

    if ( groupItem() )
86
        unlinkAction = menu.addAction( tr( "Unlink" ) );
87 88 89
    else
    {
        QList<QGraphicsItem*> items = scene()->selectedItems();
90
        linkAction = menu.addAction( tr( "Link" ) );
91 92 93 94 95

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

96 97
    menu.addSeparator();

98
    QAction* changeColorAction = menu.addAction( tr( "Set color" ) );
99

100 101 102 103 104 105 106
    QAction* selectedAction = menu.exec( event->screenPos() );

    if ( !selectedAction )
        return;

    if ( selectedAction == removeAction )
        scene()->askRemoveSelectedItems();
107 108
    else if ( selectedAction == muteAction )
    {
109 110
        qint32      trackId = trackNumber();
        Q_ASSERT( trackId >= 0 );
111 112
        if ( ( m_muted = muteAction->isChecked() ) )
        {
113
            tracksView()->m_mainWorkflow->muteClip( uuid(),
114
                                                    trackId,
115
                                                    trackType() );
116 117 118
        }
        else
        {
119
            tracksView()->m_mainWorkflow->unmuteClip( uuid(),
120
                                                    trackId,
121
                                                    trackType() );
122 123
        }
    }
124 125 126 127 128 129 130 131 132 133 134 135 136
    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 );

137 138 139 140
        if ( item1 == this )
            item1 = item2;
        //From here, the item we click on is "this" and the item to group is "item1"

141
        if ( item1->trackType() != trackType() )
142
        {
143 144
            qint32      item1TrackId = item1->trackNumber();
            Q_ASSERT( item1TrackId >= 0 );
145
            item1->group( this );
146
            tracksView()->moveItem( item1, item1TrackId , startPos() );
147
            track()->trackWorkflow()->moveClip( item1->clipHelper()->uuid(), startPos() );
148 149 150 151 152 153 154 155 156 157 158
        }
    }
    else if ( selectedAction == unlinkAction )
    {
        QList<QGraphicsItem*> items = scene()->selectedItems();

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

        item->ungroup();
    }
159
    else if ( selectedAction == changeColorAction )
160 161 162 163
    {
        m_itemColor = QColorDialog::getColor( m_itemColor, tracksView() );
        update();
    }
164 165 166

}

167 168
void
AbstractGraphicsMediaItem::clipDestroyed( Clip* clip )
169
{
170 171
    if ( m_tracksView != NULL )
        m_tracksView->removeClip( clip->uuid() );
172 173
}

174 175
ClipHelper*
AbstractGraphicsMediaItem::clipHelper()
176
{
177
    return m_clipHelper;
178
}
179

180 181
const ClipHelper*
AbstractGraphicsMediaItem::clipHelper() const
182
{
183
    return m_clipHelper;
184
}
185

186 187
const QUuid&
AbstractGraphicsMediaItem::uuid() const
188
{
189
    return m_clipHelper->uuid();
190
}
191

192
void
193
AbstractGraphicsMediaItem::setEmphasized( bool value )
194
{
195 196 197 198
    if ( value == true )
        setScale( 1.2 );
    else
        setScale( 1.0 );
199 200
}

201 202
bool
AbstractGraphicsMediaItem::hasResizeBoundaries() const
203
{
204
    return ( clipHelper()->clip()->getMedia()->fileType() != Media::Image );
205
}
206

207 208
qint64
AbstractGraphicsMediaItem::maxBegin() const
209
{
210
    return ( m_clipHelper->clip()->begin() );
211
}
212

213 214
qint64
AbstractGraphicsMediaItem::maxEnd() const
215
{
216
    return clipHelper()->clip()->end();
217
}
218

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
void
AbstractGraphicsMediaItem::hoverEnterEvent( QGraphicsSceneHoverEvent* event )
{
    TracksView* tv = tracksView();
    if ( tv )
    {
        switch ( tv->tool() )
        {
            case TOOL_DEFAULT:
            setCursor( Qt::OpenHandCursor );
            break;

            case TOOL_CUT:
            setCursor( QCursor( QPixmap( ":/images/editcut" ) ) );
            break;
        }
    }
    QGraphicsItem::hoverEnterEvent( event );
}

void
AbstractGraphicsMediaItem::mousePressEvent( QGraphicsSceneMouseEvent* event )
{
    if ( !tracksView() ) return;

    if ( tracksView()->tool() == TOOL_DEFAULT )
    {
        if ( resizeZone( event->pos() ) )
            setCursor( Qt::SizeHorCursor );
        else
            setCursor( Qt::ClosedHandCursor );
    }
    else if ( tracksView()->tool() == TOOL_CUT )
        emit split( this, qRound64( event->pos().x() ) );
}
254 255 256 257 258 259 260 261 262 263 264 265 266

qint64
AbstractGraphicsMediaItem::begin() const
{
    return m_clipHelper->begin();
}


qint64
AbstractGraphicsMediaItem::end() const
{
    return m_clipHelper->end();
}
267 268

void
269
AbstractGraphicsMediaItem::triggerMove( EffectUser *target, qint64 startPos )
270
{
271 272
    TrackWorkflow   *tw = qobject_cast<TrackWorkflow*>( target );
    if ( tw == NULL )
273
        return ;
274
    Commands::trigger( new Commands::Clip::Move( m_oldTrack, tw, m_clipHelper, startPos ) );
275 276 277 278 279 280 281
}

Workflow::Helper*
AbstractGraphicsMediaItem::helper()
{
    return m_clipHelper;
}
282 283

void
284
AbstractGraphicsMediaItem::triggerResize( EffectUser *target, Workflow::Helper *helper,
285 286 287 288 289
                                           qint64 newBegin, qint64 newEnd, qint64 pos )
{
    ClipHelper  *clipHelper = qobject_cast<ClipHelper*>( helper );
    if ( clipHelper == NULL )
        return ;
290 291 292
    TrackWorkflow   *tw = qobject_cast<TrackWorkflow*>( target );
    if ( tw == NULL )
        return ;
293
    Commands::trigger( new Commands::Clip::Resize( tw, clipHelper, newBegin,
294 295
                                                               newEnd, pos ) );
}
296 297 298 299 300 301

qint64
AbstractGraphicsMediaItem::itemHeight() const
{
    return 35;
}
302 303 304 305 306 307 308 309 310 311 312 313

qint32
AbstractGraphicsMediaItem::zSelected() const
{
    return 100;
}

qint32
AbstractGraphicsMediaItem::zNotSelected() const
{
    return 50;
}
314 315 316 317 318 319 320

void
AbstractGraphicsMediaItem::setStartPos( qint64 position )
{
    emit moved( position );
    AbstractGraphicsItem::setStartPos( position );
}