1/*
2    SPDX-FileCopyrightText: 2012 Sebastian Gottfried <sebastiangottfried@web.de>
3    SPDX-FileCopyrightText: 2013-2016 Andreas Cord-Landwehr <cordlandwehr@kde.org>
4
5    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
6*/
7
8import QtQuick 2.1
9import QtQuick.Controls 1.2
10import artikulate 1.0
11
12Item {
13    id: root
14    anchors.fill: parent
15
16    property Learner profile: null
17    state: "info"
18
19    function update() {
20        if (profile) {
21            var isNewProfile = root.profile === null
22            profileForm.name = profile.name
23            deleteConfirmationLabel.name = profile.name
24            state = isNewProfile ? "editor" : "info"
25        }
26    }
27
28    signal deletionRequest();
29
30    onProfileChanged: update()
31
32    ProfileUserImageItem {
33        id: imageLearner
34        height: 120
35        width: height
36        profile: root.profile
37        anchors {
38            top: root.top
39            right: root.right
40            topMargin: 30
41            leftMargin: 30
42        }
43    }
44
45    Label {
46        anchors {
47            top: imageLearner.top
48            topMargin: Math.floor(0.25 * imageLearner.height)
49            leftMargin: 30
50            left: parent.left
51        }
52        height: paintedHeight
53        font.pointSize: theme.fontPointSize * 1.2
54        text: root.profile != null ? root.profile.name : ""
55    }
56
57    Row {
58        id: editComponent
59        spacing: 10
60        visible: root.state == "info"
61        anchors {
62            horizontalCenter: parent.horizontalCenter
63            top: imageLearner.bottom
64            topMargin: 30
65        }
66        Button {
67            iconName: "document-edit"
68            text: i18n("Edit")
69            onClicked: root.state = "editor"
70        }
71        Button {
72            iconName: "edit-delete"
73            text: i18n("Delete")
74            enabled: g_profileManager.profileCount > 1
75            onClicked: root.state = "deleteConfirmation"
76        }
77        Button {
78            iconName: "insert-image"
79            text: i18n("Change Image")
80            onClicked: g_profileManager.openImageFileDialog()
81        }
82        ToolButton {
83            iconName: "edit-clear"
84            text: i18n("Clear Image")
85            onClicked: {
86                g_profileManager.activeProfile.clearImage()
87            }
88        }
89    }
90
91    Item {
92        id: editorContainer
93        visible: false
94        width: parent.width - 40
95        height: editorContainer.height
96        anchors {
97            horizontalCenter: parent.horizontalCenter
98            top: imageLearner.bottom
99            topMargin: 30
100        }
101
102        Column {
103            id: profileForm
104
105            property alias name: nameTextField.text
106            property alias doneButtonIconSource: doneBtn.iconName
107            property alias doneButtonText: doneBtn.text
108
109            width: parent.width
110            height: editorContainer.height
111
112            spacing: 15
113
114            TextField {
115                id: nameTextField
116                width: parent.width
117                placeholderText: i18n("Name")
118            }
119
120            Button {
121                id: doneBtn
122                anchors.horizontalCenter: parent.horizontalCenter
123                text: i18n("Done")
124                enabled: nameTextField.text !== ""
125                iconName: "dialog-ok"
126                onClicked: {
127                    root.profile.name = profileForm.name
128                    if (root.profile.id === -1) {
129                        g_profileManager.addProfile(profile)
130                    }
131                    else {
132                        g_profileManager.sync(root.profile)
133                    }
134                    root.update()
135                    root.state = "info"
136                }
137            }
138        }
139    }
140
141    Item {
142        id: deleteConfirmationContainer
143        width: parent.width - 40
144        height: editorContainer.height
145        visible: false
146
147        anchors {
148            top: editComponent.top
149            leftMargin: 20
150            left: parent.left
151        }
152        Column {
153            width: parent.width
154            height: parent.height
155            spacing: 15
156
157            Label {
158                property string name
159                id: deleteConfirmationLabel
160                width: parent.width
161                text: i18n("Do you really want to delete this identity \"<b>%1</b>\"?", name)
162                wrapMode: Text.Wrap
163                horizontalAlignment: Text.AlignHCenter
164            }
165            Row {
166                spacing: 10
167                anchors.horizontalCenter: parent.horizontalCenter
168                Button {
169                    iconName: "edit-delete"
170                    text: i18n("Delete")
171                    onClicked: {
172                        deletionRequest()
173                    }
174                }
175                Button {
176                    iconName: "dialog-cancel"
177                    text: i18n("Cancel")
178                    onClicked: root.state = "info"
179                }
180            }
181        }
182    }
183
184    states: [
185        State {
186            name: "info"
187            PropertyChanges {
188                target: editorContainer
189                visible: false
190            }
191            PropertyChanges {
192                target: deleteConfirmationContainer
193                visible: false
194            }
195        },
196        State {
197            name: "editor"
198            PropertyChanges {
199                target: editorContainer
200                visible: true
201            }
202            PropertyChanges {
203                target: deleteConfirmationContainer
204                visible: false
205            }
206        },
207        State {
208            name: "deleteConfirmation"
209            PropertyChanges {
210                target: editorContainer
211                visible: false
212            }
213            PropertyChanges {
214                target: deleteConfirmationContainer
215                visible: true
216            }
217        }
218    ]
219}
220