[libcalamaresui] Apply coding style

This commit is contained in:
Adriaan de Groot 2022-04-16 12:39:21 +02:00
parent 7e5df42fc0
commit 18fef8dfe5
2 changed files with 243 additions and 162 deletions

View File

@ -32,31 +32,32 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "waitingspinnerwidget.h" #include "waitingspinnerwidget.h"
// Standard includes // Standard includes
#include <cmath>
#include <algorithm> #include <algorithm>
#include <cmath>
// Qt includes // Qt includes
#include <QPainter> #include <QPainter>
#include <QTimer> #include <QTimer>
static bool isAlignCenter(Qt::AlignmentFlag a) static bool
isAlignCenter( Qt::AlignmentFlag a )
{ {
return a == Qt::AlignmentFlag::AlignVCenter; return a == Qt::AlignmentFlag::AlignVCenter;
} }
WaitingSpinnerWidget::WaitingSpinnerWidget(QWidget *parent, WaitingSpinnerWidget::WaitingSpinnerWidget( QWidget* parent, bool centerOnParent, bool disableParentWhenSpinning )
bool centerOnParent,
bool disableParentWhenSpinning)
: WaitingSpinnerWidget( Qt::WindowModality::NonModal, parent, centerOnParent, disableParentWhenSpinning ) : WaitingSpinnerWidget( Qt::WindowModality::NonModal, parent, centerOnParent, disableParentWhenSpinning )
{} {
}
WaitingSpinnerWidget::WaitingSpinnerWidget( Qt::WindowModality modality, WaitingSpinnerWidget::WaitingSpinnerWidget( Qt::WindowModality modality,
QWidget* parent, QWidget* parent,
bool centerOnParent, bool centerOnParent,
bool disableParentWhenSpinning ) bool disableParentWhenSpinning )
: QWidget(parent, modality == Qt::WindowModality::NonModal ? Qt::WindowFlags() : Qt::Dialog | Qt::FramelessWindowHint), : QWidget( parent,
_centerOnParent(centerOnParent), modality == Qt::WindowModality::NonModal ? Qt::WindowFlags() : Qt::Dialog | Qt::FramelessWindowHint )
_disableParentWhenSpinning(disableParentWhenSpinning) , _centerOnParent( centerOnParent )
, _disableParentWhenSpinning( disableParentWhenSpinning )
{ {
_timer = new QTimer( this ); _timer = new QTimer( this );
connect( _timer, SIGNAL( timeout() ), this, SLOT( rotate() ) ); connect( _timer, SIGNAL( timeout() ), this, SLOT( rotate() ) );
@ -76,237 +77,315 @@ WaitingSpinnerWidget::WaitingSpinnerWidget(Qt::WindowModality modality,
} }
} }
void WaitingSpinnerWidget::paintEvent(QPaintEvent *) { void
WaitingSpinnerWidget::paintEvent( QPaintEvent* )
{
updatePosition(); updatePosition();
QPainter painter( this ); QPainter painter( this );
painter.fillRect( this->rect(), Qt::transparent ); painter.fillRect( this->rect(), Qt::transparent );
painter.setRenderHint( QPainter::Antialiasing, true ); painter.setRenderHint( QPainter::Antialiasing, true );
if (_currentCounter >= _numberOfLines) { if ( _currentCounter >= _numberOfLines )
{
_currentCounter = 0; _currentCounter = 0;
} }
painter.setPen( Qt::NoPen ); painter.setPen( Qt::NoPen );
for (int i = 0; i < _numberOfLines; ++i) { for ( int i = 0; i < _numberOfLines; ++i )
{
painter.save(); painter.save();
painter.translate(_innerRadius + _lineLength, painter.translate( _innerRadius + _lineLength, _innerRadius + _lineLength );
_innerRadius + _lineLength);
painter.translate( ( width() - _imageSize.width() ) / 2, 0 ); painter.translate( ( width() - _imageSize.width() ) / 2, 0 );
qreal rotateAngle = qreal rotateAngle = static_cast< qreal >( 360 * i ) / static_cast< qreal >( _numberOfLines );
static_cast<qreal>(360 * i) / static_cast<qreal>(_numberOfLines);
painter.rotate( rotateAngle ); painter.rotate( rotateAngle );
painter.translate( _innerRadius, 0 ); painter.translate( _innerRadius, 0 );
int distance = int distance = lineCountDistanceFromPrimary( i, _currentCounter, _numberOfLines );
lineCountDistanceFromPrimary(i, _currentCounter, _numberOfLines); QColor color = currentLineColor( distance, _numberOfLines, _trailFadePercentage, _minimumTrailOpacity, _color );
QColor color =
currentLineColor(distance, _numberOfLines, _trailFadePercentage,
_minimumTrailOpacity, _color);
painter.setBrush( color ); painter.setBrush( color );
// TODO improve the way rounded rect is painted // TODO improve the way rounded rect is painted
painter.drawRoundedRect( painter.drawRoundedRect(
QRect(0, -_lineWidth / 2, _lineLength, _lineWidth), _roundness, QRect( 0, -_lineWidth / 2, _lineLength, _lineWidth ), _roundness, _roundness, Qt::RelativeSize );
_roundness, Qt::RelativeSize);
painter.restore(); painter.restore();
} }
if (!_text.isEmpty()) { if ( !_text.isEmpty() )
{
painter.setPen( QPen( _textColor ) ); painter.setPen( QPen( _textColor ) );
if (isAlignCenter(alignment())) { if ( isAlignCenter( alignment() ) )
painter.drawText(QRect(0, 0, width(), height()), {
Qt::AlignVCenter | Qt::AlignHCenter, _text); painter.drawText( QRect( 0, 0, width(), height() ), Qt::AlignVCenter | Qt::AlignHCenter, _text );
} else { }
else
{
painter.drawText( QRect( 0, _imageSize.height(), width(), height() - _imageSize.height() ), painter.drawText( QRect( 0, _imageSize.height(), width(), height() - _imageSize.height() ),
Qt::AlignBottom | Qt::AlignHCenter, _text); Qt::AlignBottom | Qt::AlignHCenter,
_text );
} }
} }
} }
void WaitingSpinnerWidget::start() { void
WaitingSpinnerWidget::start()
{
updatePosition(); updatePosition();
_isSpinning = true; _isSpinning = true;
show(); show();
if(parentWidget() && _disableParentWhenSpinning) { if ( parentWidget() && _disableParentWhenSpinning )
{
parentWidget()->setEnabled( false ); parentWidget()->setEnabled( false );
} }
if (!_timer->isActive()) { if ( !_timer->isActive() )
{
_timer->start(); _timer->start();
_currentCounter = 0; _currentCounter = 0;
} }
} }
void WaitingSpinnerWidget::stop() { void
WaitingSpinnerWidget::stop()
{
_isSpinning = false; _isSpinning = false;
hide(); hide();
if(parentWidget() && _disableParentWhenSpinning) { if ( parentWidget() && _disableParentWhenSpinning )
{
parentWidget()->setEnabled( true ); parentWidget()->setEnabled( true );
} }
if (_timer->isActive()) { if ( _timer->isActive() )
{
_timer->stop(); _timer->stop();
_currentCounter = 0; _currentCounter = 0;
} }
} }
void WaitingSpinnerWidget::setNumberOfLines(int lines) { void
WaitingSpinnerWidget::setNumberOfLines( int lines )
{
_numberOfLines = lines; _numberOfLines = lines;
_currentCounter = 0; _currentCounter = 0;
updateTimer(); updateTimer();
} }
void WaitingSpinnerWidget::setLineLength(int length) { void
WaitingSpinnerWidget::setLineLength( int length )
{
_lineLength = length; _lineLength = length;
updateSize(); updateSize();
} }
void WaitingSpinnerWidget::setLineWidth(int width) { void
WaitingSpinnerWidget::setLineWidth( int width )
{
_lineWidth = width; _lineWidth = width;
updateSize(); updateSize();
} }
void WaitingSpinnerWidget::setInnerRadius(int radius) { void
WaitingSpinnerWidget::setInnerRadius( int radius )
{
_innerRadius = radius; _innerRadius = radius;
updateSize(); updateSize();
} }
void WaitingSpinnerWidget::setText(const QString& text) { void
WaitingSpinnerWidget::setText( const QString& text )
{
_text = text; _text = text;
updateSize(); updateSize();
} }
void WaitingSpinnerWidget::setAlignment(Qt::AlignmentFlag align) void
WaitingSpinnerWidget::setAlignment( Qt::AlignmentFlag align )
{ {
_alignment = align; _alignment = align;
updateSize(); updateSize();
} }
QColor WaitingSpinnerWidget::color() const { QColor
WaitingSpinnerWidget::color() const
{
return _color; return _color;
} }
QColor WaitingSpinnerWidget::textColor() const { QColor
WaitingSpinnerWidget::textColor() const
{
return _textColor; return _textColor;
} }
QString WaitingSpinnerWidget::text() const { QString
WaitingSpinnerWidget::text() const
{
return _text; return _text;
} }
qreal WaitingSpinnerWidget::roundness() const { qreal
WaitingSpinnerWidget::roundness() const
{
return _roundness; return _roundness;
} }
qreal WaitingSpinnerWidget::minimumTrailOpacity() const { qreal
WaitingSpinnerWidget::minimumTrailOpacity() const
{
return _minimumTrailOpacity; return _minimumTrailOpacity;
} }
qreal WaitingSpinnerWidget::trailFadePercentage() const { qreal
WaitingSpinnerWidget::trailFadePercentage() const
{
return _trailFadePercentage; return _trailFadePercentage;
} }
qreal WaitingSpinnerWidget::revolutionsPersSecond() const { qreal
WaitingSpinnerWidget::revolutionsPersSecond() const
{
return _revolutionsPerSecond; return _revolutionsPerSecond;
} }
int WaitingSpinnerWidget::numberOfLines() const { int
WaitingSpinnerWidget::numberOfLines() const
{
return _numberOfLines; return _numberOfLines;
} }
int WaitingSpinnerWidget::lineLength() const { int
WaitingSpinnerWidget::lineLength() const
{
return _lineLength; return _lineLength;
} }
int WaitingSpinnerWidget::lineWidth() const { int
WaitingSpinnerWidget::lineWidth() const
{
return _lineWidth; return _lineWidth;
} }
int WaitingSpinnerWidget::innerRadius() const { int
WaitingSpinnerWidget::innerRadius() const
{
return _innerRadius; return _innerRadius;
} }
bool WaitingSpinnerWidget::isSpinning() const { bool
WaitingSpinnerWidget::isSpinning() const
{
return _isSpinning; return _isSpinning;
} }
void WaitingSpinnerWidget::setRoundness(qreal roundness) { void
WaitingSpinnerWidget::setRoundness( qreal roundness )
{
_roundness = std::max( 0.0, std::min( 100.0, roundness ) ); _roundness = std::max( 0.0, std::min( 100.0, roundness ) );
} }
void WaitingSpinnerWidget::setColor(QColor color) { void
WaitingSpinnerWidget::setColor( QColor color )
{
_color = color; _color = color;
} }
void WaitingSpinnerWidget::setTextColor(QColor color) { void
WaitingSpinnerWidget::setTextColor( QColor color )
{
_textColor = color; _textColor = color;
} }
void WaitingSpinnerWidget::setRevolutionsPerSecond(qreal revolutionsPerSecond) { void
WaitingSpinnerWidget::setRevolutionsPerSecond( qreal revolutionsPerSecond )
{
_revolutionsPerSecond = revolutionsPerSecond; _revolutionsPerSecond = revolutionsPerSecond;
updateTimer(); updateTimer();
} }
void WaitingSpinnerWidget::setTrailFadePercentage(qreal trail) { void
WaitingSpinnerWidget::setTrailFadePercentage( qreal trail )
{
_trailFadePercentage = trail; _trailFadePercentage = trail;
} }
void WaitingSpinnerWidget::setMinimumTrailOpacity(qreal minimumTrailOpacity) { void
WaitingSpinnerWidget::setMinimumTrailOpacity( qreal minimumTrailOpacity )
{
_minimumTrailOpacity = minimumTrailOpacity; _minimumTrailOpacity = minimumTrailOpacity;
} }
void WaitingSpinnerWidget::rotate() { void
WaitingSpinnerWidget::rotate()
{
++_currentCounter; ++_currentCounter;
if (_currentCounter >= _numberOfLines) { if ( _currentCounter >= _numberOfLines )
{
_currentCounter = 0; _currentCounter = 0;
} }
update(); update();
} }
void WaitingSpinnerWidget::updateSize() { void
WaitingSpinnerWidget::updateSize()
{
int size = ( _innerRadius + _lineLength ) * 2; int size = ( _innerRadius + _lineLength ) * 2;
_imageSize = QSize( size, size ); _imageSize = QSize( size, size );
if (_text.isEmpty() || isAlignCenter(alignment())) { if ( _text.isEmpty() || isAlignCenter( alignment() ) )
{
setFixedSize( size, size ); setFixedSize( size, size );
} else { }
else
{
QFontMetrics fm( font() ); QFontMetrics fm( font() );
QSize textSize = QSize( fm.width( _text ), fm.height() ); QSize textSize = QSize( fm.width( _text ), fm.height() );
setFixedSize( std::max( size, textSize.width() ), size + size / 4 + textSize.height() ); setFixedSize( std::max( size, textSize.width() ), size + size / 4 + textSize.height() );
} }
} }
void WaitingSpinnerWidget::updateTimer() { void
WaitingSpinnerWidget::updateTimer()
{
_timer->setInterval( 1000 / ( _numberOfLines * _revolutionsPerSecond ) ); _timer->setInterval( 1000 / ( _numberOfLines * _revolutionsPerSecond ) );
} }
void WaitingSpinnerWidget::updatePosition() { void
if (parentWidget() && _centerOnParent) { WaitingSpinnerWidget::updatePosition()
move(parentWidget()->width() / 2 - width() / 2, {
parentWidget()->height() / 2 - height() / 2); if ( parentWidget() && _centerOnParent )
{
move( parentWidget()->width() / 2 - width() / 2, parentWidget()->height() / 2 - height() / 2 );
} }
} }
int WaitingSpinnerWidget::lineCountDistanceFromPrimary(int current, int primary, int
int totalNrOfLines) { WaitingSpinnerWidget::lineCountDistanceFromPrimary( int current, int primary, int totalNrOfLines )
{
int distance = primary - current; int distance = primary - current;
if (distance < 0) { if ( distance < 0 )
{
distance += totalNrOfLines; distance += totalNrOfLines;
} }
return distance; return distance;
} }
QColor WaitingSpinnerWidget::currentLineColor(int countDistance, int totalNrOfLines, QColor
qreal trailFadePerc, qreal minOpacity, WaitingSpinnerWidget::currentLineColor( int countDistance,
QColor color) { int totalNrOfLines,
if (countDistance == 0) { qreal trailFadePerc,
qreal minOpacity,
QColor color )
{
if ( countDistance == 0 )
{
return color; return color;
} }
const qreal minAlphaF = minOpacity / 100.0; const qreal minAlphaF = minOpacity / 100.0;
int distanceThreshold = int distanceThreshold = static_cast< int >( ceil( ( totalNrOfLines - 1 ) * trailFadePerc / 100.0 ) );
static_cast<int>(ceil((totalNrOfLines - 1) * trailFadePerc / 100.0)); if ( countDistance > distanceThreshold )
if (countDistance > distanceThreshold) { {
color.setAlphaF( minAlphaF ); color.setAlphaF( minAlphaF );
} else { }
else
{
qreal alphaDiff = color.alphaF() - minAlphaF; qreal alphaDiff = color.alphaF() - minAlphaF;
qreal gradient = alphaDiff / static_cast< qreal >( distanceThreshold + 1 ); qreal gradient = alphaDiff / static_cast< qreal >( distanceThreshold + 1 );
qreal resultAlpha = color.alphaF() - gradient * countDistance; qreal resultAlpha = color.alphaF() - gradient * countDistance;

View File

@ -31,11 +31,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#pragma once #pragma once
// Qt includes // Qt includes
#include <QWidget>
#include <QTimer>
#include <QColor> #include <QColor>
#include <QTimer>
#include <QWidget>
class WaitingSpinnerWidget : public QWidget { class WaitingSpinnerWidget : public QWidget
{
Q_OBJECT Q_OBJECT
public: public:
/** @brief Constructor for "standard" widget behaviour /** @brief Constructor for "standard" widget behaviour
@ -89,7 +90,10 @@ public:
*/ */
void setAlignment( Qt::AlignmentFlag align ); void setAlignment( Qt::AlignmentFlag align );
/// Convenience to set text-in-the-middle (@c true) or text-at-bottom (@c false) /// Convenience to set text-in-the-middle (@c true) or text-at-bottom (@c false)
void setCenteredText(bool centered) { setAlignment(centered ? Qt::AlignmentFlag::AlignVCenter : Qt::AlignmentFlag::AlignBottom ); } void setCenteredText( bool centered )
{
setAlignment( centered ? Qt::AlignmentFlag::AlignVCenter : Qt::AlignmentFlag::AlignBottom );
}
QColor color() const; QColor color() const;
QColor textColor() const; QColor textColor() const;
@ -117,11 +121,9 @@ protected:
void paintEvent( QPaintEvent* paintEvent ) override; void paintEvent( QPaintEvent* paintEvent ) override;
private: private:
static int lineCountDistanceFromPrimary(int current, int primary, static int lineCountDistanceFromPrimary( int current, int primary, int totalNrOfLines );
int totalNrOfLines); static QColor
static QColor currentLineColor(int distance, int totalNrOfLines, currentLineColor( int distance, int totalNrOfLines, qreal trailFadePerc, qreal minOpacity, QColor color );
qreal trailFadePerc, qreal minOpacity,
QColor color);
void updateSize(); void updateSize();
void updateTimer(); void updateTimer();