Changeset View
Changeset View
Standalone View
Standalone View
src/context/applets/analyzer/plugin/BlockWorker.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2017 Malte Veerman <malte.veerman@gmail.com> | ||||
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 + 1 ); | ||||
36 | const double PRE = 1, PRO = 1; //PRE and PRO allow us to restrict the range somewhat | ||||
37 | | ||||
38 | for( int z = 0; z < m_rows; ++z ) | ||||
39 | m_yscale[z] = 1 - ( log10( PRE + z ) / log10( PRE + m_rows + PRO ) ); | ||||
40 | | ||||
41 | m_yscale[m_rows] = 0; | ||||
42 | | ||||
43 | m_store.resize( columns ); | ||||
44 | m_fadebars.resize( columns ); | ||||
45 | | ||||
46 | m_lastUpdate.start(); | ||||
47 | } | ||||
48 | | ||||
49 | void BlockWorker::setRows( int rows ) | ||||
50 | { | ||||
51 | if( m_rows == rows ) | ||||
52 | return; | ||||
53 | | ||||
54 | m_mutex.lock(); | ||||
55 | m_rows = rows; | ||||
56 | m_yscale.resize( m_rows + 1 ); | ||||
57 | | ||||
58 | const double PRE = 1, PRO = 1; //PRE and PRO allow us to restrict the range somewhat | ||||
59 | | ||||
60 | for( int z = 0; z < m_rows; ++z ) | ||||
61 | m_yscale[z] = 1 - ( log10( PRE + z ) / log10( PRE + m_rows + PRO ) ); | ||||
62 | | ||||
63 | m_yscale[m_rows] = 0; | ||||
64 | m_mutex.unlock(); | ||||
65 | } | ||||
66 | | ||||
67 | void BlockWorker::setColumns(int columns) | ||||
68 | { | ||||
69 | if( m_columns == columns ) | ||||
70 | return; | ||||
71 | | ||||
72 | m_columns = columns; | ||||
73 | } | ||||
74 | | ||||
75 | void BlockWorker::analyze() | ||||
76 | { | ||||
77 | int timeElapsed = m_lastUpdate.elapsed(); | ||||
78 | | ||||
79 | // only analyze if screen is fast enough | ||||
80 | if( timeElapsed < m_refreshTime ) | ||||
81 | QThread::currentThread()->msleep( m_refreshTime - timeElapsed - 1 ); | ||||
82 | | ||||
83 | const auto scopeData = scope(); | ||||
84 | const int scopeSize = scopeData.size(); | ||||
85 | | ||||
86 | timeElapsed = m_lastUpdate.restart(); | ||||
87 | | ||||
88 | const qreal step = m_step * timeElapsed / 1000.0; | ||||
89 | const qreal fadeStep = (qreal)timeElapsed / 20.0; | ||||
90 | | ||||
91 | // block m_store and m_fadebars | ||||
92 | QMutexLocker locker(&m_mutex); | ||||
93 | | ||||
94 | m_store.resize( scopeSize ); | ||||
95 | m_fadebars.resize( scopeSize ); | ||||
96 | | ||||
97 | for( int x = 0; x < scopeSize; ++x ) | ||||
98 | { | ||||
99 | // determine y | ||||
100 | int y = 0; | ||||
101 | while( y < m_yscale.size() && scopeData.at(x) < m_yscale.at(y) ) | ||||
102 | y++; | ||||
103 | | ||||
104 | auto &fadebars = m_fadebars[x]; | ||||
105 | auto &store = m_store[x]; | ||||
106 | | ||||
107 | // remove obscured fadebars | ||||
108 | while( !fadebars.isEmpty() && fadebars.last().y >= y ) | ||||
109 | fadebars.removeLast(); | ||||
110 | | ||||
111 | // remove completely faded fadebars | ||||
112 | while( !fadebars.isEmpty() && fadebars.first().intensity <= fadeStep ) | ||||
113 | fadebars.removeFirst(); | ||||
114 | | ||||
115 | // fade the rest | ||||
116 | for( auto &fadebar : fadebars ) | ||||
117 | fadebar.intensity -= fadeStep; | ||||
118 | | ||||
119 | if( ( double )y > store ) | ||||
120 | { | ||||
121 | // add new fadebar at old column height | ||||
122 | if( m_showFadebars ) | ||||
123 | fadebars << Fadebar( store, BlockAnalyzer::FADE_SIZE ); | ||||
124 | | ||||
125 | store = qMin( store + step, double( y ) ); | ||||
126 | } | ||||
127 | else | ||||
128 | store = y; | ||||
129 | } | ||||
130 | | ||||
131 | emit finished(); | ||||
132 | } |