using private data

This commit is contained in:
Vogel, Rick 2023-07-24 10:56:26 +02:00
parent 5cb4fea788
commit 37183bb637
3 changed files with 111 additions and 98 deletions

View File

@ -16,6 +16,7 @@
#include <QskLinearBox.h> #include <QskLinearBox.h>
#include <QskSlider.h> #include <QskSlider.h>
#include <QskTextLabel.h> #include <QskTextLabel.h>
#include <QskScaleTickmarks.h>
#include <QGuiApplication> #include <QGuiApplication>
#include <QQuickWindow> #include <QQuickWindow>

View File

@ -1,6 +1,10 @@
#include "QskLevelingSensor.h" #include "QskLevelingSensor.h"
#include <QskScaleTickmarks.h>
#include <QskFunctions.h> #include <QskFunctions.h>
#include <QskScaleTickmarks.h>
#include <unordered_map>
#include <QVector2D>
#include <QVector3D>
namespace namespace
{ {
@ -26,7 +30,8 @@ namespace
return false; 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; return axis == Qt::XAxis || axis == Qt::YAxis || axis == Qt::ZAxis;
} }
} }
@ -40,35 +45,49 @@ QSK_SUBCONTROL(QskLevelingSensor, TickmarksYLabels)
QSK_SUBCONTROL( QskLevelingSensor, TickmarksZ ) QSK_SUBCONTROL( QskLevelingSensor, TickmarksZ )
QSK_SUBCONTROL( QskLevelingSensor, TickmarksZLabels ) QSK_SUBCONTROL( QskLevelingSensor, TickmarksZLabels )
#define RETURN_IF_FALSE(expr) if(!(expr)) return; #define RETURN_IF_FALSE( expr ) \
if ( !( expr ) ) \
return;
using Q = QskLevelingSensor; using Q = QskLevelingSensor;
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 ) QskLevelingSensor::QskLevelingSensor( QQuickItem* const parent )
: Inherited( parent ) : Inherited( parent )
, m_data( std::make_unique< QskLevelingSensor::PrivateData >() )
{ {
} }
QskLevelingSensor::~QskLevelingSensor() = default;
void QskLevelingSensor::setTickmarks( const Qt::Axis axis, QskScaleTickmarks tickmarks ) void QskLevelingSensor::setTickmarks( const Qt::Axis axis, QskScaleTickmarks tickmarks )
{ {
RETURN_IF_FALSE( isAxis( axis ) ); RETURN_IF_FALSE( isAxis( axis ) );
m_tickmarks[axis] = std::move(tickmarks); m_data->m_tickmarks[ axis ] = std::move( tickmarks );
update(); update();
} }
void QskLevelingSensor::setTickmarksLabels( const Qt::Axis axis, TickmarksLabels labels ) void QskLevelingSensor::setTickmarksLabels( const Qt::Axis axis, TickmarksLabels labels )
{ {
RETURN_IF_FALSE( isAxis( axis ) ); RETURN_IF_FALSE( isAxis( axis ) );
m_tickmarksLabels[axis] = std::move(labels); m_data->m_tickmarksLabels[ axis ] = std::move( labels );
update(); 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(); update();
Q_EMIT anglesChanged(m_angle); Q_EMIT anglesChanged( m_data->m_angle );
} }
} }
@ -76,10 +95,10 @@ 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(); update();
Q_EMIT anglesChanged(m_angle); Q_EMIT anglesChanged( m_data->m_angle );
} }
} }
@ -87,7 +106,7 @@ 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; static const QskScaleTickmarks invalid;
return invalid; return invalid;
@ -97,7 +116,7 @@ const QskLevelingSensor::TickmarksLabels& QskLevelingSensor::tickmarkLabels(Qt::
{ {
if ( isAxis( axis ) ) if ( isAxis( axis ) )
{ {
return m_tickmarksLabels[axis]; return m_data->m_tickmarksLabels[ axis ];
} }
static const QskLevelingSensor::TickmarksLabels invalid; static const QskLevelingSensor::TickmarksLabels invalid;
return invalid; return invalid;
@ -105,24 +124,27 @@ const QskLevelingSensor::TickmarksLabels& QskLevelingSensor::tickmarkLabels(Qt::
const QVector3D& QskLevelingSensor::angle() const noexcept 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; static const QVector3D notFound;
const auto found = m_subControlRotation.find(subControl); const auto found = m_data->m_subControlRotation.find( subControl );
if(found == m_subControlRotation.end()) { if ( found == m_data->m_subControlRotation.end() )
{
return notFound; return notFound;
} }
return found->second; 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) auto updateSubControlRotation = [ this ]( const QskAspect::Subcontrol subControl,
{ const QVector3D& degree ) {
if ( compareExchange( m_subControlRotation[ subControl ], degree ) ) if ( compareExchange( m_data->m_subControlRotation[ subControl ], degree ) )
{ {
Q_EMIT subControlRotationChanged( subControl, degree ); Q_EMIT subControlRotationChanged( subControl, degree );
update(); update();

View File

@ -1,29 +1,25 @@
#pragma once #pragma once
#include <QskControl.h> #include <QskControl.h>
#include <QskScaleTickmarks.h> #include <memory>
#include <QVector3D>
#include <QskAspect.h>
#include <unordered_map> class QskScaleTickmarks;
class QSK_EXPORT QskLevelingSensor : public QskControl class QSK_EXPORT QskLevelingSensor : public QskControl
{ {
Q_OBJECT Q_OBJECT
using Inherited = QskControl; using Inherited = QskControl;
public: public:
QSK_SUBCONTROLS( QSK_SUBCONTROLS( OuterDisk, Horizon, TickmarksX, TickmarksXLabels, TickmarksY, TickmarksYLabels,
OuterDisk, TickmarksZ, TickmarksZLabels )
Horizon,
TickmarksX,
TickmarksXLabels,
TickmarksY,
TickmarksYLabels,
TickmarksZ,
TickmarksZLabels)
using Tickmarks = QskScaleTickmarks; using Tickmarks = QskScaleTickmarks;
using TickmarksLabels = QVector< QPair< qreal, QString > >; using TickmarksLabels = QVector< QPair< qreal, QString > >;
explicit QskLevelingSensor( QQuickItem* parent = nullptr ); explicit QskLevelingSensor( QQuickItem* parent = nullptr );
~QskLevelingSensor();
public Q_SLOTS: public Q_SLOTS:
void setTickmarks( Qt::Axis axis, Tickmarks tickmarks ); void setTickmarks( Qt::Axis axis, Tickmarks tickmarks );
void setTickmarksLabels( Qt::Axis axis, TickmarksLabels labels ); void setTickmarksLabels( Qt::Axis axis, TickmarksLabels labels );
@ -40,16 +36,10 @@ public:
Q_REQUIRED_RESULT const Tickmarks& tickmarks( Qt::Axis axis ) const; Q_REQUIRED_RESULT const Tickmarks& tickmarks( Qt::Axis axis ) const;
Q_REQUIRED_RESULT const TickmarksLabels& tickmarkLabels( 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& 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: private:
class PrivateData;
// TODO use pimpl std::unique_ptr< PrivateData > m_data;
QVector3D m_rotation;
QVector3D m_angle = { 45,45,45 };
Tickmarks m_tickmarks[3];
TickmarksLabels m_tickmarksLabels[3];
std::unordered_map<QskAspect::Subcontrol, QVector3D> m_subControlRotation;
}; };