/***************************************************************************
 *   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 "stylewidget.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"
#include "elements/urireference.h"
#include "elements/gradient.h"

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

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

// Widgets
#include "rgbselwidgetimpl.h"
#include "gradienteditorwidget.h"

// Qt
#include <qlayout.h>

// KDE
#include <ktabwidget.h>
#include <klocale.h>
#include <kdebug.h>

StyleWidget::StyleWidget(QWidget *parent, const char *name)
    : QWidget(parent, name),
    m_rgbValDirty(false)
{
  QVBoxLayout *vbox = new QVBoxLayout( this );
  m_tabs = new KTabWidget(this, "StyleWidget Tabs");
  vbox->addWidget( m_tabs );

  m_rgbSelWidget = new RGBSelWidgetImpl();
  m_tabs->addTab( m_rgbSelWidget, i18n("RGB") );
  m_gradEditor = new GradientEditorWidget();
  m_tabs->addTab( m_gradEditor, i18n("Gradient") );

  Kast::self()->elementDB().selection().addElementSelectionListener( this );
  Kast::self()->elementDB().addElementListener( this );
  connect(Kast::self(), SIGNAL(styleTargetChanged()), this, SLOT(updateWidgets()));

  connect(m_rgbSelWidget, SIGNAL(valueChanged(const QColor&)),
          this, SLOT(slotRGBValueChanged(const QColor&)));
}

StyleWidget::~StyleWidget()
{}

void StyleWidget::slotRGBValueChanged( const QColor & )
{
  m_rgbValDirty = true;
}

void StyleWidget::mouseReleaseEvent( QMouseEvent * e )
{
  // Check if the RGB Widget has an updated color
  if (m_rgbValDirty)
  {
    fireRGBCommand();
    m_rgbValDirty = false;
  }

  e->accept();
}

void StyleWidget::onElementInserted( ElementNode* )
{}

void StyleWidget::onElementRemoved( ElementNode* )
{}

void StyleWidget::onElementModified( ElementNode *e )
{
  kdDebug() << "StrokeFillPreview::onElementModified()" << endl;
  if (e == Kast::self()->elementDB().selection().visualElements().first())
  {
    updateWidgets();
  }
}

void StyleWidget::onSelectionModified( )
{
  updateWidgets();
}

void StyleWidget::updateWidgets()
{
  VectorElement* element = 0;
  element = Kast::self()->elementDB().selection().visualElements().first();
  if (!element)
    return;

  if ( Kast::self()->getStyleTarget() == Kast::ST_FILL )
  {
    m_rgbSelWidget->setValue(element->fill().color().baseVal().qColor());
    Gradient* grad = ::qt_cast<Gradient*>(element->fill().ref().getRefElement());
    if (grad)
      m_gradEditor->setGradient(*grad);
  }
  else if ( Kast::self()->getStyleTarget() == Kast::ST_STROKE )
  {
    m_rgbSelWidget->setValue(element->stroke().color().baseVal().qColor());
  }
}

void StyleWidget::fireRGBCommand( )
{
  VectorElement* element = 0;
  element = Kast::self()->elementDB().selection().visualElements().first();
  if (!element)
    return;

  QColor c = m_rgbSelWidget->value();
  if ( Kast::self()->getStyleTarget() == Kast::ST_FILL )
  {
    FillAttribute newFill(element);
    ColorAttribute ourColor( c );
    newFill.color().baseVal() = ourColor;
    newFill.setFillType( FillAttribute::FILL_SOLID );
    EFillCMD *cmd = new EFillCMD( element, newFill );
    Kast::self()->addCommand(cmd);
  }
  else if ( Kast::self()->getStyleTarget() == Kast::ST_STROKE )
  {
    StrokeAttribute newStroke(element);
    ColorAttribute ourColor( c );
    newStroke.color().baseVal() = ourColor;
    newStroke.setStrokeType( StrokeAttribute::STROKE_SOLID );
    EStrokeCMD *cmd = new EStrokeCMD( element, newStroke );
    Kast::self()->addCommand(cmd);
  }
}

#include "stylewidget.moc"
