#include <qwidgetstack.h>
#include <qvbuttongroup.h>
#include <qradiobutton.h>
#include <qhbox.h>
#include <qvbox.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qvalidator.h>

#include <qcolor.h>
#include <qpixmap.h>

#include "CTripleColorWidget.h"

CTripleColorWidget::CTripleColorWidget( QWidget *parent,
                                        const char *name)
  : QWidget( parent , name)
{
  m_hbox = new QHBox(this);
  m_hbox->setSpacing(6);
  m_vbox = new QVBox(m_hbox);
  m_vbox->setSpacing(6);

  m_colorwidgetstack = new QWidgetStack(m_hbox);
  m_buttongroup      = new QVButtonGroup(m_vbox);

  const char *String[3] = { "Ambient" , "Diffuse", "Specular" };

  for (int i = 0; i < 2; ++i)
    {
      m_colorwidgets[i] = new CColorWidget(m_colorwidgetstack);
      m_colorwidgetstack->addWidget(m_colorwidgets[i], i);
      connect( m_colorwidgets[i] , SIGNAL(Changed()) ,
               this              , SLOT(Update())     );

      m_radiobuttons[i] = new QRadioButton(String[i], m_buttongroup);
    }

  m_vbox2 = new QVBox(m_colorwidgetstack->widget(2));
  m_vbox2->setSpacing(6);

  m_colorwidgets[2] = new CColorWidget(m_vbox2);
  connect( m_colorwidgets[2] , SIGNAL(Changed()) ,
           this              , SLOT(Update())     );
  m_hbox2 = new QHBox(m_vbox2);
  m_hbox2->setSpacing(6);

  m_exponent = new QLabel("Specular exponent" , m_hbox2 );

  m_doublevalidator = new QDoubleValidator( this );
  m_doublevalidator->setBottom(0.0);
  m_doublevalidator->setDecimals(5);
  m_exponentLineEdit = new QLineEdit( m_hbox2 );
  m_exponentLineEdit->setValidator(m_doublevalidator);
  m_exponentLineEdit->setText("1");

  m_colorwidgetstack->addWidget(m_vbox2, 2);

  m_radiobuttons[2] = new QRadioButton(String[2], m_buttongroup);


  connect( m_buttongroup      , SIGNAL(clicked(int)) ,
           m_colorwidgetstack , SLOT(raiseWidget(int)) );
  connect( m_colorwidgetstack , SIGNAL(aboutToShow(QWidget *)) ,
           this               , SLOT(Update(QWidget *))         );

  m_radiobuttons[0]->setChecked(true);

  m_preview = new QLabel(m_vbox);

  m_colorwidgetstack->adjustSize();
  m_buttongroup->adjustSize();
  m_hbox->adjustSize();
  adjustSize();
  m_preview->resize(QSize(90,58));
  setMinimumSize(size());
  setMaximumSize(size());
  Update(m_colorwidgets[0]);

  connect( m_colorwidgets[0]  , SIGNAL(Changed()),
           this               , SLOT(Changed0())  );
  connect( m_colorwidgets[1]  , SIGNAL(Changed()),
           this               , SLOT(Changed1())  );
  connect( m_colorwidgets[2]  , SIGNAL(Changed()),
           this               , SLOT(Changed2())  );
  connect( m_exponentLineEdit , SIGNAL(textChanged(const QString&)),
           this               , SLOT(Changed3())  );
}


CTripleColorWidget::~CTripleColorWidget()
{
  delete m_preview;

  delete m_exponentLineEdit;
  delete m_doublevalidator;
  delete m_exponent;

  delete m_hbox2;
  delete m_vbox2;

  for (int i = 0; i < 2; ++i)
    {
      delete m_radiobuttons[i];
      delete m_colorwidgets[i];
    }
  delete m_radiobuttons[2];
  // m_colorwidgets[2] is deleted by the VBox

  delete m_buttongroup;
  delete m_colorwidgetstack;

  delete m_vbox;
  delete m_hbox;
}


float CTripleColorWidget::getSpecularExponent() const
{
  return m_exponentLineEdit->text().toDouble();
}


void CTripleColorWidget::Update(QWidget *widget)
{
  if (!widget)
    widget = m_colorwidgetstack->visibleWidget();

  if (widget)
    {
      QPixmap color(m_preview->size());

      if (widget == m_colorwidgets[0] || widget == m_colorwidgets[1])
        {
          color.fill( ((CColorWidget *) widget)->getQtColor());
          m_hbox2->hide();
        }
      else
        {
          color.fill( m_colorwidgets[2]->getQtColor() );
          m_hbox2->show();
        }

      m_preview->setPixmap(color);
    }
}
