Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
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
13class QwtSamplingThread::PrivateData
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
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#include "moc_qwt_sampling_thread.cpp"
virtual void run() override
void setInterval(double interval)
virtual void sample(double elapsed)=0
virtual ~QwtSamplingThread()
Destructor.
QwtSamplingThread(QObject *parent=NULL)
Constructor.