/*
 * ImageCanvas.cpp
 *
 * (c) 2002 by Thorsten Scheuermann <tscheuermann@gmx.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include "ImageCanvas.h"
#include "Image.h"
#include "ViewerGlobals.h"
#include <qdirectpainter_qws.h>

ImageCanvas::ImageCanvas(QWidget *parent, const char *name)
: QWidget(parent, name), theImage(NULL)
{
  setBackgroundColor(Qt::black);
}

ImageCanvas::~ImageCanvas()
{
}

void ImageCanvas::setImage(Image *img)
{
  theImage = img;
  repaint();
}

void ImageCanvas::paintEvent(QPaintEvent *pe)
{
  QDirectPainter painter(this);
  int xOff = 0;
  int yOff = 0;

/*
  printf("%d %d %d %d\n", pe->rect().left(), pe->rect().top(),
    pe->rect().right(), pe->rect().bottom());
*/

  if(!isVisible())
    return;

  if(theImage)
  {
    // compute upper left corner of image
    if((painter.transformOrientation() == (int)ROTATION_90) ||
       (painter.transformOrientation() == (int)ROTATION_270))
    {
      if(width() > theImage->height())
        xOff = (width() - theImage->height())/2;
      if(height() > theImage->width())
        yOff = (height() - theImage->width())/2;
    }
    else
    {
      if(width() > theImage->width())
        xOff = (width() - theImage->width())/2;
      if(height() > theImage->height())
        yOff = (height() - theImage->height())/2;
    }

    theImage->paint(&painter, xOff, yOff, 0, 0);
  }
}

void ImageCanvas::keyPressEvent(QKeyEvent *ke)
{
  ke->ignore();
}

