/*******************************************************************************
**
** The source file for the Jahshaka jahnodes class
** The Jahshaka Project
** Copyright (C) 2000-2005 The Jahshaka Project
** Released under the GNU General Public License
**
*******************************************************************************/

#include "keyframeeditor.h"
#include "keyframeritem.h"
#include <widget.h>


KeyframeEditor::KeyframeEditor(	QCanvas* c, QWidget* parent, const char* name, WFlags f) :
    QCanvasView(c,parent,name,f),
    zoomlevel( 4.0 )
{
    setHScrollBarMode( KeyframeEditor::AlwaysOff );
    setVScrollBarMode( KeyframeEditor::AlwaysOff );
}

void KeyframeEditor::cleareditor()
{
    // Clears the keyframes and axis, but not the playline.
    QCanvasItemList list = canvas()->allItems();
    QCanvasItemList::Iterator it = list.begin();
    for (; it != list.end(); ++it) {
    if ( *it && (*it)->rtti() != sliderRTTI2 )
        delete *it;
    }
    
}

void KeyframeEditor::contentsMousePressEvent(QMouseEvent* e)
{
    QPoint p = inverseWorldMatrix().map(e->pos());

    QCanvasItemList l=canvas()->collisions(p);

    for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
//	if ( (*it)->rtti() == imageRTTI ) {
//	    ImageItem *item= (ImageItem*)(*it);
    // if ( !item->hit( p ) )
//		 continue;
//	}
        if ( (*it)->rtti() == keyframeRTTI )
        {   moving = *it;
            moving_start = p;
            return;
        }
    }
    moving = 0;
}

void KeyframeEditor::contentsMouseMoveEvent(QMouseEvent* move_event)
{
    if ( moving ) 
    {
        QPoint point = inverseWorldMatrix().map(move_event->pos());
        /*moving->moveBy(p.x() - moving_start.x(),
        p.y() - moving_start.y());
        */
        // For now, allow y axis (value) movement, until we deal with moving keyframe times.
        int delta_y = point.y() - moving_start.y();

        moving->moveBy(0, delta_y);
        moving_start = point;
        canvas()->update();
        emit signalKeyframeChanged();
    }

    //if we are moving the last node or first node in
    //a keyframe layout we need to also move the + and -
    //infinity nodes...
    //may be easier to make them unique?
}

void KeyframeEditor::resizeEvent ( QResizeEvent* )
{
    canvas()->resize( contentsRect().width(), contentsRect().height() );
    canvas()->update();
}




