/src/qml/QtQuick/Controls/TextField.qml

https://github.com/butterfly-cody/deepin-boot-maker · QML · 620 lines · 123 code · 58 blank · 439 comment · 3 complexity · 9700a7af94a0e03ce1d34076490b8372 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the Qt Quick Controls module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** You may use this file under the terms of the BSD license as follows:
  10. **
  11. ** "Redistribution and use in source and binary forms, with or without
  12. ** modification, are permitted provided that the following conditions are
  13. ** met:
  14. ** * Redistributions of source code must retain the above copyright
  15. ** notice, this list of conditions and the following disclaimer.
  16. ** * Redistributions in binary form must reproduce the above copyright
  17. ** notice, this list of conditions and the following disclaimer in
  18. ** the documentation and/or other materials provided with the
  19. ** distribution.
  20. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
  21. ** of its contributors may be used to endorse or promote products derived
  22. ** from this software without specific prior written permission.
  23. **
  24. **
  25. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  36. **
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40. import QtQuick 2.2
  41. import QtQuick.Controls 1.2
  42. import QtQuick.Controls.Private 1.0
  43. /*!
  44. \qmltype TextField
  45. \inqmlmodule QtQuick.Controls
  46. \since 5.1
  47. \ingroup controls
  48. \brief Displays a single line of editable plain text.
  49. TextField is used to accept a line of text input. Input constraints can be
  50. placed on a TextField item (for example, through a \l validator or \l
  51. inputMask). Setting \l echoMode to an appropriate value enables
  52. TextField to be used for a password input field.
  53. You can create a custom appearance for a TextField by
  54. assigning a \l {QtQuick.Controls.Styles::TextFieldStyle}{TextFieldStyle}.
  55. \sa TextArea, TextInput
  56. */
  57. Control {
  58. id: textfield
  59. /*!
  60. \qmlproperty bool TextField::acceptableInput
  61. Returns \c true if the text field contains acceptable
  62. text.
  63. If a validator or input mask was set, this property will return \c
  64. true if the current text satisfies the validator or mask as
  65. a final string (not as an intermediate string).
  66. The default value is \c true.
  67. \sa validator, inputMask, accepted
  68. */
  69. readonly property alias acceptableInput: textInput.acceptableInput // read only
  70. /*!
  71. \qmlproperty bool TextField::activeFocusOnPress
  72. This property is set to \c true if the TextField should gain active
  73. focus on a mouse press.
  74. The default value is \c true.
  75. */
  76. property alias activeFocusOnPress: textInput.activeFocusOnPress
  77. /*!
  78. \qmlproperty bool TextField::canPaste
  79. Returns \c true if the TextField is writable and the content of the
  80. clipboard is suitable for pasting into the TextField.
  81. */
  82. readonly property alias canPaste: textInput.canPaste
  83. /*!
  84. \qmlproperty bool TextField::canRedo
  85. Returns \c true if the TextField is writable and there are \l
  86. {undo}{undone} operations that can be redone.
  87. */
  88. readonly property alias canRedo: textInput.canRedo
  89. /*!
  90. \qmlproperty bool TextField::canUndo
  91. Returns \c true if the TextField is writable and there are previous
  92. operations that can be undone.
  93. */
  94. readonly property alias canUndo: textInput.canUndo
  95. /*!
  96. \qmlproperty color TextField::textColor
  97. This property holds the text color.
  98. */
  99. property alias textColor: textInput.color
  100. /*!
  101. \qmlproperty int TextField::cursorPosition
  102. This property holds the position of the cursor in the TextField.
  103. */
  104. property alias cursorPosition: textInput.cursorPosition
  105. /*!
  106. \qmlproperty string TextField::displayText
  107. This property holds the text displayed in the TextField.
  108. If \l echoMode is set to TextInput::Normal, this holds the
  109. same value as the TextField::text property. Otherwise,
  110. this property holds the text visible to the user, while
  111. the \l text property holds the actual entered text.
  112. */
  113. readonly property alias displayText: textInput.displayText
  114. /*!
  115. \qmlproperty enumeration TextField::echoMode
  116. Specifies how the text should be displayed in the
  117. TextField.
  118. The possible modes are:
  119. \list
  120. \li TextInput.Normal - Displays the text as it is. (Default)
  121. \li TextInput.Password - Displays asterisks instead of characters.
  122. \li TextInput.NoEcho - Displays nothing.
  123. \li TextInput.PasswordEchoOnEdit - Displays characters as they are
  124. entered while editing, otherwise displays asterisks.
  125. \endlist
  126. */
  127. property alias echoMode: textInput.echoMode
  128. Accessible.passwordEdit: echoMode == TextInput.Password || echoMode === TextInput.PasswordEchoOnEdit
  129. /*!
  130. \qmlproperty font TextField::font
  131. Sets the font of the TextField.
  132. */
  133. property alias font: textInput.font
  134. /*!
  135. \qmlproperty enumeration TextField::horizontalAlignment
  136. Sets the alignment of the text within the TextField item's width.
  137. By default, the horizontal text alignment follows the natural alignment
  138. of the text, for example text that is read from left to right will be
  139. aligned to the left.
  140. The possible alignment values are:
  141. \list
  142. \li TextInput.AlignLeft
  143. \li TextInput.AlignRight
  144. \li TextInput.AlignHCenter
  145. \endlist
  146. When using the attached property, LayoutMirroring::enabled, to mirror
  147. application layouts, the horizontal alignment of text will also be
  148. mirrored. However, the property \c horizontalAlignment will remain
  149. unchanged. To query the effective horizontal alignment of TextField, use
  150. the read-only property \c effectiveHorizontalAlignment.
  151. */
  152. property alias horizontalAlignment: textInput.horizontalAlignment
  153. /*!
  154. \qmlproperty enumeration TextField::effectiveHorizontalAlignment
  155. Gets the effective horizontal alignment of the text within the TextField
  156. item's width.
  157. \l horizontalAlignment contains the default horizontal alignment.
  158. \sa horizontalAlignment
  159. */
  160. readonly property alias effectiveHorizontalAlignment: textInput.effectiveHorizontalAlignment
  161. /*!
  162. \qmlproperty enumeration TextField::verticalAlignment
  163. Sets the alignment of the text within the TextField item's height.
  164. The possible alignment values are:
  165. \list
  166. \li TextInput.AlignTop
  167. \li TextInput.AlignBottom
  168. \li TextInput.AlignVCenter (default).
  169. \endlist
  170. */
  171. property alias verticalAlignment: textInput.verticalAlignment
  172. /*!
  173. \qmlproperty string TextField::inputMask
  174. Sets an input mask on the TextField, restricting the allowable text
  175. inputs. See QLineEdit::inputMask for further details, as the exact same
  176. mask strings are used by TextField.
  177. \sa acceptableInput, validator
  178. */
  179. property alias inputMask: textInput.inputMask
  180. /*!
  181. \qmlproperty enumeration TextField::inputMethodHints
  182. Provides hints to the input method about the expected content of the
  183. text field and how it should operate.
  184. The value is a bit-wise combination of flags, or \c Qt.ImhNone if no
  185. hints are set.
  186. The default value is \c Qt.ImhNone.
  187. Flags that alter behavior are:
  188. \list
  189. \li Qt.ImhHiddenText - Characters should be hidden, as is typically used when entering passwords.
  190. This is automatically set when setting echoMode to \c TextInput.Password.
  191. \li Qt.ImhSensitiveData - Typed text should not be stored by the active input method
  192. in any persistent storage like predictive user dictionary.
  193. \li Qt.ImhNoAutoUppercase - The input method should not try to automatically switch to upper case
  194. when a sentence ends.
  195. \li Qt.ImhPreferNumbers - Numbers are preferred (but not required).
  196. \li Qt.ImhPreferUppercase - Uppercase letters are preferred (but not required).
  197. \li Qt.ImhPreferLowercase - Lowercase letters are preferred (but not required).
  198. \li Qt.ImhNoPredictiveText - Do not use predictive text (for example, dictionary lookup) while typing.
  199. \li Qt.ImhDate - The text editor functions as a date field.
  200. \li Qt.ImhTime - The text editor functions as a time field.
  201. \endlist
  202. Flags that restrict input (exclusive flags) are:
  203. \list
  204. \li Qt.ImhDigitsOnly - Only digits are allowed.
  205. \li Qt.ImhFormattedNumbersOnly - Only number input is allowed. This includes decimal point and minus sign.
  206. \li Qt.ImhUppercaseOnly - Only uppercase letter input is allowed.
  207. \li Qt.ImhLowercaseOnly - Only lowercase letter input is allowed.
  208. \li Qt.ImhDialableCharactersOnly - Only characters suitable for phone dialing are allowed.
  209. \li Qt.ImhEmailCharactersOnly - Only characters suitable for email addresses are allowed.
  210. \li Qt.ImhUrlCharactersOnly - Only characters suitable for URLs are allowed.
  211. \endlist
  212. Masks:
  213. \list
  214. \li Qt.ImhExclusiveInputMask - This mask yields nonzero if any of the exclusive flags are used.
  215. \endlist
  216. */
  217. property alias inputMethodHints: textInput.inputMethodHints
  218. /*!
  219. \qmlproperty int TextField::length
  220. Returns the total number of characters in the TextField item.
  221. If the TextField has an input mask, the length will include mask
  222. characters and may differ from the length of the string returned by the
  223. \l text property.
  224. This property can be faster than querying the length of the \l text
  225. property as it doesn't require any copying or conversion of the
  226. TextField's internal string data.
  227. */
  228. readonly property alias length: textInput.length
  229. /*!
  230. \qmlproperty int TextField::maximumLength
  231. This property holds the maximum permitted length of the text in the
  232. TextField.
  233. If the text is too long, it is truncated at the limit.
  234. */
  235. property alias maximumLength: textInput.maximumLength
  236. /*!
  237. \qmlproperty string TextField::placeholderText
  238. This property contains the text that is shown in the text field when the
  239. text field is empty.
  240. */
  241. property alias placeholderText: placeholderTextComponent.text
  242. /*!
  243. \qmlproperty bool TextField::readOnly
  244. Sets whether user input can modify the contents of the TextField. Read-
  245. only is different from a disabled text field in that the text field will
  246. appear to be active and text can still be selected and copied.
  247. If readOnly is set to \c true, then user input will not affect the text.
  248. Any bindings or attempts to set the text property will still
  249. work, however.
  250. */
  251. property alias readOnly: textInput.readOnly
  252. Accessible.readOnly: readOnly
  253. /*!
  254. \qmlproperty string TextField::selectedText
  255. Provides the text currently selected in the text input.
  256. It is equivalent to the following snippet, but is faster and easier
  257. to use.
  258. \code
  259. myTextField.text.toString().substring(myTextField.selectionStart, myTextField.selectionEnd);
  260. \endcode
  261. */
  262. readonly property alias selectedText: textInput.selectedText
  263. /*!
  264. \qmlproperty int TextField::selectionEnd
  265. The cursor position after the last character in the current selection.
  266. This property is read-only. To change the selection, use
  267. select(start,end), selectAll(), or selectWord().
  268. \sa selectionStart, cursorPosition, selectedText
  269. */
  270. readonly property alias selectionEnd: textInput.selectionEnd
  271. /*!
  272. \qmlproperty int TextField::selectionStart
  273. The cursor position before the first character in the current selection.
  274. This property is read-only. To change the selection, use select(start,end),
  275. selectAll(), or selectWord().
  276. \sa selectionEnd, cursorPosition, selectedText
  277. */
  278. readonly property alias selectionStart: textInput.selectionStart
  279. /*!
  280. \qmlproperty string TextField::text
  281. This property contains the text in the TextField.
  282. */
  283. property alias text: textInput.text
  284. /*!
  285. \qmlproperty Validator TextField::validator
  286. Allows you to set a validator on the TextField. When a validator is set,
  287. the TextField will only accept input which leaves the text property in
  288. an intermediate state. The accepted signal will only be sent
  289. if the text is in an acceptable state when enter is pressed.
  290. Currently supported validators are \l{QtQuick::}{IntValidator},
  291. \l{QtQuick::}{DoubleValidator}, and \l{QtQuick::}{RegExpValidator}. An
  292. example of using validators is shown below, which allows input of
  293. integers between 11 and 31 into the text input:
  294. \code
  295. import QtQuick 2.2
  296. import QtQuick.Controls 1.2
  297. TextField {
  298. validator: IntValidator {bottom: 11; top: 31;}
  299. focus: true
  300. }
  301. \endcode
  302. \sa acceptableInput, inputMask, accepted
  303. */
  304. property alias validator: textInput.validator
  305. /*!
  306. \qmlsignal TextField::accepted()
  307. This signal is emitted when the Return or Enter key is pressed.
  308. Note that if there is a \l validator or \l inputMask set on the text
  309. field, the signal will only be emitted if the input is in an acceptable
  310. state.
  311. The corresponding handler is \c onAccepted.
  312. */
  313. signal accepted()
  314. /*!
  315. \qmlsignal TextField::editingFinished()
  316. \since QtQuick.Controls 1.1
  317. This signal is emitted when the Return or Enter key is pressed or
  318. the text field loses focus. Note that if there is a validator or
  319. inputMask set on the text field and enter/return is pressed, this
  320. signal will only be emitted if the input follows
  321. the inputMask and the validator returns an acceptable state.
  322. The corresponding handler is \c onEditingFinished.
  323. */
  324. signal editingFinished()
  325. /*!
  326. \qmlmethod TextField::copy()
  327. Copies the currently selected text to the system clipboard.
  328. */
  329. function copy() {
  330. textInput.copy()
  331. }
  332. /*!
  333. \qmlmethod TextField::cut()
  334. Moves the currently selected text to the system clipboard.
  335. */
  336. function cut() {
  337. textInput.cut()
  338. }
  339. /*!
  340. \qmlmethod TextField::deselect()
  341. Removes active text selection.
  342. */
  343. function deselect() {
  344. textInput.deselect();
  345. }
  346. /*!
  347. \qmlmethod string TextField::getText(int start, int end)
  348. Removes the section of text that is between the \a start and \a end
  349. positions from the TextField.
  350. */
  351. function getText(start, end) {
  352. return textInput.getText(start, end);
  353. }
  354. /*!
  355. \qmlmethod TextField::insert(int position, string text)
  356. Inserts \a text into the TextField at \a position.
  357. */
  358. function insert(position, text) {
  359. textInput.insert(position, text);
  360. }
  361. /*!
  362. \qmlmethod bool TextField::isRightToLeft(int start, int end)
  363. Returns \c true if the natural reading direction of the editor text
  364. found between positions \a start and \a end is right to left.
  365. */
  366. function isRightToLeft(start, end) {
  367. return textInput.isRightToLeft(start, end);
  368. }
  369. /*!
  370. \qmlmethod TextField::paste()
  371. Replaces the currently selected text by the contents of the system
  372. clipboard.
  373. */
  374. function paste() {
  375. textInput.paste()
  376. }
  377. /*!
  378. \qmlmethod TextField::redo()
  379. Performs the last operation if redo is \l {canRedo}{available}.
  380. */
  381. function redo() {
  382. textInput.redo();
  383. }
  384. /*!
  385. \qmlmethod TextField::select(int start, int end)
  386. Causes the text from \a start to \a end to be selected.
  387. If either start or end is out of range, the selection is not changed.
  388. After calling select, selectionStart will become the lesser
  389. and selectionEnd will become the greater (regardless of the order passed
  390. to this method).
  391. \sa selectionStart, selectionEnd
  392. */
  393. function select(start, end) {
  394. textInput.select(start, end)
  395. }
  396. /*!
  397. \qmlmethod TextField::selectAll()
  398. Causes all text to be selected.
  399. */
  400. function selectAll() {
  401. textInput.selectAll()
  402. }
  403. /*!
  404. \qmlmethod TextField::selectWord()
  405. Causes the word closest to the current cursor position to be selected.
  406. */
  407. function selectWord() {
  408. textInput.selectWord()
  409. }
  410. /*!
  411. \qmlmethod TextField::undo()
  412. Reverts the last operation if undo is \l {canUndo}{available}. undo()
  413. deselects any current selection and updates the selection start to the
  414. current cursor position.
  415. */
  416. function undo() {
  417. textInput.undo();
  418. }
  419. /*! \qmlproperty bool TextField::hovered
  420. This property holds whether the control is being hovered.
  421. */
  422. readonly property alias hovered: mouseArea.containsMouse
  423. /*! \internal */
  424. property alias __contentHeight: textInput.contentHeight
  425. /*! \internal */
  426. property alias __contentWidth: textInput.contentWidth
  427. /*! \internal */
  428. property alias __baselineOffset: textInput.baselineOffset
  429. style: Qt.createComponent(Settings.style + "/TextFieldStyle.qml", textInput)
  430. activeFocusOnTab: true
  431. Accessible.name: text
  432. Accessible.role: Accessible.EditableText
  433. Accessible.description: placeholderText
  434. MouseArea {
  435. id: mouseArea
  436. anchors.fill: parent
  437. hoverEnabled: true
  438. cursorShape: Qt.IBeamCursor
  439. onClicked: textfield.forceActiveFocus()
  440. }
  441. Text {
  442. id: placeholderTextComponent
  443. anchors.fill: textInput
  444. font: textInput.font
  445. horizontalAlignment: textInput.horizontalAlignment
  446. verticalAlignment: textInput.verticalAlignment
  447. opacity: !textInput.text.length && !textInput.inputMethodComposing ? 1 : 0
  448. color: __panel ? __panel.placeholderTextColor : "darkgray"
  449. clip: contentWidth > width;
  450. elide: Text.ElideRight
  451. renderType: __style ? __style.renderType : Text.NativeRendering
  452. Behavior on opacity { NumberAnimation { duration: 90 } }
  453. }
  454. TextInput {
  455. id: textInput
  456. focus: true
  457. selectByMouse: Qt.platform.os !== "android" // Workaround for QTBUG-36515
  458. selectionColor: __panel ? __panel.selectionColor : "darkred"
  459. selectedTextColor: __panel ? __panel.selectedTextColor : "white"
  460. font: __panel ? __panel.font : undefined
  461. anchors.leftMargin: __panel ? __panel.leftMargin : 0
  462. anchors.topMargin: __panel ? __panel.topMargin : 0
  463. anchors.rightMargin: __panel ? __panel.rightMargin : 0
  464. anchors.bottomMargin: __panel ? __panel.bottomMargin : 0
  465. anchors.fill: parent
  466. verticalAlignment: Text.AlignVCenter
  467. color: __panel ? __panel.textColor : "darkgray"
  468. clip: contentWidth > width
  469. renderType: __style ? __style.renderType : Text.NativeRendering
  470. Keys.forwardTo: textfield
  471. onAccepted: {
  472. Qt.inputMethod.commit()
  473. Qt.inputMethod.hide()
  474. textfield.accepted()
  475. }
  476. onEditingFinished: textfield.editingFinished()
  477. }
  478. }