ContentItem.qml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Quick Controls module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. import QtQuick 2.2
  40. import QtQuick.Layouts 1.1
  41. Item {
  42. id: contentItem
  43. property real minimumWidth: __calcMinimum('Width')
  44. property real minimumHeight: __calcMinimum('Height')
  45. property real maximumWidth: Number.POSITIVE_INFINITY
  46. property real maximumHeight: Number.POSITIVE_INFINITY
  47. implicitWidth: __calcImplicitWidth()
  48. implicitHeight: __calcImplicitHeight()
  49. /*! \internal */
  50. property Item __layoutItem: contentItem.visibleChildren.length === 1 ? contentItem.visibleChildren[0] : null
  51. /*! \internal */
  52. property real __marginsWidth: __layoutItem ? __layoutItem.anchors.leftMargin + __layoutItem.anchors.rightMargin : 0
  53. /*! \internal */
  54. property real __marginsHeight: __layoutItem ? __layoutItem.anchors.topMargin + __layoutItem.anchors.bottomMargin : 0
  55. /*! \internal */
  56. property bool __noMinimumWidthGiven : false
  57. /*! \internal */
  58. property bool __noMinimumHeightGiven : false
  59. /*! \internal */
  60. property bool __noImplicitWidthGiven : false
  61. /*! \internal */
  62. property bool __noImplicitHeightGiven : false
  63. function __calcImplicitWidth() {
  64. if (__layoutItem && __layoutItem.anchors.fill)
  65. return __calcImplicit('Width')
  66. return contentItem.childrenRect.x + contentItem.childrenRect.width
  67. }
  68. function __calcImplicitHeight() {
  69. if (__layoutItem && __layoutItem.anchors.fill)
  70. return __calcImplicit('Height')
  71. return contentItem.childrenRect.y + contentItem.childrenRect.height
  72. }
  73. function __calcImplicit(hw) {
  74. var pref = __layoutItem.Layout['preferred' + hw]
  75. if (pref < 0) {
  76. pref = __layoutItem['implicit' + hw]
  77. }
  78. contentItem['__noImplicit' + hw + 'Given'] = (pref === 0 ? true : false)
  79. pref += contentItem['__margins' + hw]
  80. return pref
  81. }
  82. function __calcMinimum(hw) { // hw is 'Width' or 'Height'
  83. return (__layoutItem && __layoutItem.anchors.fill) ? __calcMinMax('minimum', hw) : 0
  84. }
  85. function __calcMaximum(hw) { // hw is 'Width' or 'Height'
  86. return (__layoutItem && __layoutItem.anchors.fill) ? __calcMinMax('maximum', hw) : Number.POSITIVE_INFINITY
  87. }
  88. function __calcMinMax(minMaxConstraint, hw) {
  89. var attachedPropName = minMaxConstraint + hw
  90. var extent = __layoutItem.Layout[attachedPropName]
  91. if (minMaxConstraint === 'minimum')
  92. contentItem['__noMinimum' + hw + 'Given'] = (extent === 0 ? true : false)
  93. extent += contentItem['__margins' + hw]
  94. return extent
  95. }
  96. }