Commit e945a3f6 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

GraphicsItems: Splitting common GraphicsItem part in a new class.

This intend to ease the development of a new GraphicsEffectItem.
parent aa5755f5
......@@ -194,6 +194,7 @@ ELSE(NOT WITH_GUI)
Gui/settings/PreferenceWidget.cpp
Gui/settings/Settings.cpp
Gui/settings/StringWidget.cpp
Gui/timeline/AbstractGraphicsItem.cpp
Gui/timeline/AbstractGraphicsMediaItem.cpp
Gui/timeline/GraphicsAudioItem.cpp
Gui/timeline/GraphicsCursorItem.cpp
......@@ -266,6 +267,7 @@ ELSE(NOT WITH_GUI)
Gui/settings/PreferenceWidget.h
Gui/settings/Settings.h
Gui/settings/StringWidget.h
Gui/timeline/AbstractGraphicsItem.h
Gui/timeline/AbstractGraphicsMediaItem.h
Gui/timeline/GraphicsAudioItem.h
Gui/timeline/GraphicsCursorItem.h
......
/*****************************************************************************
* AbstractGraphiscItem.cpp: Represent an abstract item on the timeline
*****************************************************************************
* Copyright (C) 2008-2010 VideoLAN
*
* Authors: Hugo Beauzée-Luyssen <beauze.h@gmail.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 "AbstractGraphicsItem.h"
#include "GraphicsTrack.h"
#include "TracksView.h"
#include "TracksScene.h"
#include <QGraphicsSceneEvent>
AbstractGraphicsItem::AbstractGraphicsItem() :
m_tracksView( NULL ),
m_oldTrack( NULL ),
m_width( 0 ),
m_height( 0 )
{
setFlags( QGraphicsItem::ItemIsSelectable );
setAcceptHoverEvents( true );
}
AbstractGraphicsItem::~AbstractGraphicsItem()
{
}
TracksScene*
AbstractGraphicsItem::scene()
{
return qobject_cast<TracksScene*>( QGraphicsItem::scene() );
}
TracksView*
AbstractGraphicsItem::tracksView()
{
return m_tracksView;
}
QRectF
AbstractGraphicsItem::boundingRect() const
{
return QRectF( 0, 0, (qreal)m_width, (qreal)m_height );
}
void
AbstractGraphicsItem::setWidth( qint64 width )
{
prepareGeometryChange();
m_width = width;
}
void
AbstractGraphicsItem::setHeight( qint64 height )
{
prepareGeometryChange();
m_height = height;
}
qint32
AbstractGraphicsItem::trackNumber()
{
if ( parentItem() )
{
GraphicsTrack* graphicsTrack = qgraphicsitem_cast<GraphicsTrack*>( parentItem() );
if ( graphicsTrack )
return graphicsTrack->trackNumber();
}
return -1;
}
void
AbstractGraphicsItem::setTrack( GraphicsTrack* track )
{
setParentItem( track );
}
GraphicsTrack*
AbstractGraphicsItem::track()
{
return qgraphicsitem_cast<GraphicsTrack*>( parentItem() );
}
void
AbstractGraphicsItem::setStartPos( qint64 position )
{
QGraphicsItem::setPos( (qreal)position, 0 );
}
qint64
AbstractGraphicsItem::startPos()
{
return qRound64( QGraphicsItem::pos().x() );
}
qint64
AbstractGraphicsItem::resize( qint64 newSize, qint64 newBegin, qint64 clipPos,
From from )
{
if ( newSize < 1 )
return 1;
if ( hasResizeBoundaries() == true )
newSize = qMin( newSize, maxEnd() );
//The from actually stands for the clip bound that stays still.
if ( from == BEGINNING )
{
if ( hasResizeBoundaries() == true )
{
if ( newBegin + newSize > maxEnd() )
return newBegin + maxEnd();
}
setWidth( newSize );
return newSize;
}
else
{
if ( hasResizeBoundaries() )
{
if ( maxBegin() > newBegin )
return maxBegin();
}
setWidth( newSize );
setStartPos( clipPos );
return newBegin;
}
}
QColor
AbstractGraphicsItem::itemColor()
{
return m_itemColor;
}
void
AbstractGraphicsItem::setColor( const QColor &color )
{
m_itemColor = color;
}
void
AbstractGraphicsItem::hoverEnterEvent( QGraphicsSceneHoverEvent* event )
{
setCursor( Qt::OpenHandCursor );
QGraphicsItem::hoverEnterEvent( event );
}
void
AbstractGraphicsItem::hoverMoveEvent( QGraphicsSceneHoverEvent* event )
{
if ( !tracksView() ) return;
if ( tracksView()->tool() == TOOL_DEFAULT )
{
if ( resizeZone( event->pos() ) )
setCursor( Qt::SizeHorCursor );
else
setCursor( Qt::OpenHandCursor );
}
}
void
AbstractGraphicsItem::mousePressEvent( QGraphicsSceneMouseEvent* event )
{
if ( !tracksView() ) return;
if ( tracksView()->tool() == TOOL_DEFAULT )
{
if ( resizeZone( event->pos() ) )
setCursor( Qt::SizeHorCursor );
else
setCursor( Qt::ClosedHandCursor );
}
}
void
AbstractGraphicsItem::mouseReleaseEvent( QGraphicsSceneMouseEvent* )
{
if ( !tracksView() ) return;
if ( tracksView()->tool() == TOOL_DEFAULT )
setCursor( Qt::OpenHandCursor );
}
bool
AbstractGraphicsItem::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.
QLineF line = transform.map( QLineF( 0, 0, RESIZE_ZONE, 0 ) );
if ( position.x() < line.x2() ||
position.x() > ( boundingRect().width() - line.x2() ) )
{
return true;
}
return false;
}
/*****************************************************************************
* AbstractGraphiscItem.cpp: Represent an abstract item on the timeline
*****************************************************************************
* Copyright (C) 2008-2010 VideoLAN
*
* Authors: Hugo Beauzée-Luyssen <beauze.h@gmail.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.
*****************************************************************************/
#ifndef ABSTRACTGRAPHICSITEM_H
#define ABSTRACTGRAPHICSITEM_H
#include <QGraphicsItem>
class GraphicsTrack;
class TrackWorkflow;
class TracksScene;
class TracksView;
class AbstractGraphicsItem : public QObject, public QGraphicsItem
{
Q_OBJECT
#if QT_VERSION >= 0x40600
Q_INTERFACES( QGraphicsItem )
#endif
public:
enum From
{
BEGINNING,
END
};
AbstractGraphicsItem();
virtual ~AbstractGraphicsItem();
/// Return the Type of the MediaItem (see http://doc.trolltech.com/4.5/qgraphicsitem.html#type)
virtual int type() const = 0;
/// Defines the outer bounds of the item as a rectangle
virtual QRectF boundingRect() const;
/// The item length can be expanded or shrinked by the user.
virtual bool expandable() const = 0;
/// The item can be moved by the user.
virtual bool moveable() const = 0;
/// Return a pointer to the TracksScene
TracksScene* scene();
/// Return the current track of the item
qint32 trackNumber();
/// Set the item's parent track
void setTrack( GraphicsTrack* track );
/// Return the item's parent track
GraphicsTrack* track();
/// Set the position of the item (in frames) for the x-axis.
void setStartPos( qint64 position );
/// Return the position of the item (in frames) for the x-axis.
qint64 startPos();
void setColor( const QColor& color );
QColor itemColor();
/**
* \brief Resize an item from its beginning or from its end.
*
* \returns A contextual info (depending on "from") to compute
* the new length. (Either the new beginning of the new length)
*/
qint64 resize( qint64 size, qint64 newBegin, qint64 clipPos, From from = BEGINNING );
protected:
virtual void hoverEnterEvent( QGraphicsSceneHoverEvent* event );
virtual void hoverMoveEvent( QGraphicsSceneHoverEvent* event );
virtual void mousePressEvent( QGraphicsSceneMouseEvent* event );
virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent* event );
/**
* \brief Set the width of the item.
* \param width Width in frames.
*/
void setWidth( qint64 width );
/**
* \brief Set the height of the item.
* \param height Height in pixels.
*/
void setHeight( qint64 height );
/**
* \details Returns a pointer to the tracksView which contains the item,
* or NULL if the item is not stored in a tracksView.
*/
TracksView* tracksView();
/**
* \returns True if the item has resize boundaries, false otherwise.
*/
virtual bool hasResizeBoundaries() const = 0;
/**
* \brief Return the begin boundaries for the item.
*/
virtual qint64 maxBegin() const { return 0; }
/**
* \brief Return the end boundaries for the item.
*/
virtual qint64 maxEnd() const { return -1; }
protected:
/// This pointer will be set when inserted in the tracksView.
TracksView* m_tracksView;
QColor m_itemColor;
private:
TrackWorkflow *m_oldTrack;
qint64 m_width;
qint64 m_height;
protected slots:
/**
* \brief Check if the position given as parameter could be taken as a resize request.
* \return Returns True if the point is in a resize zone.
*/
bool resizeZone( const QPointF& position );
friend class TracksView;
};
#endif // ABSTRACTGRAPHICSITEM_H
......@@ -39,16 +39,9 @@
#include <QtDebug>
AbstractGraphicsMediaItem::AbstractGraphicsMediaItem( Clip* clip ) :
m_oldTrack( NULL ),
oldPosition( -1 ),
m_tracksView( NULL ),
m_group( NULL ),
m_width( 0 ),
m_height( 0 ),
m_muted( false )
{
setFlags( QGraphicsItem::ItemIsSelectable );
m_clipHelper = new ClipHelper( clip );
// Adjust the width
setWidth( clip->length() );
......@@ -56,27 +49,19 @@ AbstractGraphicsMediaItem::AbstractGraphicsMediaItem( Clip* clip ) :
connect( m_clipHelper, SIGNAL( lengthUpdated() ), this, SLOT( adjustLength() ) );
connect( clip, SIGNAL( unloaded( Clip* ) ),
this, SLOT( clipDestroyed( Clip* ) ), Qt::DirectConnection );
setAcceptHoverEvents( true );
}
AbstractGraphicsMediaItem::AbstractGraphicsMediaItem( ClipHelper* ch ) :
m_oldTrack( NULL ),
oldPosition( -1 ),
m_clipHelper( ch ),
m_tracksView( NULL ),
m_group( NULL ),
m_width( 0 ),
m_height( 0 ),
m_muted( false )
{
setFlags( QGraphicsItem::ItemIsSelectable );
// 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 );
setAcceptHoverEvents( true );
}
AbstractGraphicsMediaItem::~AbstractGraphicsMediaItem()
......@@ -84,54 +69,6 @@ AbstractGraphicsMediaItem::~AbstractGraphicsMediaItem()
ungroup();
}
TracksScene* AbstractGraphicsMediaItem::scene()
{
return qobject_cast<TracksScene*>( QGraphicsItem::scene() );
}
TracksView* AbstractGraphicsMediaItem::tracksView()
{
return m_tracksView;
}
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;
}
qint32 AbstractGraphicsMediaItem::trackNumber()
{
if ( parentItem() )
{
GraphicsTrack* graphicsTrack = qgraphicsitem_cast<GraphicsTrack*>( parentItem() );
if ( graphicsTrack )
return graphicsTrack->trackNumber();
}
return -1;
}
void AbstractGraphicsMediaItem::setTrack( GraphicsTrack* track )
{
setParentItem( track );
}
GraphicsTrack* AbstractGraphicsMediaItem::track()
{
return qgraphicsitem_cast<GraphicsTrack*>( parentItem() );
}
void AbstractGraphicsMediaItem::group( AbstractGraphicsMediaItem* item )
{
Q_ASSERT( item );
......@@ -250,111 +187,64 @@ void AbstractGraphicsMediaItem::contextMenuEvent( QGraphicsSceneContextMenuEvent
}
void AbstractGraphicsMediaItem::setStartPos( qint64 position )
{
QGraphicsItem::setPos( (qreal)position, 0 );
}
qint64 AbstractGraphicsMediaItem::startPos()
{
return qRound64( QGraphicsItem::pos().x() );
}
qint64 AbstractGraphicsMediaItem::resize( qint64 newSize, qint64 newBegin, qint64 clipPos,
From from )
void
AbstractGraphicsMediaItem::clipDestroyed( Clip* clip )
{
Q_ASSERT( clipHelper() );
if ( newSize < 1 )
return 1;
if ( clipHelper()->clip()->getMedia()->fileType() != Media::Image )
if ( newSize > clipHelper()->clip()->end() )
newSize = clipHelper()->clip()->end();
//The from actually stands for the clip bound that stays still.
if ( from == BEGINNING )
{
if ( m_clipHelper->clip()->getMedia()->fileType() != Media::Image )
{
if ( m_clipHelper->begin() + newSize > m_clipHelper->clip()->end() )
return m_clipHelper->length();
}
setWidth( newSize );
return newSize;
}
else
{
if ( m_clipHelper->clip()->getMedia()->fileType() != Media::Image )
{
if ( m_clipHelper->clip()->begin() > newBegin )
return m_clipHelper->clip()->begin();
}
setWidth( newSize );
setStartPos( clipPos );
return newBegin;
}
if ( m_tracksView != NULL )
m_tracksView->removeClip( clip->uuid() );
}
void AbstractGraphicsMediaItem::adjustLength()
ClipHelper*
AbstractGraphicsMediaItem::clipHelper()
{
Q_ASSERT( m_clipHelper );
setWidth( m_clipHelper->length() );
return m_clipHelper;
}
bool AbstractGraphicsMediaItem::resizeZone( const QPointF& position )
const ClipHelper*
AbstractGraphicsMediaItem::clipHelper() const
{
// 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.
QLineF line = transform.map( QLineF( 0, 0, RESIZE_ZONE, 0 ) );
if ( position.x() < line.x2() ||
position.x() > ( boundingRect().width() - line.x2() ) )
{
return true;
}
return false;
return m_clipHelper;
}
QColor
AbstractGraphicsMediaItem::itemColor()
const QUuid&
AbstractGraphicsMediaItem::uuid() const
{
return m_itemColor;
return m_clipHelper->uuid();
}
void
AbstractGraphicsMediaItem::setColor( const QColor &color )
AbstractGraphicsMediaItem::setEmphasized( bool value )
{
m_itemColor = color;
if ( value == true )
setScale( 1.2 );
else
setScale( 1.0 );
}
void
AbstractGraphicsMediaItem::clipDestroyed( Clip* clip )
bool
AbstractGraphicsMediaItem::hasResizeBoundaries() const
{
if ( m_tracksView != NULL )
m_tracksView->removeClip( clip->uuid() );
return ( clipHelper()->clip()->getMedia()->fileType() != Media::Image );
}
ClipHelper*
AbstractGraphicsMediaItem::clipHelper()
qint64
AbstractGraphicsMediaItem::maxBegin() const
{
return m_clipHelper;
return ( m_clipHelper->clip()->begin() );
}
const QUuid&
AbstractGraphicsMediaItem::uuid() const
qint64
AbstractGraphicsMediaItem::maxEnd() const
{
return m_clipHelper->uuid();
return clipHelper()->clip()->end();
}
void
AbstractGraphicsMediaItem::setEmphasized( bool value )
AbstractGraphicsMediaItem::adjustLength()
{
if ( value == true )
setScale( 1.2 );
else
setScale( 1.0 );
Q_ASSERT( m_clipHelper );
setWidth( m_clipHelper->length() );
}
void
......@@ -377,20 +267,6 @@ AbstractGraphicsMediaItem::hoverEnterEvent( QGraphicsSceneHoverEvent* event )
QGraphicsItem::hoverEnterEvent( event );
}
void
AbstractGraphicsMediaItem::hoverMoveEvent( QGraphicsSceneHoverEvent* event )
{
if ( !tracksView() ) return;
if ( tracksView()->tool() == TOOL_DEFAULT )
{
if ( resizeZone( event->pos() ) )
setCursor( Qt::SizeHorCursor );
else
setCursor( Qt::OpenHandCursor );
}
}
void
AbstractGraphicsMediaItem::mousePressEvent( QGraphicsSceneMouseEvent* event )
{
......@@ -406,13 +282,3 @@ AbstractGraphicsMediaItem::mousePressEvent( QGraphicsSceneMouseEvent* event )
else if ( tracksView()->tool() == TOOL_CUT )
emit split( this, qRound64( event->pos().x() ) );
}
void
AbstractGraphicsMediaItem::mouseReleaseEvent( QGraphicsSceneMouseEvent* event )
{
Q_UNUSED( event );
if ( !tracksView() ) return;
if ( tracksView()->tool() == TOOL_DEFAULT )
setCursor( Qt::OpenHandCursor );
}
......@@ -24,60 +24,32 @@
#define ABSTRACTGRAPHICSMEDIAITEM_H
class ClipHelper;
#include "AbstractGraphicsItem.h"
#include "Types.h"
#include <QGraphicsItem>
#include <QUuid>
#define RESIZE_ZONE 7
class Clip;
class TracksScene;
class TracksView;
class TrackWorkflow;
class GraphicsTrack;
/**
* \brief Base class for Audio/Video items.
*/
class AbstractGraphicsMediaItem : public QObject, public QGraphicsItem
class AbstractGraphicsMediaItem : public AbstractGraphicsItem
{
Q_OBJECT
#if QT_VERSION >= 0x40600
Q_INTERFACES( QGraphicsItem )
#endif
friend class TracksView;
public:
enum From
{
BEGINNING,
END
};
AbstractGraphicsMediaItem( Clip* clip );
AbstractGraphicsMediaItem( ClipHelper* ch );
virtual ~AbstractGraphicsMediaItem();
/// Defines the outer bounds of the item as a rectangle
virtual QRectF boundingRect() const;
/// Return the Type of the MediaItem (see http://doc.trolltech.com/4.5/qgraphicsitem.html#type)
virtual int type() const = 0;
/// The item length can be expanded or shrinked by the user.
virtual bool expandable() const = 0;
/// The item can be moved by the user.
virtual bool moveable() const = 0;
/// Should return the unique uid of the contained media.
virtual const QUuid& uuid() const;
/// Return a pointer to the TracksScene
TracksScene* scene();
/// Return the type of the media
virtual Workflow::TrackType mediaType() const = 0;
......@@ -87,33 +59,8 @@ public:
/// Ungroup two items
void ungroup();
/// Return the current track of the item
qint32 trackNumber();
/// Set the item's parent track
void setTrack( GraphicsTrack* track );
/// Return the item's parent track
GraphicsTrack* track();
/// Set the position of the item (in frames) for the x-axis.