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

#include "gview.h"
#include <qpainter.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
#include <qkeycode.h>

/************************************************************************/
/*                            GVViewWindow()                            */
/************************************************************************/

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

{
    QPopupMenu	*poFile, *poView;
    QMenuBar    *poMenuBar;

    poCanvas = NULL;
    poRaster = NULL;

    poFile = new QPopupMenu;
    poFile->insertItem( "&Open", this, SLOT(Open()) );
    poFile->insertSeparator();
    poFile->insertItem( "E&xit", qApp, SLOT(quit()), CTRL+Key_Q );

    poView = new QPopupMenu;
    poView->insertItem( "Coord as Lat/Long", this, SLOT(SetDisplayLongLat()) );
    poView->insertItem( "Coord as Projected",this,SLOT(SetDisplayProjected()));
    poView->insertItem( "OGDI Hit Viewer",this,SLOT(LaunchOGDIHitViewer()));

    poMenuBar = new QMenuBar( this );
    poMenuBar->insertItem( "&File", poFile );
    poMenuBar->insertItem( "&View", poView );

    
    poGeoDisplay = new GVGeoDisplay( this, "GeoDisplay" );
    poGeoDisplay->show();
}

/************************************************************************/
/*                           ~GVViewWindow()                            */
/************************************************************************/

GVViewWindow::~GVViewWindow()

{
    if( poRaster != NULL )
        delete poRaster;
}

/************************************************************************/
/*                            SetGVRaster()                             */
/************************************************************************/

void GVViewWindow::SetGVRaster( GVRaster *poRasterIn )

{
    if( poRaster != NULL )
        delete poRaster;

    poRaster = poRasterIn;

    if( poCanvas != NULL )
        delete poCanvas;

    poCanvas = new GVCanvas( this, "Canvas", poRaster );

    resizeEvent(NULL);

    connect( poCanvas, SIGNAL(geoPosChanged(GVGeoPoint)),
             poGeoDisplay, SLOT(setGeoPos(GVGeoPoint)) );
        
    poCanvas->show();

    poGeoDisplay->SetSourceProjection(
        GDALGetProjectionRef( poRasterIn->GetDS() ) );
    poGeoDisplay->SetDisplayProjection(
        GDALGetProjectionRef( poRasterIn->GetDS() ) );
}

/************************************************************************/
/*                            resizeEvent()                             */
/************************************************************************/

void GVViewWindow::resizeEvent( QResizeEvent * )

{
    if( poCanvas != NULL )
    {
        poCanvas->resize( width(), height() - poGeoDisplay->height() - 1 );
    }

    poGeoDisplay->move( 0, height() - poGeoDisplay->height() );
}

/************************************************************************/
/*                                Open()                                */
/************************************************************************/

void GVViewWindow::Open()

{
}

/************************************************************************/
/*                         SetDisplayLongLat()                          */
/************************************************************************/

void GVViewWindow::SetDisplayLongLat()

{
    poGeoDisplay->SetDisplayProjection("+proj=longlat");
}

/************************************************************************/
/*                        SetDisplayProjected()                         */
/************************************************************************/

void GVViewWindow::SetDisplayProjected()

{
    poGeoDisplay->SetDisplayProjection(
        GDALGetProjectionRef( poRaster->GetDS() ) );
}

/************************************************************************/
/*                         LauchOGDIHitViewer()                         */
/************************************************************************/

void GVViewWindow::LaunchOGDIHitViewer()

{
    ::LaunchOGDIHitViewer( poCanvas );
}
