// $Id: figureeditor.cpp,v 1.3 2008/02/19 21:02:42 hvengel Exp $
//  
//  Lttle cms Profiler
//  Copyright (C) 1998-2001 Marti Maria
// Copyright (C) 2005 - 2007 Hal Engel
//
// THIS SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
// WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
// INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
// LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
// OF THIS SOFTWARE.
//
// 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 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Version 1.11

#include "lprofmain-priv.h"
#include "lprofmain.h"
//Added by qt3to4:
#include <QResizeEvent>
#include <QMouseEvent>

// --- The figure editor ----------------------

void FigureEditor::resizeEvent( QResizeEvent* e )
{
     // qDebug("FigureEditor::resizeEvent");
    Q3CanvasView::resizeEvent( e );
    maindialog.FigEditor-> resize(width(), height());
    maindialog.zeroProgressBars();

}

void FigureEditor::contentsMousePressEvent(QMouseEvent* e)
{
    maindialog.zeroProgressBars();
    if (!SelectWPpatch)
    {// qDebug("FigureEditor::contentsMousePressEvent");
        if (e->x()<measure_view_H/2 && e->y()<measure_view_V/2)
        {
             // upper left quadrant
            Marks.ul_x=e->x();
            Marks.ul_y=e->y();
        }
        else if (e->x()>measure_view_H/2 && e->y()<measure_view_V/2)
        {
            // upper right quadrant
            Marks.ur_x=e->x();
            Marks.ur_y=e->y();
        }
        else if (e->x()>measure_view_H/2 && e->y()>measure_view_V/2)
        {
            // lower right quadrant
            Marks.lr_x=e->x();
            Marks.lr_y=e->y();
        }
        else if (e->x()<measure_view_H/2 && e->y()>measure_view_V/2)
        {
            // lower left quadrant
            Marks.ll_x=e->x();
            Marks.ll_y=e->y();
        }


        // place corner markers to give user feed back that the corner has been marked

        grid_corners.putPoints(0, 4, Marks.ul_x,  Marks.ul_y,
                               Marks.ur_x, Marks.ur_y,
                               Marks.lr_x, Marks.lr_y,
                               Marks.ll_x, Marks.ll_y);
        maindialog.AddTemplate();

        // check to see if all corners are marked and it is time to place full template and pick

        if (Marks.ul_x>0 && Marks.ur_x<measure_view_H &&
            Marks.ll_x>0 && Marks.lr_x<measure_view_H)
        {

            corners_ready=TRUE;

            maindialog.slotPick();

        }
    }
    else
    {
        // select the white point corner locations
        if (firstClick)
        {
            // store location and create 0 size rectangle
            WPHotZone.setCoords(e->x(), e->y(), e->x(), e->y());

            // Mark the location on screen FIXME

            firstClick = false;
        }
        else
        {
            int xsize = abs(e->x()- WPHotZone.x());
            int ysize = abs(e->y() - WPHotZone.y());
            if (e->y() < WPHotZone.y())
            {
                // need to swap
                WPHotZone.setBottom(WPHotZone.y());
                WPHotZone.setY(e->y());
            }
            if (e->x() < WPHotZone.x())
            {
                // need to swap
                WPHotZone.setRight(WPHotZone.top());
                WPHotZone.setX(e->x());
            }
            WPHotZone.setWidth(xsize);
            WPHotZone.setHeight(ysize);

            if (!WPHotZone.isValid()) qDebug("WPHotZone not valid");

            // draw rectangle for user feedback

            SelectWPpatch = false;
            emit WPPointsSelected();

        }

    }
}


void FigureEditor::clear()
{
    // qDebug("FigureEditor::clear");
    Q3CanvasItemList list = canvas()->allItems();
    Q3CanvasItemList::Iterator it = list.begin();
    for (; it != list.end(); ++it) {
        if ( *it )
            delete *it;
    }

    moving = NULL;
    sizing = NULL;
    unshearing = NULL;
}
 
