GraphicsMovieItem.cpp 5.62 KB
Newer Older
1 2 3
/*****************************************************************************
 * GraphicsMovieItem.cpp: Represent a movie graphically in the timeline
 *****************************************************************************
4
 * Copyright (C) 2008-2016 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 "Media/Clip.h"
24
#include "GraphicsMovieItem.h"
25
#include "Media/Media.h"
26 27 28
#include "TracksView.h"
#include "Timeline.h"

29
#include <QPainter>
30
#include <QLinearGradient>
Ludovic Fauvet's avatar
Ludovic Fauvet committed
31
#include <QTime>
Ludovic Fauvet's avatar
Ludovic Fauvet committed
32
#include <QFontMetrics>
Ludovic Fauvet's avatar
Ludovic Fauvet committed
33
#include <QGraphicsSceneMouseEvent>
34

35 36
GraphicsMovieItem::GraphicsMovieItem( Clip* clip ) :
        AbstractGraphicsMediaItem( clip )
37
{
38
    QTime length = QTime().addMSecs( clip->media()->producer()->length() );
39 40
    QString tooltip( tr( "<p style='white-space:pre'><b>Name:</b> %1"
                     "<br><b>Length:</b> %2" )
luyikei's avatar
luyikei committed
41
                     .arg( clip->media()->fileName() )
42
                     .arg( length.toString("hh:mm:ss.zzz") ) );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
43
    setToolTip( tooltip );
44 45 46 47 48 49
}

GraphicsMovieItem::~GraphicsMovieItem()
{
}

50
Workflow::TrackType GraphicsMovieItem::trackType() const
51
{
52
    return Workflow::VideoTrack;
53 54
}

55 56
void
GraphicsMovieItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* )
57
{
58
    painter->save();
59
    paintRect( painter, option );
60
    painter->restore();
61

62
    painter->save();
63
    paintTitle( painter, option );
64
    painter->restore();
65 66
}

67 68
void
GraphicsMovieItem::paintRect( QPainter* painter, const QStyleOptionGraphicsItem* option )
69
{
70 71 72
    QRectF drawRect;
    bool drawRound;

73 74 75 76 77 78
    // Disable the matrix transformations
    painter->setWorldMatrixEnabled( false );

    painter->setRenderHint( QPainter::Antialiasing );

    // Get the transformations required to map the text on the viewport
luyikei's avatar
luyikei committed
79
    QTransform viewPortTransform = Timeline::instance()->tracksView()->viewportTransform();
80 81

    // Determine if a drawing optimization can be used
82 83
    if ( option->exposedRect.left() > AbstractGraphicsItem::RounderRectRadius &&
         option->exposedRect.right() < boundingRect().right() - AbstractGraphicsItem::RounderRectRadius )
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    {
        // Optimized: paint only the exposed (horizontal) area
        drawRect = QRectF( option->exposedRect.left(),
                           boundingRect().top(),
                           option->exposedRect.right(),
                           boundingRect().bottom() );
        drawRound = false;
    }
    else
    {
        // Unoptimized: the item must be fully repaint
        drawRect = boundingRect();
        drawRound = true;
    }

99
    // Do the transformation
100
    QRectF mapped = deviceTransform( viewPortTransform ).mapRect( drawRect );
101 102 103 104 105 106 107 108 109

    QLinearGradient gradient( mapped.topLeft(), mapped.bottomLeft() );
    gradient.setColorAt( 0, QColor::fromRgb( 78, 78, 78 ) );
    gradient.setColorAt( 0.4, QColor::fromRgb( 72, 72, 72 ) );
    gradient.setColorAt( 0.4, QColor::fromRgb( 50, 50, 50 ) );
    gradient.setColorAt( 1, QColor::fromRgb( 45, 45, 45 ) );

    painter->setPen( Qt::NoPen );
    painter->setBrush( QBrush( gradient ) );
110 111

    if ( drawRound )
112 113
        painter->drawRoundedRect( mapped, AbstractGraphicsItem::RounderRectRadius,
                                  AbstractGraphicsItem::RounderRectRadius );
114 115
    else
        painter->drawRect( mapped );
116

117 118 119 120 121 122 123
    if ( itemColor().isValid() )
    {
        QRectF mediaColorRect = mapped.adjusted( 3, 2, -3, -2 );
        painter->setPen( QPen( itemColor(), 2 ) );
        painter->drawLine( mediaColorRect.topLeft(), mediaColorRect.topRight() );
    }

124 125
    if ( isSelected() )
    {
126
        setZValue( zSelected() );
127
        painter->setPen( Qt::yellow );
128 129 130
        painter->setBrush( Qt::NoBrush );
        mapped.adjust( 0, 0, 0, -1 );
        if ( drawRound )
131 132
            painter->drawRoundedRect( mapped, AbstractGraphicsItem::RounderRectRadius,
                                      AbstractGraphicsItem::RounderRectRadius);
133 134
        else
            painter->drawRect( mapped );
135 136
    }
    else
137
        setZValue( zNotSelected() );
138 139
}

140 141
void
GraphicsMovieItem::paintTitle( QPainter* painter, const QStyleOptionGraphicsItem* option )
142
{
143 144
    Q_UNUSED( option );

145 146 147 148 149 150 151 152 153 154
    // Disable the matrix transformations
    painter->setWorldMatrixEnabled( false );

    // Setup the font
    QFont f = painter->font();
    f.setPointSize( 8 );
    painter->setFont( f );

    // Initiate the font metrics calculation
    QFontMetrics fm( painter->font() );
155
    QString text = m_clip->media()->fileName();
156

157
    // Get the transformations required to map the text on the viewport
luyikei's avatar
luyikei committed
158
    QTransform viewPortTransform = Timeline::instance()->tracksView()->viewportTransform();
159 160 161 162
    // Do the transformation
    QRectF mapped = deviceTransform( viewPortTransform ).mapRect( boundingRect() );
    // Create an inner rect
    mapped.adjust( 2, 2, -2, -2 );
163

164 165
    painter->setPen( Qt::white );
    painter->drawText( mapped, Qt::AlignVCenter, fm.elidedText( text, Qt::ElideRight, mapped.width() ) );
166
}