From 18fef8dfe5d926ec0bc979562553adf4db8db2e9 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Sat, 16 Apr 2022 12:39:21 +0200 Subject: [PATCH] [libcalamaresui] Apply coding style --- .../widgets/waitingspinnerwidget.cpp | 307 +++++++++++------- .../widgets/waitingspinnerwidget.h | 98 +++--- 2 files changed, 243 insertions(+), 162 deletions(-) diff --git a/src/libcalamaresui/widgets/waitingspinnerwidget.cpp b/src/libcalamaresui/widgets/waitingspinnerwidget.cpp index a4bb9b79c..0186fa983 100644 --- a/src/libcalamaresui/widgets/waitingspinnerwidget.cpp +++ b/src/libcalamaresui/widgets/waitingspinnerwidget.cpp @@ -32,34 +32,35 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include "waitingspinnerwidget.h" // Standard includes -#include #include +#include // Qt includes #include #include -static bool isAlignCenter(Qt::AlignmentFlag a) +static bool +isAlignCenter( Qt::AlignmentFlag a ) { return a == Qt::AlignmentFlag::AlignVCenter; } -WaitingSpinnerWidget::WaitingSpinnerWidget(QWidget *parent, - bool centerOnParent, - bool disableParentWhenSpinning) - : WaitingSpinnerWidget(Qt::WindowModality::NonModal, parent, centerOnParent, disableParentWhenSpinning) -{} - -WaitingSpinnerWidget::WaitingSpinnerWidget(Qt::WindowModality modality, - QWidget *parent, - bool centerOnParent, - bool disableParentWhenSpinning) - : QWidget(parent, modality == Qt::WindowModality::NonModal ? Qt::WindowFlags() : Qt::Dialog | Qt::FramelessWindowHint), - _centerOnParent(centerOnParent), - _disableParentWhenSpinning(disableParentWhenSpinning) +WaitingSpinnerWidget::WaitingSpinnerWidget( QWidget* parent, bool centerOnParent, bool disableParentWhenSpinning ) + : WaitingSpinnerWidget( Qt::WindowModality::NonModal, parent, centerOnParent, disableParentWhenSpinning ) { - _timer = new QTimer(this); - connect(_timer, SIGNAL(timeout()), this, SLOT(rotate())); +} + +WaitingSpinnerWidget::WaitingSpinnerWidget( Qt::WindowModality modality, + QWidget* parent, + bool centerOnParent, + bool disableParentWhenSpinning ) + : QWidget( parent, + modality == Qt::WindowModality::NonModal ? Qt::WindowFlags() : Qt::Dialog | Qt::FramelessWindowHint ) + , _centerOnParent( centerOnParent ) + , _disableParentWhenSpinning( disableParentWhenSpinning ) +{ + _timer = new QTimer( this ); + connect( _timer, SIGNAL( timeout() ), this, SLOT( rotate() ) ); updateSize(); updateTimer(); hide(); @@ -71,249 +72,327 @@ WaitingSpinnerWidget::WaitingSpinnerWidget(Qt::WindowModality modality, // Non-modal windows don't need any work if ( modality != Qt::WindowModality::NonModal ) { - setWindowModality(modality); - setAttribute(Qt::WA_TranslucentBackground); + setWindowModality( modality ); + setAttribute( Qt::WA_TranslucentBackground ); } } -void WaitingSpinnerWidget::paintEvent(QPaintEvent *) { +void +WaitingSpinnerWidget::paintEvent( QPaintEvent* ) +{ updatePosition(); - QPainter painter(this); - painter.fillRect(this->rect(), Qt::transparent); - painter.setRenderHint(QPainter::Antialiasing, true); + QPainter painter( this ); + painter.fillRect( this->rect(), Qt::transparent ); + painter.setRenderHint( QPainter::Antialiasing, true ); - if (_currentCounter >= _numberOfLines) { + if ( _currentCounter >= _numberOfLines ) + { _currentCounter = 0; } - painter.setPen(Qt::NoPen); - for (int i = 0; i < _numberOfLines; ++i) { + painter.setPen( Qt::NoPen ); + for ( int i = 0; i < _numberOfLines; ++i ) + { painter.save(); - painter.translate(_innerRadius + _lineLength, - _innerRadius + _lineLength); - painter.translate((width() - _imageSize.width()) / 2, 0); - qreal rotateAngle = - static_cast(360 * i) / static_cast(_numberOfLines); - painter.rotate(rotateAngle); - painter.translate(_innerRadius, 0); - int distance = - lineCountDistanceFromPrimary(i, _currentCounter, _numberOfLines); - QColor color = - currentLineColor(distance, _numberOfLines, _trailFadePercentage, - _minimumTrailOpacity, _color); - painter.setBrush(color); + painter.translate( _innerRadius + _lineLength, _innerRadius + _lineLength ); + painter.translate( ( width() - _imageSize.width() ) / 2, 0 ); + qreal rotateAngle = static_cast< qreal >( 360 * i ) / static_cast< qreal >( _numberOfLines ); + painter.rotate( rotateAngle ); + painter.translate( _innerRadius, 0 ); + int distance = lineCountDistanceFromPrimary( i, _currentCounter, _numberOfLines ); + QColor color = currentLineColor( distance, _numberOfLines, _trailFadePercentage, _minimumTrailOpacity, _color ); + painter.setBrush( color ); // TODO improve the way rounded rect is painted painter.drawRoundedRect( - QRect(0, -_lineWidth / 2, _lineLength, _lineWidth), _roundness, - _roundness, Qt::RelativeSize); + QRect( 0, -_lineWidth / 2, _lineLength, _lineWidth ), _roundness, _roundness, Qt::RelativeSize ); painter.restore(); } - if (!_text.isEmpty()) { - painter.setPen(QPen(_textColor)); - if (isAlignCenter(alignment())) { - painter.drawText(QRect(0, 0, width(), height()), - Qt::AlignVCenter | Qt::AlignHCenter, _text); - } else { - painter.drawText(QRect(0, _imageSize.height(), width(), height() - _imageSize.height()), - Qt::AlignBottom | Qt::AlignHCenter, _text); + if ( !_text.isEmpty() ) + { + painter.setPen( QPen( _textColor ) ); + if ( isAlignCenter( alignment() ) ) + { + painter.drawText( QRect( 0, 0, width(), height() ), Qt::AlignVCenter | Qt::AlignHCenter, _text ); + } + else + { + painter.drawText( QRect( 0, _imageSize.height(), width(), height() - _imageSize.height() ), + Qt::AlignBottom | Qt::AlignHCenter, + _text ); } } } -void WaitingSpinnerWidget::start() { +void +WaitingSpinnerWidget::start() +{ updatePosition(); _isSpinning = true; show(); - if(parentWidget() && _disableParentWhenSpinning) { - parentWidget()->setEnabled(false); + if ( parentWidget() && _disableParentWhenSpinning ) + { + parentWidget()->setEnabled( false ); } - if (!_timer->isActive()) { + if ( !_timer->isActive() ) + { _timer->start(); _currentCounter = 0; } } -void WaitingSpinnerWidget::stop() { +void +WaitingSpinnerWidget::stop() +{ _isSpinning = false; hide(); - if(parentWidget() && _disableParentWhenSpinning) { - parentWidget()->setEnabled(true); + if ( parentWidget() && _disableParentWhenSpinning ) + { + parentWidget()->setEnabled( true ); } - if (_timer->isActive()) { + if ( _timer->isActive() ) + { _timer->stop(); _currentCounter = 0; } } -void WaitingSpinnerWidget::setNumberOfLines(int lines) { +void +WaitingSpinnerWidget::setNumberOfLines( int lines ) +{ _numberOfLines = lines; _currentCounter = 0; updateTimer(); } -void WaitingSpinnerWidget::setLineLength(int length) { +void +WaitingSpinnerWidget::setLineLength( int length ) +{ _lineLength = length; updateSize(); } -void WaitingSpinnerWidget::setLineWidth(int width) { +void +WaitingSpinnerWidget::setLineWidth( int width ) +{ _lineWidth = width; updateSize(); } -void WaitingSpinnerWidget::setInnerRadius(int radius) { +void +WaitingSpinnerWidget::setInnerRadius( int radius ) +{ _innerRadius = radius; updateSize(); } -void WaitingSpinnerWidget::setText(const QString& text) { +void +WaitingSpinnerWidget::setText( const QString& text ) +{ _text = text; updateSize(); } -void WaitingSpinnerWidget::setAlignment(Qt::AlignmentFlag align) +void +WaitingSpinnerWidget::setAlignment( Qt::AlignmentFlag align ) { _alignment = align; updateSize(); } -QColor WaitingSpinnerWidget::color() const { +QColor +WaitingSpinnerWidget::color() const +{ return _color; } -QColor WaitingSpinnerWidget::textColor() const { +QColor +WaitingSpinnerWidget::textColor() const +{ return _textColor; } -QString WaitingSpinnerWidget::text() const { +QString +WaitingSpinnerWidget::text() const +{ return _text; } -qreal WaitingSpinnerWidget::roundness() const { +qreal +WaitingSpinnerWidget::roundness() const +{ return _roundness; } -qreal WaitingSpinnerWidget::minimumTrailOpacity() const { +qreal +WaitingSpinnerWidget::minimumTrailOpacity() const +{ return _minimumTrailOpacity; } -qreal WaitingSpinnerWidget::trailFadePercentage() const { +qreal +WaitingSpinnerWidget::trailFadePercentage() const +{ return _trailFadePercentage; } -qreal WaitingSpinnerWidget::revolutionsPersSecond() const { +qreal +WaitingSpinnerWidget::revolutionsPersSecond() const +{ return _revolutionsPerSecond; } -int WaitingSpinnerWidget::numberOfLines() const { +int +WaitingSpinnerWidget::numberOfLines() const +{ return _numberOfLines; } -int WaitingSpinnerWidget::lineLength() const { +int +WaitingSpinnerWidget::lineLength() const +{ return _lineLength; } -int WaitingSpinnerWidget::lineWidth() const { +int +WaitingSpinnerWidget::lineWidth() const +{ return _lineWidth; } -int WaitingSpinnerWidget::innerRadius() const { +int +WaitingSpinnerWidget::innerRadius() const +{ return _innerRadius; } -bool WaitingSpinnerWidget::isSpinning() const { +bool +WaitingSpinnerWidget::isSpinning() const +{ return _isSpinning; } -void WaitingSpinnerWidget::setRoundness(qreal roundness) { - _roundness = std::max(0.0, std::min(100.0, roundness)); +void +WaitingSpinnerWidget::setRoundness( qreal roundness ) +{ + _roundness = std::max( 0.0, std::min( 100.0, roundness ) ); } -void WaitingSpinnerWidget::setColor(QColor color) { +void +WaitingSpinnerWidget::setColor( QColor color ) +{ _color = color; } -void WaitingSpinnerWidget::setTextColor(QColor color) { +void +WaitingSpinnerWidget::setTextColor( QColor color ) +{ _textColor = color; } -void WaitingSpinnerWidget::setRevolutionsPerSecond(qreal revolutionsPerSecond) { +void +WaitingSpinnerWidget::setRevolutionsPerSecond( qreal revolutionsPerSecond ) +{ _revolutionsPerSecond = revolutionsPerSecond; updateTimer(); } -void WaitingSpinnerWidget::setTrailFadePercentage(qreal trail) { +void +WaitingSpinnerWidget::setTrailFadePercentage( qreal trail ) +{ _trailFadePercentage = trail; } -void WaitingSpinnerWidget::setMinimumTrailOpacity(qreal minimumTrailOpacity) { +void +WaitingSpinnerWidget::setMinimumTrailOpacity( qreal minimumTrailOpacity ) +{ _minimumTrailOpacity = minimumTrailOpacity; } -void WaitingSpinnerWidget::rotate() { +void +WaitingSpinnerWidget::rotate() +{ ++_currentCounter; - if (_currentCounter >= _numberOfLines) { + if ( _currentCounter >= _numberOfLines ) + { _currentCounter = 0; } update(); } -void WaitingSpinnerWidget::updateSize() { - int size = (_innerRadius + _lineLength) * 2; - _imageSize = QSize(size, size); - if (_text.isEmpty() || isAlignCenter(alignment())) { - setFixedSize(size, size); - } else { - QFontMetrics fm(font()); - QSize textSize = QSize(fm.width(_text), fm.height()); - setFixedSize(std::max(size, textSize.width()), size + size / 4 + textSize.height()); +void +WaitingSpinnerWidget::updateSize() +{ + int size = ( _innerRadius + _lineLength ) * 2; + _imageSize = QSize( size, size ); + if ( _text.isEmpty() || isAlignCenter( alignment() ) ) + { + setFixedSize( size, size ); + } + else + { + QFontMetrics fm( font() ); + QSize textSize = QSize( fm.width( _text ), fm.height() ); + setFixedSize( std::max( size, textSize.width() ), size + size / 4 + textSize.height() ); } } -void WaitingSpinnerWidget::updateTimer() { - _timer->setInterval(1000 / (_numberOfLines * _revolutionsPerSecond)); +void +WaitingSpinnerWidget::updateTimer() +{ + _timer->setInterval( 1000 / ( _numberOfLines * _revolutionsPerSecond ) ); } -void WaitingSpinnerWidget::updatePosition() { - if (parentWidget() && _centerOnParent) { - move(parentWidget()->width() / 2 - width() / 2, - parentWidget()->height() / 2 - height() / 2); +void +WaitingSpinnerWidget::updatePosition() +{ + if ( parentWidget() && _centerOnParent ) + { + move( parentWidget()->width() / 2 - width() / 2, parentWidget()->height() / 2 - height() / 2 ); } } -int WaitingSpinnerWidget::lineCountDistanceFromPrimary(int current, int primary, - int totalNrOfLines) { +int +WaitingSpinnerWidget::lineCountDistanceFromPrimary( int current, int primary, int totalNrOfLines ) +{ int distance = primary - current; - if (distance < 0) { + if ( distance < 0 ) + { distance += totalNrOfLines; } return distance; } -QColor WaitingSpinnerWidget::currentLineColor(int countDistance, int totalNrOfLines, - qreal trailFadePerc, qreal minOpacity, - QColor color) { - if (countDistance == 0) { +QColor +WaitingSpinnerWidget::currentLineColor( int countDistance, + int totalNrOfLines, + qreal trailFadePerc, + qreal minOpacity, + QColor color ) +{ + if ( countDistance == 0 ) + { return color; } const qreal minAlphaF = minOpacity / 100.0; - int distanceThreshold = - static_cast(ceil((totalNrOfLines - 1) * trailFadePerc / 100.0)); - if (countDistance > distanceThreshold) { - color.setAlphaF(minAlphaF); - } else { + int distanceThreshold = static_cast< int >( ceil( ( totalNrOfLines - 1 ) * trailFadePerc / 100.0 ) ); + if ( countDistance > distanceThreshold ) + { + color.setAlphaF( minAlphaF ); + } + else + { qreal alphaDiff = color.alphaF() - minAlphaF; - qreal gradient = alphaDiff / static_cast(distanceThreshold + 1); + qreal gradient = alphaDiff / static_cast< qreal >( distanceThreshold + 1 ); qreal resultAlpha = color.alphaF() - gradient * countDistance; // If alpha is out of bounds, clip it. - resultAlpha = std::min(1.0, std::max(0.0, resultAlpha)); - color.setAlphaF(resultAlpha); + resultAlpha = std::min( 1.0, std::max( 0.0, resultAlpha ) ); + color.setAlphaF( resultAlpha ); } return color; } diff --git a/src/libcalamaresui/widgets/waitingspinnerwidget.h b/src/libcalamaresui/widgets/waitingspinnerwidget.h index 1ecc33a87..2fe76b5b1 100644 --- a/src/libcalamaresui/widgets/waitingspinnerwidget.h +++ b/src/libcalamaresui/widgets/waitingspinnerwidget.h @@ -31,20 +31,21 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #pragma once // Qt includes -#include -#include #include +#include +#include -class WaitingSpinnerWidget : public QWidget { +class WaitingSpinnerWidget : public QWidget +{ Q_OBJECT public: /** @brief Constructor for "standard" widget behaviour * * Use this constructor if you wish to, e.g. embed your widget in another. */ - WaitingSpinnerWidget(QWidget *parent = nullptr, - bool centerOnParent = true, - bool disableParentWhenSpinning = true); + WaitingSpinnerWidget( QWidget* parent = nullptr, + bool centerOnParent = true, + bool disableParentWhenSpinning = true ); /** @brief Constructor * @@ -54,24 +55,24 @@ public: * QtWaitingSpinner automatically centres itself on it, if not, * @p centerOnParent is ignored. */ - WaitingSpinnerWidget(Qt::WindowModality modality, - QWidget *parent = nullptr, - bool centerOnParent = true, - bool disableParentWhenSpinning = true); + WaitingSpinnerWidget( Qt::WindowModality modality, + QWidget* parent = nullptr, + bool centerOnParent = true, + bool disableParentWhenSpinning = true ); - WaitingSpinnerWidget(const WaitingSpinnerWidget&) = delete; - WaitingSpinnerWidget& operator=(const WaitingSpinnerWidget&) = delete; + WaitingSpinnerWidget( const WaitingSpinnerWidget& ) = delete; + WaitingSpinnerWidget& operator=( const WaitingSpinnerWidget& ) = delete; - void setColor(QColor color); - void setTextColor(QColor color); - void setRoundness(qreal roundness); - void setMinimumTrailOpacity(qreal minimumTrailOpacity); - void setTrailFadePercentage(qreal trail); - void setRevolutionsPerSecond(qreal revolutionsPerSecond); - void setNumberOfLines(int lines); - void setLineLength(int length); - void setLineWidth(int width); - void setInnerRadius(int radius); + void setColor( QColor color ); + void setTextColor( QColor color ); + void setRoundness( qreal roundness ); + void setMinimumTrailOpacity( qreal minimumTrailOpacity ); + void setTrailFadePercentage( qreal trail ); + void setRevolutionsPerSecond( qreal revolutionsPerSecond ); + void setNumberOfLines( int lines ); + void setLineLength( int length ); + void setLineWidth( int width ); + void setInnerRadius( int radius ); /** @brief Sets the text displayed in or below the spinner * @@ -81,21 +82,24 @@ public: * centered horizontally relative to the spinner; any other alignment * will put the text in the middle of the spinner itself. */ - void setText(const QString& text); + void setText( const QString& text ); /** @brief Sets the alignment of text for the spinner * * The only meaningful values are AlignBottom and AlignVCenter, * for text below the spinner and text in the middle. */ - 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) - 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 textColor() const; QString text() const; Qt::AlignmentFlag alignment() const { return _alignment; } - qreal roundness() const; + qreal roundness() const; qreal minimumTrailOpacity() const; qreal trailFadePercentage() const; qreal revolutionsPersSecond() const; @@ -114,14 +118,12 @@ private Q_SLOTS: void rotate(); protected: - void paintEvent(QPaintEvent *paintEvent) override; + void paintEvent( QPaintEvent* paintEvent ) override; private: - static int lineCountDistanceFromPrimary(int current, int primary, - int totalNrOfLines); - static QColor currentLineColor(int distance, int totalNrOfLines, - qreal trailFadePerc, qreal minOpacity, - QColor color); + static int lineCountDistanceFromPrimary( int current, int primary, int totalNrOfLines ); + static QColor + currentLineColor( int distance, int totalNrOfLines, qreal trailFadePerc, qreal minOpacity, QColor color ); void updateSize(); void updateTimer(); @@ -132,28 +134,28 @@ private: static constexpr const auto radian = 3.14159265358979323846; // Spinner-wheel related settings - QColor _color = Qt::black; - qreal _roundness = 100.0; // 0..100 - qreal _minimumTrailOpacity = radian; - qreal _trailFadePercentage = 80.0; - qreal _revolutionsPerSecond = radian / 2; - int _numberOfLines = 20; - int _lineLength = 10; - int _lineWidth = 2; - int _innerRadius = 10; - QSize _imageSize; + QColor _color = Qt::black; + qreal _roundness = 100.0; // 0..100 + qreal _minimumTrailOpacity = radian; + qreal _trailFadePercentage = 80.0; + qreal _revolutionsPerSecond = radian / 2; + int _numberOfLines = 20; + int _lineLength = 10; + int _lineWidth = 2; + int _innerRadius = 10; + QSize _imageSize; // Text-related settings Qt::AlignmentFlag _alignment = Qt::AlignmentFlag::AlignBottom; QString _text; - QColor _textColor = Qt::black; + QColor _textColor = Qt::black; // Environment settings - bool _centerOnParent = true; - bool _disableParentWhenSpinning = true; + bool _centerOnParent = true; + bool _disableParentWhenSpinning = true; // Internal bits - QTimer *_timer = nullptr; - int _currentCounter = 0; - bool _isSpinning = false; + QTimer* _timer = nullptr; + int _currentCounter = 0; + bool _isSpinning = false; };