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

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

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

namespace djImageIoTiff {

SaveWidget::SaveWidget() :
  _callbacks(true)
{
  LabelWidget * compressLabel = new LabelWidget("Compress:");
  _compressWidget = new EnumWidget<Save::COMPRESS>(compressStr());
  LabelWidget * channelsLabel = new LabelWidget("Channels:");
  _channelsWidget = new EnumWidget<Save::CHANNELS>(channelsStr());
  
  List<LabelWidget *> labelList;
  labelList += compressLabel;
  labelList += channelsLabel;
  compressLabel->labelGroup(labelList);
  channelsLabel->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(channelsLabel);
  subLayout->add(_channelsWidget);
  layout->stretchAdd();
  
  widgetUpdate();
  
  _compressWidget->sig().set(this, compressCallback);
  _channelsWidget->sig().set(this, channelsCallback);
}

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

void SaveWidget::widgetUpdate()
{
  _callbacks = false;
  
  Save::COMPRESS compress = Save::COMPRESS(0);
  Save::CHANNELS channels = Save::CHANNELS(0);
  if (_save) {
    StrList data = _save->attr(saveAttrStr()[Save::ATTR_COMPRESS]);
    inflateStr(data, &compress, compressStr());
    data = _save->attr(saveAttrStr()[Save::ATTR_CHANNELS]);
    inflateStr(data, &channels, channelsStr());
  }
  _compressWidget->set(compress);
  _channelsWidget->set(channels);
  
  _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::channelsCallback(Save::CHANNELS in)
{
  if (! _callbacks || ! _save) return;
  _save->attr(
    saveAttrStr()[Save::ATTR_CHANNELS], deflateStr(in, channelsStr())
  );
  sig().emit(true);
  widgetUpdate();
}

}
