Debian Codegraph @

src/context/applets/analyzer/plugin/BlockWorker.cpp

raw
1 /*
2 * Copyright 2017 Malte Veerman <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License or (at your option) version 3 or any later version
8 * accepted by the membership of KDE e.V. (or its successor approved
9 * by the membership of KDE e.V.), which shall act as a proxy
10 * defined in Section 14 of version 3 of the license.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 #include "BlockWorker.h"
23 #include "BlockAnalyzer.h"
24
25 #include "core/support/Debug.h"
26
27
28 BlockWorker::BlockWorker( int rows, int columns, qreal step, bool showFadebars )
29 : m_step( step )
30 , m_rows( rows )
31 , m_columns( columns )
32 , m_refreshTime( 16 )
33 , m_showFadebars( showFadebars )
34 {
35 m_yscale.resize( m_rows );
36 const double PRO = 1; //PRO allows us to restrict the range somewhat
37
38 for( int z = 0; z < m_rows; ++z )
39 m_yscale[z] = 1 - log10( m_rows - z ) / log10( m_rows + PRO );
40
41 m_store.resize( columns );
42 m_fadebars.resize( columns );
43
44 m_lastUpdate.start();
45 }
46
47 void BlockWorker::setRows( int rows )
48 {
49 if( m_rows == rows )
50 return;
51
52 m_mutex.lock();
53 m_rows = rows;
54 m_yscale.resize( m_rows + 1 );
55
56 const double PRO = 1; //PRO allows us to restrict the range somewhat
57
58 for( int z = 0; z < m_rows; ++z )
59 m_yscale[z] = 1 - log10( m_rows - z ) / log10( m_rows + PRO );
60
61 m_mutex.unlock();
62 }
63
64 void BlockWorker::setColumns( int columns )
65 {
66 if( m_columns == columns )
67 return;
68
69 m_columns = columns;
70 }
71
72 void BlockWorker::analyze()
73 {
74 int timeElapsed = m_lastUpdate.elapsed();
75
76 // only analyze if screen is fast enough
77 if( timeElapsed < m_refreshTime - 1 )
78 QThread::currentThread()->msleep( m_refreshTime - timeElapsed - 1 );
79
80 const auto scopeData = scope();
81 const int scopeSize = scopeData.size();
82
83 timeElapsed = m_lastUpdate.restart();
84
85 const qreal step = m_step * timeElapsed / 1000.0;
86 const qreal fadeStep = (qreal)timeElapsed / 20.0;
87
88 // lock m_store and m_fadebars
89 QMutexLocker locker( &m_mutex );
90
91 m_store.resize( scopeSize );
92 m_fadebars.resize( scopeSize );
93
94 for( int x = 0; x < scopeSize; ++x )
95 {
96 // determine y
97 int y = 0;
98 const int yscaleSize=m_yscale.size();
99 while( y < yscaleSize && scopeData.at(x) > m_yscale.at(y) )
100 y++;
101
102 auto &fadebars = m_fadebars[x];
103 auto &store = m_store[x];
104
105 // remove obscured fadebars
106 while( !fadebars.isEmpty() && fadebars.last().y <= y )
107 fadebars.removeLast();
108
109 // remove completely faded fadebars
110 while( !fadebars.isEmpty() && fadebars.first().intensity <= fadeStep )
111 fadebars.removeFirst();
112
113 // fade the rest
114 for( auto &fadebar : fadebars )
115 fadebar.intensity -= fadeStep;
116
117 if( ( double )y < store )
118 {
119 // add new fadebar at old column height
120 if( m_showFadebars )
121 fadebars << Fadebar( store, BlockAnalyzer::FADE_SIZE );
122
123 store = qMax( store - step, double( y ) );
124 }
125 else
126 store = y;
127 }
128
129 Q_EMIT finished();
130 }