/***************************************************************************
*   Copyright (C) 2006-2008 by Ian Reinhart Geiser                        *
*   geiseri@yahoo.com                                                     *
*                                                                         *
*   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.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "emblemselector.h"
#include "ui_emblemselector.h"
#include "mindmap.h"

#include <QStandardItemModel>
#include <QStandardItem>
#include <QFileInfo>
#include <QDir>
#include <QImageReader>
#include <QFileDialog>

EmblemSelector::EmblemSelector(QWidget *parent, MindMap *map )
:QDialog(parent ), m_mindmap(map)
{
	m_ui = new Ui::EmblemSelector;
	m_ui->setupUi(this);

	m_model = new QStandardItemModel(this);
	QDir emblemdir(":/emblems");
	foreach( QFileInfo info, emblemdir.entryInfoList(QDir::Readable|QDir::Files, QDir::Name) )
	{
		QStandardItem *item = new QStandardItem( QIcon(info.absoluteFilePath()), info.baseName());
		item->setData(info.absoluteFilePath(), Qt::UserRole + 1);
		item->setEditable( false );
		m_model->appendRow(item);
	}
	
	if( m_mindmap )
	{
		foreach( QString key, m_mindmap->imageAttachments() )
		{
			QImage image = m_mindmap->imageAttachment(key);
			QStandardItem *item = new QStandardItem( QPixmap::fromImage(image), key);
			item->setData(key, Qt::UserRole + 1);
			item->setEditable( false );
			m_model->appendRow(item);
		}
	}
	
	m_ui->iconSelection->setModel(m_model);
}

EmblemSelector::~ EmblemSelector( )
{
	delete m_ui;
}

void EmblemSelector::on_iconSelection_doubleClicked( const QModelIndex &  )
{
	accept();
}

void EmblemSelector::setCurrentIcon( const QString & name )
{
	QList<QStandardItem *> items = m_model->findItems(name);
	if( items.size() == 1)
	{
		m_ui->iconSelection->setCurrentIndex( m_model->indexFromItem( items.at(0) ) );
	}
}

QString EmblemSelector::currentIcon( ) const
{
	QModelIndex index = m_ui->iconSelection->currentIndex();
	return m_model->data(index, Qt::UserRole + 1).toString();
}

QString generateReadImageFilter()
{
	QList<QByteArray> formats = QImageReader::supportedImageFormats();
	QStringList returnFormats;
	const QString filterTemplate = "*.%1";
	foreach( QString format, formats )
		returnFormats += filterTemplate.arg(format);

	return returnFormats.join(" ");
}

void EmblemSelector::on_addImage_clicked()
{
	if( m_mindmap == 0 )
		return;
	
	QString fileName = QFileDialog::getOpenFileName(
	                                                 QApplication::activeWindow(), tr("Select an image file"),
	                                                 m_mindmap->currentPath(),
	                                                 tr("Image Files (%1)").arg(generateReadImageFilter()));
	if( !fileName.isEmpty() )
	{
		QImage img(fileName);
		if(!img.isNull())
		{
			QFileInfo fileInfo(fileName);
			m_mindmap->setImageAttachment( fileInfo.fileName(), img );
			QImage image = m_mindmap->imageAttachment(fileInfo.fileName());
			QStandardItem *item = new QStandardItem( QPixmap::fromImage(image), fileInfo.fileName());
			item->setData(fileInfo.fileName(), Qt::UserRole + 1);
			item->setEditable( false );
			m_model->appendRow(item);
		}
		else
		{
			// Error loading image
		}
	}
}

void EmblemSelector::on_removeImage_clicked()
{
	if( m_mindmap == 0 )
		return;
	
	if( !currentIcon().startsWith(":/emblems") )
	{
		m_mindmap->removeImageAttachment( currentIcon() );
		m_model->removeRow(  m_ui->iconSelection->currentIndex().row() );
	}
}

