/***************************************************************************
                          qrecttracker.cpp  -  description
                             -------------------
    begin                : dom ene 19 2003
    copyright            : (C) 2003 by Armando Navarro-Vazquez
    email                : qoajnv@usc.es
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "qrecttracker.h"
#include <qpainter.h>
#include <qmessagebox.h>
//Added by qt3to4:
#include <QMouseEvent>

QRectTracker::QRectTracker(QWidget *parent, const char *name ) {
  m_parent=parent;
  m_painter=new QPainter(m_parent);
  m_painter->setRasterOp(Qt::XorROP);
  m_bfirst=true;
}
QRectTracker::~QRectTracker(){
}


void QRectTracker::begin(QMouseEvent* e){

  m_point=m_oldpoint=e->pos();
  mouseMoveEvent(e);
}

void QRectTracker::mouseMoveEvent(QMouseEvent* e){

  QColor color(255,245,14);
  QPen pen(color,1,Qt::DotLine);
  m_painter->setPen(pen);
  QPoint parentpoint=e->pos();

  QPoint vertex2;
  QPoint  vertex4;

  if(!m_bfirst)       //erase the last line
  {
    vertex2.setX(m_oldpoint.x());
    vertex2.setY(m_point.y());
    vertex4.setX(m_point.x());
    vertex4.setY(m_oldpoint.y());
    m_painter->moveTo(m_point);
    m_painter->lineTo(vertex2);
    m_painter->lineTo(m_oldpoint);
    m_painter->lineTo(vertex4);
    m_painter->lineTo(m_point);
  }

  vertex2.setX(parentpoint.x());
  vertex2.setY(m_point.y());
  vertex4.setX(m_point.x());
  vertex4.setY(parentpoint.y());
  m_painter->moveTo(m_point);
  m_painter->lineTo(vertex2);
  m_painter->lineTo(parentpoint);
  m_painter->lineTo(vertex4);
  m_painter->lineTo(m_point);
  m_oldpoint=parentpoint;
  m_bfirst=false;
}
/** No descriptions */
QRect QRectTracker::stop(QMouseEvent* e){

  QRect rct;
  if(m_point.x()<m_oldpoint.x())
  {
    rct.setLeft(m_point.x());
    rct.setRight(m_oldpoint.x());
  }
  else
  {
    rct.setLeft(m_oldpoint.x());
    rct.setRight(m_point.x());
  }

  if(m_point.y()<m_oldpoint.y())
  {
    rct.setTop(m_point.y());
    rct.setBottom(m_oldpoint.y());
  }
  else
  {
    rct.setTop(m_oldpoint.y());
    rct.setBottom(m_point.y());
  }
  mouseMoveEvent(e);
    m_bfirst=true;
    return rct;  
}
