1/****************************************************************************
2**
3** Copyright (C) 2021 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of Qt Creator.
7**
8** Commercial License Usage
9** Licensees holding valid commercial Qt licenses may use this file in
10** accordance with the commercial license agreement provided with the
11** Software or, alternatively, in accordance with the terms contained in
12** a written agreement between you and The Qt Company. For licensing terms
13** and conditions see https://www.qt.io/terms-conditions. For further
14** information use the contact form at https://www.qt.io/contact-us.
15**
16** GNU General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU
18** General Public License version 3 as published by the Free Software
19** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20** included in the packaging of this file. Please review the following
21** information to ensure the GNU General Public License requirements will
22** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23**
24****************************************************************************/
25
26import QtQuick 2.15
27import QtQuick.Templates 2.15 as T
28import StudioTheme 1.0 as StudioTheme
29
30TextField {
31    id: myTextField
32
33    property real relativePopupX: 0 // TODO Maybe call it leftPadding
34    property real popupWidth: myTextField.width
35    property string txtStorage
36
37    property int temp: 0
38
39    T.Popup {
40        id: popup
41        x: myTextField.relativePopupX
42        y: myTextField.height - StudioTheme.Values.border
43        width: myTextField.popupWidth
44        height: scrollView.height
45        background: Rectangle {
46            color: StudioTheme.Values.themePopupBackground
47            border.color: StudioTheme.Values.themeInteraction
48            border.width: StudioTheme.Values.border
49        }
50
51        contentItem: ScrollView {
52            id: scrollView
53            padding: 0
54            height: Math.min(textAreaPopup.contentHeight + scrollView.topPadding
55                             + scrollView.bottomPadding,
56                             StudioTheme.Values.maxTextAreaPopupHeight)
57            ScrollBar.horizontal.policy: ScrollBar.AlwaysOn
58            ScrollBar.vertical.policy: ScrollBar.AlwaysOn
59
60            T.TextArea {
61                id: textAreaPopup
62                padding: 10
63                width: textAreaPopup.contentWidth + textAreaPopup.leftPadding
64                       + textAreaPopup.rightPadding
65                anchors.fill: parent
66                font.pixelSize: StudioTheme.Values.myFontSize
67                color: StudioTheme.Values.themeTextColor
68                selectionColor: StudioTheme.Values.themeTextSelectionColor
69                selectedTextColor: StudioTheme.Values.themeTextSelectedTextColor
70                selectByMouse: true
71                persistentSelection: textAreaPopup.focus
72
73                MouseArea {
74                    id: mouseArea
75                    anchors.fill: parent
76                    enabled: true
77                    cursorShape: Qt.IBeamCursor
78                    acceptedButtons: Qt.RightButton
79                    onPressed: contextMenu.popup(textAreaPopup)
80                }
81            }
82        }
83
84        ContextMenu {
85            id: contextMenu
86            myTextEdit: textAreaPopup
87        }
88
89        AbstractButton {
90            id: acceptButton
91            x: popup.width - acceptButton.width
92            y: popup.height - StudioTheme.Values.border
93            width: Math.round(StudioTheme.Values.smallRectWidth)
94            height: Math.round(StudioTheme.Values.smallRectWidth)
95            buttonIcon: StudioTheme.Constants.tickIcon
96        }
97
98        AbstractButton {
99            id: discardButton
100            x: popup.width - acceptButton.width - discardButton.width + StudioTheme.Values.border
101            y: popup.height - StudioTheme.Values.border
102            width: Math.round(StudioTheme.Values.smallRectWidth)
103            height: Math.round(StudioTheme.Values.smallRectWidth)
104            buttonIcon: StudioTheme.Constants.closeCross
105        }
106
107        Component.onCompleted: {
108            storeAndFormatTextInput(myTextField.text)
109        }
110
111        onOpened: {
112            textAreaPopup.text = txtStorage
113            myTextField.clear()
114        }
115
116        onClosed: {
117            storeAndFormatTextInput(textAreaPopup.text)
118            myTextField.forceActiveFocus()
119            textAreaPopup.deselect()
120        }
121    }
122
123    function storeAndFormatTextInput(inputText) {
124        txtStorage = inputText
125        var pos = txtStorage.search(/\n/g)
126        var sliceAt = Math.min(pos, 15)
127        myTextField.text = txtStorage.slice(0, sliceAt).padEnd(sliceAt + 3, '.')
128    }
129
130    Keys.onPressed: function(event) {
131        if (event.key === Qt.Key_Escape)
132            popup.opened ? popup.close() : myTextField.focus = false
133
134        if ((event.key === Qt.Key_Return || event.key === Qt.Key_Enter)
135                && !popup.opened) {
136            popup.open()
137            textAreaPopup.forceActiveFocus()
138        }
139    }
140}
141