/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2011  Alexander Konovalov
 * Copyright (C) 2006-2014  Georg Rudoy
 * 
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "uistatepersist.h"
#include <QTreeView>
#include <QString>
#include <QSettings>
#include <QDebug>
#include <QApplication>

namespace LeechCraft
{
namespace Aggregator
{
	void SaveColumnWidth (const QTreeView *tree, const QString& keyName)
	{
		// check that we can work with 'tree' argument
		if (!tree)
		{
			qWarning () << Q_FUNC_INFO << "tree == 0, not saving widths";
			return;
		}
		if (!tree->model ())
		{
			qWarning () << Q_FUNC_INFO << "tree->model () == 0, not saving widths";
			return;
		}
		// get width
		QList<QVariant> sizes;
		for (int i = 0, count = tree->model ()->columnCount (); i < count; ++i)
			sizes += tree->columnWidth (i);
		// save column width
		QSettings settings (QApplication::organizationName (), QApplication::applicationName () + "_Aggregator");
		settings.beginGroup ("tabs-width");
		settings.setValue (keyName, sizes);
		settings.endGroup ();
	}

	void LoadColumnWidth (QTreeView *tree, const QString& keyName)
	{
		// check that we can work with 'tree' argument
		if (!tree)
		{
			qWarning () << Q_FUNC_INFO << "tree == 0, not loading widths";
			return;
		}
		if (!tree->model ())
		{
			qWarning () << Q_FUNC_INFO << "tree->model () == 0, not loading widths";
			return;
		}
		// load column width
		QSettings settings (QApplication::organizationName (), QApplication::applicationName () + "_Aggregator");
		settings.beginGroup ("tabs-width");
		QList<QVariant> sizes = settings.value (keyName).toList ();
		settings.endGroup ();
		// column count check
		if (sizes.size () != tree->model ()->columnCount ())
		{
			qWarning () << Q_FUNC_INFO <<
				"Column count of tree (" << tree->model ()->columnCount () <<
				") != column count in settings (" << sizes.size () << ")";
			return;
		}
		// set width
		const int minColumnSize = 4;
		for (int i = 0; i < sizes.size (); i++)
		{
			// type check
			if (!sizes.at (i).canConvert<int> ())
			{
				qWarning() << Q_FUNC_INFO << "Can`t convert QVariant to int, "
					"(sizes[" << i << "]=" << sizes.at (i) << ")";
				return;
			}
			
			// min.size check
			int s = sizes.at (i).toInt ();
			if (s < minColumnSize)
			{
				qWarning() << Q_FUNC_INFO << "Size of column #" << i <<
					"(" << s << ") is too small (min." << minColumnSize << ")";
				continue;
			}
			
			tree->setColumnWidth (i, s);
		}
	}
}
}
