/******************************************************************************
 * $Id: gvgeodisplay.cpp,v 1.1 1999/01/11 15:27:23 warmerda Exp $
 *
 * Name:     gvgeodisplay.cpp
 * Project:  GDAL Qt Viewer
 * Purpose:  Implements GVGeoDisplay 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: gvgeodisplay.cpp,v $
 * Revision 1.1  1999/01/11 15:27:23  warmerda
 * New
 *
 */

#include <qpainter.h>

#include "gvgeodisplay.h"
#include "cpl_conv.h"
#include "gdal.h"

/************************************************************************/
/*                            GVGeoDisplay()                            */
/************************************************************************/

GVGeoDisplay::GVGeoDisplay( QWidget *poParent, const char * pszName )
        : QWidget( poParent, pszName )

{
    resize( 200, 30 );
    pszSourceProjection = NULL;
    pszDisplayProjection = NULL;
}

/************************************************************************/
/*                           ~GVGeoDisplay()                            */
/************************************************************************/

GVGeoDisplay::~GVGeoDisplay()

{
    CPLFree( pszSourceProjection );
    CPLFree( pszDisplayProjection );
}


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

void GVGeoDisplay::paintEvent( QPaintEvent * )

{
    QString	oString;
    double	dfX, dfY;

    dfX = oGeoPosition.dfGeoX;
    dfY = oGeoPosition.dfGeoY;

    if( pszSourceProjection != NULL && pszDisplayProjection != NULL
        && !EQUAL(pszSourceProjection,pszDisplayProjection) )
    {
        GDALProjDefH	hSrcProj, hDispProj;

        hSrcProj = GDALCreateProjDef( pszSourceProjection );
        hDispProj = GDALCreateProjDef( pszDisplayProjection );

        if( GDALReprojectToLongLat( hSrcProj, &dfX, &dfY ) == CE_None )
            GDALReprojectFromLongLat( hDispProj, &dfX, &dfY );

        GDALDestroyProjDef( hSrcProj );
        GDALDestroyProjDef( hDispProj );
    }

    if( pszDisplayProjection != NULL
        && strstr(pszDisplayProjection,"longlat") != NULL )
        oString.sprintf( "(%.7f,%.7f)", dfX, dfY );
    else
        oString.sprintf( "(%.2f,%.2f)", dfX, dfY );

    drawText( height() - 1, 10, oString );
}

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

void GVGeoDisplay::setGeoPos( GVGeoPoint oNewPos )

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

    oGeoPosition = oNewPos;

    emit geoPosChanged( oGeoPosition );

    update();
}

/************************************************************************/
/*                        SetSourceProjection()                         */
/************************************************************************/

void GVGeoDisplay::SetSourceProjection( const char * pszProj )

{
    CPLFree( pszSourceProjection );
    pszSourceProjection = CPLStrdup( pszProj );
}

/************************************************************************/
/*                        SetDisplayProjection()                        */
/************************************************************************/

void GVGeoDisplay::SetDisplayProjection( const char * pszProj )

{
    CPLFree( pszDisplayProjection );
    pszDisplayProjection = CPLStrdup( pszProj );
}

