00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- ***************************** 00002 * Qwt Widget Library 00003 * Copyright (C) 1997 Josef Wilgen 00004 * Copyright (C) 2002 Uwe Rathmann 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the Qwt License, Version 1.0 00008 *****************************************************************************/ 00009 00010 #include "qwt_sampling_thread.h" 00011 #include "qwt_system_clock.h" 00012 00013 class QwtSamplingThread::PrivateData 00014 { 00015 public: 00016 QwtSystemClock clock; 00017 00018 double interval; 00019 bool isStopped; 00020 }; 00021 00022 00024 QwtSamplingThread::QwtSamplingThread( QObject *parent ): 00025 QThread( parent ) 00026 { 00027 d_data = new PrivateData; 00028 d_data->interval = 1000; // 1 second 00029 d_data->isStopped = true; 00030 } 00031 00033 QwtSamplingThread::~QwtSamplingThread() 00034 { 00035 delete d_data; 00036 } 00037 00045 void QwtSamplingThread::setInterval( double interval ) 00046 { 00047 if ( interval < 0.0 ) 00048 interval = 0.0; 00049 00050 d_data->interval = interval; 00051 } 00052 00057 double QwtSamplingThread::interval() const 00058 { 00059 return d_data->interval; 00060 } 00061 00066 double QwtSamplingThread::elapsed() const 00067 { 00068 if ( d_data->isStopped ) 00069 return 0.0; 00070 00071 return d_data->clock.elapsed(); 00072 } 00073 00078 void QwtSamplingThread::stop() 00079 { 00080 d_data->isStopped = true; 00081 } 00082 00087 void QwtSamplingThread::run() 00088 { 00089 d_data->clock.start(); 00090 d_data->isStopped = false; 00091 00092 while ( !d_data->isStopped ) 00093 { 00094 const double elapsed = d_data->clock.elapsed(); 00095 sample( elapsed / 1000.0 ); 00096 00097 if ( d_data->interval > 0.0 ) 00098 { 00099 const double msecs = 00100 d_data->interval - ( d_data->clock.elapsed() - elapsed ); 00101 00102 if ( msecs > 0.0 ) 00103 usleep( qRound( 1000.0 * msecs ) ); 00104 } 00105 } 00106 }