qwt_sampling_thread.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Qwt Widget Library
3  * Copyright (C) 1997 Josef Wilgen
4  * Copyright (C) 2002 Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #include "qwt_sampling_thread.h"
11 #include <qelapsedtimer.h>
12 
14 {
15  public:
16  QElapsedTimer timer;
17  double msecsInterval;
18 };
19 
22  : QThread( parent )
23 {
24  m_data = new PrivateData;
25  m_data->msecsInterval = 1e3; // 1 second
26 }
27 
30 {
31  delete m_data;
32 }
33 
41 void QwtSamplingThread::setInterval( double msecs )
42 {
43  if ( msecs < 0.0 )
44  msecs = 0.0;
45 
46  m_data->msecsInterval = msecs;
47 }
48 
54 {
55  return m_data->msecsInterval;
56 }
57 
63 {
64  if ( m_data->timer.isValid() )
65  return m_data->timer.nsecsElapsed() / 1e6;
66 
67  return 0.0;
68 }
69 
75 {
76  m_data->timer.invalidate();
77 }
78 
84 {
85  m_data->timer.start();
86 
87  /*
88  We should have all values in nsecs/qint64, but
89  this would break existing code. TODO ...
90  Anyway - for QThread::usleep we even need microseconds( usecs )
91  */
92  while ( m_data->timer.isValid() )
93  {
94  const qint64 timestamp = m_data->timer.nsecsElapsed();
95  sample( timestamp / 1e9 ); // seconds
96 
97  if ( m_data->msecsInterval > 0.0 )
98  {
99  const double interval = m_data->msecsInterval * 1e3;
100  const double elapsed = ( m_data->timer.nsecsElapsed() - timestamp ) / 1e3;
101 
102  const double usecs = interval - elapsed;
103 
104  if ( usecs > 0.0 )
105  QThread::usleep( qRound( usecs ) );
106  }
107  }
108 }
109 
110 #if QWT_MOC_INCLUDE
111 #include "moc_qwt_sampling_thread.cpp"
112 #endif
QwtSamplingThread::stop
void stop()
Definition: qwt_sampling_thread.cpp:74
QwtSamplingThread::~QwtSamplingThread
virtual ~QwtSamplingThread()
Destructor.
Definition: qwt_sampling_thread.cpp:29
QwtSamplingThread::elapsed
double elapsed() const
Definition: qwt_sampling_thread.cpp:62
qwt_sampling_thread.h
QwtSamplingThread::PrivateData::msecsInterval
double msecsInterval
Definition: qwt_sampling_thread.cpp:24
QwtSamplingThread::m_data
PrivateData * m_data
Definition: qwt_sampling_thread.h:65
QwtSamplingThread::PrivateData
Definition: qwt_sampling_thread.cpp:13
QwtSamplingThread::run
virtual void run() QWT_OVERRIDE
Definition: qwt_sampling_thread.cpp:83
QwtSamplingThread::QwtSamplingThread
QwtSamplingThread(QObject *parent=NULL)
Constructor.
Definition: qwt_sampling_thread.cpp:21
QwtSamplingThread::PrivateData::timer
QElapsedTimer timer
Definition: qwt_sampling_thread.cpp:23
QwtSamplingThread::setInterval
void setInterval(double interval)
Definition: qwt_sampling_thread.cpp:41
QwtSamplingThread::interval
double interval() const
Definition: qwt_sampling_thread.cpp:53
QwtSamplingThread::sample
virtual void sample(double elapsed)=0


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:46