/*------------------------------------------------------------------------------
 src/lib/djWidget/imageOpChannelWidget.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 <djNumWidget.h>

namespace dj {

ImageOpChannelWidget::ImageOpChannelWidget() :
  _callbacks(true)
{
  LabelWidget * label = new LabelWidget("Channel:");
  _widget = new IntWidget;
  
  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 ImageOpChannelWidget::set(ImageOp * in)
{
  ImageOpWidget::set(in);
  widgetUpdate();
}

void ImageOpChannelWidget::widgetUpdate()
{
  _callbacks = false;
  
  int channel = 0;
  if (_op) {
    StrList data = _op->attr(
      imageOpChannelAttrStr()[ImageOpChannel::ATTR_CHANNEL]
    );
    inflateStr(data, &channel);
  }
  _widget->set(channel);
  
  _callbacks = true;
}

void ImageOpChannelWidget::widgetCallback(int in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(
    imageOpChannelAttrStr()[ImageOpChannel::ATTR_CHANNEL], deflateStr(in)
  );
  sig().emit(true);
  widgetUpdate();
}

}
