Warning, /libraries/kqtquickcharts/demo/dynamicdata/ValueEdit.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *  Copyright 2014  Sebastian Gottfried <sebastiangottfried@web.de>
0003  *
0004  *  This library is free software; you can redistribute it and/or
0005  *  modify it under the terms of the GNU Lesser General Public
0006  *  License as published by the Free Software Foundation; either
0007  *  version 2.1 of the License, or (at your option) version 3, or any
0008  *  later version accepted by the membership of KDE e.V. (or its
0009  *  successor approved by the membership of KDE e.V.), which shall
0010  *  act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  *  This library is distributed in the hope that it will be useful,
0013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  *  Lesser General Public License for more details.
0016  *
0017  *  You should have received a copy of the GNU Lesser General Public
0018  */
0019 
0020 import QtQuick 2.2
0021 import QtQuick.Controls 1.2
0022 
0023 Rectangle {
0024     color: "#ddd"
0025     id: root
0026     property real value: 0.0
0027     property bool editing: false
0028     property real internalPadding: 3
0029 
0030     Text {
0031         anchors {
0032             left: parent.left
0033             right: parent.right
0034             leftMargin: internalPadding
0035             rightMargin: internalPadding
0036             verticalCenter: parent.verticalCenter
0037         }
0038 
0039         verticalAlignment: Text.Center
0040         text: value
0041         visible: !editing
0042     }
0043 
0044     MouseArea {
0045         anchors.fill: parent
0046         onClicked: {
0047             editing = true
0048             if (loader.item) {
0049                 loader.item.forceActiveFocus()
0050             }
0051             else {
0052                 loader.sourceComponent = editorComponent
0053             }
0054         }
0055     }
0056 
0057     Loader {
0058         id: loader
0059         anchors.fill: parent
0060     }
0061 
0062     Component {
0063         id: editorComponent
0064         TextField {
0065             anchors.fill: parent
0066             opacity: root.editing? 1: 0
0067             text: root.value
0068             Component.onCompleted: {
0069                 forceActiveFocus()
0070             }
0071             onTextChanged: {
0072                 var value = parseFloat(text)
0073                 if (!isNaN(value)) {
0074                     root.value = value
0075                 }
0076             }
0077             onActiveFocusChanged: {
0078                 if (!activeFocus) {
0079                     root.editing = false
0080                 }
0081             }
0082             onAccepted: {
0083                 root.editing = false
0084             }
0085         }
0086     }
0087 }