/******************************************************************************
 * $Id: gvcanvas.cpp,v 1.1 1999/01/11 15:27:23 warmerda Exp $
 *
 * Name:     gvcanvas.cpp
 * Project:  GDAL Qt Viewer
 * Purpose:  Implements GVCanvas class.
 * Author:   Frank Warmerdam, warmerda@home.com
 *
 ******************************************************************************
 * Copyright (c) 1999, Frank Warmerdam
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *****************************************************************************
 *
 * $Log: gvcanvas.cpp,v $
 * Revision 1.1  1999/01/11 15:27:23  warmerda
 * New
 *
 */

#include <qpainter.h>

#include "gview.h"

/************************************************************************/
/*                              GVCanvas()                              */
/************************************************************************/

GVCanvas::GVCanvas( QWidget *poParent, const char * pszName,
                    GVRaster *poRasterIn )
        : QWidget( poParent, pszName )

{
    CPLAssert( poRasterIn != NULL );
    
    poRaster = poRasterIn;
}

/************************************************************************/
/*                             ~GVCanvas()                              */
/************************************************************************/

GVCanvas::~GVCanvas()

{
}


/************************************************************************/
/*                             paintEvent()                             */
/************************************************************************/

void GVCanvas::paintEvent( QPaintEvent * )

{
    QPainter	oPainter;
    QImage	*poImage;

    if( poRaster == NULL )
        return;

    poImage = poRaster->GetQImage();
    if( poImage == NULL )
        return;

    oPainter.begin( this );

    oPainter.drawImage( 0, 0, *poImage );

    oPainter.end();
}

/************************************************************************/
/*                          mousePressEvent()                           */
/************************************************************************/

void GVCanvas::mousePressEvent( QMouseEvent * poEvent )

{
    double	adfGeoTransform[6];

    if( poRaster == NULL )
        return;
    
    GDALGetGeoTransform( poRaster->GetDS(), adfGeoTransform );
    
    setGeoPos(
        GVGeoPoint( poEvent->x() * adfGeoTransform[1] + adfGeoTransform[0],
                    poEvent->y() * adfGeoTransform[5] + adfGeoTransform[3] ));
}

/************************************************************************/
/*                             SetGeoPos()                              */
/************************************************************************/

void GVCanvas::setGeoPos( GVGeoPoint oNewPos )

{
    if( oNewPos.dfGeoX == oGeoPosition.dfGeoX
        && oNewPos.dfGeoY == oGeoPosition.dfGeoY )
        return;

    oGeoPosition = oNewPos;

    emit geoPosChanged( oGeoPosition );
}

