aboutsummaryrefslogtreecommitdiffstats
path: root/src/imports/nativestyle/qstyle
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2020-06-05 09:28:00 +0200
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2020-06-08 09:06:43 +0000
commit62fa08d89eadfcd79cd4a51b997223eb6ec24d5e (patch)
tree5838ee3777cd5ac03db9a369afab175e8600e0d8 /src/imports/nativestyle/qstyle
parent2275a0896c7b7e648641f4c6977a02006a581577 (diff)
Native style: make it possible to set nine patch margins from the style
Up till now, all images have been scaled using nine patch images with margins set to be at the center. This was doomed to be too simplistic at some point, at that point is now (when working on comboboxes). So instead, calculate the nine patch margins from the style. This will let the different styles set pixel perfect margins per control that matches the image they draw. If left unspecified by a style, the default implementation in QCommonStyle will return the old logic of using the center. We also add support for specifying if an image can be scaled horizontally or vertically by setting the right or bottom nine patch margin to -1 respectively. E.g on macOS, an NSButton cannot be scaled vertically, so the image we draw will not look native if we do. Change-Id: Icaf232748b8d15f06f9b289e164b5c8fb86a6c7b Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
Diffstat (limited to 'src/imports/nativestyle/qstyle')
-rw-r--r--src/imports/nativestyle/qstyle/qquickcommonstyle.cpp16
-rw-r--r--src/imports/nativestyle/qstyle/qquickcommonstyle.h2
-rw-r--r--src/imports/nativestyle/qstyle/qquickstyle.h2
3 files changed, 20 insertions, 0 deletions
diff --git a/src/imports/nativestyle/qstyle/qquickcommonstyle.cpp b/src/imports/nativestyle/qstyle/qquickcommonstyle.cpp
index 0cb8e848..e8dadb9e 100644
--- a/src/imports/nativestyle/qstyle/qquickcommonstyle.cpp
+++ b/src/imports/nativestyle/qstyle/qquickcommonstyle.cpp
@@ -4694,6 +4694,22 @@ QFont QCommonStyle::font(QStyle::ControlElement element, const QStyle::State sta
return QGuiApplication::font();
}
+QMargins QCommonStyle::ninePatchMargins(QStyle::ControlElement ce, const QStyleOption *opt, const QSize &imageSize) const
+{
+ // By default, we just divide the image at the center
+ int w = imageSize.width() / 2;
+ int h = imageSize.height() / 2;
+ return QMargins(w, h, w, h);
+}
+
+QMargins QCommonStyle::ninePatchMargins(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, const QSize &imageSize) const
+{
+ // By default, we just divide the image at the center
+ int w = imageSize.width() / 2;
+ int h = imageSize.height() / 2;
+ return QMargins(w, h, w, h);
+}
+
int QCommonStyle::styleHint(StyleHint sh, const QStyleOption *opt, QStyleHintReturn *hret) const
{
int ret = 0;
diff --git a/src/imports/nativestyle/qstyle/qquickcommonstyle.h b/src/imports/nativestyle/qstyle/qquickcommonstyle.h
index 77626843..894194a0 100644
--- a/src/imports/nativestyle/qstyle/qquickcommonstyle.h
+++ b/src/imports/nativestyle/qstyle/qquickcommonstyle.h
@@ -62,6 +62,8 @@ public:
QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize) const override;
QFont font(ControlElement element, const QStyle::State state) const override;
+ QMargins ninePatchMargins(ControlElement ce, const QStyleOption *opt, const QSize &imageSize) const override;
+ QMargins ninePatchMargins(ComplexControl cc, const QStyleOptionComplex *opt, const QSize &imageSize) const override;
int pixelMetric(PixelMetric m, const QStyleOption *opt = nullptr) const override;
int styleHint(StyleHint sh, const QStyleOption *opt = nullptr, QStyleHintReturn *shret = nullptr) const override;
diff --git a/src/imports/nativestyle/qstyle/qquickstyle.h b/src/imports/nativestyle/qstyle/qquickstyle.h
index 91e09f4b..bc0cccf7 100644
--- a/src/imports/nativestyle/qstyle/qquickstyle.h
+++ b/src/imports/nativestyle/qstyle/qquickstyle.h
@@ -782,6 +782,8 @@ public:
virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize) const = 0;
virtual QFont font(ControlElement element, const QStyle::State state) const = 0;
+ virtual QMargins ninePatchMargins(ControlElement ce, const QStyleOption *opt, const QSize &imageSize) const = 0;
+ virtual QMargins ninePatchMargins(ComplexControl cc, const QStyleOptionComplex *opt, const QSize &imageSize) const = 0;
virtual SubControl hitTestComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, const QPoint &pt) const = 0;