/***************************************************************************
 *   Copyright (C) 2008, 2009, 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 "util/helpers.h"

#include "veneer/contextmenu.h"

#include <QMenu>
#include <QTreeWidget>
#include <QHeaderView>
#include <QBuffer>
#include <QDebug>

#ifndef VERSION
#define VERSION "(undefined)"
#endif

bool caseInsensitiveLessThan(const QString& s1, const QString& s2)
{
	return s1.toLower() < s2.toLower();
}

void showColumnsContextMenu(const QPoint& p, QTreeView& tree)
{
	Veneer::ContextMenu headerMenu;
	headerMenu.addTitle(QObject::tr("Columns"));

	QHeaderView* header = tree.header();

	for (qint32 i = 0; i < tree.model()->columnCount(); i++)
	{
		const int idx = header->logicalIndex(i);
		const QString text = tree.model()->headerData(idx, Qt::Horizontal).toString();

		QAction* action = headerMenu.addAction(text);
		action->setCheckable(true);
		action->setChecked(!header->isSectionHidden(idx));
		action->setData(idx);
		action->setEnabled(idx > 0);
	}

	QAction* action = headerMenu.exec(tree.header()->mapToGlobal(p));

	if (action != NULL)
	{
		const bool hidden = !action->isChecked();
		tree.setColumnHidden(action->data().toInt(), hidden);
		if (!hidden)
			tree.resizeColumnToContents(action->data().toInt());
	}
}

QByteArray pixmapToByteArray(const QPixmap& p)
{
	QByteArray a;
	QBuffer buffer(&a);

	buffer.open(QIODevice::WriteOnly);

	p.save(&buffer, "PNG");

	return a;
}

void loadTreeWidgetHeaderConfig(QTreeView* tree, const QList<int>& colWidths, const QList<int>& colPositions, const QList<int>& colVisible)
{
	for (int i = 0; i < tree->model()->columnCount(); i++)
	{
		if (colPositions[0] != -1 && colPositions.size() > i)
			tree->header()->moveSection(tree->header()->visualIndex(i), colPositions[i]);

		if (colVisible[0] != -1 && colVisible.size() > i)
			tree->setColumnHidden(i, colVisible[i] == 0);

		if (colWidths[0] != -1 && colWidths.size() > i)
			tree->setColumnWidth(i, colWidths[i]);
	}
}

QList<int> treeViewHeaderColWidths(QTreeView* tree)
{
	QList<int> colWidths;

	for (int i = 0; i < tree->model()->columnCount(); i++)
		colWidths.append(tree->columnWidth(i));

	return colWidths;
}

QList<int> treeViewHeaderColPositions(QTreeView* tree)
{
	QList<int> colPositions;

	for (int i = 0; i < tree->model()->columnCount(); i++)
		colPositions.append(tree->header()->visualIndex(i));

	return colPositions;
}

QList<int> treeViewHeaderColVisible(QTreeView* tree)
{
	QList<int> colVisible;

	for (int i = 0; i < tree->model()->columnCount(); i++)
		colVisible.append(tree->isColumnHidden(i) ? 0 : 1);

	return colVisible;
}
