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

//#define DJ_DBG
#include "djImageOpCoreWidget.h"
#include <djLabelWidget.h>
#include <djPixelWidget.h>

namespace dj {

ImageOpConvertWidget::ImageOpConvertWidget() :
  _callbacks(true)
{
  LabelWidget * label = new LabelWidget("Pixel:");
  _widget = new PixelWidget;
  
  VLayout * layout = new VLayout(this);
  HLayout * subLayout = new HLayout(layout);
  subLayout->margin(0);
  subLayout->add(label);
  subLayout->add(_widget);
  layout->stretchAdd();
  
  widgetUpdate();
  
  _widget->sig().set(this, widgetCallback);
}

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

void ImageOpConvertWidget::widgetUpdate()
{
  _callbacks = false;
  
  Pixel pixel;
  if (_op) {
    StrList data = _op->attr(
      imageOpConvertAttrStr()[ImageOpConvert::ATTR_PIXEL]
    );
    inflateStr(data, &pixel);
  }
  _widget->set(pixel);
  
  _callbacks = true;
}

void ImageOpConvertWidget::widgetCallback(const Pixel & in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(
    imageOpConvertAttrStr()[ImageOpConvert::ATTR_PIXEL], deflateStr(in)
  );
  sig().emit(true);
  widgetUpdate();
}

}
