InputPanel.qml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2016 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
  3. import QtQuick
  4. // Deliberately imported after QtQuick to avoid missing restoreMode property in Binding. Fix in Qt 6.
  5. import QtQml
  6. import QtQuick.VirtualKeyboard
  7. import QtQuick.VirtualKeyboard.Components
  8. /*!
  9. \qmltype InputPanel
  10. \inqmlmodule QtQuick.VirtualKeyboard
  11. \brief Provides the virtual keyboard UI.
  12. \ingroup qmlclass
  13. \ingroup qtvirtualkeyboard-qml
  14. The keyboard size is automatically calculated from the available
  15. width; that is, the keyboard maintains the aspect ratio specified by the current
  16. style. Therefore the application should only set the \l {Item::}{width} and \l {Item::}{y}
  17. coordinates of the InputPanel, and not the \l {Item::}{height}.
  18. As with \l {Qt Virtual Keyboard QML Types}{all other QML types} provided by
  19. the module, the \c QT_IM_MODULE environment variable must be set to
  20. \c qtvirtualkeyboard before using InputPanel. For more information, see
  21. \l {Loading the Plugin}.
  22. \note You can have only one InputPanel instance in your application. The panel
  23. will not be blocked by modal dialogs, but it can be obscured by items with a higher
  24. \l {Item::}{z} value.
  25. */
  26. Item {
  27. id: inputPanel
  28. /*!
  29. \qmlproperty bool InputPanel::active
  30. \since QtQuick.VirtualKeyboard 2.0
  31. This property reflects the active status of the input panel.
  32. The keyboard should be made visible to the user when this property is
  33. \c true.
  34. */
  35. property alias active: keyboard.active
  36. /*!
  37. \qmlproperty bool InputPanel::externalLanguageSwitchEnabled
  38. \since QtQuick.VirtualKeyboard 2.4
  39. This property enables the external language switch mechanism.
  40. When this property is \c true, the virtual keyboard will not show
  41. the built-in language popup, but will emit the \l externalLanguageSwitch
  42. signal instead. The application can handle this signal and show a
  43. custom language selection dialog instead.
  44. */
  45. property bool externalLanguageSwitchEnabled
  46. /*!
  47. \qmlsignal InputPanel::externalLanguageSwitch(var localeList, int currentIndex)
  48. \since QtQuick.VirtualKeyboard 2.4
  49. This signal is emitted when \l externalLanguageSwitchEnabled is \c true
  50. and the \l {user-guide-language}{language switch key} is pressed by the user.
  51. It serves as a hook to display a custom language dialog instead of
  52. the built-in language popup in the virtual keyboard.
  53. The \a localeList parameter contains a list of locale names to choose
  54. from. To get more information about a particular language, use the
  55. \l[QtQml]{Qt::locale()}{Qt.locale()} function. The \a currentIndex
  56. is the index of current locale in the \a localeList. This item should
  57. be highlighted as the current item in the UI.
  58. To select a new language, use the \l {VirtualKeyboardSettings::locale}
  59. {VirtualKeyboardSettings.locale} property.
  60. Below is an example that demonstrates a custom language dialog implementation:
  61. \snippet qtvirtualkeyboard-custom-language-popup.qml popup
  62. The dialog would then be declared:
  63. \snippet qtvirtualkeyboard-custom-language-popup.qml declaring
  64. In the application's InputPanel, add the following code:
  65. \snippet qtvirtualkeyboard-custom-language-popup.qml using
  66. The custom dialog will now be shown when the language switch key is pressed.
  67. */
  68. signal externalLanguageSwitch(var localeList, int currentIndex)
  69. /*! \internal */
  70. property alias keyboard: keyboard
  71. /*! \internal */
  72. property bool desktopPanel: false
  73. SelectionControl {
  74. objectName: "selectionControl"
  75. x: -parent.x
  76. y: -parent.y
  77. enabled: active && !keyboard.fullScreenMode && !desktopPanel
  78. }
  79. implicitHeight: keyboard.height - keyboard.wordCandidateView.y
  80. Keyboard {
  81. id: keyboard
  82. anchors.left: parent.left
  83. anchors.right: parent.right
  84. anchors.bottom: parent.bottom
  85. }
  86. MouseArea {
  87. z: -1
  88. anchors.fill: keyboard
  89. enabled: active
  90. hoverEnabled: active
  91. }
  92. Binding {
  93. target: InputContext.priv
  94. property: "keyboardRectangle"
  95. value: keyboardRectangle()
  96. when: !InputContext.animating && inputPanel.active
  97. }
  98. /*! \internal */
  99. function keyboardRectangle() {
  100. const forBindingX = x
  101. const forBindingY = y
  102. const rect = desktopPanel ?
  103. Qt.rect(keyboard.x,
  104. keyboard.y + keyboard.wordCandidateView.y,
  105. keyboard.width,
  106. keyboard.height - keyboard.wordCandidateView.y) :
  107. Qt.rect(0, 0, width, height)
  108. if (keyboard.shadowInputControl.visible) {
  109. rect.y -= keyboard.shadowInputControl.height
  110. rect.height += keyboard.shadowInputControl.height
  111. }
  112. return mapToItem(null, rect)
  113. }
  114. }