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

GeneratePreviewThread Class Reference

#include <addPhotosDialog.h>

Inheritance diagram for GeneratePreviewThread:

Inheritance graph
[legend]
Collaboration diagram for GeneratePreviewThread:

Collaboration graph
[legend]
List of all members.

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::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     QPixmap scaledPixmap = QPixmap(NULL);
00112     if( validExtension )
00113     {
00114       QImage scaledImage;
00115       scaleImage(filename, scaledImage, MIN_WIDTH, MIN_HEIGHT );
00116       scaledPixmap.convertFromImage( scaledImage );
00117     }
00118     //------------------------------------------
00119     //Get image resolution
00120     QString imageRes = "";
00121     if(validExtension)
00122     {
00123       QSize res;
00124       getImageSize( filename, res );
00125       imageRes = QString("%1 x %2").arg(res.width()).arg(res.height());
00126     }
00127     //------------------------------------------
00128     //Determine file size and construct a nicely formatted size string
00129     QString fileSize = "?";
00130     QFileInfo info;
00131     info.setFile( filename );
00132     int sizeOnDisk = info.size();
00133     
00134     if(sizeOnDisk < 1024)
00135       fileSize = QString("%1 Byte%2").arg(sizeOnDisk).arg( sizeOnDisk == 0 || sizeOnDisk > 1 ? "s" : "");
00136     else if( sizeOnDisk/1024 < 1024)
00137       //    fileSize = QString("%1 Kb").arg( ((float)*sizeOnDisk)/1024 );
00138       fileSize = QString("%1 Kb").arg( ((float)((100*sizeOnDisk)/1024))/100 );
00139     else if( sizeOnDisk/(1024*1024) < 1024)
00140       fileSize = QString("%1 Mb").arg( ((float)((100*sizeOnDisk)/(1024*1024)))/100 );
00141     else
00142       fileSize = QString("%1 Gigs").arg( ((float)((100*sizeOnDisk)/(1024*1024*1024)))/100 );   
00143     //------------------------------------------
00144     //Setup image details string  
00145     QString fileDetails = QString("%1 %2, %3")
00146                                   .arg(imageRes)
00147                                   .arg(extension)
00148                                   .arg(fileSize);
00149     //------------------------------------------
00150     //Post UPDATE_PREVIEW_DETAILS event
00151     UpdatePreviewEvent* upe = new UpdatePreviewEvent( scaledPixmap, fileDetails );
00152     QApplication::postEvent( previewWidget, upe );
00153     //------------------------------------------
00154     //get lock
00155     lockingMutex.lock();
00156     
00157     //if the queue is empty we're done!
00158     if( queue.isNull() )
00159     {
00160       updating = false;
00161       lockingMutex.unlock();
00162       return; 
00163     }
00164     //clear queue and process pending job
00165     else
00166     {
00167       filename = queue;
00168       queue = QString::null;
00169       lockingMutex.unlock();
00170     }
00171     
00172   } //end while(true)
00173 }

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 }


Member Data Documentation

QString GeneratePreviewThread::filename [private]
 

current file being processed

Definition at line 38 of file addPhotosDialog.h.

Referenced by run().

QMutex GeneratePreviewThread::lockingMutex [private]
 

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

FilePreview* GeneratePreviewThread::previewWidget [private]
 

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

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

bool GeneratePreviewThread::updating [private]
 

is the worker thread currently generating a file preview?

Definition at line 45 of file addPhotosDialog.h.

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


The documentation for this class was generated from the following files:
Generated on Sat Apr 2 05:44:57 2005 for AlbumShaper by  doxygen 1.3.9.1