QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskMargins.h
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#ifndef QSK_MARGINS_H
7#define QSK_MARGINS_H
8
9#include "QskGlobal.h"
10
11#include <qmargins.h>
12#include <qmetatype.h>
13#include <qnamespace.h>
14
15class QVariant;
16
17/*
18 Having a wrapper like this feels a bit stupid, but unfortunately QMarginsF
19 is not registered as Qt metatype and doing this in an external library
20 is an obvious source for conflicts with application code.
21 */
22class QSK_EXPORT QskMargins : public QMarginsF
23{
24 Q_GADGET
25
26 Q_PROPERTY( qreal left READ left WRITE setLeft )
27 Q_PROPERTY( qreal top READ top WRITE setTop )
28 Q_PROPERTY( qreal right READ right WRITE setRight )
29 Q_PROPERTY( qreal bottom READ bottom WRITE setBottom )
30
31 public:
32 constexpr QskMargins() noexcept = default;
33 constexpr QskMargins( const QMarginsF& ) noexcept;
34 constexpr QskMargins( const QMargins& ) noexcept;
35
36 constexpr QskMargins( qreal left, qreal top, qreal right, qreal bottom ) noexcept;
37 constexpr QskMargins( qreal margin ) noexcept;
38 constexpr QskMargins( qreal horizontal, qreal vertical ) noexcept;
39
40 QskMargins mirrored( Qt::Orientations ) const noexcept;
41 constexpr QskMargins rotated() const noexcept;
42
43 constexpr QskMargins translated( qreal dx, qreal dy ) const noexcept;
44
45 constexpr QskMargins grownBy( qreal dx, qreal dy ) const noexcept;
46 constexpr QskMargins shrunkBy( qreal dx, qreal dy ) const noexcept;
47
48 constexpr QskMargins expandedTo( const QskMargins& ) const noexcept;
49 constexpr QskMargins boundedTo( const QskMargins& ) const noexcept;
50
51 void setMargins( qreal margin ) noexcept;
52 void setMargins( qreal horizontal, qreal vertical ) noexcept;
53
54 void setMarginsAt( Qt::Edges, qreal ) noexcept;
55 qreal marginAt( Qt::Edge ) const noexcept;
56
57 constexpr qreal width() const noexcept;
58 constexpr qreal height() const noexcept;
59 constexpr qreal extent( Qt::Orientation ) const noexcept;
60
61 QskMargins interpolated( const QskMargins&, qreal progress ) const noexcept;
62
63 constexpr bool isExpanding() const noexcept;
64 constexpr bool isEquidistant() const noexcept;
65
66 static QVariant interpolate( const QskMargins&,
67 const QskMargins&, qreal progress ) noexcept;
68};
69
70constexpr inline QskMargins::QskMargins( qreal margin ) noexcept
71 : QskMargins( margin, margin, margin, margin )
72{
73}
74
75constexpr inline QskMargins::QskMargins(
76 qreal horizontal, qreal vertical ) noexcept
77 : QskMargins( horizontal, vertical, horizontal, vertical )
78{
79}
80
81constexpr QskMargins::QskMargins(
82 qreal left, qreal top, qreal right, qreal bottom ) noexcept
83 : QMarginsF( left, top, right, bottom )
84{
85}
86
87constexpr inline QskMargins::QskMargins( const QMarginsF& margins ) noexcept
88 : QMarginsF( margins )
89{
90}
91
92constexpr inline QskMargins::QskMargins( const QMargins& margins ) noexcept
93 : QMarginsF( margins )
94{
95}
96
97inline void QskMargins::setMargins( qreal margin ) noexcept
98{
99 *this = QskMargins( margin );
100}
101
102inline constexpr bool QskMargins::isExpanding() const noexcept
103{
104 return ( left() > 0 ) || ( right() > 0 ) || ( top() > 0 ) || ( right() > 0 );
105}
106
107inline void QskMargins::setMargins( qreal horizontal, qreal vertical ) noexcept
108{
109 *this = QskMargins( horizontal, vertical );
110}
111
112inline QskMargins QskMargins::mirrored(
113 Qt::Orientations orientations ) const noexcept
114{
115 switch ( int( orientations ) )
116 {
117 case Qt::Vertical:
118 return QskMargins( left(), bottom(), right(), top() );
119
120 case Qt::Horizontal:
121 return QskMargins( right(), top(), left(), bottom() );
122
123 case Qt::Vertical | Qt::Horizontal:
124 return QskMargins( right(), bottom(), left(), top() );
125
126 default:
127 return *this;
128 }
129}
130
131constexpr inline QskMargins QskMargins::rotated() const noexcept
132{
133 return QskMargins( top(), left(), bottom(), right() );
134}
135
136constexpr inline QskMargins QskMargins::translated( qreal dx, qreal dy ) const noexcept
137{
138 return QskMargins( left() + dx, top() + dy, right() - dx, bottom() - dy );
139}
140
141constexpr inline QskMargins QskMargins::grownBy( qreal dx, qreal dy ) const noexcept
142{
143 return QskMargins( left() + dx, top() + dy, right() + dx, bottom() + dy );
144}
145
146constexpr inline QskMargins QskMargins::shrunkBy( qreal dx, qreal dy ) const noexcept
147{
148 return QskMargins( left() - dx, top() - dy, right() - dx, bottom() - dy );
149}
150
151constexpr inline QskMargins QskMargins::expandedTo( const QskMargins& other ) const noexcept
152{
153 return QskMargins(
154 qMax( left(), other.left() ),
155 qMax( top(), other.top() ),
156 qMax( right(), other.right() ),
157 qMax( bottom(), other.bottom() )
158 );
159}
160
161constexpr inline QskMargins QskMargins::boundedTo( const QskMargins& other ) const noexcept
162{
163 return QskMargins(
164 qMin( left(), other.left() ),
165 qMin( top(), other.top() ),
166 qMin( right(), other.right() ),
167 qMin( bottom(), other.bottom() )
168 );
169}
170constexpr inline qreal QskMargins::extent( Qt::Orientation orientation ) const noexcept
171{
172 return ( orientation == Qt::Horizontal ) ? width() : height();
173}
174
175constexpr inline qreal QskMargins::width() const noexcept
176{
177 return left() + right();
178}
179
180constexpr inline qreal QskMargins::height() const noexcept
181{
182 return top() + bottom();
183}
184
185inline constexpr bool QskMargins::isEquidistant() const noexcept
186{
187 return ( left() == top() ) && ( left() == right() ) && ( left() == bottom() );
188}
189
190Q_DECLARE_TYPEINFO( QskMargins, Q_MOVABLE_TYPE );
191Q_DECLARE_METATYPE( QskMargins )
192
193#endif