File indexing completed on 2025-01-05 04:25:41

0001 /*
0002  * Copyright 2017  Malte Veerman <malte.veerman@gmail.com>
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU General Public License as
0006  * published by the Free Software Foundation; either version 2 of
0007  * the License or (at your option) version 3 or any later version
0008  * accepted by the membership of KDE e.V. (or its successor approved
0009  * by the membership of KDE e.V.), which shall act as a proxy
0010  * defined in Section 14 of version 3 of the license.
0011  *
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU General Public License
0018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0019  *
0020  */
0021 
0022 #include "BlockWorker.h"
0023 #include "BlockAnalyzer.h"
0024 
0025 #include "core/support/Debug.h"
0026 
0027 
0028 BlockWorker::BlockWorker( int rows, int columns, qreal step, bool showFadebars )
0029     : m_step( step )
0030     , m_rows( rows )
0031     , m_columns( columns )
0032     , m_refreshTime( 16 )
0033     , m_showFadebars( showFadebars )
0034 {
0035     m_yscale.resize( m_rows );
0036     const double PRO = 1; //PRO allows us to restrict the range somewhat
0037 
0038     for( int z = 0; z < m_rows; ++z )
0039         m_yscale[z] = 1 - log10( m_rows - z ) / log10( m_rows + PRO );
0040 
0041     m_store.resize( columns );
0042     m_fadebars.resize( columns );
0043 
0044     m_lastUpdate.start();
0045 }
0046 
0047 void BlockWorker::setRows( int rows )
0048 {
0049     if( m_rows == rows )
0050         return;
0051 
0052     m_mutex.lock();
0053     m_rows = rows;
0054     m_yscale.resize( m_rows + 1 );
0055 
0056     const double PRO = 1; //PRO allows us to restrict the range somewhat
0057 
0058     for( int z = 0; z < m_rows; ++z )
0059         m_yscale[z] = 1 - log10( m_rows - z ) / log10( m_rows + PRO );
0060 
0061     m_mutex.unlock();
0062 }
0063 
0064 void BlockWorker::setColumns( int columns )
0065 {
0066     if( m_columns == columns )
0067         return;
0068 
0069     m_columns = columns;
0070 }
0071 
0072 void BlockWorker::analyze()
0073 {
0074     int timeElapsed = m_lastUpdate.elapsed();
0075 
0076     // only analyze if screen is fast enough
0077     if( timeElapsed < m_refreshTime - 1 )
0078         QThread::currentThread()->msleep( m_refreshTime - timeElapsed - 1 );
0079 
0080     const auto scopeData = scope();
0081     const int scopeSize = scopeData.size();
0082 
0083     timeElapsed = m_lastUpdate.restart();
0084 
0085     const qreal step = m_step * timeElapsed / 1000.0;
0086     const qreal fadeStep = (qreal)timeElapsed / 20.0;
0087 
0088     // lock m_store and m_fadebars
0089     QMutexLocker locker( &m_mutex );
0090 
0091     m_store.resize( scopeSize );
0092     m_fadebars.resize( scopeSize );
0093 
0094     for( int x = 0; x < scopeSize; ++x )
0095     {
0096         // determine y
0097         int y = 0;
0098         while( y < m_yscale.size() && scopeData.at(x) > m_yscale.at(y) )
0099             y++;
0100 
0101         auto &fadebars = m_fadebars[x];
0102         auto &store = m_store[x];
0103 
0104         // remove obscured fadebars
0105         while( !fadebars.isEmpty() && fadebars.last().y <= y )
0106             fadebars.removeLast();
0107 
0108         // remove completely faded fadebars
0109         while( !fadebars.isEmpty() && fadebars.first().intensity <= fadeStep )
0110             fadebars.removeFirst();
0111 
0112         // fade the rest
0113         for( auto &fadebar : fadebars )
0114             fadebar.intensity -= fadeStep;
0115 
0116         if( ( double )y < store )
0117         {
0118             // add new fadebar at old column height
0119             if( m_showFadebars )
0120                 fadebars << Fadebar( store, BlockAnalyzer::FADE_SIZE );
0121 
0122             store = qMax( store - step, double( y ) );
0123         }
0124         else
0125             store = y;
0126     }
0127 
0128     Q_EMIT finished();
0129 }