DefaultDialogWrapper.qml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Dialogs 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.Controls 1.2
  41. import QtQuick.Dialogs 1.2
  42. import QtQuick.Layouts 1.1
  43. import QtQuick.Window 2.1
  44. import "qml"
  45. AbstractDialog {
  46. id: root
  47. default property alias data: defaultContentItem.data
  48. signal actionChosen(var action)
  49. onVisibilityChanged: if (visible && contentItem) contentItem.forceActiveFocus()
  50. Rectangle {
  51. id: content
  52. property real spacing: 6
  53. property real outerSpacing: 12
  54. property real buttonsRowImplicitHeight: 0
  55. property real buttonsRowImplicitWidth: Screen.pixelDensity * 50
  56. property bool buttonsInSingleRow: defaultContentItem.width >= buttonsRowImplicitWidth
  57. property real minimumHeight: implicitHeight
  58. property real minimumWidth: implicitWidth
  59. implicitHeight: defaultContentItem.implicitHeight + spacing + outerSpacing * 2 + Math.max(buttonsRight.implicitHeight, buttonsRowImplicitHeight)
  60. implicitWidth: Math.min(root.__maximumDimension, Math.max(
  61. defaultContentItem.implicitWidth, buttonsRowImplicitWidth, Screen.pixelDensity * 50) + outerSpacing * 2)
  62. color: palette.window
  63. Keys.onPressed: {
  64. event.accepted = handleKey(event.key)
  65. }
  66. SystemPalette { id: palette }
  67. Item {
  68. id: defaultContentItem
  69. anchors {
  70. left: parent.left
  71. right: parent.right
  72. top: parent.top
  73. bottom: buttonsLeft.implicitHeight ? buttonsLeft.top : buttonsRight.top
  74. margins: content.outerSpacing
  75. bottomMargin: buttonsLeft.implicitHeight + buttonsRight.implicitHeight > 0 ? content.spacing : 0
  76. }
  77. implicitHeight: children.length === 1 ? children[0].implicitHeight
  78. : (children.length ? childrenRect.height : 0)
  79. implicitWidth: children.length === 1 ? children[0].implicitWidth
  80. : (children.length ? childrenRect.width : 0)
  81. }
  82. Flow {
  83. id: buttonsLeft
  84. spacing: content.spacing
  85. anchors {
  86. left: parent.left
  87. bottom: content.buttonsInSingleRow ? parent.bottom : buttonsRight.top
  88. margins: content.outerSpacing
  89. }
  90. Repeater {
  91. id: buttonsLeftRepeater
  92. Button {
  93. text: (buttonsLeftRepeater.model && buttonsLeftRepeater.model[index] ? buttonsLeftRepeater.model[index].text : index)
  94. onClicked: content.handleButton(buttonsLeftRepeater.model[index].standardButton)
  95. }
  96. }
  97. Button {
  98. id: moreButton
  99. text: qsTr("Show Details...")
  100. visible: false
  101. }
  102. }
  103. Flow {
  104. id: buttonsRight
  105. spacing: content.spacing
  106. layoutDirection: Qt.RightToLeft
  107. anchors {
  108. left: parent.left
  109. right: parent.right
  110. bottom: parent.bottom
  111. margins: content.outerSpacing
  112. }
  113. Repeater {
  114. id: buttonsRightRepeater
  115. // TODO maybe: insert gaps if the button requires it (destructive buttons only)
  116. Button {
  117. text: (buttonsRightRepeater.model && buttonsRightRepeater.model[index] ? buttonsRightRepeater.model[index].text : index)
  118. onClicked: content.handleButton(buttonsRightRepeater.model[index].standardButton)
  119. }
  120. }
  121. }
  122. function handleButton(button) {
  123. var action = {
  124. "button": button,
  125. "key": 0,
  126. "accepted": true,
  127. }
  128. root.actionChosen(action)
  129. if (action.accepted) {
  130. click(button)
  131. }
  132. }
  133. function handleKey(key) {
  134. var button = 0
  135. switch (key) {
  136. case Qt.Key_Escape:
  137. case Qt.Key_Back:
  138. button = StandardButton.Cancel
  139. break
  140. case Qt.Key_Enter:
  141. case Qt.Key_Return:
  142. button = StandardButton.Ok
  143. break
  144. default:
  145. return false
  146. }
  147. var action = {
  148. "button": button,
  149. "key": key,
  150. "accepted": true,
  151. }
  152. root.actionChosen(action)
  153. if (action.accepted) {
  154. switch (button) {
  155. case StandardButton.Cancel:
  156. reject()
  157. break
  158. case StandardButton.Ok:
  159. accept()
  160. break
  161. }
  162. }
  163. return true
  164. }
  165. }
  166. function setupButtons() {
  167. buttonsLeftRepeater.model = root.__standardButtonsLeftModel()
  168. buttonsRightRepeater.model = root.__standardButtonsRightModel()
  169. if (buttonsRightRepeater.model && buttonsRightRepeater.model.length > 0)
  170. content.buttonsRowImplicitHeight = buttonsRight.visibleChildren[0].implicitHeight
  171. if (buttonsLeftRepeater.count + buttonsRightRepeater.count < 1)
  172. return;
  173. var calcWidth = 0;
  174. function calculateForButton(i, b) {
  175. var buttonWidth = b.implicitWidth;
  176. if (buttonWidth > 0) {
  177. if (i > 0)
  178. buttonWidth += content.spacing
  179. calcWidth += buttonWidth
  180. }
  181. }
  182. for (var i = 0; i < buttonsRight.visibleChildren.length; ++i)
  183. calculateForButton(i, buttonsRight.visibleChildren[i])
  184. content.minimumWidth = Math.max(calcWidth + content.outerSpacing * 2, content.implicitWidth)
  185. for (i = 0; i < buttonsLeft.visibleChildren.length; ++i)
  186. calculateForButton(i, buttonsLeft.visibleChildren[i])
  187. content.buttonsRowImplicitWidth = calcWidth + content.spacing
  188. }
  189. onStandardButtonsChanged: setupButtons()
  190. Component.onCompleted: setupButtons()
  191. }