qskinny/src/controls/QskSkinHintTable.h

312 lines
8.5 KiB
C
Raw Normal View History

/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
#ifndef QSK_SKIN_HINT_TABLE_H
#define QSK_SKIN_HINT_TABLE_H
2018-08-03 08:15:28 +02:00
#include "QskAnimationHint.h"
#include "QskAspect.h"
#include "QskBoxBorderColors.h"
2018-08-03 08:15:28 +02:00
#include "QskBoxBorderMetrics.h"
#include "QskBoxShapeMetrics.h"
#include "QskGradient.h"
#include "QskMargins.h"
2018-07-19 14:10:48 +02:00
#include <qcolor.h>
2018-08-03 08:15:28 +02:00
#include <qvariant.h>
#include <unordered_map>
2020-12-20 16:04:19 +01:00
class QSK_EXPORT QskSkinHintTable
{
2018-08-03 08:15:28 +02:00
public:
QskSkinHintTable();
QskSkinHintTable( const QskSkinHintTable& other );
~QskSkinHintTable();
QskSkinHintTable& operator=( const QskSkinHintTable& );
2020-12-21 16:06:58 +01:00
void setColor( QskAspect, Qt::GlobalColor );
void setColor( QskAspect, QRgb );
2020-12-21 16:06:58 +01:00
void setColor( QskAspect, const QColor& );
QColor color( QskAspect ) const;
2020-12-21 16:06:58 +01:00
void setMetric( QskAspect, qreal );
qreal metric( QskAspect ) const;
2020-12-21 16:06:58 +01:00
void setStrutSize( QskAspect, const QSizeF& );
QSizeF strutSize( QskAspect ) const;
2020-12-21 16:06:58 +01:00
void setMargin( QskAspect, const QskMargins& );
QskMargins margin( QskAspect ) const;
2020-12-21 16:06:58 +01:00
void setPadding( QskAspect, const QskMargins& );
QskMargins padding( QskAspect ) const;
2020-12-21 16:06:58 +01:00
void setGradient( QskAspect, const QskGradient& );
QskGradient gradient( QskAspect ) const;
2020-12-21 16:06:58 +01:00
void setBoxShape( QskAspect, const QskBoxShapeMetrics& );
QskBoxShapeMetrics boxShape( QskAspect ) const;
2020-12-21 16:06:58 +01:00
void setBoxBorder( QskAspect, const QskBoxBorderMetrics& );
QskBoxBorderMetrics boxBorder( QskAspect ) const;
2020-12-21 16:06:58 +01:00
void setBoxBorderColors( QskAspect, const QskBoxBorderColors& );
QskBoxBorderColors boxBorderColors( QskAspect ) const;
2020-12-21 16:06:58 +01:00
void setSpacing( QskAspect, qreal );
qreal spacing( QskAspect ) const;
2020-12-21 16:06:58 +01:00
void setAlignment( QskAspect, Qt::Alignment );
Qt::Alignment alignment( QskAspect ) const;
2020-12-21 16:06:58 +01:00
void setAnimation( QskAspect, QskAnimationHint );
QskAnimationHint animation( QskAspect ) const;
2020-12-21 16:06:58 +01:00
void setGraphicRole( QskAspect, int role );
void setFontRole( QskAspect, int role );
void setFlagHint( QskAspect, int flag );
2020-12-21 16:06:58 +01:00
void setHint( QskAspect, const QVariant& );
const QVariant& hint( QskAspect ) const;
2020-12-21 16:06:58 +01:00
bool removeHint( QskAspect );
QVariant takeHint( QskAspect );
2020-12-21 16:06:58 +01:00
bool hasHint( QskAspect ) const;
2020-12-21 16:06:58 +01:00
const std::unordered_map< QskAspect, QVariant >& hints() const;
bool hasAnimators() const;
bool hasStates() const;
bool hasHints() const;
void clear();
2020-12-21 16:06:58 +01:00
const QVariant* resolvedHint( QskAspect,
QskAspect* resolvedAspect = nullptr ) const;
2020-12-21 16:06:58 +01:00
QskAspect resolvedAspect( QskAspect ) const;
2020-12-21 16:06:58 +01:00
QskAspect resolvedAnimator(
QskAspect, QskAnimationHint& ) const;
2018-08-03 08:15:28 +02:00
private:
static QVariant invalidHint;
2020-12-21 16:06:58 +01:00
typedef std::unordered_map< QskAspect, QVariant > HintMap;
HintMap* m_hints;
quint16 m_animatorCount;
bool m_hasStates : 1;
};
inline bool QskSkinHintTable::hasHints() const
{
return m_hints != nullptr;
}
inline bool QskSkinHintTable::hasStates() const
{
return m_hasStates;
}
inline bool QskSkinHintTable::hasAnimators() const
{
return m_animatorCount;
}
2020-12-21 16:06:58 +01:00
inline bool QskSkinHintTable::hasHint( QskAspect aspect ) const
{
if ( m_hints != nullptr )
return m_hints->find( aspect ) != m_hints->cend();
return false;
}
2020-12-21 16:06:58 +01:00
inline const QVariant& QskSkinHintTable::hint( QskAspect aspect ) const
{
if ( m_hints != nullptr )
{
auto it = m_hints->find( aspect );
if ( it != m_hints->cend() )
return it->second;
}
return invalidHint;
}
2020-12-21 16:06:58 +01:00
inline void QskSkinHintTable::setColor( QskAspect aspect, const QColor& color )
{
setHint( aspect | QskAspect::Color, color );
}
2020-12-21 16:06:58 +01:00
inline void QskSkinHintTable::setColor( QskAspect aspect, Qt::GlobalColor color )
{
setHint( aspect | QskAspect::Color, QColor( color ) );
}
2020-12-21 16:06:58 +01:00
inline void QskSkinHintTable::setColor( QskAspect aspect, QRgb rgb )
{
setHint( aspect | QskAspect::Color, QColor::fromRgba( rgb ) );
}
2020-12-21 16:06:58 +01:00
inline QColor QskSkinHintTable::color( QskAspect aspect ) const
{
2018-08-03 08:15:28 +02:00
return hint( aspect | QskAspect::Color ).value< QColor >();
}
2020-12-21 16:06:58 +01:00
inline void QskSkinHintTable::setMetric( QskAspect aspect, qreal metric )
{
setHint( aspect | QskAspect::Metric, metric );
}
2020-12-21 16:06:58 +01:00
inline qreal QskSkinHintTable::metric( QskAspect aspect ) const
{
return hint( aspect | QskAspect::Metric ).toReal();
}
2020-12-21 16:06:58 +01:00
inline void QskSkinHintTable::setStrutSize( QskAspect aspect, const QSizeF& size )
{
const auto aspectStrut = aspect | QskAspect::Metric | QskAspect::StrutSize;
setHint( aspectStrut, QVariant::fromValue( size ) );
}
2020-12-21 16:06:58 +01:00
inline QSizeF QskSkinHintTable::strutSize( QskAspect aspect ) const
{
const auto aspectStrut = aspect | QskAspect::Metric | QskAspect::StrutSize;
return hint( aspectStrut ).value< QSizeF >();
}
inline void QskSkinHintTable::setMargin(
2020-12-21 16:06:58 +01:00
QskAspect aspect, const QskMargins& margins )
{
const auto aspectMargin = aspect | QskAspect::Metric | QskAspect::Margin;
setHint( aspectMargin, QVariant::fromValue( margins ) );
}
2020-12-21 16:06:58 +01:00
inline QskMargins QskSkinHintTable::margin( QskAspect aspect ) const
{
const auto aspectMargin = aspect | QskAspect::Metric | QskAspect::Margin;
return hint( aspectMargin ).value< QskMargins >();
}
inline void QskSkinHintTable::setPadding(
2020-12-21 16:06:58 +01:00
QskAspect aspect, const QskMargins& padding )
{
const auto aspectPadding = aspect | QskAspect::Metric | QskAspect::Padding;
setHint( aspectPadding, QVariant::fromValue( padding ) );
}
2020-12-21 16:06:58 +01:00
inline QskMargins QskSkinHintTable::padding( QskAspect aspect ) const
{
const auto aspectPadding = aspect | QskAspect::Metric | QskAspect::Padding;
return hint( aspectPadding ).value< QskMargins >();
}
inline void QskSkinHintTable::setGradient(
2020-12-21 16:06:58 +01:00
QskAspect aspect, const QskGradient& gradient )
{
setHint( aspect | QskAspect::Color, QVariant::fromValue( gradient ) );
}
2020-12-21 16:06:58 +01:00
inline QskGradient QskSkinHintTable::gradient( QskAspect aspect ) const
{
return hint( aspect | QskAspect::Color ).value< QskGradient >();
}
inline void QskSkinHintTable::setBoxShape(
2020-12-21 16:06:58 +01:00
QskAspect aspect, const QskBoxShapeMetrics& shape )
{
2020-12-21 16:06:58 +01:00
using A = QskAspect;
setHint( aspect | A::Shape | A::Metric, QVariant::fromValue( shape ) );
}
2020-12-21 16:06:58 +01:00
inline QskBoxShapeMetrics QskSkinHintTable::boxShape( QskAspect aspect ) const
{
2020-12-21 16:06:58 +01:00
using A = QskAspect;
return hint( aspect | A::Shape | A::Metric ).value< QskBoxShapeMetrics >();
}
inline void QskSkinHintTable::setBoxBorder(
2020-12-21 16:06:58 +01:00
QskAspect aspect, const QskBoxBorderMetrics& border )
{
2020-12-21 16:06:58 +01:00
using A = QskAspect;
setHint( aspect | A::Border | A::Metric, QVariant::fromValue( border ) );
}
2018-08-03 08:15:28 +02:00
2020-12-21 16:06:58 +01:00
inline QskBoxBorderMetrics QskSkinHintTable::boxBorder( QskAspect aspect ) const
{
2020-12-21 16:06:58 +01:00
using A = QskAspect;
return hint( aspect | A::Border | A::Metric ).value< QskBoxBorderMetrics >();
}
2018-08-03 08:15:28 +02:00
inline void QskSkinHintTable::setBoxBorderColors(
2020-12-21 16:06:58 +01:00
QskAspect aspect, const QskBoxBorderColors& colors )
2018-08-03 08:15:28 +02:00
{
2020-12-21 16:06:58 +01:00
using A = QskAspect;
setHint( aspect | A::Border | A::Color, QVariant::fromValue( colors ) );
2018-08-03 08:15:28 +02:00
}
2020-12-21 16:06:58 +01:00
inline QskBoxBorderColors QskSkinHintTable::boxBorderColors( QskAspect aspect ) const
{
2020-12-21 16:06:58 +01:00
using A = QskAspect;
return hint( aspect | A::Border | A::Color ).value< QskBoxBorderColors >();
2018-08-03 08:15:28 +02:00
}
2020-12-21 16:06:58 +01:00
inline void QskSkinHintTable::setSpacing( QskAspect aspect, qreal spacing )
{
setMetric( aspect | QskAspect::Spacing, spacing );
}
2020-12-21 16:06:58 +01:00
inline qreal QskSkinHintTable::spacing( QskAspect aspect ) const
{
return metric( aspect | QskAspect::Spacing );
2020-08-09 10:45:48 +02:00
}
2020-12-21 16:06:58 +01:00
inline QskAnimationHint QskSkinHintTable::animation( QskAspect aspect ) const
{
aspect.setAnimator( true );
return hint( aspect ).value< QskAnimationHint >();
}
2020-12-21 16:06:58 +01:00
inline void QskSkinHintTable::setGraphicRole( QskAspect aspect, int role )
{
setHint( aspect | QskAspect::GraphicRole, role );
}
2020-12-21 16:06:58 +01:00
inline void QskSkinHintTable::setFontRole( QskAspect aspect, int role )
{
setHint( aspect | QskAspect::FontRole, role );
}
inline void QskSkinHintTable::setAlignment(
2020-12-21 16:06:58 +01:00
QskAspect aspect, Qt::Alignment alignment )
{
setFlagHint( aspect | QskAspect::Alignment, alignment );
}
2020-12-21 16:06:58 +01:00
inline Qt::Alignment QskSkinHintTable::alignment( QskAspect aspect ) const
{
return hint( aspect | QskAspect::Alignment ).value< Qt::Alignment >();
}
2020-12-21 16:06:58 +01:00
inline void QskSkinHintTable::setFlagHint( QskAspect aspect, int flag )
{
setHint( aspect, QVariant( flag ) );
}
inline void QskSkinHintTable::setAnimation(
2020-12-21 16:06:58 +01:00
QskAspect aspect, QskAnimationHint animation )
2018-08-03 08:15:28 +02:00
{
aspect.setAnimator( true );
2018-08-03 08:15:28 +02:00
setHint( aspect, QVariant::fromValue( animation ) );
}
#endif