Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

SelectionPlacementInterface Class Reference

#include <selectionPlacementInterface.h>

Inheritance diagram for SelectionPlacementInterface:

Inheritance graph
[legend]
Collaboration diagram for SelectionPlacementInterface:

Collaboration graph
[legend]
List of all members.

Detailed Description

A selection region placement interface.

Definition at line 26 of file selectionPlacementInterface.h.

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.


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.

00024                                                         : QWidget (parent, name )
00025 {                  
00026   //store original image dimensions
00027   getImageSize( imageFilename, origImageSize );
00028   
00029   //construct scaled image
00030   scaleImage( imageFilename, scaledImage, 200, 200 );
00031   
00032   //construct an unselected scaled image
00033   unselectedScaledImage = scaledImage.copy();  
00034   int x, y;
00035   QRgb* rgb;
00036   uchar* scanLine;
00037   for( y=0; y<unselectedScaledImage.height(); y++)
00038   {   
00039     //iterate over each selected pixel in scanline
00040     scanLine = unselectedScaledImage.scanLine(y);
00041     for( x=0; x<unselectedScaledImage.width(); x++)
00042     {
00043       //compress dynamic range to 25% of original
00044       rgb = ((QRgb*)scanLine+x);
00045       
00046       double r = ((double)qRed(*rgb)   )/255.0;
00047       double g = ((double)qGreen(*rgb) )/255.0;
00048       double b = ((double)qBlue(*rgb)  )/255.0;
00049       
00050       //convert to hsv
00051       double h,s,v;
00052       RGBtoHSV(r,g,b,&h,&s,&v);
00053       
00054       //scale and clamp v
00055       v*=0.25;
00056       
00057       //convert adjusted color back to rgb colorspace and clamp
00058       HSVtoRGB( &r,&g,&b, h,s,v);         
00059       int rp = (int) QMIN( QMAX((r*255), 0), 255 );
00060       int gp = (int) QMIN( QMAX((g*255), 0), 255 );
00061       int bp = (int) QMIN( QMAX((b*255), 0), 255 );
00062       
00063       //set adjusted color value
00064       *rgb = qRgb(rp,gp,bp);          
00065     }
00066   }  
00067   
00068   //watch mouse movements in order to drag selection
00069   //watch mouse movements in order to move split point between adjusted and original image
00070   setMouseTracking(true);
00071   
00072   //by default no in dragging mode
00073   currentlyDragging = false;
00074   currentMouseShapeIsDrag = false;
00075   
00076   //accept focus when clicked on
00077   setFocusPolicy( QWidget::ClickFocus );
00078 
00079   //init selection area
00080   selection.setTopLeft( QPoint( -1, -1 ) );
00081   selection.setBottomRight( QPoint( -1, -1 ) );
00082 }

SelectionPlacementInterface::~SelectionPlacementInterface  ) 
 

Deletes objects.

Definition at line 84 of file selectionPlacementInterface.cpp.

00084 { }


Member Function Documentation

QRect SelectionPlacementInterface::getSelectedRegion  ) 
 

Returns the selected region in image space.

Definition at line 281 of file selectionPlacementInterface.cpp.

00282 {
00283   return selection;
00284 }

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().

00258 {
00259   //set top left
00260   QRect res;
00261   res.setTopLeft(QPoint( (int) (0.5+ (1.0*scaledImage.width()*r.left()) / origImageSize.width()),
00262                          (int) (0.5+ (1.0*scaledImage.height()*r.top()) / origImageSize.height()) ));                 
00263 
00264   //set width/height
00265   res.setWidth( (scaledImage.width() *r.width()) / origImageSize.width() );
00266   res.setHeight( (scaledImage.height() *r.height()) / origImageSize.height() );
00267   
00268   //if against the right hand size make sure scaled display coordiantes are also
00269   //against edge. rounding prevents this from occuring and is noticeable since selection
00270   //rectangle appears to never be at every edge.
00271   if( r.right() == origImageSize.width() - 1)
00272   { res.moveBy( (scaledImage.width()-1) - res.right(), 0 ); }
00273 
00274   if( r.bottom() == origImageSize.height() - 1)
00275   { res.moveBy( 0, (scaledImage.height()-1) - res.bottom() ); }
00276   
00277   //return new rect
00278   return res; 
00279 }

QSize SelectionPlacementInterface::minimumSizeHint  )  const [virtual]
 

Definition at line 158 of file selectionPlacementInterface.cpp.

References scaledImage.

Referenced by sizeHint().

00159 { return scaledImage.size(); }

void SelectionPlacementInterface::mouseMoveEvent QMouseEvent *  e  )  [protected]
 

Definition at line 230 of file selectionPlacementInterface.cpp.

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

00231 {
00232   //if not dragging update mosue cursor
00233   if(!currentlyDragging)
00234   { 
00235     if( !overRegion(e->pos() ) && currentMouseShapeIsDrag )
00236     { 
00237       currentMouseShapeIsDrag = false;
00238       setCursor( Qt::ArrowCursor ); 
00239     }
00240     else if( overRegion(e->pos() ) && !currentMouseShapeIsDrag )
00241     { 
00242       currentMouseShapeIsDrag = true;
00243       setCursor( getCursor(MOVE_SELECTION_CURSOR) );
00244 
00245     }
00246   }
00247   //move selection
00248   else { recenterSelection(e->pos()); }
00249 }

void SelectionPlacementInterface::mousePressEvent QMouseEvent *  e  )  [protected]
 

Definition at line 216 of file selectionPlacementInterface.cpp.

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

00217 { 
00218   //if mouse press is not over the region then center viewed area over mouse press
00219   if( !currentMouseShapeIsDrag )
00220   { 
00221     recenterSelection(e->pos()); 
00222     currentMouseShapeIsDrag = true;
00223     setCursor( getCursor(MOVE_SELECTION_CURSOR) );
00224   }
00225 
00226   //enter dragging mode
00227   currentlyDragging = true; 
00228 }

void SelectionPlacementInterface::mouseReleaseEvent QMouseEvent *   )  [protected]
 

Definition at line 251 of file selectionPlacementInterface.cpp.

References currentlyDragging.

00252 {
00253   //disable dragging
00254   currentlyDragging = false;
00255 }

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().

00162 {
00163   if( selection.width()  == 0 || selection.height() == 0 )
00164   { return false; }
00165   
00166   QRect displayRect = imageToDisplay( selection );
00167 
00168   //if the entire image is visible then no rectangle can be dragged so just
00169   //return false so a drag cursor never becomes visible since it might
00170   //confuse the user into thinking s/he could actually drag it
00171   if( displayRect.width() == scaledImage.width() &&
00172       displayRect.height() == scaledImage.height() )
00173     return false;
00174 
00175   //determine if mouse cursor is over region
00176   int xOffset = (width()  - scaledImage.width() ) / 2;
00177   int yOffset = (height() - scaledImage.height()) / 2;
00178 
00179   return ( p.x() >= xOffset + displayRect.left()  &&
00180            p.x() <= xOffset + displayRect.right() &&
00181            p.y() >= yOffset + displayRect.top()   &&
00182            p.y() <= yOffset + displayRect.bottom() );
00183 }

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.

00087 { 
00088   //if no scaled image just return
00089   if(scaledImage.isNull()) { return; }
00090   
00091   //create buffer to draw in
00092   QPixmap buffer( size() );
00093   
00094   //create a painter pointing to the buffer
00095   QPainter bufferPainter( &buffer );
00096   
00097   //turn off clipping to make painting operations faster
00098   bufferPainter.setClipping(false);
00099 
00100   //initialize buffer with background brush
00101   bufferPainter.fillRect( buffer.rect(), backgroundBrush() );
00102 
00103   int xOffset = (width() - scaledImage.width()) / 2;
00104   int yOffset = (height() - scaledImage.height()) / 2;
00105 
00106   //selection not set yet, simply paint the scaled image normally
00107   if(selection.width() == 0 || selection.height() == 0 )
00108   {
00109     bufferPainter.drawImage( QPoint(xOffset, yOffset), scaledImage );
00110   }
00111   //selection present...
00112   else
00113   {
00114     //first paint using unselected coloring
00115     bufferPainter.drawImage( QPoint(xOffset, yOffset), unselectedScaledImage );
00116     
00117     //convert selection coordinates to display space
00118     QRect displayRect = imageToDisplay( selection );
00119     QPoint topLeft = displayRect.topLeft() + QPoint( xOffset, yOffset );
00120     QPoint bottomRight = displayRect.bottomRight() + QPoint( xOffset, yOffset );
00121     
00122     //now paint selected region in color
00123     bufferPainter.drawImage( topLeft.x(), topLeft.y(),
00124                              scaledImage,
00125                              displayRect.left(), displayRect.top(),
00126                              displayRect.width(), displayRect.height() );                                   
00127     
00128 
00129     //paint thin line around selected region to help it stand out even more
00130     if( selection.width() < origImageSize.width() ||
00131         selection.height() < origImageSize.height() )
00132     {
00133       QPen pen;
00134       pen.setColor( gray );
00135       pen.setStyle( Qt::SolidLine );
00136       pen.setWidth( 2 );
00137       bufferPainter.setPen( pen);
00138     
00139       QRect selctRect( topLeft, bottomRight );
00140       bufferPainter.drawRect(selctRect);       
00141     }
00142   }
00143   
00144   //end painter  
00145   bufferPainter.end();
00146   
00147   //blit buffer to screen
00148   bitBlt( this,
00149           e->rect().x(), e->rect().y(),
00150           &buffer, 
00151           e->rect().x(), e->rect().y(),
00152           e->rect().width(), e->rect().height() );
00153 }

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().

00186 {
00187   //compute new viewing center
00188   QPoint center = QPoint( ((origImageSize.width()-1) * mousePosition.x()) / (width()-1),
00189                           ((origImageSize.height()-1) * mousePosition.y()) / (height()-1) );
00190   //move selection
00191   int sW = selection.width();
00192   int sH = selection.height();
00193   selection.setLeft( center.x() - sW/2 );
00194   selection.setTop( center.y() - sH/2 );
00195   selection.setRight( selection.left() + sW -1 );
00196   selection.setBottom( selection.top() + sH -1 );
00197   
00198   //ensure selection window never goes out of bounds
00199   if(selection.left() < 0 )
00200     selection.moveBy( -selection.left(), 0 );
00201   
00202   if(selection.right() > origImageSize.width() - 1 )
00203     selection.moveBy( (origImageSize.width() - 1) - selection.right(), 0 );
00204   
00205   if(selection.top() < 0 )
00206     selection.moveBy( 0, -selection.top() );
00207   
00208   if(selection.bottom() > origImageSize.height() - 1 )
00209     selection.moveBy( 0, (origImageSize.height() - 1) - selection.bottom() );
00210   
00211   //repaint and emit placement changed signal
00212   repaint(false); 
00213   emit placementChanged( selection );
00214 }

void SelectionPlacementInterface::setSelectedRegion QRect  selection  ) 
 

Set the select region using image space coordinates.

Definition at line 286 of file selectionPlacementInterface.cpp.

Referenced by GrainEditor::previewResized().

00287 {
00288   this->selection = selection;
00289   repaint(false); 
00290 }

QSize SelectionPlacementInterface::sizeHint  )  const [virtual]
 

Definition at line 155 of file selectionPlacementInterface.cpp.

References minimumSizeHint().

00156 { return minimumSizeHint(); }


Member Data Documentation

bool SelectionPlacementInterface::currentlyDragging [private]
 

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 mousePressEvent(), mouseReleaseEvent(), and SelectionPlacementInterface().

bool SelectionPlacementInterface::currentMouseShapeIsDrag [private]
 

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().

QSize SelectionPlacementInterface::origImageSize [private]
 

original image dimensions

Definition at line 71 of file selectionPlacementInterface.h.

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

QImage SelectionPlacementInterface::scaledImage [private]
 

Scaled image used for display purposes.

Definition at line 65 of file selectionPlacementInterface.h.

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

QRect SelectionPlacementInterface::selection [private]
 

selection

Definition at line 74 of file selectionPlacementInterface.h.

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

QImage SelectionPlacementInterface::unselectedScaledImage [private]
 

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:
Generated on Sat Apr 2 05:45:12 2005 for AlbumShaper by  doxygen 1.3.9.1