AlbumShaper  1.0a3
Public Member Functions | Private Attributes
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

Detailed Description

Definition at line 29 of file addPhotosDialog.h.


Constructor & Destructor Documentation

GeneratePreviewThread::GeneratePreviewThread ( FilePreview previewWidget)

Definition at line 60 of file addPhotosDialog.cpp.

References previewWidget, queue, and updating.

{
  //we'll need to store a previewWidget handle to 
  //posting update events when updates are
  //ready to be shown
  this->previewWidget = previewWidget;

  //by default worker thread isn't busy yet
  updating = false;
  queue = QString::null;
}

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.

{
  //since it is possible for another job
  //to be added to the queue while processing this one, it is necessary
  //to loop until the queue is empty
  while(true)
  {
    //------------------------------------------
    //Get image type extension and convert to caps  
    QString extension = QFileInfo(filename).extension(false).upper();
    bool validExtension = ( (extension.compare("GIF") == 0) ||
                            (extension.compare("JPG") == 0) ||
                            (extension.compare("JPEG") == 0) ||
                            (extension.compare("PNG") == 0) ||
                            (extension.compare("XPM") == 0) );
    //------------------------------------------
    //Scale the image to fit nicely on the screen, aka < 300x225
    QImage scaledImage;
    if( validExtension )
    {
      scaleImage(filename, scaledImage, MIN_WIDTH, MIN_HEIGHT );
    }
    //------------------------------------------
    //Get image resolution
    QString imageRes = "";
    if(validExtension)
    {
      QSize res;
      getImageSize( filename, res );
      imageRes = QString("%1 x %2").arg(res.width()).arg(res.height());
    }
    //------------------------------------------
    //Determine file size and construct a nicely formatted size string
    QString fileSize = "?";
    QFileInfo info;
    info.setFile( filename );
    int sizeOnDisk = info.size();
    
    if(sizeOnDisk < 1024)
      fileSize = QString("%1 Byte%2").arg(sizeOnDisk).arg( sizeOnDisk == 0 || sizeOnDisk > 1 ? "s" : "");
    else if( sizeOnDisk/1024 < 1024)
      //    fileSize = QString("%1 Kb").arg( ((float)*sizeOnDisk)/1024 );
      fileSize = QString("%1 Kb").arg( ((float)((100*sizeOnDisk)/1024))/100 );
    else if( sizeOnDisk/(1024*1024) < 1024)
      fileSize = QString("%1 Mb").arg( ((float)((100*sizeOnDisk)/(1024*1024)))/100 );
    else
      fileSize = QString("%1 Gigs").arg( ((float)((100*sizeOnDisk)/(1024*1024*1024)))/100 );   
    //------------------------------------------
    //Setup image details string  
    QString fileDetails = QString("%1 %2, %3")
                                  .arg(imageRes)
                                  .arg(extension)
                                  .arg(fileSize);
    //------------------------------------------
    //Post UPDATE_PREVIEW_DETAILS event
    UpdatePreviewEvent* upe = new UpdatePreviewEvent( scaledImage, fileDetails );
    QApplication::postEvent( previewWidget, upe );
    //------------------------------------------
    //get lock
    lockingMutex.lock();
    
    //if the queue is empty we're done!
    if( queue.isNull() )
    {
      updating = false;
      lockingMutex.unlock();
      return; 
    }
    //clear queue and process pending job
    else
    {
      filename = queue;
      queue = QString::null;
      lockingMutex.unlock();
    }
    
  } //end while(true)
}
void GeneratePreviewThread::start ( QString  filename)

Definition at line 72 of file addPhotosDialog.cpp.

References filename, lockingMutex, queue, and updating.

Referenced by FilePreview::updatePreview().

{
  //get lock
  lockingMutex.lock();
  
  //if currently animating then append job to queue
  if(updating)
  {
    queue = filename;
    lockingMutex.unlock();
    return; 
  }
  //else set animating to true, actually initiate job
  else
  {
    updating = true;
    this->filename = filename;
    lockingMutex.unlock();
    QThread::start();
  }
}

Member Data Documentation

current file being processed

Definition at line 38 of file addPhotosDialog.h.

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

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 GeneratePreviewThread(), and 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().

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: