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

//#define DJ_DBG
#include "djImageOpGenWidget.h"
#include <djLabelWidget.h>
#include <djGroupBoxWidget.h>
#include <djNumWidget.h>
#include <djFileNameWidget.h>

namespace dj {

ImageOpInputWidget::ImageOpInputWidget() :
  _callbacks(true)
{
  GroupBoxWidget * fileNameGroup = new GroupBoxWidget("File Name");
  _fileNameWidget = new FileNameWidget;
  GroupBoxWidget * optionsGroup = new GroupBoxWidget("Options");
  LabelWidget * proxyLabel = new LabelWidget("Proxy:");
  _proxyWidget = new IntWidget;
  LabelWidget * pixelLabel = new LabelWidget("Pixel:");
  _formatWidget =
    new EnumWidget<int>(StrList() + Str("Default") + pixelFormatStr());
  _typeWidget =
    new EnumWidget<int>(StrList() + Str("Default") + pixelTypeStr());
  
  List<LabelWidget *> labelList;
  labelList += proxyLabel;
  labelList += pixelLabel;
  proxyLabel->labelGroup(labelList);
  pixelLabel->labelGroup(labelList);
  
  VLayout * layout = new VLayout(this);
  layout->add(fileNameGroup);
  fileNameGroup->layout()->add(_fileNameWidget);
  layout->add(optionsGroup);
  HLayout * subLayout = new HLayout(optionsGroup->layout());
  subLayout->margin(0);
  subLayout->add(proxyLabel);
  subLayout->add(_proxyWidget);
  subLayout = new HLayout(optionsGroup->layout());
  subLayout->margin(0);
  subLayout->add(pixelLabel);
  subLayout->add(_formatWidget);
  subLayout->add(_typeWidget);
  layout->stretchAdd();

  widgetUpdate();
  
  _fileNameWidget->sig().set(this, fileNameCallback);
  _proxyWidget->sig().set(this, proxyCallback);
  _formatWidget->sig().set(this, formatCallback);
  _typeWidget->sig().set(this, typeCallback);
}

void ImageOpInputWidget::set(ImageOp * in)
{
  ImageOpWidget::set(in);
  widgetUpdate();
}

void ImageOpInputWidget::widgetUpdate()
{
  _callbacks = false;
  
  Str fileName;
  int proxy = 0, format = -1, type = -1;
  if (_op) {
    StrList data = _op->attr(
      imageOpInputAttrStr()[ImageOpInput::ATTR_FILE_NAME]
    );
    inflateStr(data, &fileName);
    data = _op->attr(imageOpInputAttrStr()[ImageOpInput::ATTR_PROXY]);
    inflateStr(data, &proxy);
    data = _op->attr(imageOpInputAttrStr()[ImageOpInput::ATTR_FORMAT]);
    inflateStr(data, &format);
    data = _op->attr(imageOpInputAttrStr()[ImageOpInput::ATTR_TYPE]);
    inflateStr(data, &type);
  }
  _fileNameWidget->set(fileName);
  _proxyWidget->set(proxy);
  _formatWidget->set(format + 1);
  _typeWidget->set(type + 1);
  
  _callbacks = true;
}

void ImageOpInputWidget::fileNameCallback(const Str & in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(
    imageOpInputAttrStr()[ImageOpInput::ATTR_FILE_NAME], deflateStr(in)
  );
  _sig.emit(true);
  widgetUpdate();
}

void ImageOpInputWidget::proxyCallback(int in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(imageOpInputAttrStr()[ImageOpInput::ATTR_PROXY], deflateStr(in));
  _sig.emit(true);
  widgetUpdate();
}

void ImageOpInputWidget::formatCallback(int in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(
    imageOpInputAttrStr()[ImageOpInput::ATTR_FORMAT], deflateStr(in - 1)
  );
  _sig.emit(true);
  widgetUpdate();
}

void ImageOpInputWidget::typeCallback(int in)
{
  if (! _callbacks || ! _op) return;
  _op->attr(imageOpInputAttrStr()[ImageOpInput::ATTR_TYPE], deflateStr(in - 1));
  _sig.emit(true);
  widgetUpdate();
}

}
