1 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
---|
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 "qwt_system_clock.h"
|
---|
12 |
|
---|
13 | class QwtSamplingThread::PrivateData
|
---|
14 | {
|
---|
15 | public:
|
---|
16 | QwtSystemClock clock;
|
---|
17 |
|
---|
18 | double interval;
|
---|
19 | bool isStopped;
|
---|
20 | };
|
---|
21 |
|
---|
22 |
|
---|
23 | //! Constructor
|
---|
24 | QwtSamplingThread::QwtSamplingThread( QObject *parent ):
|
---|
25 | QThread( parent )
|
---|
26 | {
|
---|
27 | d_data = new PrivateData;
|
---|
28 | d_data->interval = 1000; // 1 second
|
---|
29 | d_data->isStopped = true;
|
---|
30 | }
|
---|
31 |
|
---|
32 | //! Destructor
|
---|
33 | QwtSamplingThread::~QwtSamplingThread()
|
---|
34 | {
|
---|
35 | delete d_data;
|
---|
36 | }
|
---|
37 |
|
---|
38 | /*!
|
---|
39 | Change the interval (in ms), when sample() is called.
|
---|
40 | The default interval is 1000.0 ( = 1s )
|
---|
41 |
|
---|
42 | \param interval Interval
|
---|
43 | \sa interval()
|
---|
44 | */
|
---|
45 | void QwtSamplingThread::setInterval( double interval )
|
---|
46 | {
|
---|
47 | if ( interval < 0.0 )
|
---|
48 | interval = 0.0;
|
---|
49 |
|
---|
50 | d_data->interval = interval;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /*!
|
---|
54 | \return Interval (in ms), between 2 calls of sample()
|
---|
55 | \sa setInterval()
|
---|
56 | */
|
---|
57 | double QwtSamplingThread::interval() const
|
---|
58 | {
|
---|
59 | return d_data->interval;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /*!
|
---|
63 | \return Time (in ms) since the thread was started
|
---|
64 | \sa QThread::start(), run()
|
---|
65 | */
|
---|
66 | double QwtSamplingThread::elapsed() const
|
---|
67 | {
|
---|
68 | if ( d_data->isStopped )
|
---|
69 | return 0.0;
|
---|
70 |
|
---|
71 | return d_data->clock.elapsed();
|
---|
72 | }
|
---|
73 |
|
---|
74 | /*!
|
---|
75 | Terminate the collecting thread
|
---|
76 | \sa QThread::start(), run()
|
---|
77 | */
|
---|
78 | void QwtSamplingThread::stop()
|
---|
79 | {
|
---|
80 | d_data->isStopped = true;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /*!
|
---|
84 | Loop collecting samples started from QThread::start()
|
---|
85 | \sa stop()
|
---|
86 | */
|
---|
87 | void QwtSamplingThread::run()
|
---|
88 | {
|
---|
89 | d_data->clock.start();
|
---|
90 | d_data->isStopped = false;
|
---|
91 |
|
---|
92 | while ( !d_data->isStopped )
|
---|
93 | {
|
---|
94 | const double elapsed = d_data->clock.elapsed();
|
---|
95 | sample( elapsed / 1000.0 );
|
---|
96 |
|
---|
97 | if ( d_data->interval > 0.0 )
|
---|
98 | {
|
---|
99 | const double msecs =
|
---|
100 | d_data->interval - ( d_data->clock.elapsed() - elapsed );
|
---|
101 |
|
---|
102 | if ( msecs > 0.0 )
|
---|
103 | usleep( qRound( 1000.0 * msecs ) );
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|