GeneratePreviewThread Class Reference

#include <addPhotosDialog.h>

Inheritance diagram for GeneratePreviewThread:

Inheritance graph
[legend]
Collaboration diagram for GeneratePreviewThread:

Collaboration graph
[legend]

List of all members.


Detailed Description

Definition at line 29 of file addPhotosDialog.h.


Public Member Functions

 GeneratePreviewThread (FilePreview *previewWidget)
void start (QString filename)
virtual void run ()

Private Attributes

QString filename
 current file being processed
FilePreviewpreviewWidget
 handle on preview widget necessary for posting an update event once the current file has been processed
bool updating
 is the worker thread currently generating a file preview?
QString queue
 next file to be processed by worker thread
QMutex lockingMutex
 locking mutex - necessary to prevent multiple threads from accessing the updating bool or queue variable simultaniously

Constructor & Destructor Documentation

GeneratePreviewThread::GeneratePreviewThread ( FilePreview previewWidget  ) 

Definition at line 60 of file addPhotosDialog.cpp.

References queue, and updating.

00061 {
00062   //we'll need to store a previewWidget handle to 
00063   //posting update events when updates are
00064   //ready to be shown
00065   this->previewWidget = previewWidget;
00066 
00067   //by default worker thread isn't busy yet
00068   updating = false;
00069   queue = QString::null;
00070 }


Member Function Documentation

void GeneratePreviewThread::start ( QString  filename  ) 

Definition at line 72 of file addPhotosDialog.cpp.

References lockingMutex, queue, and updating.

Referenced by FilePreview::updatePreview().

00073 {
00074   //get lock
00075   lockingMutex.lock();
00076   
00077   //if currently animating then append job to queue
00078   if(updating)
00079   {
00080     queue = filename;
00081     lockingMutex.unlock();
00082     return; 
00083   }
00084   //else set animating to true, actually initiate job
00085   else
00086   {
00087     updating = true;
00088     this->filename = filename;
00089     lockingMutex.unlock();
00090     QThread::start();
00091   }
00092 }

void GeneratePreviewThread::run (  )  [virtual]

Definition at line 94 of file addPhotosDialog.cpp.

References filename, getImageSize(), lockingMutex, MIN_HEIGHT, MIN_WIDTH, previewWidget, queue, scaleImage(), and updating.

00095 {
00096   //since it is possible for another job
00097   //to be added to the queue while processing this one, it is necessary
00098   //to loop until the queue is empty
00099   while(true)
00100   {
00101     //------------------------------------------
00102     //Get image type extension and convert to caps  
00103     QString extension = QFileInfo(filename).extension(false).upper();
00104     bool validExtension = ( (extension.compare("GIF") == 0) ||
00105                             (extension.compare("JPG") == 0) ||
00106                             (extension.compare("JPEG") == 0) ||
00107                             (extension.compare("PNG") == 0) ||
00108                             (extension.compare("XPM") == 0) );
00109     //------------------------------------------
00110     //Scale the image to fit nicely on the screen, aka < 300x225
00111     QImage scaledImage;
00112     if( validExtension )
00113     {
00114       scaleImage(filename, scaledImage, MIN_WIDTH, MIN_HEIGHT );
00115     }
00116     //------------------------------------------
00117     //Get image resolution
00118     QString imageRes = "";
00119     if(validExtension)
00120     {
00121       QSize res;
00122       getImageSize( filename, res );
00123       imageRes = QString("%1 x %2").arg(res.width()).arg(res.height());
00124     }
00125     //------------------------------------------
00126     //Determine file size and construct a nicely formatted size string
00127     QString fileSize = "?";
00128     QFileInfo info;
00129     info.setFile( filename );
00130     int sizeOnDisk = info.size();
00131     
00132     if(sizeOnDisk < 1024)
00133       fileSize = QString("%1 Byte%2").arg(sizeOnDisk).arg( sizeOnDisk == 0 || sizeOnDisk > 1 ? "s" : "");
00134     else if( sizeOnDisk/1024 < 1024)
00135       //    fileSize = QString("%1 Kb").arg( ((float)*sizeOnDisk)/1024 );
00136       fileSize = QString("%1 Kb").arg( ((float)((100*sizeOnDisk)/1024))/100 );
00137     else if( sizeOnDisk/(1024*1024) < 1024)
00138       fileSize = QString("%1 Mb").arg( ((float)((100*sizeOnDisk)/(1024*1024)))/100 );
00139     else
00140       fileSize = QString("%1 Gigs").arg( ((float)((100*sizeOnDisk)/(1024*1024*1024)))/100 );   
00141     //------------------------------------------
00142     //Setup image details string  
00143     QString fileDetails = QString("%1 %2, %3")
00144                                   .arg(imageRes)
00145                                   .arg(extension)
00146                                   .arg(fileSize);
00147     //------------------------------------------
00148     //Post UPDATE_PREVIEW_DETAILS event
00149     UpdatePreviewEvent* upe = new UpdatePreviewEvent( scaledImage, fileDetails );
00150     QApplication::postEvent( previewWidget, upe );
00151     //------------------------------------------
00152     //get lock
00153     lockingMutex.lock();
00154     
00155     //if the queue is empty we're done!
00156     if( queue.isNull() )
00157     {
00158       updating = false;
00159       lockingMutex.unlock();
00160       return; 
00161     }
00162     //clear queue and process pending job
00163     else
00164     {
00165       filename = queue;
00166       queue = QString::null;
00167       lockingMutex.unlock();
00168     }
00169     
00170   } //end while(true)
00171 }


Member Data Documentation

current file being processed

Definition at line 38 of file addPhotosDialog.h.

Referenced by run().

handle on preview widget necessary for posting an update event once the current file has been processed

Definition at line 42 of file addPhotosDialog.h.

Referenced by run().

is the worker thread currently generating a file preview?

Definition at line 45 of file addPhotosDialog.h.

Referenced by GeneratePreviewThread(), run(), and start().

QString GeneratePreviewThread::queue [private]

next file to be processed by worker thread

Definition at line 48 of file addPhotosDialog.h.

Referenced by GeneratePreviewThread(), run(), and start().

locking mutex - necessary to prevent multiple threads from accessing the updating bool or queue variable simultaniously

Definition at line 52 of file addPhotosDialog.h.

Referenced by run(), and start().


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

Generated on Thu Jun 19 15:43:40 2008 for AlbumShaper by  doxygen 1.5.6