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

//#define DJ_DBG
#include "djImageIoDpxWidget.h"
#include <djLabelWidget.h>
#include <djGroupBoxWidget.h>
#include <djNumWidget.h>
#include <djSliderWidget.h>

namespace dj {
namespace ImageIoDpx {

LoadWidget::LoadWidget() :
  _callbacks(true)
{
  GroupBoxWidget * colorGroup = new GroupBoxWidget("Color Transfer");
  _colorWidget = new EnumWidget<COLOR>(colorStr());
  _colorWidget->alignPolicy(LEFT, CENTER);
  GroupBoxWidget * printGroup = new GroupBoxWidget("Print Options");
  LabelWidget * printBlackLabel = new LabelWidget("Black point:");
  _printBlackWidget = new IntWidget;
  _printBlackWidget->range(0, 1023);
  _printBlackSlider = new FloatSliderWidget;
  LabelWidget * printWhiteLabel = new LabelWidget("White point:");
  _printWhiteWidget = new IntWidget;
  _printWhiteWidget->range(0, 1023);
  _printWhiteSlider = new FloatSliderWidget;
  LabelWidget * printGammaLabel = new LabelWidget("Gamma:");
  _printGammaWidget = new FloatWidget;
  _printGammaSlider = new FloatSliderWidget;
  _printGammaSlider->range(0.0, 2.2);
  
  // XXX - Not until softclip is fixed.
  //
  /*LabelWidget * printSoftClipLabel = new LabelWidget("Soft clip:");
  _printSoftClipWidget = new IntWidget;
  _printSoftClipWidget->range(0, 50);
  _printSoftClipSlider = new FloatSliderWidget;*/
  
  VLayout * layout = new VLayout(this);
  layout->add(colorGroup);
  colorGroup->layout()->add(_colorWidget);
  layout->add(printGroup);
  printGroup->layout()->add(printBlackLabel);
  HLayout * subLayout = new HLayout(printGroup->layout());
  subLayout->margin(0);
  subLayout->add(_printBlackWidget);
  subLayout->add(_printBlackSlider);
  printGroup->layout()->add(printWhiteLabel);
  subLayout = new HLayout(printGroup->layout());
  subLayout->margin(0);
  subLayout->add(_printWhiteWidget);
  subLayout->add(_printWhiteSlider);
  printGroup->layout()->add(printGammaLabel);
  subLayout = new HLayout(printGroup->layout());
  subLayout->margin(0);
  subLayout->add(_printGammaWidget);
  subLayout->add(_printGammaSlider);
  /*printGroup->layout()->add(printSoftClipLabel);
  subLayout = new HLayout(printGroup->layout());
  subLayout->margin(0);
  subLayout->add(_printSoftClipWidget);
  subLayout->add(_printSoftClipSlider);*/
  layout->stretchAdd();
  
  widgetUpdate();
  
  _colorWidget->sig().set(this, colorCallback);
  _printBlackWidget->sig().set(this, printBlackCallback);
  _printBlackSlider->sig().set(this, printBlackCallback);
  _printWhiteWidget->sig().set(this, printWhiteCallback);
  _printWhiteSlider->sig().set(this, printWhiteCallback);
  _printGammaWidget->sig().set(this, printGammaCallback);
  _printGammaSlider->sig().set(this, printGammaCallback);
  //_printSoftClipWidget->sig().set(this, printSoftClipCallback);
  //_printSoftClipSlider->sig().set(this, printSoftClipCallback);
}

void LoadWidget::set(ImageLoad * in)
{
  ImageLoadWidget::set(in);
  widgetUpdate();
}

void LoadWidget::widgetUpdate()
{
  _callbacks = false;
  
  COLOR color = COLOR(0);
  double printBlack = 0.0, printWhite = 0.0, printGamma = 0.0,
    printSoftClip = 0.0;
  if (_load) {
    StrList data = _load->attr(loadAttrStr()[Load::ATTR_COLOR]);
    inflateStr(data, &color, colorStr());
    data = _load->attr(loadAttrStr()[Load::ATTR_PRINT_BLACK]);
    inflateStr(data, &printBlack);
    data = _load->attr(loadAttrStr()[Load::ATTR_PRINT_WHITE]);
    inflateStr(data, &printWhite);
    data = _load->attr(loadAttrStr()[Load::ATTR_PRINT_GAMMA]);
    inflateStr(data, &printGamma);
    data = _load->attr(loadAttrStr()[Load::ATTR_PRINT_SOFT_CLIP]);
    inflateStr(data, &printSoftClip);
  }
  _colorWidget->set(color);
  _printBlackWidget->set(int(printBlack * 1023.0 + 0.5));
  _printBlackSlider->set(printBlack);
  _printWhiteWidget->set(int(printWhite * 1023.0 + 0.5));
  _printWhiteSlider->set(printWhite);
  _printGammaWidget->set(printGamma);
  _printGammaSlider->set(printGamma);
  //_printSoftClipWidget->set(int(printSoftClip * 50 + 0.5));
  //_printSoftClipSlider->set(printSoftClip);
  
  _callbacks = true;
}

void LoadWidget::colorCallback(COLOR in)
{
  if (! _callbacks || ! _load) return;
  _load->attr(loadAttrStr()[Load::ATTR_COLOR], deflateStr(in, colorStr()));
  sig().emit(true);
  widgetUpdate();
}

void LoadWidget::printBlackCallback(double in)
{
  if (! _callbacks || ! _load) return;
  _load->attr(loadAttrStr()[Load::ATTR_PRINT_BLACK], deflateStr(in));
  sig().emit(true);
  widgetUpdate();
}

void LoadWidget::printBlackCallback(int in)
  { printBlackCallback(in / 1023.0); }

void LoadWidget::printWhiteCallback(double in)
{
  if (! _callbacks || ! _load) return;
  _load->attr(loadAttrStr()[Load::ATTR_PRINT_WHITE], deflateStr(in));
  sig().emit(true);
  widgetUpdate();
}

void LoadWidget::printWhiteCallback(int in)
  { printWhiteCallback(in / 1023.0); }

void LoadWidget::printGammaCallback(double in)
{
  if (! _callbacks || ! _load) return;
  _load->attr(loadAttrStr()[Load::ATTR_PRINT_GAMMA], deflateStr(in));
  sig().emit(true);
  widgetUpdate();
}

void LoadWidget::printSoftClipCallback(double in)
{
  if (! _callbacks || ! _load) return;
  _load->attr(loadAttrStr()[Load::ATTR_PRINT_SOFT_CLIP], deflateStr(in));
  sig().emit(true);
  widgetUpdate();
}

void LoadWidget::printSoftClipCallback(int in)
  { printSoftClipCallback(in / 50.0); }

}
}
