/*------------------------------------------------------------------------------
 src/lib/djWidget/imageOpGradientWidget.cxx
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

//#define DJ_DBG
#include "djImageOpGenWidget.h"
#include <djLabelWidget.h>
#include <djGroupBoxWidget.h>
#include <djVecWidget.h>
#include <djPixelWidget.h>
#include <djColorWidget.h>

namespace dj {

ImageOpGradientWidget::ImageOpGradientWidget() :
  _callbacks(true)
{
  GroupBoxWidget * imageGroup = new GroupBoxWidget("Image");
  LabelWidget * sizeLabel = new LabelWidget("Size:");
  _sizeWidget = new Vec2iWidget;
  LabelWidget * pixelLabel = new LabelWidget("Pixel:");
  _pixelWidget = new PixelWidget;
  GroupBoxWidget * positionGroup = new GroupBoxWidget("Position");
  LabelWidget * positionLabel = new LabelWidget("Start:");
  _positionWidget = new Vec2iWidget;
  LabelWidget * position2Label = new LabelWidget("End:");
  _position2Widget = new Vec2iWidget;
  GroupBoxWidget * colorGroup = new GroupBoxWidget("Color");
  _colorWidget = new ColorWidget;
  _color2Widget = new ColorWidget;
  
  List<LabelWidget *> labelList;
  labelList += sizeLabel;
  labelList += pixelLabel;
  sizeLabel->labelGroup(labelList);
  pixelLabel->labelGroup(labelList);
  labelList.del();
  labelList += positionLabel;
  labelList += position2Label;
  positionLabel->labelGroup(labelList);
  position2Label->labelGroup(labelList);
  
  VLayout * layout = new VLayout(this);
  layout->add(imageGroup);
  HLayout * subLayout = new HLayout(imageGroup->layout());
  subLayout->margin(0);
  subLayout->add(sizeLabel);
  subLayout->add(_sizeWidget);
  subLayout = new HLayout(imageGroup->layout());
  subLayout->margin(0);
  subLayout->add(pixelLabel);
  subLayout->add(_pixelWidget);
  layout->add(positionGroup);
  subLayout = new HLayout(positionGroup->layout());
  subLayout->margin(0);
  subLayout->add(positionLabel);
  subLayout->add(_positionWidget);
  subLayout = new HLayout(positionGroup->layout());
  subLayout->margin(0);
  subLayout->add(position2Label);
  subLayout->add(_position2Widget);
  layout->add(colorGroup);
  colorGroup->layout()->add(_colorWidget);
  colorGroup->layout()->add(_color2Widget);
  layout->stretchAdd();
  
  widgetUpdate();
  
  _sizeWidget->sig().set(this, sizeCallback);
  _pixelWidget->sig().set(this, pixelCallback);
  _positionWidget->sig().set(this, positionCallback);
  _position2Widget->sig().set(this, position2Callback);
  _colorWidget->sig().set(this, colorCallback);
  _color2Widget->sig().set(this, color2Callback);
}

void ImageOpGradientWidget::set(ImageOp * in)
{
  ImageOpWidget::set(in);
  widgetUpdate();
}

void ImageOpGradientWidget::widgetUpdate()
{
  _callbacks = false;
  
  Vec2i size;
  Pixel pixel;
  Vec2i position, position2;
  Color color, color2;
  if (_op) {
    StrList data = _op->attr(
      imageOpGradientAttrStr()[ImageOpGradient::ATTR_SIZE]
    );
    inflateStr(data, &size);
    data = _op->attr(
      imageOpGradientAttrStr()[ImageOpGradient::ATTR_PIXEL]
    );
    inflateStr(data, &pixel);
    data = _op->attr(
      imageOpGradientAttrStr()[ImageOpGradient::ATTR_POSITION]
    );
    inflateStr(data, &position);
    data = _op->attr(
      imageOpGradientAttrStr()[ImageOpGradient::ATTR_POSITION_2]
    );
    inflateStr(data, &position2);
    data = _op->attr(
      imageOpGradientAttrStr()[ImageOpGradient::ATTR_COLOR]
    );
    inflateStr(data, &color);
    data = _op->attr(
      imageOpGradientAttrStr()[ImageOpGradient::ATTR_COLOR_2]
    );
    inflateStr(data, &color2);
  }
  _sizeWidget->set(size);
  _pixelWidget->set(pixel);
  _positionWidget->set(position);
  _position2Widget->set(position2);
  _colorWidget->set(color);
  _color2Widget->set(color2);
  
  _callbacks = true;
}

void ImageOpGradientWidget::sizeCallback(const Vec2i & in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(
    imageOpGradientAttrStr()[ImageOpGradient::ATTR_SIZE], deflateStr(in)
  );
  _sig.emit(true);
  widgetUpdate();
}

void ImageOpGradientWidget::pixelCallback(const Pixel & in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(
    imageOpGradientAttrStr()[ImageOpGradient::ATTR_PIXEL], deflateStr(in)
  );
  _sig.emit(true);
  widgetUpdate();
}

void ImageOpGradientWidget::positionCallback(const Vec2i & in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(
    imageOpGradientAttrStr()[ImageOpGradient::ATTR_POSITION], deflateStr(in)
  );
  _sig.emit(true);
  widgetUpdate();
}

void ImageOpGradientWidget::position2Callback(const Vec2i & in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(
    imageOpGradientAttrStr()[ImageOpGradient::ATTR_POSITION_2], deflateStr(in)
  );
  _sig.emit(true);
  widgetUpdate();
}
  
void ImageOpGradientWidget::colorCallback(const Color & in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(
    imageOpGradientAttrStr()[ImageOpGradient::ATTR_COLOR], deflateStr(in)
  );
  _sig.emit(true);
  widgetUpdate();
}

void ImageOpGradientWidget::color2Callback(const Color & in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(
    imageOpGradientAttrStr()[ImageOpGradient::ATTR_COLOR_2], deflateStr(in)
  );
  _sig.emit(true);
  widgetUpdate();
}
  
}
