densityloadthread.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                      densityloadthread.cpp  -  description
00003                              -------------------
00004     begin                : Thu Mar 24 2005
00005     copyright            : (C) 2005-2006 by Ben Swerts
00006     email                : bswerts@users.sourceforge.net
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00018 
00022 
00023 
00024 
00026 
00027 // C++ header files
00028 #include <cassert>
00029 
00030 // Qt header files
00031 #include <qapplication.h>
00032 #include <qevent.h>
00033 #include <qfile.h>
00034 #include <qtextstream.h>
00035 
00036 // Xbrabo header files
00037 #include "densitybase.h"
00038 #include "densityloadthread.h"
00039 
00043 
00045 DensityLoadThread::DensityLoadThread(std::vector<double>* densityPoints, QTextStream* stream, DensityBase* densityDialog, const unsigned int numSkipValues, const unsigned int totalPoints) : QThread(), 
00046   data(densityPoints), 
00047   textStream(stream), 
00048   numSkip(numSkipValues), 
00049   numValues(totalPoints),
00050   stopRequested(false),
00051   parent(densityDialog) // according to GCC this one should be last to coincide with the declaration order
00052                         // But now it doe'sn't anymore AFAICS
00059 {
00060   assert(data != 0);
00061   assert(textStream != 0);
00062   assert(parent != 0);
00063 }
00064 
00066 DensityLoadThread::~DensityLoadThread()
00068 {
00069 
00070 }
00071 
00073 void DensityLoadThread::run()
00076 {  
00078   QFile* file = dynamic_cast<QFile*>(textStream->device());
00079   assert(file != 0);
00080 
00081   if(numValues == 0)
00082   {
00083     delete textStream;
00084     delete file;
00085     return;
00086   }
00087 
00088   data->clear();
00089   data->reserve(numValues);
00090   const unsigned int updateFreq = numValues/100;
00091   double value = 0.0;
00092 
00093   for(unsigned int i = 0; i < numValues; i++)
00094   {
00095     // read the next density point
00096     *textStream >> value;
00097           data->push_back(value);
00098     if(i % updateFreq == 0)
00099     {
00100       progress = i;
00101       QCustomEvent* e = new QCustomEvent(static_cast<QEvent::Type>(1001),&progress);
00102       QApplication::postEvent(parent, e);
00103     }
00104     // skip the next numSkipValues density points from other MO's
00105     for(unsigned int skip = 0; skip < numSkip; skip++)
00106       *textStream >> value;
00107     if(stopRequested || textStream->atEnd())
00108       break;
00109   }
00110 
00111   // cleanup if stopped prematurely
00112   if(data->size() != numValues)
00113     data->clear();
00114 
00115   // cleanup
00116   delete textStream;
00117   delete file;
00118 
00119   // notify the thread has ended
00120   QCustomEvent* e = new QCustomEvent(static_cast<QEvent::Type>(1002));
00121   QApplication::postEvent(parent, e);
00122 }
00123 
00125 void DensityLoadThread::stop()
00127 {
00128   stopRequested = true;
00129 }
00130 
00132 bool DensityLoadThread::success()
00134 {
00135   return data->size() == numValues;
00136 }
00137 

Generated on Fri May 19 14:31:54 2006 for Brabosphere by  doxygen 1.4.6-NO