raDIYo 0.4
swspectrumwidget.cpp
1/***************************************************************************
2
3 source::worx raDIYo
4 Copyright © 2020-2022 c.holzheuer
5 c.holzheuer@sourceworx.org
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12***************************************************************************/
13
14
15#include <QDebug>
16#include <QPainter>
17#include <QRandomGenerator>
18#include <swspectrumwidget.h>
19
20
21SWSpectrumWidget::SWSpectrumWidget( QWidget* parent )
22 : SWAbstractBarWidget( parent )
23{
24
25 setNumBarsBlocks( 16, 16 );
26 _valueList.resize( _numBars );
27
28
29 //connect( &_timer, &QTimer::timeout, this, &SWSpectrumWidget::onTimer );
30 //_timer.start( 100 );
31
32
33}
34
35void SWSpectrumWidget::onTimer()
36{
37 //qDebug() << "onTimer()";
38 /*
39 for( int i=0; i < _numBars; ++i )
40 {
41 double xx = (double) QRandomGenerator::global()->bounded( -7, +7 );
42 _valueList[i] = 0.7 + xx/10;
43 }
44 */
45 //update();
46}
47
48
49void SWSpectrumWidget::paintEvent( QPaintEvent* event )
50{
51
52 Q_UNUSED( event )
53 Q_ASSERT( _colors.size() >= _maxNumBarsBlocks );
54
55 QPainter painter( this );
56 painter.setBackgroundMode( Qt::TransparentMode );
57 //painter.fillRect(rect(), Qt::darkGray);
58
59 // Draw the bars
60
61 // der Rand wird im Parentwidget 'beschlossen'
62 int frameWidth = rect().width();
63 int frameHeight = rect().height();
64 int blockHeight = frameHeight / _numBlocks;
65 //int blockWidth = qRound( double( frameWidth ) / double( _numBars ) );
66 int blockWidth = frameWidth / _numBars;
67
68 int blockPosX = 0;
69
70 for( int x=0; x<_numBars; ++x )
71 {
72 //qDebug() << "mookoo:" << _numBars << ": " << _volume << ": " << _valueList[i] << " : " << blockWidth;
73
74 int barHeight = qRound( _valueList[x] * frameHeight );
75 int blocks = barHeight / blockHeight;
76 int blockPosY = frameHeight - blocks * blockHeight;
77
78 //qDebug() << "aha: " << i << ": " << _valueList[i] << ":" << blocks << " posY: " << blockPosY;
79
80 for( int y = 0; y < blocks; ++y )
81 {
82 QRect block = QRect( blockPosX, blockPosY, blockWidth - _padding, blockHeight - _padding );
83 painter.fillRect( block, getBlockColor( x, y ) );
84
85 blockPosY += blockHeight;
86 }
87
88 blockPosX += blockWidth;
89
90 }
91
92}
93
94
95
96