EditMenu_base.qml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.Controls 1.2
  41. import QtQuick.Controls.Private 1.0
  42. Item {
  43. id: editMenuBase
  44. anchors.fill: parent
  45. Component {
  46. id: undoAction
  47. Action {
  48. text: qsTr("&Undo")
  49. shortcut: StandardKey.Undo
  50. iconName: "edit-undo"
  51. enabled: input.canUndo
  52. onTriggered: input.undo()
  53. }
  54. }
  55. Component {
  56. id: redoAction
  57. Action {
  58. text: qsTr("&Redo")
  59. shortcut: StandardKey.Redo
  60. iconName: "edit-redo"
  61. enabled: input.canRedo
  62. onTriggered: input.redo()
  63. }
  64. }
  65. Component {
  66. id: cutAction
  67. Action {
  68. text: qsTr("Cu&t")
  69. shortcut: StandardKey.Cut
  70. iconName: "edit-cut"
  71. enabled: !input.readOnly && selectionStart !== selectionEnd
  72. onTriggered: {
  73. input.cut();
  74. input.select(input.cursorPosition, input.cursorPosition);
  75. }
  76. }
  77. }
  78. Component {
  79. id: copyAction
  80. Action {
  81. text: qsTr("&Copy")
  82. shortcut: StandardKey.Copy
  83. iconName: "edit-copy"
  84. enabled: input.selectionStart !== input.selectionEnd
  85. onTriggered: {
  86. input.copy();
  87. input.select(input.cursorPosition, input.cursorPosition);
  88. }
  89. }
  90. }
  91. Component {
  92. id: pasteAction
  93. Action {
  94. text: qsTr("&Paste")
  95. shortcut: StandardKey.Paste
  96. iconName: "edit-paste"
  97. enabled: input.canPaste
  98. onTriggered: input.paste()
  99. }
  100. }
  101. Component {
  102. id: deleteAction
  103. Action {
  104. text: qsTr("Delete")
  105. shortcut: StandardKey.Delete
  106. iconName: "edit-delete"
  107. enabled: !input.readOnly && input.selectionStart !== input.selectionEnd
  108. onTriggered: input.remove(input.selectionStart, input.selectionEnd)
  109. }
  110. }
  111. Component {
  112. id: clearAction
  113. Action {
  114. text: qsTr("Clear")
  115. shortcut: StandardKey.DeleteCompleteLine
  116. iconName: "edit-clear"
  117. enabled: !input.readOnly && input.length > 0
  118. onTriggered: input.remove(0, input.length)
  119. }
  120. }
  121. Component {
  122. id: selectAllAction
  123. Action {
  124. text: qsTr("Select All")
  125. shortcut: StandardKey.SelectAll
  126. enabled: !(input.selectionStart === 0 && input.selectionEnd === input.length)
  127. onTriggered: input.selectAll()
  128. }
  129. }
  130. property Component defaultMenu: Menu {
  131. MenuItem { action: undoAction.createObject(editMenuBase) }
  132. MenuItem { action: redoAction.createObject(editMenuBase) }
  133. MenuSeparator {}
  134. MenuItem { action: cutAction.createObject(editMenuBase) }
  135. MenuItem { action: copyAction.createObject(editMenuBase) }
  136. MenuItem { action: pasteAction.createObject(editMenuBase) }
  137. MenuItem { action: deleteAction.createObject(editMenuBase) }
  138. MenuItem { action: clearAction.createObject(editMenuBase) }
  139. MenuSeparator {}
  140. MenuItem { action: selectAllAction.createObject(editMenuBase) }
  141. }
  142. Connections {
  143. target: mouseArea
  144. function onClicked() {
  145. if (input.selectionStart === input.selectionEnd) {
  146. var cursorPos = input.positionAt(mouse.x, mouse.y)
  147. input.moveHandles(cursorPos, cursorPos)
  148. }
  149. input.activate()
  150. if (control.menu) {
  151. var menu = getMenuInstance();
  152. menu.__dismissAndDestroy();
  153. var menuPos = mapToItem(null, mouse.x, mouse.y)
  154. menu.__popup(Qt.rect(menuPos.x, menuPos.y, 0, 0), -1, MenuPrivate.EditMenu);
  155. }
  156. }
  157. }
  158. }