AlbumShaper  1.0a3
Signals | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes
SelectionPlacementInterface Class Reference

A selection region placement interface. More...

#include <selectionPlacementInterface.h>

Inheritance diagram for SelectionPlacementInterface:
Inheritance graph
[legend]
Collaboration diagram for SelectionPlacementInterface:
Collaboration graph
[legend]

List of all members.

Signals

void placementChanged (QRect)

Public Member Functions

 SelectionPlacementInterface (QString imageFilename, QWidget *parent=0, const char *name=0)
 Creates layout.
 ~SelectionPlacementInterface ()
 Deletes objects.
QRect getSelectedRegion ()
 Returns the selected region in image space.
void setSelectedRegion (QRect selection)
 Set the select region using image space coordinates.
virtual QSize sizeHint () const
virtual QSize minimumSizeHint () const

Protected Member Functions

void paintEvent (QPaintEvent *e)
void mousePressEvent (QMouseEvent *e)
void mouseReleaseEvent (QMouseEvent *)
void mouseMoveEvent (QMouseEvent *e)

Private Member Functions

QRect imageToDisplay (QRect r)
 convert rectangle from image coordinates to display coordinates
bool overRegion (QPoint p)
 util function used to determine if mouse is over selected region
void recenterSelection (QPoint mousePosition)
 util function used to center selection about mouse location

Private Attributes

QImage scaledImage
 Scaled image used for display purposes.
QImage unselectedScaledImage
 Unselected scaled image (desaturated version of scaled image)
QSize origImageSize
 original image dimensions
QRect selection
 selection
bool currentlyDragging
 dragging the mouse only moves the selection if the mouse button is pressed first over the selected region
bool currentMouseShapeIsDrag
 current mouse shape.

Detailed Description

A selection region placement interface.

Definition at line 26 of file selectionPlacementInterface.h.


Constructor & Destructor Documentation

SelectionPlacementInterface::SelectionPlacementInterface ( QString  imageFilename,
QWidget parent = 0,
const char *  name = 0 
)

Creates layout.

Definition at line 22 of file selectionPlacementInterface.cpp.

References b, currentlyDragging, currentMouseShapeIsDrag, getImageSize(), HSVtoRGB(), origImageSize, RGBtoHSV(), scaledImage, scaleImage(), selection, and unselectedScaledImage.

                                                        : QWidget (parent, name )
{                  
  //store original image dimensions
  getImageSize( imageFilename, origImageSize );
  
  //construct scaled image
  scaleImage( imageFilename, scaledImage, 200, 200 );
  
  //construct an unselected scaled image
  unselectedScaledImage = scaledImage.copy();  
  int x, y;
  QRgb* rgb;
  uchar* scanLine;
  for( y=0; y<unselectedScaledImage.height(); y++)
  {   
    //iterate over each selected pixel in scanline
    scanLine = unselectedScaledImage.scanLine(y);
    for( x=0; x<unselectedScaledImage.width(); x++)
    {
      //compress dynamic range to 25% of original
      rgb = ((QRgb*)scanLine+x);
      
      double r = ((double)qRed(*rgb)   )/255.0;
      double g = ((double)qGreen(*rgb) )/255.0;
      double b = ((double)qBlue(*rgb)  )/255.0;
      
      //convert to hsv
      double h,s,v;
      RGBtoHSV(r,g,b,&h,&s,&v);
      
      //scale and clamp v
      v*=0.25;
      
      //convert adjusted color back to rgb colorspace and clamp
      HSVtoRGB( &r,&g,&b, h,s,v);         
      int rp = (int) QMIN( QMAX((r*255), 0), 255 );
      int gp = (int) QMIN( QMAX((g*255), 0), 255 );
      int bp = (int) QMIN( QMAX((b*255), 0), 255 );
      
      //set adjusted color value
      *rgb = qRgb(rp,gp,bp);          
    }
  }  
  
  //watch mouse movements in order to drag selection
  //watch mouse movements in order to move split point between adjusted and original image
  setMouseTracking(true);
  
  //by default no in dragging mode
  currentlyDragging = false;
  currentMouseShapeIsDrag = false;
  
  //accept focus when clicked on
  setFocusPolicy( QWidget::ClickFocus );

  //init selection area
  selection.setTopLeft( QPoint( -1, -1 ) );
  selection.setBottomRight( QPoint( -1, -1 ) );
}
SelectionPlacementInterface::~SelectionPlacementInterface ( )

Deletes objects.

Definition at line 84 of file selectionPlacementInterface.cpp.

{ }

Member Function Documentation

QRect SelectionPlacementInterface::getSelectedRegion ( )

Returns the selected region in image space.

Definition at line 281 of file selectionPlacementInterface.cpp.

References selection.

{
  return selection;
}
QRect SelectionPlacementInterface::imageToDisplay ( QRect  r) [private]

convert rectangle from image coordinates to display coordinates

Definition at line 257 of file selectionPlacementInterface.cpp.

References origImageSize, and scaledImage.

Referenced by overRegion(), and paintEvent().

{
  //set top left
  QRect res;
  res.setTopLeft(QPoint( (int) (0.5+ (1.0*scaledImage.width()*r.left()) / origImageSize.width()),
                         (int) (0.5+ (1.0*scaledImage.height()*r.top()) / origImageSize.height()) ));                 

  //set width/height
  res.setWidth( (scaledImage.width() *r.width()) / origImageSize.width() );
  res.setHeight( (scaledImage.height() *r.height()) / origImageSize.height() );
  
  //if against the right hand size make sure scaled display coordiantes are also
  //against edge. rounding prevents this from occuring and is noticeable since selection
  //rectangle appears to never be at every edge.
  if( r.right() == origImageSize.width() - 1)
  { res.moveBy( (scaledImage.width()-1) - res.right(), 0 ); }

  if( r.bottom() == origImageSize.height() - 1)
  { res.moveBy( 0, (scaledImage.height()-1) - res.bottom() ); }
  
  //return new rect
  return res; 
}
QSize SelectionPlacementInterface::minimumSizeHint ( ) const [virtual]

Definition at line 158 of file selectionPlacementInterface.cpp.

References scaledImage.

Referenced by sizeHint().

{ return scaledImage.size(); }
void SelectionPlacementInterface::mouseMoveEvent ( QMouseEvent *  e) [protected]

Definition at line 230 of file selectionPlacementInterface.cpp.

References currentlyDragging, currentMouseShapeIsDrag, getCursor(), MOVE_SELECTION_CURSOR, overRegion(), and recenterSelection().

{
  //if not dragging update mosue cursor
  if(!currentlyDragging)
  { 
    if( !overRegion(e->pos() ) && currentMouseShapeIsDrag )
    { 
      currentMouseShapeIsDrag = false;
      setCursor( Qt::ArrowCursor ); 
    }
    else if( overRegion(e->pos() ) && !currentMouseShapeIsDrag )
    { 
      currentMouseShapeIsDrag = true;
      setCursor( getCursor(MOVE_SELECTION_CURSOR) );

    }
  }
  //move selection
  else { recenterSelection(e->pos()); }
}
void SelectionPlacementInterface::mousePressEvent ( QMouseEvent *  e) [protected]

Definition at line 216 of file selectionPlacementInterface.cpp.

References currentlyDragging, currentMouseShapeIsDrag, getCursor(), MOVE_SELECTION_CURSOR, and recenterSelection().

{ 
  //if mouse press is not over the region then center viewed area over mouse press
  if( !currentMouseShapeIsDrag )
  { 
    recenterSelection(e->pos()); 
    currentMouseShapeIsDrag = true;
    setCursor( getCursor(MOVE_SELECTION_CURSOR) );
  }

  //enter dragging mode
  currentlyDragging = true; 
}
void SelectionPlacementInterface::mouseReleaseEvent ( QMouseEvent *  ) [protected]

Definition at line 251 of file selectionPlacementInterface.cpp.

References currentlyDragging.

{
  //disable dragging
  currentlyDragging = false;
}
bool SelectionPlacementInterface::overRegion ( QPoint  p) [private]

util function used to determine if mouse is over selected region

Definition at line 161 of file selectionPlacementInterface.cpp.

References height, imageToDisplay(), scaledImage, selection, and width.

Referenced by mouseMoveEvent().

{
  if( selection.width()  == 0 || selection.height() == 0 )
  { return false; }
  
  QRect displayRect = imageToDisplay( selection );

  //if the entire image is visible then no rectangle can be dragged so just
  //return false so a drag cursor never becomes visible since it might
  //confuse the user into thinking s/he could actually drag it
  if( displayRect.width() == scaledImage.width() &&
      displayRect.height() == scaledImage.height() )
    return false;

  //determine if mouse cursor is over region
  int xOffset = (width()  - scaledImage.width() ) / 2;
  int yOffset = (height() - scaledImage.height()) / 2;

  return ( p.x() >= xOffset + displayRect.left()  &&
           p.x() <= xOffset + displayRect.right() &&
           p.y() >= yOffset + displayRect.top()   &&
           p.y() <= yOffset + displayRect.bottom() );
}
void SelectionPlacementInterface::paintEvent ( QPaintEvent *  e) [protected]

Definition at line 86 of file selectionPlacementInterface.cpp.

References bottomRight, buffer, height, imageToDisplay(), origImageSize, scaledImage, selection, topLeft, unselectedScaledImage, and width.

{ 
  //if no scaled image just return
  if(scaledImage.isNull()) { return; }
  
  //create buffer to draw in
  QPixmap buffer( size() );
  
  //create a painter pointing to the buffer
  QPainter bufferPainter( &buffer );
  
  //turn off clipping to make painting operations faster
  bufferPainter.setClipping(false);

  //initialize buffer with background brush
  bufferPainter.fillRect( buffer.rect(), backgroundBrush() );

  int xOffset = (width() - scaledImage.width()) / 2;
  int yOffset = (height() - scaledImage.height()) / 2;

  //selection not set yet, simply paint the scaled image normally
  if(selection.width() == 0 || selection.height() == 0 )
  {
    bufferPainter.drawImage( QPoint(xOffset, yOffset), scaledImage );
  }
  //selection present...
  else
  {
    //first paint using unselected coloring
    bufferPainter.drawImage( QPoint(xOffset, yOffset), unselectedScaledImage );
    
    //convert selection coordinates to display space
    QRect displayRect = imageToDisplay( selection );
    QPoint topLeft = displayRect.topLeft() + QPoint( xOffset, yOffset );
    QPoint bottomRight = displayRect.bottomRight() + QPoint( xOffset, yOffset );
    
    //now paint selected region in color
    bufferPainter.drawImage( topLeft.x(), topLeft.y(),
                             scaledImage,
                             displayRect.left(), displayRect.top(),
                             displayRect.width(), displayRect.height() );                                   
    

    //paint thin line around selected region to help it stand out even more
    if( selection.width() < origImageSize.width() ||
        selection.height() < origImageSize.height() )
    {
      QPen pen;
      pen.setColor( gray );
      pen.setStyle( Qt::SolidLine );
      pen.setWidth( 2 );
      bufferPainter.setPen( pen);
    
      QRect selctRect( topLeft, bottomRight );
      bufferPainter.drawRect(selctRect);       
    }
  }
  
  //end painter  
  bufferPainter.end();
  
  //blit buffer to screen
  bitBlt( this,
          e->rect().x(), e->rect().y(),
          &buffer, 
          e->rect().x(), e->rect().y(),
          e->rect().width(), e->rect().height() );
}
void SelectionPlacementInterface::placementChanged ( QRect  ) [signal]

Referenced by recenterSelection().

void SelectionPlacementInterface::recenterSelection ( QPoint  mousePosition) [private]

util function used to center selection about mouse location

Definition at line 185 of file selectionPlacementInterface.cpp.

References height, origImageSize, placementChanged(), selection, and width.

Referenced by mouseMoveEvent(), and mousePressEvent().

{
  //compute new viewing center
  QPoint center = QPoint( ((origImageSize.width()-1) * mousePosition.x()) / (width()-1),
                          ((origImageSize.height()-1) * mousePosition.y()) / (height()-1) );
  //move selection
  int sW = selection.width();
  int sH = selection.height();
  selection.setLeft( center.x() - sW/2 );
  selection.setTop( center.y() - sH/2 );
  selection.setRight( selection.left() + sW -1 );
  selection.setBottom( selection.top() + sH -1 );
  
  //ensure selection window never goes out of bounds
  if(selection.left() < 0 )
    selection.moveBy( -selection.left(), 0 );
  
  if(selection.right() > origImageSize.width() - 1 )
    selection.moveBy( (origImageSize.width() - 1) - selection.right(), 0 );
  
  if(selection.top() < 0 )
    selection.moveBy( 0, -selection.top() );
  
  if(selection.bottom() > origImageSize.height() - 1 )
    selection.moveBy( 0, (origImageSize.height() - 1) - selection.bottom() );
  
  //repaint and emit placement changed signal
  repaint(false); 
  emit placementChanged( selection );
}
void SelectionPlacementInterface::setSelectedRegion ( QRect  selection)

Set the select region using image space coordinates.

Definition at line 286 of file selectionPlacementInterface.cpp.

References selection.

Referenced by GrainEditor::previewResized().

{
  this->selection = selection;
  repaint(false); 
}
QSize SelectionPlacementInterface::sizeHint ( ) const [virtual]

Definition at line 155 of file selectionPlacementInterface.cpp.

References minimumSizeHint().

{ return minimumSizeHint(); }

Member Data Documentation

dragging the mouse only moves the selection if the mouse button is pressed first over the selected region

Definition at line 78 of file selectionPlacementInterface.h.

Referenced by mouseMoveEvent(), mousePressEvent(), mouseReleaseEvent(), and SelectionPlacementInterface().

current mouse shape.

by caching this value we avoid resetting the mouse cursor every time it moves etc.

Definition at line 82 of file selectionPlacementInterface.h.

Referenced by mouseMoveEvent(), mousePressEvent(), and SelectionPlacementInterface().

original image dimensions

Definition at line 71 of file selectionPlacementInterface.h.

Referenced by imageToDisplay(), paintEvent(), recenterSelection(), and SelectionPlacementInterface().

Scaled image used for display purposes.

Definition at line 65 of file selectionPlacementInterface.h.

Referenced by imageToDisplay(), minimumSizeHint(), overRegion(), paintEvent(), and SelectionPlacementInterface().

Unselected scaled image (desaturated version of scaled image)

Definition at line 68 of file selectionPlacementInterface.h.

Referenced by paintEvent(), and SelectionPlacementInterface().


The documentation for this class was generated from the following files: