/*------------------------------------------------------------------------------
 src/lib/djWidget/imageOpNoiseWidget.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 <djNumWidget.h>
#include <djVecWidget.h>
#include <djPixelWidget.h>
#include <djColorWidget.h>

namespace dj {

ImageOpNoiseWidget::ImageOpNoiseWidget() :
  _callbacks(true)
{
  GroupBoxWidget * imageGroup = new GroupBoxWidget("Image");
  LabelWidget * sizeLabel = new LabelWidget("Size:");
  _sizeWidget = new Vec2iWidget;
  LabelWidget * pixelLabel = new LabelWidget("Pixel:");
  _pixelWidget = new PixelWidget;
  GroupBoxWidget * colorGroup = new GroupBoxWidget("Color");
  _colorWidget = new ColorWidget;
  _color2Widget = new ColorWidget;
  GroupBoxWidget * optionsGroup = new GroupBoxWidget("Options");
  LabelWidget * randSeedLabel = new LabelWidget("Random seed:");
  _randSeedWidget = new IntWidget;
  _randSeedWidget->alignPolicy(LEFT, CENTER);
  
  List<LabelWidget *> labelList;
  labelList += sizeLabel;
  labelList += pixelLabel;
  sizeLabel->labelGroup(labelList);
  pixelLabel->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(colorGroup);
  colorGroup->layout()->add(_colorWidget);
  colorGroup->layout()->add(_color2Widget);
  layout->add(optionsGroup);
  subLayout = new HLayout(optionsGroup->layout());
  subLayout->margin(0);
  subLayout->add(randSeedLabel);
  subLayout->add(_randSeedWidget);
  layout->stretchAdd();
  
  widgetUpdate();
  
  _sizeWidget->sig().set(this, sizeCallback);
  _pixelWidget->sig().set(this, pixelCallback);
  _colorWidget->sig().set(this, colorCallback);
  _color2Widget->sig().set(this, color2Callback);
  _randSeedWidget->sig().set(this, randSeedCallback);
}

void ImageOpNoiseWidget::set(ImageOp * in)
{
  ImageOpWidget::set(in);
  widgetUpdate();
}
  
void ImageOpNoiseWidget::widgetUpdate()
{
  _callbacks = false;
  
  Vec2i size;
  Pixel pixel;
  Color color, color2;
  int randSeed = 0;
  if (_op) {
    StrList data = _op->attr(imageOpNoiseAttrStr()[ImageOpNoise::ATTR_SIZE]);
    inflateStr(data, &size);
    data = _op->attr(imageOpNoiseAttrStr()[ImageOpNoise::ATTR_PIXEL]);
    inflateStr(data, &pixel);
    data = _op->attr(imageOpNoiseAttrStr()[ImageOpNoise::ATTR_COLOR]);
    inflateStr(data, &color);
    data = _op->attr(imageOpNoiseAttrStr()[ImageOpNoise::ATTR_COLOR_2]);
    inflateStr(data, &color2);
    data = _op->attr(imageOpNoiseAttrStr()[ImageOpNoise::ATTR_RANDOM_SEED]);
    inflateStr(data, &randSeed);
  }
  _sizeWidget->set(size);
  _pixelWidget->set(pixel);
  _colorWidget->set(color);
  _color2Widget->set(color2);
  _randSeedWidget->set(randSeed);
  
  _callbacks = true;
}

void ImageOpNoiseWidget::sizeCallback(const Vec2i & in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(imageOpNoiseAttrStr()[ImageOpNoise::ATTR_SIZE], deflateStr(in));
  _sig.emit(true);
  widgetUpdate();
}
  
void ImageOpNoiseWidget::pixelCallback(const Pixel & in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(imageOpNoiseAttrStr()[ImageOpNoise::ATTR_PIXEL], deflateStr(in));
  _sig.emit(true);
  widgetUpdate();
}
  
void ImageOpNoiseWidget::colorCallback(const Color & in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(imageOpNoiseAttrStr()[ImageOpNoise::ATTR_COLOR], deflateStr(in));
  _sig.emit(true);
  widgetUpdate();
}

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

void ImageOpNoiseWidget::randSeedCallback(int in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(
    imageOpNoiseAttrStr()[ImageOpNoise::ATTR_RANDOM_SEED], deflateStr(in)
  );
  _sig.emit(true);
  widgetUpdate();
}

}
