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

//#define DJ_DBG
#include "djImageIoTgaWidget.h"
#include <djLabelWidget.h>

namespace dj {
namespace ImageIoTga {

SaveWidget::SaveWidget() :
  _callbacks(true)
{
  LabelWidget * compressLabel = new LabelWidget("Compress:");
  _compressWidget = new EnumWidget<Save::COMPRESS>(compressStr());
  
  VLayout * layout = new VLayout(this);
  HLayout * subLayout = new HLayout(layout);
  subLayout->margin(0);
  subLayout->add(compressLabel);
  subLayout->add(_compressWidget);
  layout->stretchAdd();
  
  widgetUpdate();
  
  _compressWidget->sig().set(this, compressCallback);
}

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

void SaveWidget::widgetUpdate()
{
  _callbacks = false;
  
  Save::COMPRESS compress = Save::COMPRESS(0);
  if (_save) {
    StrList data = _save->attr(saveAttrStr()[Save::ATTR_COMPRESS]);
    inflateStr(data, &compress, compressStr());
  }
  _compressWidget->set(compress);
  
  _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();
}

}
}
