/***************************************************************************
 *   Copyright (C) 2004 by Hans Oischinger                                 *
 *   hans.oischinger@kdemail.net                                           *
 *                                                                         *
 *   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 Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#include "timelinedocker.h"

// Core
#include "core/direktorinstance.h"
#include "core/elementdatabase.h"

// Dockers
#include "elementtreeview.h"
#include "timelineview.h"

// Qt & KDE
#include <qhbox.h>
#include <qheader.h>
#include <kdebug.h>

TimelineDocker::TimelineDocker(QWidget *parent, const char *name)
    : DDocker( parent, name )
{
  //   m_table = new TimelineTable(1,200,this);
  // //   m_table->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  //   m_table->show();
  //
  //   connect( m_table, SIGNAL(timeFrameClicked(int)), SLOT(slotTimeFrameClicked(int)) );

  m_hbox = new QHBox(this);
  m_listview = new ElementTreeView(m_hbox, "EL");
  m_timeline = new TimelineView(m_hbox, "TL");
  m_hbox->setStretchFactor(m_listview, 30);
  m_hbox->setStretchFactor(m_timeline, 70);

  Kast::self()->elementDB().addElementListener(this);

  // Track any changes to the listview contents and notice the timeline
  connect( m_listview, SIGNAL(contentsMoving(int, int)), SLOT(elemTreeViewportChanged(int, int)) );
  /*  connect( m_listview->horizontalScrollBar(), SIGNAL(valueChanged(int)), SLOT(elemTreeViewportChanged()) );
    connect( m_listview->verticalScrollBar(), SIGNAL(valueChanged(int)), SLOT(elemTreeViewportChanged()) );*/
  connect( m_listview, SIGNAL(contentsChanged()), SLOT(elemTreeViewportChanged()) );
  connect( m_listview, SIGNAL(expanded(QListViewItem*)), SLOT(elemTreeViewportChanged()) );
  connect( m_listview, SIGNAL(collapsed(QListViewItem*)), SLOT(elemTreeViewportChanged()) );
}


TimelineDocker::~TimelineDocker()
{}

void TimelineDocker::resizeEvent ( QResizeEvent *e )
{
  m_hbox->resize(e->size());
  //   m_listview->resize(e->size().width()/3, e->size().height());
  //   m_table->resize(e->size());
}

void TimelineDocker::elemTreeViewportChanged( int, int y )
{
  kdDebug() << "TimelineDocker::elemTreeViewportChanged()" << endl;
  m_timeline->clear();

  // Find the first visible item in the ListView
  int tmpY = 0;
  ElementTreeViewItem* item = 0;
  QListViewItemIterator it( m_listview, QListViewItemIterator::Visible );
  while ( it.current() )
  {
    tmpY += it.current()->height();
    if ( y < tmpY )
    {
      item = static_cast<ElementTreeViewItem*>(it.current());
      break;
    }
    ++it;
  }
  if (!item)
    return;

  // Show all visible items
  int headerHeight = m_listview->header()->height();
  while ( item )
  {
    // FIXME: replace item->itemPos() with own code as it's very slow
    m_timeline->showElement(item->element(), item->itemPos() - y + headerHeight);
    item = static_cast<ElementTreeViewItem*>(item->itemBelow());
  }
}

void TimelineDocker::elemTreeViewportChanged( )
{
  kdDebug() << k_funcinfo << endl;
  elemTreeViewportChanged( m_listview->contentsX(), m_listview->contentsY() );
}

void TimelineDocker::onElementInserted( ElementNode *e )
{
  // kdDebug() << k_funcinfo << endl;
  // Make sure that every newly inserted Element is visible
  ElementTreeViewItem* item = 0;
  item = m_listview->elemtentsToItems().find(e);
  if (item)
    m_listview->ensureItemVisible(item);
}

#include "timelinedocker.moc"
