/***************************************************************************
 *   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 "strokedocker.h"

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

// Elements
#include "elements/strokeattribute.h"
#include "elements/animatednumber.h"
#include "elements/number.h"
#include "elements/elementselection.h"
#include "elements/vectorelement.h"

// Commands
#include "commands/estrokecmd.h"

// Qt
#include <qhbuttongroup.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qspinbox.h>
#include <qwidget.h>

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

StrokeDocker::StrokeDocker(QWidget *parent, const char *name)
    : DDocker( parent, name)
{
  m_stroke = new StrokeAttribute(0);

  // Setup GUI
  setCaption( i18n( "Stroke Properties" ) );

  QPushButton *button;
  mainWidget = new QWidget( this );
  QGridLayout *mainLayout = new QGridLayout( mainWidget, 4, 2 );

  QLabel* widthLabel = new QLabel( i18n ( "Width:" ), mainWidget );
  mainLayout->addWidget( widthLabel, 0, 0 );
  m_setLineWidth = new QSpinBox( 0, 1000, 1, mainWidget );
  mainLayout->addWidget ( m_setLineWidth, 0, 1 );
  connect( m_setLineWidth, SIGNAL( valueChanged( int ) ), this, SLOT( slotWidthChanged() ) );

  QLabel* capLabel = new QLabel( i18n ( "Cap:" ), mainWidget );
  mainLayout->addWidget( capLabel, 1, 0 );
  m_capGroup = new QHButtonGroup( mainWidget );
  m_capGroup->setFrameShape( QFrame::NoFrame );
  m_capGroup->setInsideMargin( 1 );
  m_capGroup->setExclusive( true );
  button = new QPushButton( "", m_capGroup );
  button->setPixmap( SmallIcon( "cap_butt" ) );
  button->setToggleButton( true );
  m_capGroup->insert( button );
  button = new QPushButton( "", m_capGroup );
  button->setPixmap( SmallIcon( "cap_round" ) );
  button->setToggleButton( true );
  m_capGroup->insert( button );
  button = new QPushButton( "", m_capGroup );
  button->setPixmap( SmallIcon( "cap_square" ) );
  button->setToggleButton( true );
  m_capGroup->insert( button );
  mainLayout->addWidget( m_capGroup, 1, 1 );
  connect( m_capGroup, SIGNAL( clicked( int ) ), this, SLOT( slotCapChanged( int ) ) );

  QLabel* joinLabel = new QLabel( i18n ( "Join:" ), mainWidget );
  mainLayout->addWidget( joinLabel, 2, 0 );

  m_joinGroup = new QHButtonGroup( mainWidget );
  m_joinGroup->setFrameShape( QFrame::NoFrame );
  m_joinGroup->setInsideMargin( 1 );
  m_joinGroup->setExclusive( true );
  button = new QPushButton( "", m_joinGroup );
  button->setPixmap( SmallIcon( "join_miter" ) );
  button->setToggleButton( true );
  m_joinGroup->insert( button );
  button = new QPushButton( "", m_joinGroup );
  button->setPixmap( SmallIcon( "join_round" ) );
  button->setToggleButton( true );
  m_joinGroup->insert( button );
  button = new QPushButton( "", m_joinGroup );
  button->setPixmap( SmallIcon( "join_bevel" ) );
  button->setToggleButton( true );
  m_joinGroup->insert( button );
  mainLayout->addWidget( m_joinGroup, 2, 1 );
  connect( m_joinGroup, SIGNAL( clicked( int ) ), this, SLOT( slotJoinChanged( int ) ) );

  mainLayout->activate();
  mainWidget->resize(120,90);

  Kast::self()->elementDB().selection().addElementSelectionListener(this);
  updateDocker();
}

StrokeDocker::~StrokeDocker()
{
  delete m_stroke;
}

void StrokeDocker::onSelectionModified( )
{
  updateDocker();
}

void StrokeDocker::fireStrokeCMD()
{
  if ( Kast::self()->elementDB().selection().visualElements().isEmpty() )
  {
    kdDebug() << "StrokeDocker::fireStrokeCMD() - Selection empty: discarding" << endl;
    return;
  }
  kdDebug() << "StrokeDocker::fireStrokeCMD() - Firing stroke command" << endl;
  EStrokeCMD *strokecmd = new EStrokeCMD( *m_stroke );
  Kast::self()->addCommand(strokecmd);
}

void StrokeDocker::slotCapChanged( int ID )
{
  //   switch( ID )
  //   {
  //   case 1:
  //     m_stroke.setLineCap( VStroke::capRound ); break;
  //   case 2:
  //     m_stroke.setLineCap( VStroke::capSquare ); break;
  //   default:
  //     m_stroke.setLineCap( VStroke::capButt );
  //   }
  fireStrokeCMD();
}

void StrokeDocker::slotJoinChanged( int ID )
{
  //   switch( ID )
  //   {
  //   case 1:
  //     m_stroke.setLineJoin( VStroke::joinRound ); break;
  //   case 2:
  //     m_stroke.setLineJoin( VStroke::joinBevel ); break;
  //   default:
  //     m_stroke.setLineJoin( VStroke::joinMiter );
  //   }
  fireStrokeCMD();
}

void StrokeDocker::updateDocker()
{
  disconnect( m_setLineWidth, SIGNAL( valueChanged( int ) ), this, SLOT( slotWidthChanged() ) );
  disconnect( m_capGroup, SIGNAL( clicked( int ) ), this, SLOT( slotCapChanged( int ) ) );
  disconnect( m_joinGroup, SIGNAL( clicked( int ) ), this, SLOT( slotJoinChanged( int ) ) );

  bool enableWidgets = !Kast::self()->elementDB().selection().visualElements().isEmpty();

  m_capGroup->setEnabled( enableWidgets );
  m_joinGroup->setEnabled( enableWidgets );
  m_setLineWidth->setEnabled( enableWidgets );

  if ( enableWidgets )
    *m_stroke = Kast::self()->elementDB().selection().visualElements().getFirst()->stroke();

  /*
    switch( m_stroke.lineCap() )
    {
    case VStroke::capRound:
      m_capGroup->setButton( 1 ); break;
    case VStroke::capSquare:
      m_capGroup->setButton( 2 ); break;
    default:
      m_capGroup->setButton( 0 );
    }

    switch( m_stroke.lineJoin() )
    {
    case VStroke::joinRound:
      m_joinGroup->setButton( 1 ); break;
    case VStroke::joinBevel:
      m_joinGroup->setButton( 2 ); break;
    default:
      m_joinGroup->setButton( 0 );
    }*/

  m_setLineWidth->setValue( m_stroke->strokeWidth().animVal().value() );

  connect( m_setLineWidth, SIGNAL( valueChanged( int ) ), this, SLOT( slotWidthChanged() ) );
  connect( m_capGroup, SIGNAL( clicked( int ) ), this, SLOT( slotCapChanged( int ) ) );
  connect( m_joinGroup, SIGNAL( clicked( int ) ), this, SLOT( slotJoinChanged( int ) ) );
}

void StrokeDocker::slotWidthChanged()
{
  m_stroke->strokeWidth().baseVal().setValue( m_setLineWidth->value() );
  fireStrokeCMD();
}


#include "strokedocker.moc"
