raDIYo 0.4
swvolumewidget.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 <QMouseEvent>
18#include <QtGlobal>
19
20#include <raDIYo.h>
21#include <swvolumewidget.h>
22
23
24
25SWVolumeWidget::SWVolumeWidget( QWidget* parent )
26 : SWAbstractBarWidget( parent )
27{
28
29
30 setNumBarsBlocks( 32, 32 );
31
32 _valueList.resize( _numBars );
33 double dNb = double( _numBars );
34 double dSt = ( 1.0 / dNb ) / dNb;
35 for( int i=0; i < _numBars; ++i )
36 _valueList[i] = double( i ) / dNb + i * dSt + 0.03;
37
38 _value = 0.2;
39
40}
41
42
43SWVolumeWidget::~SWVolumeWidget()
44{
45
46}
47
48
49void SWVolumeWidget::mousePressEvent( QMouseEvent* event )
50{
51 if( event->button() == Qt::LeftButton )
52 {
53 _lastPosX = event->pos().x();
54 _moving = true;
55 // move to ...
56 }
57}
58
59
60void SWVolumeWidget::mouseMoveEvent( QMouseEvent* event )
61{
62 if( ( event->buttons() & Qt::LeftButton ) && _moving )
63 {
64
65 double newvalue = 1.0 / (double) _rangeTo * event->pos().x();
66
67 _value = qBound( 0.0, newvalue, 1.0 );
68 int intvalue = 100 * _value;
69 //int intvalue = _rangeTo * _value;
70 //qDebug() << "volume: " << _value << " int "<< intvalue << " range: " << _rangeTo;
71 //onDialValueChanged( _value );
72 update();
73 emit valueChanged( intvalue );
74 }
75
76}
77
78void SWVolumeWidget::mouseReleaseEvent( QMouseEvent* event )
79{
80
81 if( event->button() == Qt::LeftButton && _moving )
82 _moving = false;
83
84}
85
86
87void SWVolumeWidget::paintEvent( QPaintEvent* event )
88{
89
90 Q_UNUSED( event )
91 //Q_ASSERT( _colors.size() >= _numBlocks );
92 setRange( 0, width() );
93
94 QPainter painter( this );
95 //painter.setBackgroundMode( Qt::TransparentMode );
96 //painter.fillRect(rect(), QColor( 44,44,44 ) );
97
98 QPen pen( Qt::gray );
99 pen.setWidth( _frameOffset );
100 //pen.setBrush( QColor( 44,44,44 ) );
101 painter.setPen( pen );
102 QRect frame = rect();
103 frame.adjust( 0, 0, -_frameOffset, -_frameOffset );
104 painter.fillRect( frame, QColor( 44,44,44 ) );
105 painter.drawRoundedRect( frame, 10, 10 );
106
107 // Draw the bars
108
109 // der Rand wird im Parentwidget 'beschlossen'
110 int frameWidth = rect().width();
111 int frameHeight = rect().height();
112 //int frameWidth = frame.width();
113 //int frameHeight = frame.height();
114
115 int blockHeight = frameHeight / _numBlocks;
116 int blockWidth = frameWidth / _numBars;
117
118 int blockPosX = 0;
119 int numColors = _colors.size();
120 double factorBars = (double) numColors / (double) _numBars;
121
122
123 for( int i=0; i<_numBars; ++i )
124 {
125
126 if( _value < _valueList[i] )
127 return;
128
129 int barHeight = qRound( _valueList[i] * frameHeight ) - 4 * _frameOffset;
130 int blocks = barHeight / blockHeight;
131 int blockPosY = frameHeight - blocks * blockHeight - 2 * _frameOffset;
132 double factorBlocks= (double) _colors.size() / (double) blocks;
133
134 for( int j = 0; j < blocks; ++j )
135 {
136 QRect block = QRect( blockPosX, blockPosY, blockWidth - _padding, blockHeight - _padding );
137 int colorIndex = _gradientMode ? numColors - 1 - factorBlocks * (double) j : qRound( factorBars * (double) i ) ;
138 painter.fillRect( block, _colors[ colorIndex ] );
139
140 blockPosY += blockHeight;
141 }
142
143 blockPosX += blockWidth;
144
145 }
146
147
148
149}
150
151
void setRange(int rangeFrom, int rangeTo)
SWAbstractBarWidget::setRange.