/**********************************************************************
 *     EPSCAN, an EPSON ES-1200 scanner driver     
 *     Adam P. Jenkins <ajenkins@cs.umass.edu>
 *     
 *     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.
 *     
 *     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, write to the Free Software
 *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 **********************************************************************/

// -*-c++-*-
// class to show a pixmap and let the user select a region.
// The mouse bindings for using the widget are:
// - Hold down the left mouse button and drag to select a rectangular region
//   of the widget.  The region is surrounded by a visible box.
// - Click the right mouse button anywhere in the widget to make the region
//   go back to full size.
// - middle mouse button does nothing.

#include <qframe.h>
#include <qpixmap.h>
#include <qrect.h>
#include <qevent.h>
#include <qwmatrix.h>

class EPGetRegion : public QFrame
{
  Q_OBJECT;
public:
  EPGetRegion(QWidget *parent=0, const char *name=0);
  ~EPGetRegion();


public slots:
  // set the currently selected region to r
  void setRegion(const QRect &r);
  // set pixmap displayed on the background of the widget
  void setPixmap(const QPixmap &p);
  // This sets the virtual coordinates to r.  This means for example that
  // selecting the whole widget would return a rectangle the same size as
  // r.  By default the range is the same as the widget dimensions.
  void setRange(const QRect &r); // set the virtual dimensions of the widget

signals:
  // This one is sent every time the region changes, i.e. while a user is
  // dragging the mouse.
  void regionChanged(const QRect &r);	// sent whenever region changes
  // this one only is sent when the user is done changing the region, i.e.
  // when they let up the button.
  void regionSelected(const QRect &r); // sent when mouse is released

protected:
  void mousePressEvent(QMouseEvent *e);
  void mouseReleaseEvent(QMouseEvent *e);
  void mouseMoveEvent(QMouseEvent *e);
  void leaveEvent(QEvent *e);
  void paintEvent(QPaintEvent *e);
  void resizeEvent(QResizeEvent *e);

private:
  QPixmap pix;
  QPixmap spix;			// scaled version of pix
  QRect region;
  
  QRect range;			// the virtual dimensions
  QWMatrix rangeMat;		// transform matrix for converting dimensions
  
  QPoint delta;			// how sensitive the mouse is when selecting
  
  enum {
    TOPLEFT,
    BOTTOMLEFT,
    LEFT,
    RIGHT,
    TOPRIGHT,
    BOTTOMRIGHT,
    TOP,
    BOTTOM,
    NONE
  } regionDir;			// direction in which the region is resized

  void updateRegion();		// redraw the region
};
