/*
 * Copyright 2009-2010  Stefan Gehn <stefan@srcbox.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, see <http://www.gnu.org/licenses/>.
 */

#include "TrashDropWidget.hpp"
#include "Draggable.hpp"

#include <qapplication.h>
#include <qdebug.h>
#include <qdir.h>
#include <qpainter.h>
#include <qstyleoption.h>
#include <qtimer.h>

#include <qtparallelanimationgroup.h>
#include <qtpropertyanimation.h>

static QString locateIcon(const QString &iconName)
{
  static QString sBase;

  if (sBase.isNull())
  {
    QDir locDir(QApplication::applicationDirPath());
    if (locDir.dirName() == "debug" || locDir.dirName() == "release")
    {
      locDir.cdUp();
      if (locDir.dirName() == "build")
        locDir.cdUp();
    }
    sBase = locDir.absolutePath();
  }
  return sBase + QDir::separator() + "icons" + QDir::separator() + iconName;
}



TrashDropWidget::TrashDropWidget(QGraphicsItem * parent): DropTarget(parent)
{
	mIcon = QIcon(locateIcon("trash.png"));
}


void TrashDropWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
  Q_UNUSED(widget);
  painter->setClipRect(option->exposedRect);

  QSize intSize = size().toSize();
	QPixmap pm = mIcon.pixmap(intSize, (isEnabled() ? QIcon::Normal : QIcon::Disabled));
  // center icon
  int xOff = (intSize.width() - pm.width()) / 2;
  int yOff = (intSize.height() - pm.height()) / 2;

  painter->drawPixmap(xOff, yOff, pm);
}



void TrashDropWidget::dropped(Draggable *dr)
{
  qDebug() << "Starting delete-animation on dropped item";

  QtPropertyAnimation *anim;
  QtParallelAnimationGroup *animGroup = new QtParallelAnimationGroup(this);
  QObject::connect(animGroup, SIGNAL(finished()), this, SLOT(slotDeleteDropped()));

  anim = new QtPropertyAnimation(dr, "xScale");
  anim->setEndValue(0.05);
  anim->setDuration(200);
  anim->setEasingCurve(QtEasingCurve::InQuad);
  animGroup->addAnimation(anim);

  anim = new QtPropertyAnimation(dr, "yScale");
  anim->setEndValue(0.05);
  anim->setDuration(200);
  anim->setEasingCurve(QtEasingCurve::InQuad);
  animGroup->addAnimation(anim);

  anim = new QtPropertyAnimation(dr, "pos");
  QPointF trashCenter = mapToScene(boundingRect().center());
  anim->setEndValue(trashCenter - dr->boundingRect().center());
  anim->setDuration(200);
  anim->setEasingCurve(QtEasingCurve::InQuad);
  animGroup->addAnimation(anim);

	animGroup->start(QtAbstractAnimation::DeleteWhenStopped);
}

void TrashDropWidget::slotDeleteDropped()
{
  qDebug() << "Deleting trashed item";
  QtAnimationGroup *animGroup= qobject_cast<QtAnimationGroup *>(sender());
  if (animGroup)
  {
    QtPropertyAnimation *anim = qobject_cast<QtPropertyAnimation *>(animGroup->animationAt(0));
    if (anim && anim->targetObject())
    {
      qDebug() << "deleting target object of animation";
      anim->targetObject()->deleteLater();
    }
  }
}

#include "moc_TrashDropWidget.cpp"
