/*------------------------------------------------------------------------------
 src/bin/djview/imageInfoTool.cxx

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

//#define DJ_DBG
#include "imageInfoTool.h"
#include <djImage.h>
#include <djLabelWidget.h>
#include <djButtonWidget.h>
#include <djTextWidget.h>
#include <FL/Fl_Multiline_Output.H>

namespace djview {

class _ImageInfoToolTagsWidget : public Fl_Multiline_Output, public WidgetBase {
public:

  _ImageInfoToolTagsWidget();
  
  void set(const Str &);
  const Str & get() const { return _value; }
  
  void resize(int, int, int, int);
  int visible() const;
  
private:

  void widgetUpdate();

  Str _value;
};

_ImageInfoToolTagsWidget::_ImageInfoToolTagsWidget() :
  Fl_Multiline_Output(0, 0, 0, 0)
{
  textfont(dj::font());
  textsize(dj::fontSize());
  
  widgetUpdate();
}

void _ImageInfoToolTagsWidget::set(const Str & in)
{
  if (in == _value) return;
  _value = in;
  widgetUpdate();
}

void _ImageInfoToolTagsWidget::resize(int x, int y, int w, int h)
{
  Fl_Widget::resize(x, y, w, h);
  LayoutItem::resize(x, y, w, h);
}

int _ImageInfoToolTagsWidget::visible() const
{
  return Fl_Multiline_Output::visible();
}

void _ImageInfoToolTagsWidget::widgetUpdate()
{
  value(_value.toAscii());
}

ImageInfoTool::ImageInfoTool() :
  DialogBase("Image Information Tool")
{
  // Widgets.
  //
  Widget * frameWidget = new Widget;
  frameWidget->box(FL_THIN_UP_BOX);
  LabelWidget * fileNameLabel = new LabelWidget("File name:");
  _fileNameWidget = new TextDisplayWidget;
  LabelWidget * sizeLabel = new LabelWidget("Size:");
  _sizeWidget = new TextDisplayWidget;
  LabelWidget * aspectLabel = new LabelWidget("Aspect:");
  _aspectWidget = new TextDisplayWidget;
  LabelWidget * pixelLabel = new LabelWidget("Pixel:");
  _pixelWidget = new TextDisplayWidget;
  LabelWidget * tagsLabel = new LabelWidget("Tags:");
  _tagsWidget = new _ImageInfoToolTagsWidget;
  Widget * dialogFrameWidget = new Widget;
  dialogFrameWidget->box(FL_THIN_UP_BOX);
  PushButtonWidget * closeWidget = new PushButtonWidget("Clos&e");
  
  List<LabelWidget *> labelList;
  labelList += fileNameLabel;
  labelList += sizeLabel;
  labelList += aspectLabel;
  labelList += pixelLabel;
  fileNameLabel->labelGroup(labelList);
  sizeLabel->labelGroup(labelList);
  aspectLabel->labelGroup(labelList);
  pixelLabel->labelGroup(labelList);
  
  // Layout.
  //
  VLayout * windowLayout = new VLayout(this);
  windowLayout->spacing(0);
  windowLayout->margin(0);
  windowLayout->add(frameWidget);
  windowLayout->stretch(frameWidget);
  VLayout * layout = new VLayout(frameWidget);
  HLayout * subLayout = new HLayout(layout);
  subLayout->margin(0);
  subLayout->add(fileNameLabel);
  subLayout->add(_fileNameWidget);
  subLayout = new HLayout(layout);
  subLayout->margin(0);
  subLayout->add(sizeLabel);
  subLayout->add(_sizeWidget);
  subLayout = new HLayout(layout);
  subLayout->margin(0);
  subLayout->add(aspectLabel);
  subLayout->add(_aspectWidget);
  subLayout = new HLayout(layout);
  subLayout->margin(0);
  subLayout->add(pixelLabel);
  subLayout->add(_pixelWidget);
  layout->add(tagsLabel);
  layout->add(_tagsWidget);
  layout->stretch(_tagsWidget);
  windowLayout->add(dialogFrameWidget);
  HLayout * dialogFrameLayout = new HLayout(dialogFrameWidget);
  dialogFrameLayout->stretchAdd();
  dialogFrameLayout->add(closeWidget);
  
  // Callbacks.
  //
  closeWidget->sig().set(this, closeCallback);
}

void ImageInfoTool::set(const Str & fileName, const Image * image)
{
  _fileName = fileName;
  if (image) {
    _size = image->size();
    _pixel = image->pixel();
    _tagList = image->tagList();
  } else {
    _size = Vec2i();
    _pixel = Pixel();
    _tagList = ImageTagList();
  }
  widgetUpdate();
}

void ImageInfoTool::closeCallback(bool)
{
  hide();
}

void ImageInfoTool::widgetUpdate()
{
  _fileNameWidget->set(_fileName);
  
  _sizeWidget->set(Str("%1 %2").arg(_size.x()).arg(_size.y()));
  _aspectWidget->set(Str("%1").arg(dj::aspect(_size), 0, 2));
  _pixelWidget->set(
    Str("%1 %2").
      arg(pixelFormatStr()[_pixel.format()]).
      arg(pixelTypeStr()[_pixel.type()])
  );
    
  Str tmp;
  const StrList keyList = _tagList.keyList();
  for (ConstStrListIt it = keyList; it.isValid(); ++it)
    tmp += Str("%1 = %2\n").arg(it()).arg(_tagList[it()]);
  _tagsWidget->set(tmp);
}

}
