AlbumShaper  1.0a3
Public Member Functions | Private Attributes | List of all members
GeneratePreviewThread Class Reference

#include <addPhotosDialog.h>

Inheritance diagram for GeneratePreviewThread:
Inheritance graph
[legend]
Collaboration diagram for GeneratePreviewThread:
Collaboration graph
[legend]

Public Member Functions

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

Private Attributes

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

Detailed Description

Definition at line 33 of file addPhotosDialog.h.

Constructor & Destructor Documentation

§ GeneratePreviewThread()

GeneratePreviewThread::GeneratePreviewThread ( FilePreview previewWidget)

Definition at line 63 of file addPhotosDialog.cpp.

64 {
65  //we'll need to store a previewWidget handle to
66  //posting update events when updates are
67  //ready to be shown
68  this->previewWidget = previewWidget;
69 
70  //by default worker thread isn't busy yet
71  updating = false;
72  queue = QString::null;
73 }
QString queue
next file to be processed by worker thread
bool updating
is the worker thread currently generating a file preview?
FilePreview * previewWidget
handle on preview widget necessary for posting an update event once the current file has been process...

Member Function Documentation

§ run()

void GeneratePreviewThread::run ( )
virtual

Definition at line 97 of file addPhotosDialog.cpp.

References getImageSize(), MIN_HEIGHT, MIN_WIDTH, scaleImage(), and UpdatePreviewEvent::UpdatePreviewEvent().

98 {
99  //since it is possible for another job
100  //to be added to the queue while processing this one, it is necessary
101  //to loop until the queue is empty
102  while(true)
103  {
104  //------------------------------------------
105  //Get image type extension and convert to caps
106  QString extension = QFileInfo(filename).extension(false).upper();
107  bool validExtension = ( (extension.compare("GIF") == 0) ||
108  (extension.compare("JPG") == 0) ||
109  (extension.compare("JPEG") == 0) ||
110  (extension.compare("PNG") == 0) ||
111  (extension.compare("XPM") == 0) );
112  //------------------------------------------
113  //Scale the image to fit nicely on the screen, aka < 300x225
114  QImage scaledImage;
115  if( validExtension )
116  {
117  scaleImage(filename, scaledImage, MIN_WIDTH, MIN_HEIGHT );
118  }
119  //------------------------------------------
120  //Get image resolution
121  QString imageRes = "";
122  if(validExtension)
123  {
124  QSize res;
125  getImageSize( filename, res );
126  imageRes = QString("%1 x %2").arg(res.width()).arg(res.height());
127  }
128  //------------------------------------------
129  //Determine file size and construct a nicely formatted size string
130  QString fileSize = "?";
131  QFileInfo info;
132  info.setFile( filename );
133  int sizeOnDisk = info.size();
134 
135  if(sizeOnDisk < 1024)
136  fileSize = QString("%1 Byte%2").arg(sizeOnDisk).arg( sizeOnDisk == 0 || sizeOnDisk > 1 ? "s" : "");
137  else if( sizeOnDisk/1024 < 1024)
138  // fileSize = QString("%1 Kb").arg( ((float)*sizeOnDisk)/1024 );
139  fileSize = QString("%1 Kb").arg( ((float)((100*sizeOnDisk)/1024))/100 );
140  else if( sizeOnDisk/(1024*1024) < 1024)
141  fileSize = QString("%1 Mb").arg( ((float)((100*sizeOnDisk)/(1024*1024)))/100 );
142  else
143  fileSize = QString("%1 Gigs").arg( ((float)((100*sizeOnDisk)/(1024*1024*1024)))/100 );
144  //------------------------------------------
145  //Setup image details string
146  QString fileDetails = QString("%1 %2, %3")
147  .arg(imageRes)
148  .arg(extension)
149  .arg(fileSize);
150  //------------------------------------------
151  //Post UPDATE_PREVIEW_DETAILS event
152  UpdatePreviewEvent* upe = new UpdatePreviewEvent( scaledImage, fileDetails );
153  QApplication::postEvent( previewWidget, upe );
154  //------------------------------------------
155  //get lock
156  lockingMutex.lock();
157 
158  //if the queue is empty we're done!
159  if( queue.isNull() )
160  {
161  updating = false;
162  lockingMutex.unlock();
163  return;
164  }
165  //clear queue and process pending job
166  else
167  {
168  filename = queue;
169  queue = QString::null;
170  lockingMutex.unlock();
171  }
172 
173  } //end while(true)
174 }
#define MIN_HEIGHT
QString queue
next file to be processed by worker thread
bool updating
is the worker thread currently generating a file preview?
QMutex lockingMutex
locking mutex - necessary to prevent multiple threads from accessing the updating bool or queue varia...
bool scaleImage(QString fileIn, QString fileOut, int newWidth, int newHeight)
Scale image and save copy to disk.
Definition: imageTools.cpp:157
bool getImageSize(const char *filename, QSize &size)
Get image dimensions.
Definition: imageTools.cpp:192
#define MIN_WIDTH
QString filename
current file being processed
FilePreview * previewWidget
handle on preview widget necessary for posting an update event once the current file has been process...

§ start()

void GeneratePreviewThread::start ( QString  filename)

Definition at line 75 of file addPhotosDialog.cpp.

Referenced by FilePreview::updatePreview().

76 {
77  //get lock
78  lockingMutex.lock();
79 
80  //if currently animating then append job to queue
81  if(updating)
82  {
83  queue = filename;
84  lockingMutex.unlock();
85  return;
86  }
87  //else set animating to true, actually initiate job
88  else
89  {
90  updating = true;
91  this->filename = filename;
92  lockingMutex.unlock();
93  QThread::start();
94  }
95 }
QString queue
next file to be processed by worker thread
bool updating
is the worker thread currently generating a file preview?
QMutex lockingMutex
locking mutex - necessary to prevent multiple threads from accessing the updating bool or queue varia...
QString filename
current file being processed

Member Data Documentation

§ filename

QString GeneratePreviewThread::filename
private

current file being processed

Definition at line 42 of file addPhotosDialog.h.

§ lockingMutex

QMutex GeneratePreviewThread::lockingMutex
private

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

Definition at line 56 of file addPhotosDialog.h.

§ previewWidget

FilePreview* GeneratePreviewThread::previewWidget
private

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

Definition at line 46 of file addPhotosDialog.h.

§ queue

QString GeneratePreviewThread::queue
private

next file to be processed by worker thread

Definition at line 52 of file addPhotosDialog.h.

§ updating

bool GeneratePreviewThread::updating
private

is the worker thread currently generating a file preview?

Definition at line 49 of file addPhotosDialog.h.


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