[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,34 +32,35 @@ 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, : WaitingSpinnerWidget( Qt::WindowModality::NonModal, parent, centerOnParent, disableParentWhenSpinning )
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)
{ {
_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(); updateSize();
updateTimer(); updateTimer();
hide(); hide();
@ -71,249 +72,327 @@ WaitingSpinnerWidget::WaitingSpinnerWidget(Qt::WindowModality modality,
// Non-modal windows don't need any work // Non-modal windows don't need any work
if ( modality != Qt::WindowModality::NonModal ) if ( modality != Qt::WindowModality::NonModal )
{ {
setWindowModality(modality); setWindowModality( modality );
setAttribute(Qt::WA_TranslucentBackground); setAttribute( Qt::WA_TranslucentBackground );
} }
} }
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 = static_cast< qreal >( 360 * i ) / static_cast< qreal >( _numberOfLines );
qreal rotateAngle = painter.rotate( rotateAngle );
static_cast<qreal>(360 * i) / static_cast<qreal>(_numberOfLines); painter.translate( _innerRadius, 0 );
painter.rotate(rotateAngle); int distance = lineCountDistanceFromPrimary( i, _currentCounter, _numberOfLines );
painter.translate(_innerRadius, 0); QColor color = currentLineColor( distance, _numberOfLines, _trailFadePercentage, _minimumTrailOpacity, _color );
int distance = painter.setBrush( color );
lineCountDistanceFromPrimary(i, _currentCounter, _numberOfLines);
QColor color =
currentLineColor(distance, _numberOfLines, _trailFadePercentage,
_minimumTrailOpacity, _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)); {
if (isAlignCenter(alignment())) { painter.setPen( QPen( _textColor ) );
painter.drawText(QRect(0, 0, width(), height()), if ( isAlignCenter( alignment() ) )
Qt::AlignVCenter | Qt::AlignHCenter, _text); {
} else { painter.drawText( QRect( 0, 0, width(), height() ), Qt::AlignVCenter | Qt::AlignHCenter, _text );
painter.drawText(QRect(0, _imageSize.height(), width(), height() - _imageSize.height()), }
Qt::AlignBottom | 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(); 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
_roundness = std::max(0.0, std::min(100.0, roundness)); 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; _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
int size = (_innerRadius + _lineLength) * 2; WaitingSpinnerWidget::updateSize()
_imageSize = QSize(size, size); {
if (_text.isEmpty() || isAlignCenter(alignment())) { int size = ( _innerRadius + _lineLength ) * 2;
setFixedSize(size, size); _imageSize = QSize( size, size );
} else { if ( _text.isEmpty() || isAlignCenter( alignment() ) )
QFontMetrics fm(font()); {
QSize textSize = QSize(fm.width(_text), fm.height()); setFixedSize( size, size );
setFixedSize(std::max(size, textSize.width()), size + size / 4 + textSize.height()); }
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() { void
_timer->setInterval(1000 / (_numberOfLines * _revolutionsPerSecond)); WaitingSpinnerWidget::updateTimer()
{
_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;
// If alpha is out of bounds, clip it. // If alpha is out of bounds, clip it.
resultAlpha = std::min(1.0, std::max(0.0, resultAlpha)); resultAlpha = std::min( 1.0, std::max( 0.0, resultAlpha ) );
color.setAlphaF(resultAlpha); color.setAlphaF( resultAlpha );
} }
return color; return color;
} }

View File

@ -31,20 +31,21 @@ 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
* *
* Use this constructor if you wish to, e.g. embed your widget in another. * Use this constructor if you wish to, e.g. embed your widget in another.
*/ */
WaitingSpinnerWidget(QWidget *parent = nullptr, WaitingSpinnerWidget( QWidget* parent = nullptr,
bool centerOnParent = true, bool centerOnParent = true,
bool disableParentWhenSpinning = true); bool disableParentWhenSpinning = true );
/** @brief Constructor /** @brief Constructor
* *
@ -54,24 +55,24 @@ public:
* QtWaitingSpinner automatically centres itself on it, if not, * QtWaitingSpinner automatically centres itself on it, if not,
* @p centerOnParent is ignored. * @p centerOnParent is ignored.
*/ */
WaitingSpinnerWidget(Qt::WindowModality modality, WaitingSpinnerWidget( Qt::WindowModality modality,
QWidget *parent = nullptr, QWidget* parent = nullptr,
bool centerOnParent = true, bool centerOnParent = true,
bool disableParentWhenSpinning = true); bool disableParentWhenSpinning = true );
WaitingSpinnerWidget(const WaitingSpinnerWidget&) = delete; WaitingSpinnerWidget( const WaitingSpinnerWidget& ) = delete;
WaitingSpinnerWidget& operator=(const WaitingSpinnerWidget&) = delete; WaitingSpinnerWidget& operator=( const WaitingSpinnerWidget& ) = delete;
void setColor(QColor color); void setColor( QColor color );
void setTextColor(QColor color); void setTextColor( QColor color );
void setRoundness(qreal roundness); void setRoundness( qreal roundness );
void setMinimumTrailOpacity(qreal minimumTrailOpacity); void setMinimumTrailOpacity( qreal minimumTrailOpacity );
void setTrailFadePercentage(qreal trail); void setTrailFadePercentage( qreal trail );
void setRevolutionsPerSecond(qreal revolutionsPerSecond); void setRevolutionsPerSecond( qreal revolutionsPerSecond );
void setNumberOfLines(int lines); void setNumberOfLines( int lines );
void setLineLength(int length); void setLineLength( int length );
void setLineWidth(int width); void setLineWidth( int width );
void setInnerRadius(int radius); void setInnerRadius( int radius );
/** @brief Sets the text displayed in or below the spinner /** @brief Sets the text displayed in or below the spinner
* *
@ -81,21 +82,24 @@ public:
* centered horizontally relative to the spinner; any other alignment * centered horizontally relative to the spinner; any other alignment
* will put the text in the middle of the spinner itself. * 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 /** @brief Sets the alignment of text for the spinner
* *
* The only meaningful values are AlignBottom and AlignVCenter, * The only meaningful values are AlignBottom and AlignVCenter,
* for text below the spinner and text in the middle. * 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) /// 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;
QString text() const; QString text() const;
Qt::AlignmentFlag alignment() const { return _alignment; } Qt::AlignmentFlag alignment() const { return _alignment; }
qreal roundness() const; qreal roundness() const;
qreal minimumTrailOpacity() const; qreal minimumTrailOpacity() const;
qreal trailFadePercentage() const; qreal trailFadePercentage() const;
qreal revolutionsPersSecond() const; qreal revolutionsPersSecond() const;
@ -114,14 +118,12 @@ private Q_SLOTS:
void rotate(); void rotate();
protected: 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();
@ -132,28 +134,28 @@ private:
static constexpr const auto radian = 3.14159265358979323846; static constexpr const auto radian = 3.14159265358979323846;
// Spinner-wheel related settings // Spinner-wheel related settings
QColor _color = Qt::black; QColor _color = Qt::black;
qreal _roundness = 100.0; // 0..100 qreal _roundness = 100.0; // 0..100
qreal _minimumTrailOpacity = radian; qreal _minimumTrailOpacity = radian;
qreal _trailFadePercentage = 80.0; qreal _trailFadePercentage = 80.0;
qreal _revolutionsPerSecond = radian / 2; qreal _revolutionsPerSecond = radian / 2;
int _numberOfLines = 20; int _numberOfLines = 20;
int _lineLength = 10; int _lineLength = 10;
int _lineWidth = 2; int _lineWidth = 2;
int _innerRadius = 10; int _innerRadius = 10;
QSize _imageSize; QSize _imageSize;
// Text-related settings // Text-related settings
Qt::AlignmentFlag _alignment = Qt::AlignmentFlag::AlignBottom; Qt::AlignmentFlag _alignment = Qt::AlignmentFlag::AlignBottom;
QString _text; QString _text;
QColor _textColor = Qt::black; QColor _textColor = Qt::black;
// Environment settings // Environment settings
bool _centerOnParent = true; bool _centerOnParent = true;
bool _disableParentWhenSpinning = true; bool _disableParentWhenSpinning = true;
// Internal bits // Internal bits
QTimer *_timer = nullptr; QTimer* _timer = nullptr;
int _currentCounter = 0; int _currentCounter = 0;
bool _isSpinning = false; bool _isSpinning = false;
}; };