From 37183bb637577c20c7b3f7d5f8310fab289ff228 Mon Sep 17 00:00:00 2001 From: "Vogel, Rick" Date: Mon, 24 Jul 2023 10:56:26 +0200 Subject: [PATCH] using private data --- playground/levelingsensor/main.cpp | 1 + .../LevelingSensor/QskLevelingSensor.cpp | 136 ++++++++++-------- .../LevelingSensor/QskLevelingSensor.h | 72 ++++------ 3 files changed, 111 insertions(+), 98 deletions(-) diff --git a/playground/levelingsensor/main.cpp b/playground/levelingsensor/main.cpp index 587365f5..de61fb25 100644 --- a/playground/levelingsensor/main.cpp +++ b/playground/levelingsensor/main.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include diff --git a/src/controls/LevelingSensor/QskLevelingSensor.cpp b/src/controls/LevelingSensor/QskLevelingSensor.cpp index 6295f651..8c73f0b0 100644 --- a/src/controls/LevelingSensor/QskLevelingSensor.cpp +++ b/src/controls/LevelingSensor/QskLevelingSensor.cpp @@ -1,13 +1,17 @@ #include "QskLevelingSensor.h" -#include #include +#include +#include + +#include +#include namespace { - template - bool compareExchange(T& dst, const T& src) + template< typename T > + bool compareExchange( T& dst, const T& src ) { - if (dst != src) + if ( dst != src ) { dst = src; return true; @@ -16,9 +20,9 @@ namespace } template<> - bool compareExchange(float& dst, const float& src) + bool compareExchange< float >( float& dst, const float& src ) { - if (!qskFuzzyCompare(dst, src)) + if ( !qskFuzzyCompare( dst, src ) ) { dst = src; return true; @@ -26,78 +30,93 @@ namespace return false; } - inline bool isAxis(const Qt::Axis axis) { + inline bool isAxis( const Qt::Axis axis ) + { return axis == Qt::XAxis || axis == Qt::YAxis || axis == Qt::ZAxis; } } -QSK_SUBCONTROL(QskLevelingSensor, OuterDisk) -QSK_SUBCONTROL(QskLevelingSensor, Horizon) -QSK_SUBCONTROL(QskLevelingSensor, TickmarksX) -QSK_SUBCONTROL(QskLevelingSensor, TickmarksXLabels) -QSK_SUBCONTROL(QskLevelingSensor, TickmarksY) -QSK_SUBCONTROL(QskLevelingSensor, TickmarksYLabels) -QSK_SUBCONTROL(QskLevelingSensor, TickmarksZ) -QSK_SUBCONTROL(QskLevelingSensor, TickmarksZLabels) +QSK_SUBCONTROL( QskLevelingSensor, OuterDisk ) +QSK_SUBCONTROL( QskLevelingSensor, Horizon ) +QSK_SUBCONTROL( QskLevelingSensor, TickmarksX ) +QSK_SUBCONTROL( QskLevelingSensor, TickmarksXLabels ) +QSK_SUBCONTROL( QskLevelingSensor, TickmarksY ) +QSK_SUBCONTROL( QskLevelingSensor, TickmarksYLabels ) +QSK_SUBCONTROL( QskLevelingSensor, TickmarksZ ) +QSK_SUBCONTROL( QskLevelingSensor, TickmarksZLabels ) -#define RETURN_IF_FALSE(expr) if(!(expr)) return; +#define RETURN_IF_FALSE( expr ) \ + if ( !( expr ) ) \ + return; using Q = QskLevelingSensor; -QskLevelingSensor::QskLevelingSensor(QQuickItem* const parent) - : Inherited(parent) +class QskLevelingSensor::PrivateData +{ + public: + QVector3D m_angle = { 45, 45, 45 }; + Tickmarks m_tickmarks[ 3 ]; + TickmarksLabels m_tickmarksLabels[ 3 ]; + std::unordered_map< QskAspect::Subcontrol, QVector3D > m_subControlRotation; +}; + +QskLevelingSensor::QskLevelingSensor( QQuickItem* const parent ) + : Inherited( parent ) + , m_data( std::make_unique< QskLevelingSensor::PrivateData >() ) { } -void QskLevelingSensor::setTickmarks(const Qt::Axis axis, QskScaleTickmarks tickmarks) +QskLevelingSensor::~QskLevelingSensor() = default; + +void QskLevelingSensor::setTickmarks( const Qt::Axis axis, QskScaleTickmarks tickmarks ) { - RETURN_IF_FALSE(isAxis(axis)); - m_tickmarks[axis] = std::move(tickmarks); + RETURN_IF_FALSE( isAxis( axis ) ); + m_data->m_tickmarks[ axis ] = std::move( tickmarks ); update(); } -void QskLevelingSensor::setTickmarksLabels(const Qt::Axis axis, TickmarksLabels labels) +void QskLevelingSensor::setTickmarksLabels( const Qt::Axis axis, TickmarksLabels labels ) { - RETURN_IF_FALSE(isAxis(axis)); - m_tickmarksLabels[axis] = std::move(labels); + RETURN_IF_FALSE( isAxis( axis ) ); + m_data->m_tickmarksLabels[ axis ] = std::move( labels ); update(); } -void QskLevelingSensor::setAngle(const QVector3D& degree) +void QskLevelingSensor::setAngle( const QVector3D& degree ) { - if (compareExchange(m_angle, degree)) + if ( compareExchange( m_data->m_angle, degree ) ) { update(); - Q_EMIT anglesChanged(m_angle); + Q_EMIT anglesChanged( m_data->m_angle ); } } -void QskLevelingSensor::setAngle(const Qt::Axis axis, const float degree) +void QskLevelingSensor::setAngle( const Qt::Axis axis, const float degree ) { - RETURN_IF_FALSE(isAxis(axis)); + RETURN_IF_FALSE( isAxis( axis ) ); - if (compareExchange(m_angle[axis], degree)) + if ( compareExchange( m_data->m_angle[ axis ], degree ) ) { update(); - Q_EMIT anglesChanged(m_angle); + Q_EMIT anglesChanged( m_data->m_angle ); } } -const QskScaleTickmarks& QskLevelingSensor::tickmarks(Qt::Axis axis) const +const QskScaleTickmarks& QskLevelingSensor::tickmarks( Qt::Axis axis ) const { - if (isAxis(axis)) + if ( isAxis( axis ) ) { - return m_tickmarks[axis]; + return m_data->m_tickmarks[ axis ]; } static const QskScaleTickmarks invalid; return invalid; } -const QskLevelingSensor::TickmarksLabels& QskLevelingSensor::tickmarkLabels(Qt::Axis axis) const +const QskLevelingSensor::TickmarksLabels& QskLevelingSensor::tickmarkLabels( Qt::Axis axis ) const { - if (isAxis(axis)) + if ( isAxis( axis ) ) { - return m_tickmarksLabels[axis]; + return m_data->m_tickmarksLabels[ axis ]; } static const QskLevelingSensor::TickmarksLabels invalid; return invalid; @@ -105,48 +124,51 @@ const QskLevelingSensor::TickmarksLabels& QskLevelingSensor::tickmarkLabels(Qt:: const QVector3D& QskLevelingSensor::angle() const noexcept { - return m_angle; + return m_data->m_angle; } -const QVector3D& QskLevelingSensor::subControlRotation(const QskAspect::Subcontrol subControl) const noexcept +const QVector3D& QskLevelingSensor::subControlRotation( + const QskAspect::Subcontrol subControl ) const noexcept { static const QVector3D notFound; - const auto found = m_subControlRotation.find(subControl); - if(found == m_subControlRotation.end()) { + const auto found = m_data->m_subControlRotation.find( subControl ); + if ( found == m_data->m_subControlRotation.end() ) + { return notFound; } return found->second; } -void QskLevelingSensor::setSubControlRotation(const QskAspect::Subcontrol subControl, const QVector3D& degree) +void QskLevelingSensor::setSubControlRotation( + const QskAspect::Subcontrol subControl, const QVector3D& degree ) { - auto updateSubControlRotation = [this](const QskAspect::Subcontrol subControl, const QVector3D& degree) - { - if ( compareExchange( m_subControlRotation[ subControl ], degree ) ) + auto updateSubControlRotation = [ this ]( const QskAspect::Subcontrol subControl, + const QVector3D& degree ) { + if ( compareExchange( m_data->m_subControlRotation[ subControl ], degree ) ) { - Q_EMIT subControlRotationChanged(subControl, degree); + Q_EMIT subControlRotationChanged( subControl, degree ); update(); } }; - if(subControl == Q::TickmarksX || subControl == Q::TickmarksXLabels) + if ( subControl == Q::TickmarksX || subControl == Q::TickmarksXLabels ) { - updateSubControlRotation(Q::TickmarksX, degree); - updateSubControlRotation(Q::TickmarksXLabels, degree); + updateSubControlRotation( Q::TickmarksX, degree ); + updateSubControlRotation( Q::TickmarksXLabels, degree ); } - else if(subControl == Q::TickmarksY || subControl == Q::TickmarksYLabels) + else if ( subControl == Q::TickmarksY || subControl == Q::TickmarksYLabels ) { - updateSubControlRotation(Q::TickmarksY, degree); - updateSubControlRotation(Q::TickmarksYLabels, degree); + updateSubControlRotation( Q::TickmarksY, degree ); + updateSubControlRotation( Q::TickmarksYLabels, degree ); } - else if(subControl == Q::TickmarksZ || subControl == TickmarksZLabels) + else if ( subControl == Q::TickmarksZ || subControl == TickmarksZLabels ) { - updateSubControlRotation(TickmarksZ, degree); - updateSubControlRotation(TickmarksZLabels, degree); + updateSubControlRotation( TickmarksZ, degree ); + updateSubControlRotation( TickmarksZLabels, degree ); } - else + else { - updateSubControlRotation(subControl, degree); + updateSubControlRotation( subControl, degree ); } } diff --git a/src/controls/LevelingSensor/QskLevelingSensor.h b/src/controls/LevelingSensor/QskLevelingSensor.h index 89a05032..2949c8a3 100644 --- a/src/controls/LevelingSensor/QskLevelingSensor.h +++ b/src/controls/LevelingSensor/QskLevelingSensor.h @@ -1,55 +1,45 @@ #pragma once #include -#include -#include -#include +#include -#include +class QskScaleTickmarks; class QSK_EXPORT QskLevelingSensor : public QskControl { Q_OBJECT - using Inherited = QskControl; -public: - QSK_SUBCONTROLS( - OuterDisk, - Horizon, - TickmarksX, - TickmarksXLabels, - TickmarksY, - TickmarksYLabels, - TickmarksZ, - TickmarksZLabels) - using Tickmarks = QskScaleTickmarks; - using TickmarksLabels = QVector>; - explicit QskLevelingSensor(QQuickItem* parent = nullptr); -public Q_SLOTS: - void setTickmarks(Qt::Axis axis, Tickmarks tickmarks); - void setTickmarksLabels(Qt::Axis axis, TickmarksLabels labels); - void setAngle(const QVector3D& degree); - void setAngle(Qt::Axis axis, float degree); + using Inherited = QskControl; - void setSubControlRotation(QskAspect::Subcontrol subControl, const QVector3D& degree); + public: + QSK_SUBCONTROLS( OuterDisk, Horizon, TickmarksX, TickmarksXLabels, TickmarksY, TickmarksYLabels, + TickmarksZ, TickmarksZLabels ) -Q_SIGNALS: - void anglesChanged(const QVector3D& degree); - void subControlRotationChanged(QskAspect::Subcontrol subControl, const QVector3D& degree); + using Tickmarks = QskScaleTickmarks; + using TickmarksLabels = QVector< QPair< qreal, QString > >; -public: - Q_REQUIRED_RESULT const Tickmarks& tickmarks(Qt::Axis axis) const; - Q_REQUIRED_RESULT const TickmarksLabels& tickmarkLabels(Qt::Axis axis) const; + explicit QskLevelingSensor( QQuickItem* parent = nullptr ); + ~QskLevelingSensor(); + + public Q_SLOTS: + void setTickmarks( Qt::Axis axis, Tickmarks tickmarks ); + void setTickmarksLabels( Qt::Axis axis, TickmarksLabels labels ); + void setAngle( const QVector3D& degree ); + void setAngle( Qt::Axis axis, float degree ); + + void setSubControlRotation( QskAspect::Subcontrol subControl, const QVector3D& degree ); + + Q_SIGNALS: + void anglesChanged( const QVector3D& degree ); + void subControlRotationChanged( QskAspect::Subcontrol subControl, const QVector3D& degree ); + + public: + Q_REQUIRED_RESULT const Tickmarks& tickmarks( Qt::Axis axis ) const; + Q_REQUIRED_RESULT const TickmarksLabels& tickmarkLabels( Qt::Axis axis ) const; Q_REQUIRED_RESULT const QVector3D& angle() const noexcept; - Q_REQUIRED_RESULT const QVector3D& subControlRotation(QskAspect::Subcontrol subControl) const noexcept; + Q_REQUIRED_RESULT const QVector3D& subControlRotation( + QskAspect::Subcontrol subControl ) const noexcept; -private: - - // TODO use pimpl - - QVector3D m_rotation; - QVector3D m_angle = { 45,45,45 }; - Tickmarks m_tickmarks[3]; - TickmarksLabels m_tickmarksLabels[3]; - - std::unordered_map m_subControlRotation; + private: + class PrivateData; + std::unique_ptr< PrivateData > m_data; }; \ No newline at end of file