/* This file is part of the KDE project
   Made by Tomislav Lukman (tomislav.lukman@ck.tel.hr)
   Copyright (C) 2002, 2003 The Karbon Developers

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <qlabel.h>
#include <qlayout.h>
#include <qtabwidget.h>
#include <qsize.h>
#include <qhbuttongroup.h>
#include <qtoolbutton.h>

#include <klocale.h>
#include <kiconloader.h>
#include <koMainWindow.h>
#include <koFilterManager.h>
#include <kfiledialog.h>

#include "karbon_part.h"
#include "karbon_view.h"
#include "karbon_factory.h"
#include "karbon_resourceserver.h"
#include "karbon_drag.h"
#include "vselection.h"
#include "vlayer.h"
#include "vfill.h"
#include "vfillcmd.h"
#include "vtransformcmd.h"

#include "vstyledocker.h"

#include <unistd.h>

ClipartChooser::ClipartChooser( QSize iconSize, QWidget *parent, const char *name )
	: KoIconChooser( iconSize, parent, name )
{
	setDragEnabled( true );
}

void
ClipartChooser::startDrag()
{
	KoIconChooser::startDrag();
	KarbonDrag* kd = new KarbonDrag( this );
	VObjectList objects;
	VClipartIconItem *selectedClipart = (VClipartIconItem *)currentItem();
	double s = kMax( selectedClipart->originalWidth(), selectedClipart->originalHeight() );
	VObject *clipart = selectedClipart->clipart()->clone();

	QWMatrix mat( s, 0, 0, -s, -( s / 2 ), ( s / 2 ) );

	VTransformCmd trafo( 0L, mat );
	trafo.visit( *clipart );

	objects.append( clipart );
	kd->setObjectList( objects );
	kd->dragCopy();
}

VStyleDocker::VStyleDocker( KarbonPart* part, KarbonView* parent, const char* /*name*/ )
	: VDocker( parent->shell() ), m_part ( part ), m_view( parent )
{
	setCaption( i18n( "Style Settings" ) );

	mainWidget = new QWidget( this );
	mTabWidget = new QTabWidget( mainWidget );

	//Pattern
	KoPatternChooser *pPatternChooser = new KoPatternChooser( KarbonFactory::rServer()->patterns(), mTabWidget );
	pPatternChooser->setCaption( i18n( "Patterns" ) );

	connect( pPatternChooser, SIGNAL(selected( KoIconItem * ) ), this, SLOT( slotItemSelected( KoIconItem * )));
	mTabWidget->addTab( pPatternChooser, i18n( "Patterns" ) );

	//Clipart
	ClipartWidget *pClipartWidget = new ClipartWidget( KarbonFactory::rServer()->cliparts(), part, mTabWidget );
	mTabWidget->addTab( pClipartWidget, i18n( "Clipart" ) );

	QVBoxLayout *mainWidgetLayout = new QVBoxLayout( mainWidget, 2 );
	mainWidgetLayout->addWidget( mTabWidget );
	mainWidgetLayout->activate();
	mainWidget->setMinimumHeight( 174 );
	mainWidget->setMinimumWidth( 194 );

	setWidget( mainWidget );
}

VStyleDocker::~VStyleDocker()
{
}

void VStyleDocker::slotItemSelected( KoIconItem *item )
{
	VPattern *pattern = (VPattern *)item;
	if( !pattern ) return;
	kdDebug(38000) << "loading pattern : " << pattern->tilename().latin1() << endl;
	if( m_part && m_part->document().selection() )
	{
		VFill fill;
		fill.pattern() = *pattern;//.load( pattern->tilename() );
		//fill.setColor( *m_color );
		fill.setType( VFill::patt );
		m_part->addCommand( new VFillCmd( &m_part->document(), fill ), true );
	}
}

void
VStyleDocker::mouseReleaseEvent( QMouseEvent * )
{
}

ClipartWidget::ClipartWidget( QPtrList<VClipartIconItem>* clipartItems, KarbonPart *part, QWidget* parent )
	: QWidget( parent ), m_part( part )
{
	KIconLoader il;

	QVBoxLayout* layout = new QVBoxLayout( this );
	layout->addWidget( m_clipartChooser = new ClipartChooser( QSize( 32, 32 ), this ) );
	layout->addWidget( m_buttonGroup = new QHButtonGroup( this ) );
	QToolButton* m_addClipartButton;
	m_buttonGroup->insert( m_addClipartButton = new QToolButton( m_buttonGroup ) );
	m_buttonGroup->insert( m_importClipartButton = new QToolButton( m_buttonGroup ) );
	m_buttonGroup->insert( m_deleteClipartButton = new QToolButton( m_buttonGroup ) );
	m_clipartChooser->setFixedSize( 180, 120 );
	m_addClipartButton->setIconSet( SmallIcon( "14_layer_newlayer" ) );
	m_addClipartButton->setTextLabel( i18n( "Add" ) );
	m_importClipartButton->setIconSet( SmallIcon( "fileimport" ) );
	m_importClipartButton->setTextLabel( i18n( "Import" ) );
	m_deleteClipartButton->setIconSet( SmallIcon( "14_layer_deletelayer" ) );
	m_deleteClipartButton->setTextLabel( i18n( "Delete" ) );

	m_buttonGroup->setInsideMargin( 3 );

	//setFrameStyle( Box | Sunken );
	layout->setMargin( 3 );

	connect( m_buttonGroup, SIGNAL( clicked( int ) ), this, SLOT( slotButtonClicked( int ) ) );
	//connect( m_deleteClipartButton, SIGNAL( clicked() ), this, SLOT( deleteClipart() ) );
	connect( m_clipartChooser, SIGNAL( selected( KoIconItem* ) ), this, SLOT( clipartSelected( KoIconItem* ) ) );

	m_clipartChooser->setAutoDelete( false );
	VClipartIconItem* item = 0L;

	for( item = clipartItems->first(); item; item = clipartItems->next() )
		m_clipartChooser->addItem( item );

	m_clipartItem = ( clipartItems->first() ) ? clipartItems->first()->clone() : 0;
	if( !m_clipartItem )
		m_deleteClipartButton->setEnabled( false );
}

ClipartWidget::~ClipartWidget()
{
	delete m_clipartItem;
}

VClipartIconItem* ClipartWidget::selectedClipart()
{
	return m_clipartItem;
}

void
ClipartWidget::clipartSelected( KoIconItem* item )
{
	if( item )
	{
		delete m_clipartItem;
		VClipartIconItem* clipartItem = ( VClipartIconItem* ) item;
		m_deleteClipartButton->setEnabled( clipartItem->canDelete() );
		m_selectedItem = clipartItem;
		m_clipartItem = clipartItem->clone();
	}
}

void
ClipartWidget::addClipart()
{
	VObject* clipart = 0L;
	VSelection* selection = m_part->document().selection();

	if( selection->objects().count() == 1 )
	{
		clipart = selection->objects().getFirst()->clone();
		clipart->setParent( 0L );
	}

	if( selection->objects().count() > 1 )
	{
		QPtrVector<VObject> objects;
		selection->objects().toVector( &objects );
		VGroup* group = new VGroup( 0L );

		for( unsigned int i = 0; i < objects.count(); i++ )
		{
			VObject *obj = objects[ i ]->clone();
			obj->setParent( 0L );
			group->append( obj );
		}

		clipart = group;
	}

	if( clipart )
	{
		KoRect clipartBox = clipart->boundingBox();
		double scaleFactor = 1. / kMax( clipartBox.width(), clipartBox.height() );
		QWMatrix trMatrix( scaleFactor, 0, 0, scaleFactor, -clipartBox.x() * scaleFactor, -clipartBox.y() * scaleFactor );

		VTransformCmd trafo( 0L, trMatrix );
		trafo.visit( *clipart );

		// center the clipart
		trMatrix.reset();
		double size = kMax( clipart->boundingBox().width(), clipart->boundingBox().height() );
		trMatrix.translate( ( size - clipart->boundingBox().width() ) / 2, ( size - clipart->boundingBox().height() ) / 2 );

		trafo.setMatrix( trMatrix );
		trafo.visit( *clipart );

		// remove Y-mirroring
		trMatrix.reset();
		trMatrix.scale( 1, -1 );
		trMatrix.translate( 0, -1 );

		trafo.setMatrix( trMatrix );
		trafo.visit( *clipart );

		m_clipartChooser->addItem( KarbonFactory::rServer()->addClipart( clipart, clipartBox.width(), clipartBox.height() ) );
	}

	m_clipartChooser->updateContents();
}

void
ClipartWidget::importClipart()
{
	QStringList filter;
	filter << "application/x-karbon" << "image/svg+xml" << "image/x-wmf" << "image/x-eps" << "application/postscript";
	KFileDialog *dialog = new KFileDialog( "foo", QString::null, 0L, "Choose Graphic to Add", true);
	dialog->setMimeFilter( filter, "application/x-karbon" );
	if( dialog->exec()!=QDialog::Accepted )
	{
		delete dialog;
		return;
	}
	QString fname = dialog->selectedFile();
	delete dialog;
	if( m_part->nativeFormatMimeType() == dialog->currentMimeFilter().latin1() )
		m_part->mergeNativeFormat( fname );
	else
	{
		KoFilterManager man( m_part );
		KoFilter::ConversionStatus status;
		QString importedFile = man.import( fname, status );
		if( status == KoFilter::OK )
			m_part->mergeNativeFormat( importedFile );
		if( !importedFile.isEmpty() )
			unlink( QFile::encodeName( importedFile ) );
		if( status != KoFilter::OK )
			return;
	}
	m_part->document().selection()->clear();
	m_part->document().selection()->append( m_part->document().activeLayer()->objects() );
	addClipart();
	m_part->document().selection()->clear();
	m_part->document().removeLayer( m_part->document().activeLayer() );
}

void
ClipartWidget::deleteClipart()
{
	VClipartIconItem* clipartItem = m_clipartItem;
	KarbonFactory::rServer()->removeClipart( clipartItem );
	m_clipartChooser->removeItem( m_selectedItem );
	m_clipartChooser->updateContents();
}

void
ClipartWidget::slotButtonClicked( int id )
{
	switch( id )
	{
		case 0: addClipart(); break;
		case 1: importClipart(); break;
		case 2: deleteClipart();
	}
}

#include "vstyledocker.moc"

