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

Improving resize behaviour.

parent 4017acef
......@@ -35,6 +35,9 @@
#include <QColorDialog>
#include <QGraphicsSceneContextMenuEvent>
#include <QCoreApplication>
#include <QtDebug>
AbstractGraphicsMediaItem::AbstractGraphicsMediaItem( Clip* clip ) :
m_oldTrack( NULL ),
oldPosition( -1 ),
......@@ -251,50 +254,36 @@ qint64 AbstractGraphicsMediaItem::startPos()
return qRound64( QGraphicsItem::pos().x() );
}
void AbstractGraphicsMediaItem::resize( qint64 size, From from )
bool AbstractGraphicsMediaItem::resize( qint64 newSize, qint64 newBegin, From from )
{
Q_ASSERT( clipHelper() );
if ( size < 1 )
return;
if ( newSize < 1 )
return false;
if ( clipHelper()->clip()->getMedia()->fileType() != Media::Image )
if ( size > clipHelper()->clip()->end() )
size = clipHelper()->clip()->end();
if ( newSize > clipHelper()->clip()->end() )
newSize = clipHelper()->clip()->end();
if ( from == BEGINNING ) //Inverted logic ?!
//The from actually stands for the clip bound that stays still.
if ( from == BEGINNING )
{
if ( clipHelper()->clip()->getMedia()->fileType() != Media::Image )
if ( clipHelper()->begin() + size > clipHelper()->clip()->end() )
return;
m_clipHelper->setEnd( m_clipHelper->begin() + size );
{
if ( clipHelper()->begin() + newSize > clipHelper()->clip()->end() )
return false;
}
setWidth( newSize );
}
else
{
if ( m_clipHelper->clip()->getMedia()->fileType() != Media::Image )
{
qint64 newBegin = qMax( m_clipHelper->end() - size, (qint64)0 );
if ( m_clipHelper->clip()->begin() > newBegin )
return;
qint64 oldLength = m_clipHelper->length();
qint64 newStart = startPos() + ( oldLength - size );
if ( newStart < 0 )
return ;
track()->trackWorkflow()->moveClip( m_clipHelper->uuid(), newStart );
m_clipHelper->setBegin( m_clipHelper->end() - size );
setStartPos( newStart );
}
else
{
qint64 oldLength = m_clipHelper->length();
track()->trackWorkflow()->moveClip( m_clipHelper->uuid(), startPos() + ( oldLength - size ) );
m_clipHelper->setBegin( startPos() + ( oldLength - size ) );
setStartPos( startPos() + ( oldLength - size ) );
}
if ( newBegin < 0 || m_clipHelper->clip()->begin() > newBegin )
return false;
setWidth( newSize );
setStartPos( newBegin );
}
setWidth( m_clipHelper->length() );
return true;
}
void AbstractGraphicsMediaItem::adjustLength()
......
......@@ -102,8 +102,12 @@ public:
/// Return the position of the item (in frames) for the x-axis.
qint64 startPos();
/// Resize an item from its beginning or from its end.
void resize( qint64 size, From from = BEGINNING );
/**
* \brief Resize an item from its beginning or from its end.
*
* \returns True if the element may be resized.
*/
bool resize( qint64 size, qint64 newBegin, From from = BEGINNING );
ClipHelper *clipHelper();
......
......@@ -818,13 +818,15 @@ TracksView::mouseMoveEvent( QMouseEvent *event )
{
if ( m_actionResizeType == AbstractGraphicsMediaItem::END )
{
qint64 distance = mapToScene( event->pos() ).x() - m_actionResizeStart;
qint64 newsize = qMax( m_actionResizeBase - distance, (qint64)0 );
m_actionItem->resize( newsize , AbstractGraphicsMediaItem::END );
m_actionResizeNewBegin = mapToScene( event->pos() ).x();
qint64 newSize = qMax( (qint64)(m_actionItem->clipHelper()->length() - mapToScene( event->pos() ).x()), (qint64)0 );
if ( m_actionItem->resize( newSize, mapToScene( event->pos() ).x(), AbstractGraphicsMediaItem::END ) == true )
m_actionResizeSize = newSize;
}
else
{
m_actionItem->resize( itemNewSize.x(), AbstractGraphicsMediaItem::BEGINNING );
if ( m_actionItem->resize( itemNewSize.x(), m_actionResizeNewBegin, AbstractGraphicsMediaItem::BEGINNING ) == true )
m_actionResizeSize = itemNewSize.x();
}
}
}
......@@ -864,8 +866,7 @@ TracksView::mousePressEvent( QMouseEvent *event )
else
m_actionResizeType = AbstractGraphicsMediaItem::BEGINNING;
m_actionResize = true;
m_actionResizeStart = mapToScene( event->pos() ).x();
m_actionResizeBase = item->clipHelper()->length();
m_actionResizeNewBegin = item->pos().x();
m_actionItem = item;
}
else if ( item->moveable() )
......@@ -956,11 +957,11 @@ TracksView::mouseReleaseEvent( QMouseEvent *event )
else if ( m_actionResize )
{
ClipHelper *ch = m_actionItem->clipHelper();
//This is a "pointless action". The resize already occurred. However, by doing this
//we can have an undo action.
qDebug() << "Putting clip in" << m_actionResizeNewBegin << "size:" << m_actionResizeSize;
Commands::trigger( new Commands::MainWorkflow::ResizeClip( m_actionItem->track()->trackWorkflow(),
ch, ch->begin(), ch->end(),
m_actionItem->pos().x() ) );
ch, m_actionResizeNewBegin,
m_actionResizeNewBegin + m_actionResizeSize,
m_actionResizeNewBegin ) );
updateDuration();
}
......
......@@ -328,8 +328,8 @@ private:
bool m_actionMove;
bool m_actionMoveExecuted;
bool m_actionResize;
qint64 m_actionResizeStart;
qint64 m_actionResizeBase;
qint64 m_actionResizeNewBegin;
qint64 m_actionResizeSize;
int m_actionRelativeX;
AbstractGraphicsMediaItem::From m_actionResizeType;
AbstractGraphicsMediaItem *m_actionItem;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment