/*
 * 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 "PhotoListView.hpp"

#include <qapplication.h>
#include <qdebug.h>
#include <qevent.h>

#include <qdir.h>
#include <qicon.h>
#include <qscrollbar.h>
#include <qstandarditemmodel.h>



static QPoint scrollOffset(QWidget *widget)
{
  int x = 0, y = 0;

  QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*> (widget);
  if (scrollArea)
  {
    x = scrollArea->horizontalScrollBar()->value();
    y = scrollArea->verticalScrollBar()->value();
  }

  return QPoint(x, y);
}

static void setScrollOffset(QWidget *widget, const QPoint &p)
{
  QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*> (widget);
  if (scrollArea)
  {
    scrollArea->horizontalScrollBar()->setValue(p.x());
    scrollArea->verticalScrollBar()->setValue(p.y());
  }
}


PhotoListView::PhotoListView() : QListView(), mMouseAction(None)
{
  QStandardItemModel *model = new QStandardItemModel(this);

	QString picsPath = QCoreApplication::applicationDirPath() + QDir::separator() + "pictures";
	qDebug() << "Adding pictures from" << picsPath;

	QDir picsDir(picsPath, "*.png *.jpg *.jpeg", QDir::Name | QDir::IgnoreCase, QDir::Files);
	Q_FOREACH(const QFileInfo fi, picsDir.entryInfoList())
  {
		QIcon ic(fi.filePath());
		if (ic.isNull() || ic.availableSizes().isEmpty())
      continue;

    qDebug() << "Adding picture" << fi.fileName();
    QStandardItem *item = new QStandardItem(ic, fi.baseName());
    item->setData(fi.filePath(), Qt::UserRole + 1);
    model->appendRow(item);
  }
  setModel(model);
}


PhotoListView::~PhotoListView()
{
}

void PhotoListView::mouseMoveEvent(QMouseEvent *event)
{
  if (!(event->buttons() & Qt::LeftButton) || mMouseStartPos.isNull())
  {
    event->ignore();
    return;
  }

  switch(mMouseAction)
  {
    case None: // detect wether user wants to scroll or drag
    {
			if (qAbs(event->pos().x() - mMouseStartPos.x()) > 8)
        mMouseAction = Drag;
			else if (qAbs(event->pos().y() - mMouseStartPos.y()) > 8)
        mMouseAction = Scroll;
      break;
    }

    case Scroll:
    {
      QPoint delta = event->pos() - mMouseStartPos;
      setScrollOffset(this, mScrollOffset - delta);
      break;
    }

    case Drag:
    {
      QModelIndex index = indexAt(mMouseStartPos);
      mMouseStartPos = QPoint(); // invalidate start position
      if (!index.isValid())
        return;

      QString filePath = index.data(Qt::UserRole + 1).toString();
      qDebug() << "emitting photoDragStarted at" << event->pos();
      emit photoDragStarted(event->pos(), filePath);
      break;
    }
  }
  event->accept();
}


void PhotoListView::mousePressEvent(QMouseEvent *event)
{
  if (event->button() != Qt::LeftButton)
  {
    event->ignore();
    return;
  }

  mMouseStartPos = event->pos(); // remember current mouse position
  mScrollOffset = scrollOffset(this); // remember current scrollbar position
  event->accept();
}


void PhotoListView::mouseReleaseEvent(QMouseEvent *event)
{
  if (event->button() != Qt::LeftButton)
  {
    event->ignore();
    return;
  }

  mMouseStartPos = QPoint();
  mScrollOffset = QPoint();
  mMouseAction = None;
  event->accept();
}

#include "moc_PhotoListView.cpp"
