/***************************************************************************
 *   Copyright (C) 2004 by Hans Oischinger                                 *
 *   hans.oischinger@kdemail.net                                           *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#include "strokefillpreview.h"

// Elements
#include "elements/elementnode.h"
#include "elements/elementselection.h"
#include "elements/vectorelement.h"
#include "elements/strokeattribute.h"
#include "elements/fillattribute.h"
#include "elements/animatedcolorattr.h"
#include "elements/colorattribute.h"

// Core
#include "core/direktorinstance.h"
#include "core/elementdatabase.h"

// Command
#include "commands/efillcmd.h"
#include "commands/estrokecmd.h"

// Qt
#include <qframe.h>
#include <qpainter.h>
#include <qrect.h>
#include <qlayout.h>

// KDE
#include <kdebug.h>
#include <kdualcolorbutton.h>

StrokeFillPreview::StrokeFillPreview(QWidget *parent, const char *name)
    : QWidget(parent, name),
    m_element(0)
{
  setFixedSize(50, 50);
  QGridLayout *layout = new QGridLayout(this,1,1);
  m_colorBtn = new KDualColorButton(this, "StrokeFillPreview DualColorButton");
  layout->addWidget(m_colorBtn,0,0);
  setElement(0);

  Kast::self()->elementDB().selection().addElementSelectionListener( this );
  Kast::self()->elementDB().addElementListener( this );
  connect(m_colorBtn, SIGNAL(bgChanged(const QColor& )), this, SLOT(slotBgChanged(const QColor& )));
  connect(m_colorBtn, SIGNAL(fgChanged(const QColor& )), this, SLOT(slotFgChanged(const QColor& )));
  connect(m_colorBtn, SIGNAL(currentChanged(KDualColorButton::DualColor )),
          this, SLOT(slotCurrentChanged()));
}


StrokeFillPreview::~StrokeFillPreview()
{}

void StrokeFillPreview::onElementInserted( ElementNode* )
{}

void StrokeFillPreview::onElementRemoved( ElementNode* e )
{
  kdDebug() << k_funcinfo << endl;

  if (e == m_element)
    setElement(0);
}

void StrokeFillPreview::onElementModified( ElementNode *e )
{
  kdDebug() << "StrokeFillPreview::onElementModified()" << endl;
  if (e == m_element)
    setElement(m_element);
}

void StrokeFillPreview::onSelectionModified( )
{
  setElement( Kast::self()->elementDB().selection().visualElements().first() );
}

void StrokeFillPreview::setElement(VectorElement* element)
{
  m_element = element;
  if (!m_element)
    m_colorBtn->setDisabled(true);
  else
  {
    m_colorBtn->setDisabled(false);
    m_colorBtn->setForeground( m_element->fill().color().baseVal().qColor() );
    m_colorBtn->setBackground( m_element->stroke().color().baseVal().qColor() );
  }
}

void StrokeFillPreview::slotBgChanged( const QColor &c )
{
  if ( c == m_element->stroke().color().baseVal().qColor() )
    return;

  StrokeAttribute newStroke(m_element);
  ColorAttribute ourColor( c );
  newStroke.color().baseVal() = ourColor;
  newStroke.setStrokeType( StrokeAttribute::STROKE_SOLID );
  EStrokeCMD *cmd = new EStrokeCMD( m_element, newStroke );
  Kast::self()->addCommand(cmd);
}

void StrokeFillPreview::slotFgChanged( const QColor &c )
{
  if ( c == m_element->fill().color().baseVal().qColor() )
    return;

  FillAttribute newFill(m_element);
  ColorAttribute ourColor( c );
  newFill.color().baseVal() = ourColor;
  newFill.setFillType( FillAttribute::FILL_SOLID );
  EFillCMD *cmd = new EFillCMD( m_element, newFill );
  Kast::self()->addCommand(cmd);
}

void StrokeFillPreview::slotCurrentChanged()
{
  if ( m_colorBtn->current() == KDualColorButton::Foreground )
    Kast::self()->setStyleTarget( Kast::ST_FILL );
  else
    Kast::self()->setStyleTarget( Kast::ST_STROKE );
}



#include "strokefillpreview.moc"
