/*------------------------------------------------------------------------------
 src/lib/djWidget/pixelWidget.cxx

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

//#define DJ_DBG
#include "djPixelWidget.h"

namespace dj {

PixelWidget::PixelWidget() :
  _callbacks(true)
{
  _formatWidget = new EnumWidget<PIXEL_FORMAT>(pixelFormatStr());
  _formatWidget->tooltip("Pixel format.");
  _typeWidget = new EnumWidget<PIXEL_TYPE>(pixelTypeStr());
  _typeWidget->tooltip("Pixel type.");
  
  sizePolicy(FIXED, FIXED);
  HLayout * layout = new HLayout(this);
  layout->margin(0);
  layout->add(_formatWidget);
  layout->add(_typeWidget);
  
  widgetUpdate();
  
  _formatWidget->sig().set(this, formatCallback);
  _typeWidget->sig().set(this, typeCallback);
}

void PixelWidget::set(const Pixel & in)
{
  if (in == _value) return;
  _value = in;
  _sig.emit(_value);
  widgetUpdate();
}

void PixelWidget::formatCallback(PIXEL_FORMAT in)
{
  if (! _callbacks) return;
  set(Pixel(in, _value.type()));
}

void PixelWidget::typeCallback(PIXEL_TYPE in)
{
  if (! _callbacks) return;
  set(Pixel(_value.format(), in));
}

void PixelWidget::widgetUpdate()
{
  _callbacks = false;
  _typeWidget->set(_value.type());
  _formatWidget->set(_value.format());
  _callbacks = true;
}

}
