GraphicsAudioItem.cpp 7.54 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * GraphicsAudioItem.cpp: Represent an audio region graphically in the
 * timeline
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
5
 * Copyright (C) 2008-2010 VideoLAN
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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 "GraphicsAudioItem.h"
25 26

#include "ClipHelper.h"
27
#include "Media.h"
28 29 30
#include "TracksView.h"
#include "Timeline.h"

31 32 33
#include <QPainter>
#include <QLinearGradient>
#include <QTime>
Ludovic Fauvet's avatar
Ludovic Fauvet committed
34 35
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneHoverEvent>
36

37 38
GraphicsAudioItem::GraphicsAudioItem( Clip* clip ) :
        AbstractGraphicsMediaItem( clip )
39 40 41
{
    setFlags( QGraphicsItem::ItemIsSelectable );

42
    QTime length = QTime().addMSecs( clip->getMedia()->lengthMS() );
43 44
    QString tooltip( tr( "<p style='white-space:pre'><b>Name:</b> %1"
                     "<br><b>Length:</b> %2" )
45
                     .arg( clip->getMedia()->fileName() )
46 47 48 49 50
                     .arg( length.toString("hh:mm:ss.zzz") ) );
    setToolTip( tooltip );
    setAcceptHoverEvents( true );
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64
GraphicsAudioItem::GraphicsAudioItem( ClipHelper* ch ) :
        AbstractGraphicsMediaItem( ch )
{
    setFlags( QGraphicsItem::ItemIsSelectable );

    QTime length = QTime().addMSecs( ch->clip()->getMedia()->lengthMS() );
    QString tooltip( tr( "<p style='white-space:pre'><b>Name:</b> %1"
                     "<br><b>Length:</b> %2" )
                     .arg( ch->clip()->getMedia()->fileName() )
                     .arg( length.toString("hh:mm:ss.zzz") ) );
    setToolTip( tooltip );
    setAcceptHoverEvents( true );
}

65 66 67 68
GraphicsAudioItem::~GraphicsAudioItem()
{
}

69 70 71 72 73
MainWorkflow::TrackType GraphicsAudioItem::mediaType() const
{
    return MainWorkflow::AudioTrack;
}

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
void GraphicsAudioItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* )
{
    painter->save();
    paintRect( painter, option );
    painter->restore();

    painter->save();
    paintTitle( painter, option );
    painter->restore();
}

void GraphicsAudioItem::paintRect( QPainter* painter, const QStyleOptionGraphicsItem* option )
{
    QRectF drawRect;
    bool drawRound;

    // Disable the matrix transformations
    painter->setWorldMatrixEnabled( false );

    painter->setRenderHint( QPainter::Antialiasing );

    // Get the transformations required to map the text on the viewport
    QTransform viewPortTransform = Timeline::getInstance()->tracksView()->viewportTransform();

    // Determine if a drawing optimization can be used
    if ( option->exposedRect.left() > ROUNDED_RECT_RADIUS &&
         option->exposedRect.right() < boundingRect().right() - ROUNDED_RECT_RADIUS )
    {
        // 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;
    }

    // Do the transformation
    QRectF mapped = deviceTransform( viewPortTransform ).mapRect( drawRect );

    QLinearGradient gradient( mapped.topLeft(), mapped.bottomLeft() );

121 122 123 124
    gradient.setColorAt( 0, QColor::fromRgb( 88, 88, 88 ) );
    gradient.setColorAt( 0.4, QColor::fromRgb( 82, 82, 82 ) );
    gradient.setColorAt( 0.4, QColor::fromRgb( 60, 60, 60 ) );
    gradient.setColorAt( 1, QColor::fromRgb( 55, 55, 55 ) );
125 126 127 128 129 130 131 132 133

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

    if ( drawRound )
        painter->drawRoundedRect( mapped, ROUNDED_RECT_RADIUS, ROUNDED_RECT_RADIUS );
    else
        painter->drawRect( mapped );

134 135 136 137 138 139 140
    if ( itemColor().isValid() )
    {
        QRectF mediaColorRect = mapped.adjusted( 3, 2, -3, -2 );
        painter->setPen( QPen( itemColor(), 2 ) );
        painter->drawLine( mediaColorRect.topLeft(), mediaColorRect.topRight() );
    }

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    if ( isSelected() )
    {
        setZValue( Z_SELECTED );
        painter->setPen( Qt::yellow );
        painter->setBrush( Qt::NoBrush );
        mapped.adjust( 0, 0, 0, -1 );
        if ( drawRound )
            painter->drawRoundedRect( mapped, ROUNDED_RECT_RADIUS, ROUNDED_RECT_RADIUS );
        else
            painter->drawRect( mapped );
    }
    else
        setZValue( Z_NOT_SELECTED );
}

void GraphicsAudioItem::paintTitle( QPainter* painter, const QStyleOptionGraphicsItem* option )
{
    Q_UNUSED( option );

    // 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() );
170
    QString text = m_clipHelper->clip()->getMedia()->fileName();
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

    // Get the transformations required to map the text on the viewport
    QTransform viewPortTransform = Timeline::getInstance()->tracksView()->viewportTransform();
    // Do the transformation
    QRectF mapped = deviceTransform( viewPortTransform ).mapRect( boundingRect() );
    // Create an inner rect
    mapped.adjust( 2, 2, -2, -2 );

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

void GraphicsAudioItem::hoverEnterEvent( QGraphicsSceneHoverEvent* event )
{
    TracksView* tv = Timeline::getInstance()->tracksView();
    if ( tv )
    {
        switch ( tv->tool() )
        {
            case TOOL_DEFAULT:
            setCursor( Qt::OpenHandCursor );
            break;

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

    AbstractGraphicsMediaItem::hoverEnterEvent( event );
}

void GraphicsAudioItem::hoverLeaveEvent( QGraphicsSceneHoverEvent* event )
{
    AbstractGraphicsMediaItem::hoverLeaveEvent( event );
}

208 209 210 211 212 213 214 215 216 217 218 219 220
void GraphicsAudioItem::hoverMoveEvent( QGraphicsSceneHoverEvent* event )
{
    if ( !tracksView() ) return;

    if ( tracksView()->tool() == TOOL_DEFAULT )
    {
        if ( resizeZone( event->pos() ) )
            setCursor( Qt::SizeHorCursor );
        else
            setCursor( Qt::OpenHandCursor );
    }
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
void GraphicsAudioItem::mousePressEvent( QGraphicsSceneMouseEvent* event )
{
    TracksView* tv = Timeline::getInstance()->tracksView();
    if ( tv->tool() == TOOL_DEFAULT )
        setCursor( Qt::ClosedHandCursor );
    else if ( tv->tool() == TOOL_CUT )
        emit split( this, qRound64( event->pos().x() ) );
}

void GraphicsAudioItem::mouseReleaseEvent( QGraphicsSceneMouseEvent*  event )
{
    Q_UNUSED( event );
    TracksView* tv = Timeline::getInstance()->tracksView();
    if ( tv->tool() == TOOL_DEFAULT )
        setCursor( Qt::OpenHandCursor );
}