11#include " marginwrapper.hpp"
2- #include < algorithm>
32
43#include < qobject.h>
54#include < qquickitem.h>
@@ -17,6 +16,35 @@ MarginWrapperManager::MarginWrapperManager(QObject* parent): WrapperManager(pare
1716 this ,
1817 &MarginWrapperManager::onChildChanged
1918 );
19+
20+ this ->bTopMargin .setBinding ([this ] {
21+ return this ->bExtraMargin
22+ + (this ->bTopMarginSet .value () ? this ->bTopMarginValue : this ->bMargin );
23+ });
24+
25+ this ->bBottomMargin .setBinding ([this ] {
26+ return this ->bExtraMargin
27+ + (this ->bBottomMarginSet .value () ? this ->bBottomMarginValue : this ->bMargin );
28+ });
29+
30+ this ->bLeftMargin .setBinding ([this ] {
31+ return this ->bExtraMargin
32+ + (this ->bLeftMarginSet .value () ? this ->bLeftMarginValue : this ->bMargin );
33+ });
34+
35+ this ->bRightMargin .setBinding ([this ] {
36+ return this ->bExtraMargin
37+ + (this ->bRightMarginSet .value () ? this ->bRightMarginValue : this ->bMargin );
38+ });
39+
40+ // Coalesces updates via binding infrastructure
41+ this ->bUpdateWatcher .setBinding ([this ] {
42+ this ->bTopMargin .value ();
43+ this ->bBottomMargin .value ();
44+ this ->bLeftMargin .value ();
45+ this ->bRightMargin .value ();
46+ return 0 ;
47+ });
2048}
2149
2250void MarginWrapperManager::componentComplete () {
@@ -41,15 +69,6 @@ void MarginWrapperManager::componentComplete() {
4169 if (!this ->mChild ) this ->updateGeometry ();
4270}
4371
44- qreal MarginWrapperManager::margin () const { return this ->mMargin ; }
45-
46- void MarginWrapperManager::setMargin (qreal margin) {
47- if (margin == this ->mMargin ) return ;
48- this ->mMargin = margin;
49- this ->updateGeometry ();
50- emit this ->marginChanged ();
51- }
52-
5372bool MarginWrapperManager::resizeChild () const { return this ->mResizeChild ; }
5473
5574void MarginWrapperManager::setResizeChild (bool resizeChild) {
@@ -82,33 +101,36 @@ void MarginWrapperManager::onChildChanged() {
82101}
83102
84103qreal MarginWrapperManager::targetChildWidth () const {
85- auto max = this ->mWrapper ->width () - this ->mMargin * 2 ;
104+ auto max = this ->mWrapper ->width () - ( this ->bLeftMargin + this -> bRightMargin ) ;
86105
87106 if (this ->mResizeChild ) return max;
88- else return std::min ( this ->mChild ->implicitWidth (), max );
107+ else return this ->mChild ->implicitWidth ();
89108}
90109
91110qreal MarginWrapperManager::targetChildHeight () const {
92- auto max = this ->mWrapper ->height () - this ->mMargin * 2 ;
111+ auto max = this ->mWrapper ->height () - ( this ->bTopMargin + this -> bBottomMargin ) ;
93112
94113 if (this ->mResizeChild ) return max;
95- else return std::min ( this ->mChild ->implicitHeight (), max );
114+ else return this ->mChild ->implicitHeight ();
96115}
97116
98117qreal MarginWrapperManager::targetChildX () const {
99- if (this ->mResizeChild ) return this ->mMargin ;
118+ if (this ->mResizeChild ) return this ->bLeftMargin ;
100119 else {
101- return std::max (this ->mMargin , this ->mWrapper ->width () / 2 - this ->mChild ->implicitWidth () / 2 );
120+ auto total = this ->bLeftMargin + this ->bRightMargin ;
121+ auto mul = total == 0 ? 0.5 : this ->bLeftMargin / total;
122+ auto margin = this ->mWrapper ->width () - this ->mChild ->implicitWidth ();
123+ return margin * mul;
102124 }
103125}
104126
105127qreal MarginWrapperManager::targetChildY () const {
106- if (this ->mResizeChild ) return this ->mMargin ;
128+ if (this ->mResizeChild ) return this ->bTopMargin ;
107129 else {
108- return std::max (
109- this ->mMargin ,
110- this ->mWrapper ->height () / 2 - this ->mChild ->implicitHeight () / 2
111- ) ;
130+ auto total = this -> bTopMargin + this -> bBottomMargin ;
131+ auto mul = total == 0 ? 0.5 : this ->bTopMargin / total;
132+ auto margin = this ->mWrapper ->height () - this ->mChild ->implicitHeight ();
133+ return margin * mul ;
112134 }
113135}
114136
@@ -126,7 +148,9 @@ void MarginWrapperManager::updateChildY() {
126148
127149void MarginWrapperManager::onChildImplicitWidthChanged () {
128150 if (!this ->mChild || !this ->mWrapper ) return ;
129- this ->mWrapper ->setImplicitWidth (this ->mChild ->implicitWidth () + this ->mMargin * 2 );
151+ this ->mWrapper ->setImplicitWidth (
152+ this ->mChild ->implicitWidth () + this ->bLeftMargin + this ->bRightMargin
153+ );
130154
131155 // If the implicit width change does not result in an actual width change,
132156 // this will not be called anywhere else.
@@ -135,7 +159,9 @@ void MarginWrapperManager::onChildImplicitWidthChanged() {
135159
136160void MarginWrapperManager::onChildImplicitHeightChanged () {
137161 if (!this ->mChild || !this ->mWrapper ) return ;
138- this ->mWrapper ->setImplicitHeight (this ->mChild ->implicitHeight () + this ->mMargin * 2 );
162+ this ->mWrapper ->setImplicitHeight (
163+ this ->mChild ->implicitHeight () + this ->bTopMargin + this ->bBottomMargin
164+ );
139165
140166 // If the implicit height change does not result in an actual height change,
141167 // this will not be called anywhere else.
@@ -146,15 +172,19 @@ void MarginWrapperManager::updateGeometry() {
146172 if (!this ->mWrapper ) return ;
147173
148174 if (this ->mChild ) {
149- this ->mWrapper ->setImplicitWidth (this ->mChild ->implicitWidth () + this ->mMargin * 2 );
150- this ->mWrapper ->setImplicitHeight (this ->mChild ->implicitHeight () + this ->mMargin * 2 );
175+ this ->mWrapper ->setImplicitWidth (
176+ this ->mChild ->implicitWidth () + this ->bLeftMargin + this ->bRightMargin
177+ );
178+ this ->mWrapper ->setImplicitHeight (
179+ this ->mChild ->implicitHeight () + this ->bTopMargin + this ->bBottomMargin
180+ );
151181 this ->mChild ->setX (this ->targetChildX ());
152182 this ->mChild ->setY (this ->targetChildY ());
153183 this ->mChild ->setWidth (this ->targetChildWidth ());
154184 this ->mChild ->setHeight (this ->targetChildHeight ());
155185 } else {
156- this ->mWrapper ->setImplicitWidth (this ->mMargin * 2 );
157- this ->mWrapper ->setImplicitHeight (this ->mMargin * 2 );
186+ this ->mWrapper ->setImplicitWidth (this ->bLeftMargin + this -> bRightMargin );
187+ this ->mWrapper ->setImplicitHeight (this ->bTopMargin + this -> bBottomMargin );
158188 }
159189}
160190
0 commit comments