first re-commit.
This commit is contained in:
167
swvolumewidget.cpp
Normal file
167
swvolumewidget.cpp
Normal file
@@ -0,0 +1,167 @@
|
||||
/***************************************************************************
|
||||
|
||||
source::worx raDIYo
|
||||
Copyright © 2020-2022 c.holzheuer
|
||||
chris@sourceworx.org
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include <QDebug>
|
||||
#include <QPainter>
|
||||
#include <QMouseEvent>
|
||||
#include <QtGlobal>
|
||||
|
||||
#include <raDIYo.h>
|
||||
#include <swvolumewidget.h>
|
||||
|
||||
|
||||
/**
|
||||
* @brief Standardkonstruktor. Block- und Balkenanzahl auf 32
|
||||
* @see setNumblocks
|
||||
* @see setNumBars
|
||||
*/
|
||||
|
||||
SWVolumeWidget::SWVolumeWidget( QWidget* parent )
|
||||
: SWBarWidget( parent )
|
||||
{
|
||||
setNumBlocks( 32 );
|
||||
setNumBars( 32 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Wird aufgerufen, wenn das (externe) Dial gedreht wurde.
|
||||
*
|
||||
* Hier wird _ein_ Wert gesetzt, zum Beispiel Lautstärke, Bässe,
|
||||
* Höhen etc.
|
||||
*/
|
||||
|
||||
void SWVolumeWidget::onDialDeltaChanged( int delta )
|
||||
{
|
||||
|
||||
// gültigkeit sicherstellen
|
||||
int newval = _value + delta;
|
||||
newval = qBound( _rangeFrom, newval, _rangeTo );
|
||||
|
||||
if( _value == newval )
|
||||
return;
|
||||
|
||||
setValue( newval );
|
||||
update();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Teil der Maussteuerung.
|
||||
* @param event
|
||||
*/
|
||||
|
||||
void SWVolumeWidget::mousePressEvent( QMouseEvent* event )
|
||||
{
|
||||
if( event->button() == Qt::LeftButton )
|
||||
_moving = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Zieht bei gedrücktem Mousebutton die Lautstärke hoch bzw. runter
|
||||
* @param event
|
||||
*/
|
||||
|
||||
void SWVolumeWidget::mouseMoveEvent( QMouseEvent* event )
|
||||
{
|
||||
|
||||
if( ( event->buttons() & Qt::LeftButton ) && _moving )
|
||||
{
|
||||
double newX = (double) _rangeTo * (double) event->pos().x() / ( rect().width() - _frameOffset );
|
||||
int lstVal = _value;
|
||||
_value = qRound( newX );
|
||||
int delta = _value - lstVal;
|
||||
if( !delta )
|
||||
return;
|
||||
update();
|
||||
emit deltaChanged( delta > 0 ? 1 : -1 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Teil der Maussteuerung.
|
||||
* @param event
|
||||
*/
|
||||
|
||||
void SWVolumeWidget::mouseReleaseEvent( QMouseEvent* event )
|
||||
{
|
||||
if( event->button() == Qt::LeftButton && _moving )
|
||||
_moving = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Zeichnet das Balkendiagramm.
|
||||
* @param event
|
||||
*/
|
||||
|
||||
void SWVolumeWidget::paintEvent( QPaintEvent* event )
|
||||
{
|
||||
|
||||
Q_UNUSED( event )
|
||||
|
||||
|
||||
QPainter painter( this );
|
||||
|
||||
// wir malen ein dunkles Rähmchen um eine graue Fläche
|
||||
QPen pen( QColor( 66,66,66 ) );
|
||||
pen.setWidth( 2 );
|
||||
painter.setPen( pen );
|
||||
QRect frame = rect();
|
||||
frame.adjust( _frameOffset, _frameOffset, -_frameOffset, -_frameOffset );
|
||||
painter.fillRect( frame, QColor( 44,44,44 ) );
|
||||
painter.drawRoundedRect( frame, 2, 2 );
|
||||
|
||||
double sizeX = frame.width();
|
||||
double sizeY = frame.height();
|
||||
|
||||
int blockWidth = qRound( sizeX / _numBars );
|
||||
int blockHeight = qRound( sizeY / _numBlocks );
|
||||
|
||||
//_padding = 2;
|
||||
|
||||
int numColors = _colors.size();
|
||||
double factorColors = (double) numColors / (double) _numBlocks;
|
||||
int blockPosX = _frameOffset + _padding;
|
||||
int maxX = qRound( (double) _value / double( _rangeTo ) * sizeX );
|
||||
int maxBlocks = (double) sizeY / blockHeight - 3;
|
||||
|
||||
for( ; blockPosX < maxX; blockPosX += blockWidth )
|
||||
{
|
||||
// hack
|
||||
int numBlocks = qMin( 1 + qRound( (double) blockPosX / sizeX * maxBlocks ), maxBlocks );
|
||||
int blockPosY = sizeY - blockHeight;// + _padding;
|
||||
for( int j = 0; j < numBlocks; ++j )
|
||||
{
|
||||
QRect blockRect = QRect( blockPosX, blockPosY, blockWidth - _padding, blockHeight - _padding );
|
||||
int colorIndex = numColors - 1 - factorColors * (double) j;
|
||||
//int colorIndex = 2 + factorColors * (double) j;
|
||||
painter.fillRect( blockRect, _colors[ colorIndex ] );
|
||||
blockPosY -= blockHeight;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user