From be37e3123808be44170beeea413b97ef9a312ef2 Mon Sep 17 00:00:00 2001
From: Jan Arve Saether <jan-arve.saether@digia.com>
Date: Fri, 28 Feb 2014 09:40:47 +0100
Subject: [PATCH] Avoid warning when maximum size < minimum size

This only happens in intermediate states, where both *minimumHeight*
and *maximumHeight* is supposed to be updated.
However, since the order of the bindings is not know, we might
have intermediate states where minimumHeight > maximumHeight.

In our case minimumHeight was set first to a smaller size than
maximumHeight, this would trigger the height binding, causing it to try
to set its geometry while its minimumHeight and maximumHeight properties
were not in sync.

This also happened when maximumWidth < minimumWidth.

The output was something like this:

qwindowswindow.cpp(1306):QWindowsWindow::setGeometry: Attempt to set a
size (116x190) violating the constraints(0x200 - 16777215x190) on
window ApplicationWindow_QMLTYPE_12_QML_51/''

Change-Id: Ia4f6e340b608bb4cff5a35bc887adc4eea7efbad
Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
---
 src/controls/ApplicationWindow.qml            | 20 +++++++++-
 .../controls/data/tst_applicationwindow.qml   | 38 +++++++++++++++++++
 2 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/src/controls/ApplicationWindow.qml b/src/controls/ApplicationWindow.qml
index f54fa8f51..bb969946e 100644
--- a/src/controls/ApplicationWindow.qml
+++ b/src/controls/ApplicationWindow.qml
@@ -149,8 +149,24 @@ Window {
     */
     readonly property real __qwindowsize_max: (1 << 24) - 1
 
-    width: contentArea.__noImplicitWidthGiven ? 0 : Math.min(Math.max(minimumWidth, contentArea.implicitWidth), maximumWidth)
-    height: contentArea.__noImplicitHeightGiven ? 0 : Math.min(Math.max(minimumHeight, contentArea.implicitHeight + __topBottomMargins), maximumHeight)
+    /*! \internal */
+    property real __width: 0
+    Binding {
+        target: root
+        property: "__width"
+        when: root.minimumWidth <= root.maximumWidth
+        value: Math.max(Math.min(root.maximumWidth, contentArea.implicitWidth), root.minimumWidth)
+    }
+    /*! \internal */
+    property real __height: 0
+    Binding {
+        target: root
+        property: "__height"
+        when: root.minimumHeight <= root.maximumHeight
+        value: Math.max(Math.min(root.maximumHeight, contentArea.implicitHeight), root.minimumHeight)
+    }
+    width: contentArea.__noImplicitWidthGiven ? 0 : __width
+    height: contentArea.__noImplicitHeightGiven ? 0 : __height
 
     minimumWidth: contentArea.__noMinimumWidthGiven ? 0 : contentArea.minimumWidth
     minimumHeight: contentArea.__noMinimumHeightGiven ? 0 : (contentArea.minimumHeight + __topBottomMargins)
diff --git a/tests/auto/controls/data/tst_applicationwindow.qml b/tests/auto/controls/data/tst_applicationwindow.qml
index cfe6e0a33..8e35caff1 100644
--- a/tests/auto/controls/data/tst_applicationwindow.qml
+++ b/tests/auto/controls/data/tst_applicationwindow.qml
@@ -168,5 +168,43 @@ TestCase {
         }
     }
 
+    function test_minimumSizeLargerThan_MaximumSize() {
+        var test_control = 'import QtQuick 2.1; \
+        import QtQuick.Controls 1.1;            \
+        import QtQuick.Layouts 1.1;             \
+        ApplicationWindow {                     \
+            minimumWidth: 200;                  \
+            maximumWidth: 200;                  \
+            minimumHeight: 200;                 \
+            maximumHeight: 200;                 \
+            Rectangle {                         \
+                implicitWidth: 1;               \
+                implicitHeight: 20;             \
+            }                                   \
+        }                                       '
+
+        var window = Qt.createQmlObject(test_control, container, '')
+        window.visible = true
+        wait(0)
+        // The following two calls will set the min,max range to be invalid
+        // this should *not* produce a warning
+        compare(window.height, 200)
+        window.maximumHeight -= 10
+        window.minimumHeight += 10
+        // Restore min,max range back to sane values
+        window.maximumHeight += 20
+        compare(window.height, 210)
+
+        // Do the same test for width
+        compare(window.width, 200)
+        window.maximumWidth-= 10
+        window.minimumWidth+= 10
+        // Restore back to sane values
+        window.maximumWidth += 20
+        compare(window.width, 210)
+
+        window.destroy()
+    }
+
 }
 }
-- 
GitLab