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

#include "widget.h"
#include <djLabelWidget.h>

extern "C" {
const dj::List<dj::ImageSaveWidget *> djImageSaveWidget()
{
  dj::List<dj::ImageSaveWidget *> out;
  out += new djImageIoExr::SaveWidget;
  return out;
}
}

namespace djImageIoExr {

SaveWidget::SaveWidget() :
  _callbacks(true)
{
  LabelWidget * compressLabel = new LabelWidget("Compress:");
  _compressWidget = new EnumWidget<Save::COMPRESS>(compressStr());
  LabelWidget * bitDepthLabel = new LabelWidget("Bit depth:");
  _bitDepthWidget = new EnumWidget<Save::BIT_DEPTH>(bitDepthStr());
  
  List<LabelWidget *> labelList;
  labelList += compressLabel;
  labelList += bitDepthLabel;
  compressLabel->labelGroup(labelList);
  bitDepthLabel->labelGroup(labelList);
  
  VLayout * layout = new VLayout(this);
  HLayout * subLayout = new HLayout(layout);
  subLayout->margin(0);
  subLayout->add(compressLabel);
  subLayout->add(_compressWidget);
  subLayout = new HLayout(layout);
  subLayout->margin(0);
  subLayout->add(bitDepthLabel);
  subLayout->add(_bitDepthWidget);
  layout->stretchAdd();
  
  widgetUpdate();
  
  _compressWidget->sig().set(this, compressCallback);
  _bitDepthWidget->sig().set(this, bitDepthCallback);
}

void SaveWidget::set(ImageSave * in)
{
  ImageSaveWidget::set(in);
  widgetUpdate();
}

void SaveWidget::widgetUpdate()
{
  _callbacks = false;
  
  Save::COMPRESS compress = Save::COMPRESS(0);
  Save::BIT_DEPTH bitDepth = Save::BIT_DEPTH(0);
  if (_save) {
    StrList data = _save->attr(saveAttrStr()[Save::ATTR_COMPRESS]);
    inflateStr(data, &compress, compressStr());
    data = _save->attr(saveAttrStr()[Save::ATTR_BIT_DEPTH]);
    inflateStr(data, &bitDepth, bitDepthStr());
  }
  _compressWidget->set(compress);
  _bitDepthWidget->set(bitDepth);
  
  _callbacks = true;
}

void SaveWidget::compressCallback(Save::COMPRESS in)
{
  if (! _callbacks || ! _save) return;
  _save->attr(
    saveAttrStr()[Save::ATTR_COMPRESS], deflateStr(in, compressStr())
  );
  sig().emit(true);
  widgetUpdate();
}

void SaveWidget::bitDepthCallback(Save::BIT_DEPTH in)
{
  if (! _callbacks || ! _save) return;
  _save->attr(
    saveAttrStr()[Save::ATTR_BIT_DEPTH], deflateStr(in, bitDepthStr())
  );
  sig().emit(true);
  widgetUpdate();
}

}
