Qwt User's Guide: qwt_sampling_thread.cpp Source File

Qwt User's Guide 6.2.0

qwt_sampling_thread.cpp
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
13 class QwtSamplingThread::PrivateData
14 {
15 public:
16 QElapsedTimer timer;
17 double msecsInterval;
18 };
19
21 QwtSamplingThread::QwtSamplingThread( QObject* parent )
22 : QThread( parent )
23 {
24 m_data = new PrivateData;
25 m_data->msecsInterval = 1e3; // 1 second
26 }
27
29 QwtSamplingThread::~QwtSamplingThread()
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
53 double QwtSamplingThread::interval() const
54 {
55 return m_data->msecsInterval;
56 }
57
62 double QwtSamplingThread::elapsed() const
63 {
64 if ( m_data->timer.isValid() )
65 return m_data->timer.nsecsElapsed() / 1e6;
66
67 return 0.0;
68 }
69
74 void QwtSamplingThread::stop()
75 {
76 m_data->timer.invalidate();
77 }
78
83 void QwtSamplingThread::run()
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::run
virtual void run() override
Definition: qwt_sampling_thread.cpp:83
QwtSamplingThread::elapsed
double elapsed() const
Definition: qwt_sampling_thread.cpp:62
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
QwtSamplingThread::~QwtSamplingThread
virtual ~QwtSamplingThread()
Destructor.
Definition: qwt_sampling_thread.cpp:29
QwtSamplingThread::stop
void stop()
Definition: qwt_sampling_thread.cpp:74
QwtSamplingThread::QwtSamplingThread
QwtSamplingThread(QObject *parent=NULL)
Constructor.
Definition: qwt_sampling_thread.cpp:21