/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * 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.
 **********************************************************************/

#pragma once

#include <QAbstractItemModel>
#include "modelsconfig.h"

namespace LeechCraft
{
namespace Util
{
	/** @brief Proxy model flattening a hierarchical model.
	 *
	 * This model takes another model and folds its structure into a flat
	 * model, possibly filtering out some rows (via the IsIndexAccepted()
	 * method).
	 *
	 * Since this model changes the structure of the source model quite a
	 * lot, it doesn't derive from QAbstractProxyModel.
	 *
	 * @ingroup ModelUtil
	 */
	class UTIL_MODELS_API FlattenFilterModel : public QAbstractItemModel
	{
		Q_OBJECT
	protected:
		QAbstractItemModel *Source_;
		QList<QPersistentModelIndex> SourceIndexes_;
	public:
		/** @brief Constructs the model with the given \em parent.
		 *
		 * @param[in] parent The parent object of this model.
		 */
		FlattenFilterModel (QObject *parent = 0);

		/** @brief Reimplemented from QAbstractItemModel.
		 */
		QModelIndex index (int, int, const QModelIndex& = QModelIndex ()) const;

		/** @brief Reimplemented from QAbstractItemModel.
		 */
		QModelIndex parent (const QModelIndex&) const;

		/** @brief Reimplemented from QAbstractItemModel.
		 */
		int rowCount (const QModelIndex& parent = QModelIndex()) const;

		/** @brief Reimplemented from QAbstractItemModel.
		 */
		int columnCount (const QModelIndex& parent = QModelIndex()) const;

		/** @brief Reimplemented from QAbstractItemModel.
		 */
		QVariant data (const QModelIndex& index, int role = Qt::DisplayRole) const;

		/** @brief Sets the source model to \em model.
		 *
		 * If another source model has been set already, this function
		 * rebuilds the whole model, effectively resetting it.
		 *
		 * @param[in] model The new source model.
		 */
		void SetSource (QAbstractItemModel *model);
	protected:
		/** @brief Checks whether the given \em index should be included
		 * in the model.
		 *
		 * Reimplement this function in derived classes to provide
		 * filtering capabilities akin to QSortFilterProxyModel.
		 *
		 * The children of the \em index will be checked even if this
		 * function returns false for \em index.
		 *
		 * @note The model operates on rows: that is, only the first
		 * column is checked, and <code>index.column() == 0</code>
		 * will always hold.
		 *
		 * The default implementation simply returns <code>true</code>.
		 *
		 * @param[in] index The index of the source model to check.
		 * @return Whether the \em index should be included in the
		 * resulting model.
		 */
		virtual bool IsIndexAccepted (const QModelIndex& index) const;
	private slots:
		void handleDataChanged (const QModelIndex&, const QModelIndex&);
		void handleRowsInserted (const QModelIndex&, int, int);
		void handleRowsAboutRemoved (const QModelIndex&, int, int);
	};
}
}
