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

//#define DJ_DBG
#include "widget.h"
#include "io.h"
#include <djLabelWidget.h>
#include <djNumWidget.h>
#include <djSliderWidget.h>

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

namespace djImageIoJpeg {

SaveWidget::SaveWidget() :
  _callbacks(true)
{
  LabelWidget * qualityLabel = new LabelWidget("Quality:");
  _qualityWidget = new IntWidget;
  _qualitySlider = new IntSliderWidget;
  _qualitySlider->range(0, 100);
  
  VLayout * layout = new VLayout(this);
  HLayout * subLayout = new HLayout(layout);
  subLayout->margin(0);
  subLayout->add(qualityLabel);
  subLayout->add(_qualityWidget);
  subLayout->add(_qualitySlider);
  subLayout->stretch(_qualitySlider);
  layout->stretchAdd();
  
  widgetUpdate();
  
  _qualityWidget->sig().set(this, qualityCallback);
  _qualitySlider->sig().set(this, qualityCallback);
}

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

void SaveWidget::widgetUpdate()
{
  _callbacks = false;
  
  int quality = 0;
  if (_save) {
    StrList data = _save->attr(saveAttrStr()[Save::ATTR_QUALITY]);
    inflateStr(data, &quality);
  }
  _qualityWidget->set(quality);
  _qualitySlider->set(quality);
  
  _callbacks = true;
}

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

}
