AlbumShaper 1.0a3
SelectionPlacementInterface Class Reference

A selection region placement interface. More...

#include <selectionPlacementInterface.h>

Inheritance diagram for SelectionPlacementInterface:
Collaboration diagram for SelectionPlacementInterface:

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 29 of file selectionPlacementInterface.h.

Constructor & Destructor Documentation

◆ SelectionPlacementInterface()

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

Creates layout.

Definition at line 29 of file selectionPlacementInterface.cpp.

31 : QWidget (parent, name )
32{
33 //store original image dimensions
34 getImageSize( imageFilename, origImageSize );
35
36 //construct scaled image
37 scaleImage( imageFilename, scaledImage, 200, 200 );
38
39 //construct an unselected scaled image
41 int x, y;
42 QRgb* rgb;
43 uchar* scanLine;
44 for( y=0; y<unselectedScaledImage.height(); y++)
45 {
46 //iterate over each selected pixel in scanline
47 scanLine = unselectedScaledImage.scanLine(y);
48 for( x=0; x<unselectedScaledImage.width(); x++)
49 {
50 //compress dynamic range to 25% of original
51 rgb = ((QRgb*)scanLine+x);
52
53 double r = ((double)qRed(*rgb) )/255.0;
54 double g = ((double)qGreen(*rgb) )/255.0;
55 double b = ((double)qBlue(*rgb) )/255.0;
56
57 //convert to hsv
58 double h,s,v;
59 RGBtoHSV(r,g,b,&h,&s,&v);
60
61 //scale and clamp v
62 v*=0.25;
63
64 //convert adjusted color back to rgb colorspace and clamp
65 HSVtoRGB( &r,&g,&b, h,s,v);
66 int rp = (int) MIN( MAX((r*255), 0), 255 );
67 int gp = (int) MIN( MAX((g*255), 0), 255 );
68 int bp = (int) MIN( MAX((b*255), 0), 255 );
69
70 //set adjusted color value
71 *rgb = qRgb(rp,gp,bp);
72 }
73 }
74
75 //watch mouse movements in order to drag selection
76 //watch mouse movements in order to move split point between adjusted and original image
77 setMouseTracking(true);
78
79 //by default no in dragging mode
80 currentlyDragging = false;
82
83 //accept focus when clicked on
84 setFocusPolicy( Qt::ClickFocus );
85
86 //init selection area
87 selection.setTopLeft( QPoint( -1, -1 ) );
88 selection.setBottomRight( QPoint( -1, -1 ) );
89}
bool currentlyDragging
dragging the mouse only moves the selection if the mouse button is pressed first over the selected re...
QImage unselectedScaledImage
Unselected scaled image (desaturated version of scaled image)
bool currentMouseShapeIsDrag
current mouse shape.
QSize origImageSize
original image dimensions
QImage scaledImage
Scaled image used for display purposes.
#define MIN(x, y)
Definition color.cpp:20
#define MAX(x, y)
Definition color.cpp:21
bool scaleImage(QString fileIn, QString fileOut, int newWidth, int newHeight)
Scale image and save copy to disk.
bool getImageSize(const char *filename, QSize &size)
Get image dimensions.
void RGBtoHSV(double r, double g, double b, double *h, double *s, double *v)
Convert a RGB color triplet to HSV.
void HSVtoRGB(double *r, double *g, double *b, double h, double s, double v)
Convert a HSV color triplet to RGB.
long b

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

◆ ~SelectionPlacementInterface()

SelectionPlacementInterface::~SelectionPlacementInterface ( )

Deletes objects.

Definition at line 91 of file selectionPlacementInterface.cpp.

91{ }

Member Function Documentation

◆ getSelectedRegion()

QRect SelectionPlacementInterface::getSelectedRegion ( )

Returns the selected region in image space.

Definition at line 288 of file selectionPlacementInterface.cpp.

289{
290 return selection;
291}

References selection.

◆ imageToDisplay()

QRect SelectionPlacementInterface::imageToDisplay ( QRect r)
private

convert rectangle from image coordinates to display coordinates

Definition at line 264 of file selectionPlacementInterface.cpp.

265{
266 //set top left
267 QRect res;
268 res.setTopLeft(QPoint( (int) (0.5+ (1.0*scaledImage.width()*r.left()) / origImageSize.width()),
269 (int) (0.5+ (1.0*scaledImage.height()*r.top()) / origImageSize.height()) ));
270
271 //set width/height
272 res.setWidth( (scaledImage.width() *r.width()) / origImageSize.width() );
273 res.setHeight( (scaledImage.height() *r.height()) / origImageSize.height() );
274
275 //if against the right hand size make sure scaled display coordiantes are also
276 //against edge. rounding prevents this from occuring and is noticeable since selection
277 //rectangle appears to never be at every edge.
278 if( r.right() == origImageSize.width() - 1)
279 { res.moveBy( (scaledImage.width()-1) - res.right(), 0 ); }
280
281 if( r.bottom() == origImageSize.height() - 1)
282 { res.moveBy( 0, (scaledImage.height()-1) - res.bottom() ); }
283
284 //return new rect
285 return res;
286}

References origImageSize, and scaledImage.

Referenced by overRegion(), and paintEvent().

◆ minimumSizeHint()

QSize SelectionPlacementInterface::minimumSizeHint ( ) const
virtual

Definition at line 165 of file selectionPlacementInterface.cpp.

166{ return scaledImage.size(); }

References scaledImage.

Referenced by sizeHint().

◆ mouseMoveEvent()

void SelectionPlacementInterface::mouseMoveEvent ( QMouseEvent * e)
protected

Definition at line 237 of file selectionPlacementInterface.cpp.

238{
239 //if not dragging update mosue cursor
241 {
242 if( !overRegion(e->pos() ) && currentMouseShapeIsDrag )
243 {
245 setCursor( Qt::ArrowCursor );
246 }
247 else if( overRegion(e->pos() ) && !currentMouseShapeIsDrag )
248 {
250 setCursor( getCursor(MOVE_SELECTION_CURSOR) );
251
252 }
253 }
254 //move selection
255 else { recenterSelection(e->pos()); }
256}
void recenterSelection(QPoint mousePosition)
util function used to center selection about mouse location
bool overRegion(QPoint p)
util function used to determine if mouse is over selected region
const QCursor & getCursor(CUSTOM_CURSOR_TYPE type)
Definition cursors.cpp:52
@ MOVE_SELECTION_CURSOR
Definition cursors.h:19

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

◆ mousePressEvent()

void SelectionPlacementInterface::mousePressEvent ( QMouseEvent * e)
protected

Definition at line 223 of file selectionPlacementInterface.cpp.

224{
225 //if mouse press is not over the region then center viewed area over mouse press
227 {
228 recenterSelection(e->pos());
230 setCursor( getCursor(MOVE_SELECTION_CURSOR) );
231 }
232
233 //enter dragging mode
234 currentlyDragging = true;
235}

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

◆ mouseReleaseEvent()

void SelectionPlacementInterface::mouseReleaseEvent ( QMouseEvent * )
protected

Definition at line 258 of file selectionPlacementInterface.cpp.

259{
260 //disable dragging
261 currentlyDragging = false;
262}

References currentlyDragging.

◆ overRegion()

bool SelectionPlacementInterface::overRegion ( QPoint p)
private

util function used to determine if mouse is over selected region

Definition at line 168 of file selectionPlacementInterface.cpp.

169{
170 if( selection.width() == 0 || selection.height() == 0 )
171 { return false; }
172
173 QRect displayRect = imageToDisplay( selection );
174
175 //if the entire image is visible then no rectangle can be dragged so just
176 //return false so a drag cursor never becomes visible since it might
177 //confuse the user into thinking s/he could actually drag it
178 if( displayRect.width() == scaledImage.width() &&
179 displayRect.height() == scaledImage.height() )
180 return false;
181
182 //determine if mouse cursor is over region
183 int xOffset = (width() - scaledImage.width() ) / 2;
184 int yOffset = (height() - scaledImage.height()) / 2;
185
186 return ( p.x() >= xOffset + displayRect.left() &&
187 p.x() <= xOffset + displayRect.right() &&
188 p.y() >= yOffset + displayRect.top() &&
189 p.y() <= yOffset + displayRect.bottom() );
190}
int width
Definition blur.cpp:79
int height
Definition blur.cpp:79
QRect imageToDisplay(QRect r)
convert rectangle from image coordinates to display coordinates

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

Referenced by mouseMoveEvent().

◆ paintEvent()

void SelectionPlacementInterface::paintEvent ( QPaintEvent * e)
protected

Definition at line 93 of file selectionPlacementInterface.cpp.

94{
95 //if no scaled image just return
96 if(scaledImage.isNull()) { return; }
97
98 //create buffer to draw in
99 QPixmap buffer( size() );
100
101 //create a painter pointing to the buffer
102 QPainter bufferPainter( &buffer );
103
104 //turn off clipping to make painting operations faster
105 bufferPainter.setClipping(false);
106
107 //initialize buffer with background brush
108 bufferPainter.fillRect( buffer.rect(), backgroundBrush() );
109
110 int xOffset = (width() - scaledImage.width()) / 2;
111 int yOffset = (height() - scaledImage.height()) / 2;
112
113 //selection not set yet, simply paint the scaled image normally
114 if(selection.width() == 0 || selection.height() == 0 )
115 {
116 bufferPainter.drawImage( QPoint(xOffset, yOffset), scaledImage );
117 }
118 //selection present...
119 else
120 {
121 //first paint using unselected coloring
122 bufferPainter.drawImage( QPoint(xOffset, yOffset), unselectedScaledImage );
123
124 //convert selection coordinates to display space
125 QRect displayRect = imageToDisplay( selection );
126 QPoint topLeft = displayRect.topLeft() + QPoint( xOffset, yOffset );
127 QPoint bottomRight = displayRect.bottomRight() + QPoint( xOffset, yOffset );
128
129 //now paint selected region in color
130 bufferPainter.drawImage( topLeft.x(), topLeft.y(),
132 displayRect.left(), displayRect.top(),
133 displayRect.width(), displayRect.height() );
134
135
136 //paint thin line around selected region to help it stand out even more
137 if( selection.width() < origImageSize.width() ||
138 selection.height() < origImageSize.height() )
139 {
140 QPen pen;
141 pen.setColor( Qt::gray );
142 pen.setStyle( Qt::SolidLine );
143 pen.setWidth( 2 );
144 bufferPainter.setPen( pen);
145
146 QRect selctRect( topLeft, bottomRight );
147 bufferPainter.drawRect(selctRect);
148 }
149 }
150
151 //end painter
152 bufferPainter.end();
153
154 //blit buffer to screen
155 bitBlt( this,
156 e->rect().x(), e->rect().y(),
157 &buffer,
158 e->rect().x(), e->rect().y(),
159 e->rect().width(), e->rect().height() );
160}
float * buffer
Definition blur.cpp:80
QPoint topLeft
QPoint bottomRight

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

◆ placementChanged

void SelectionPlacementInterface::placementChanged ( QRect )
signal

Referenced by recenterSelection().

◆ recenterSelection()

void SelectionPlacementInterface::recenterSelection ( QPoint mousePosition)
private

util function used to center selection about mouse location

Definition at line 192 of file selectionPlacementInterface.cpp.

193{
194 //compute new viewing center
195 QPoint center = QPoint( ((origImageSize.width()-1) * mousePosition.x()) / (width()-1),
196 ((origImageSize.height()-1) * mousePosition.y()) / (height()-1) );
197 //move selection
198 int sW = selection.width();
199 int sH = selection.height();
200 selection.setLeft( center.x() - sW/2 );
201 selection.setTop( center.y() - sH/2 );
202 selection.setRight( selection.left() + sW -1 );
203 selection.setBottom( selection.top() + sH -1 );
204
205 //ensure selection window never goes out of bounds
206 if(selection.left() < 0 )
207 selection.moveBy( -selection.left(), 0 );
208
209 if(selection.right() > origImageSize.width() - 1 )
210 selection.moveBy( (origImageSize.width() - 1) - selection.right(), 0 );
211
212 if(selection.top() < 0 )
213 selection.moveBy( 0, -selection.top() );
214
215 if(selection.bottom() > origImageSize.height() - 1 )
216 selection.moveBy( 0, (origImageSize.height() - 1) - selection.bottom() );
217
218 //repaint and emit placement changed signal
219 repaint(false);
221}

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

Referenced by mouseMoveEvent(), and mousePressEvent().

◆ setSelectedRegion()

void SelectionPlacementInterface::setSelectedRegion ( QRect selection)

Set the select region using image space coordinates.

Definition at line 293 of file selectionPlacementInterface.cpp.

294{
295 this->selection = selection;
296 repaint(false);
297}

References selection.

Referenced by GrainEditor::previewResized().

◆ sizeHint()

QSize SelectionPlacementInterface::sizeHint ( ) const
virtual

Definition at line 162 of file selectionPlacementInterface.cpp.

163{ return minimumSizeHint(); }

References minimumSizeHint().

Member Data Documentation

◆ currentlyDragging

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 81 of file selectionPlacementInterface.h.

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

◆ currentMouseShapeIsDrag

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 85 of file selectionPlacementInterface.h.

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

◆ origImageSize

QSize SelectionPlacementInterface::origImageSize
private

original image dimensions

Definition at line 74 of file selectionPlacementInterface.h.

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

◆ scaledImage

QImage SelectionPlacementInterface::scaledImage
private

Scaled image used for display purposes.

Definition at line 68 of file selectionPlacementInterface.h.

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

◆ selection

QRect SelectionPlacementInterface::selection
private

◆ unselectedScaledImage

QImage SelectionPlacementInterface::unselectedScaledImage
private

Unselected scaled image (desaturated version of scaled image)

Definition at line 71 of file selectionPlacementInterface.h.

Referenced by paintEvent(), and SelectionPlacementInterface().


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