/* ============================================================
 * File  : tetrahedron.cpp
 * Author: Stephane Gibault
 * Date  : Sun Feb 4 2007
 * Description :
 *
 * 
 *  (C) 2007 by Stephane Gibault
 *
 * 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, 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.
 *
 * ============================================================ */
#include <QtGui>
#include "mylstfiles.h"

mylstfiles::mylstfiles ( QWidget * parent )
    : QListWidget(parent)
{
	action1 = new QAction(tr("Importer toutes les images de ce dossier"), this);
	action1->setIcon ( QIcon(":/new/prefix2/Interface/Theme/frame_image.png") );
	action2 = new QAction(tr("Importer les images selectionnees"), this);
	action2->setIcon ( QIcon(":/new/prefix2/Interface/Theme/frame_image.png") );
	action3 = new QAction(tr("Importer comme arriere plan"), this);
	action3->setIcon ( QIcon(":/new/prefix2/Interface/Theme/frame_image.png") );
	connect(action1, SIGNAL(triggered()), this, SLOT(import_folder()));
	connect(action2, SIGNAL(triggered()), this, SLOT(import_pictures()));
	connect(action3, SIGNAL(triggered()), this, SLOT(import_background()));
}

void mylstfiles::contextMenuEvent(QContextMenuEvent *event)
{
	if (currentItem() == NULL) 
	{
		return;
	}
	else
	{
		if ( status == 0 )
		{
			if (currentItem()->statusTip() == "FOLDER" && currentItem()->text() != "..") 
			{
				QMenu menu(this);
				menu.addAction(action1);
				menu.exec(event->globalPos());
			}
			else
			{
				if (currentItem()->statusTip() == "IMAGE") 
				{
					QMenu menu(this);
					menu.addAction(action2);
					menu.exec(event->globalPos());
				}
			}
		}
		else
		{
			if (currentItem()->statusTip() == "IMAGE") 
			{
				QMenu menu(this);
				menu.addAction(action3);
				menu.exec(event->globalPos());
			}
		}
	}
}

void mylstfiles::import_folder()
{
	emit folder_import();
}

void mylstfiles::import_pictures()
{
	emit pictures_import();
}

void mylstfiles::import_background()
{
	emit background_import();
}

void mylstfiles::mouseMoveEvent(QMouseEvent *event)
{
	if (!(event->buttons() & Qt::LeftButton)) return;
	if (currentItem() == NULL) 
	{
		return;
	}
	if (currentItem()->statusTip() == "FOLDER" || status == 1 )
	{
	}
	else
	{
		QIcon myico = currentItem()->icon();
		currentselect = myico.pixmap ( 128, 128, QIcon::Normal ,  QIcon::On);
		//
		QMimeData *mimedata = new QMimeData;
		QList<QListWidgetItem *> lstmul = this->selectedItems();
		QList<QUrl> lsturl;
		QUrl myurl;
		int nbrebad=0;
		for (int i = 0; i < lstmul.size(); ++i) 
		{
			if ( lstmul.at(i)->statusTip() != "FOLDER" )
			{
				myurl.setPath(wl_current_path + "/" + lstmul.at(i)->toolTip());
				lsturl.insert ( i, myurl );
			}
			else
			{
				nbrebad++;
			}
		}
		//myurl.setPath("");
		//lsturl.insert ( lstmul.size()-nbrebad, myurl );
		mimedata->setUrls(lsturl);
		QDrag *drag = new QDrag(this);
	 	drag->setMimeData(mimedata);
		drag->setPixmap(currentselect);
		drag->exec(Qt::MoveAction);
	}
}

Qt::DropActions mylstfiles::supportedDropActions () const
{
	// returns what actions are supported when dropping
	return Qt::CopyAction | Qt::MoveAction;
}

