/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.de>                       *
 *                                                                         *
 *   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 Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#include "gui/activitytree.h"

#include "gui/activitymodel.h"

#include "core/activity.h"

#include "veneer/datetime.h"

#include "util/unit.h"

#include <QSortFilterProxyModel>
#include <QDebug>

ActivityTree::ActivityTree(QWidget* parent)
	: QTreeView(parent)
{
	ActivityModel* activityModel = new ActivityModel(this);

	QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);
	proxyModel->setSortRole(ActivityModel::sortingRole);
	proxyModel->setDynamicSortFilter(true);
	proxyModel->setSourceModel(activityModel);

	setModel(proxyModel);

	connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
	connect(model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));

}

void ActivityTree::onSelectionChanged(const QItemSelection&, const QItemSelection&)
{
	emit selectedActivityChanged();
}

void ActivityTree::onDataChanged(const QModelIndex& from, const QModelIndex&)
{
	emit activityChanged(sourceModel()->activity(from), from.column());
}

void ActivityTree::add(Activity* a)
{
	sourceModel()->add(a);
}

void ActivityTree::remove(Activity* a)
{
	sourceModel()->remove(a);
}

void ActivityTree::clear()
{
	sourceModel()->clear();
}

Activities ActivityTree::selectedActivities()
{
	Activities rval;

	foreach(const QModelIndex& idx, selectionModel()->selectedRows(0))
	{
		const QModelIndex mappedIdx = static_cast<QSortFilterProxyModel*>(model())->mapToSource(idx);
		rval.append(sourceModel()->activity(mappedIdx));
	}

	return rval;
}

void ActivityTree::selectActivity(const Activity* a)
{
	QModelIndex index = sourceModel()->find(a);

	index = static_cast<QSortFilterProxyModel*>(model())->mapFromSource(index);

	if (index.isValid())
	{
		setCurrentIndex(index);
		scrollTo(index);
	}
}

void ActivityTree::selectFirstActivity()
{
	QModelIndex index = model()->index(0, 0);

	if (index.isValid())
	{
		setCurrentIndex(index);
		scrollTo(index);
	}
}

ActivityModel* ActivityTree::sourceModel()
{
	QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
	return static_cast<ActivityModel*>(proxyModel->sourceModel());
}

const ActivityModel* ActivityTree::sourceModel() const
{
	const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
	return static_cast<const ActivityModel*>(proxyModel->sourceModel());
}

void ActivityTree::activityChanged()
{
	const QModelIndex idx = static_cast<QSortFilterProxyModel*>(model())->mapToSource(selectedIndexes()[0]);
	QModelIndex topLeft = model()->index(0, idx.row());
	QModelIndex bottomRight = model()->index(model()->columnCount(), idx.row());
	dataChanged(topLeft, bottomRight);
}
