/*------------------------------------------------------------------------------
 src/lib/djWidget/imageOpSolidWidget.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 {

ImageOpSolidWidget::ImageOpSolidWidget() :
  _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;
  
  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);
  layout->stretchAdd();

  widgetUpdate();
  
  _sizeWidget->sig().set(this, sizeCallback);
  _pixelWidget->sig().set(this, pixelCallback);
  _colorWidget->sig().set(this, colorCallback);
}

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

void ImageOpSolidWidget::widgetUpdate()
{
  _callbacks = false;
  
  Vec2i size;
  Pixel pixel;
  Color color;
  if (_op) {
    StrList data = _op->attr(imageOpSolidAttrStr()[ImageOpSolid::ATTR_SIZE]);
    inflateStr(data, &size);
    data = _op->attr(imageOpSolidAttrStr()[ImageOpSolid::ATTR_PIXEL]);
    inflateStr(data, &pixel);
    data = _op->attr(imageOpSolidAttrStr()[ImageOpSolid::ATTR_COLOR]);
    inflateStr(data, &color);
  }
  _sizeWidget->set(size);
  _pixelWidget->set(pixel);
  _colorWidget->set(color);
  
  _callbacks = true;
}

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

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

void ImageOpSolidWidget::colorCallback(const Color & in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(imageOpSolidAttrStr()[ImageOpSolid::ATTR_COLOR], deflateStr(in));
  _sig.emit(true);
  widgetUpdate();
}

}
