GraphicsCursorItem.cpp 3.48 KB
Newer Older
Ludovic Fauvet's avatar
Ludovic Fauvet committed
1 2 3
/*****************************************************************************
 * GraphicsCursorItem.cpp: Timeline's cursor
 *****************************************************************************
4
 * Copyright (C) 2008-2014 VideoLAN
Ludovic Fauvet's avatar
Ludovic Fauvet committed
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 <QGraphicsScene>
24
#include <QGraphicsItem>
Ludovic Fauvet's avatar
Ludovic Fauvet committed
25 26 27 28 29
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QCursor>
#include <QPen>
#include <QRectF>
30 31
#include "GraphicsCursorItem.h"

32
GraphicsCursorItem::GraphicsCursorItem( const QPen& pen ) :
33
        m_pen( pen ), m_mouseDown( false )
34
{
35
    setFlags( QGraphicsItem::ItemIgnoresTransformations |
Ludovic Fauvet's avatar
Ludovic Fauvet committed
36 37 38 39 40
              QGraphicsItem::ItemIsMovable );
#if QT_VERSION >= 0x040600
    setFlag( QGraphicsItem::ItemSendsGeometryChanges );
#endif

41
    setCursor( QCursor( Qt::SizeHorCursor ) );
42
    setZValue( 100 );
43

Ludovic Fauvet's avatar
Ludovic Fauvet committed
44
    m_boundingRect = QRectF( -2, 0, 3, 0 );
45 46
}

47 48
QRectF
GraphicsCursorItem::boundingRect() const
49 50 51 52
{
    return m_boundingRect;
}

53 54
void
GraphicsCursorItem::paint( QPainter* painter, const QStyleOptionGraphicsItem*, QWidget* )
55 56
{
    painter->setPen( m_pen );
57
    painter->drawLine( 0, 0, 0, m_boundingRect.height() );
58
}
59

60 61
QVariant
GraphicsCursorItem::itemChange( GraphicsItemChange change, const QVariant& value )
62
{
63
    //Position is changing :
64
    if ( change == QGraphicsItem::ItemPositionChange )
65
    {
66 67 68 69 70
        // When the cursor is moving fast, the viewport buffer
        // is not correctly updated, forcing it now.
        scene()->update( pos().x(), pos().y(), m_boundingRect.width(), m_boundingRect.height() );

        // Keep the y axis in-place.
71 72
        qreal posX = value.toPointF().x();
        if ( posX < 0 ) posX = 0;
73
        return QPoint( ( int ) posX, ( int ) pos().y() );
74
    }
75
    //The position HAS changed, ie we released the slider, or setPos has been called.
76
    else if ( change == QGraphicsItem::ItemPositionHasChanged )
77
    {
78 79
        if ( m_mouseDown )
            emit cursorPositionChanged( ( qint64 ) pos().x() );
80
        emit cursorMoved( ( qint64 ) pos().x() );
81
    }
82 83 84
    return QGraphicsItem::itemChange( change, value );
}

85 86
void
GraphicsCursorItem::mousePressEvent( QGraphicsSceneMouseEvent* event )
87 88 89 90 91 92 93
{
    m_mouseDown = true;

    event->accept();
    QGraphicsItem::mousePressEvent( event );
}

94 95
void
GraphicsCursorItem::mouseReleaseEvent( QGraphicsSceneMouseEvent* event )
96 97 98 99 100 101 102
{
    m_mouseDown = false;

    event->accept();
    QGraphicsItem::mouseReleaseEvent( event );
}

103 104
void
GraphicsCursorItem::frameChanged( qint64 newFrame, Vlmc::FrameChangedReason reason )
105
{
106
    if ( reason != Vlmc::TimelineCursor )
107 108 109
    {
        setPos( newFrame, pos().y() );
    }
110
}
111

112 113
void
GraphicsCursorItem::setHeight( int height )
114
{
115
    prepareGeometryChange();
116 117
    m_boundingRect.setHeight( height );
}