Changeset View
Changeset View
Standalone View
Standalone View
src/qtquick/qml/QuestionAsker.qml
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 Dan Leinir Turthra Jensen <admin@leinir.dk> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Lesser General Public | ||||
6 | * License as published by the Free Software Foundation; either | ||||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | * later version accepted by the membership of KDE e.V. (or its | ||||
9 | * successor approved by the membership of KDE e.V.), which shall | ||||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | * | ||||
12 | * This library is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | * Lesser General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU Lesser General Public | ||||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | * | ||||
20 | */ | ||||
21 | | ||||
22 | /** | ||||
23 | * @brief A component used to forward questions from KNewStuff's engine to the UI | ||||
24 | * | ||||
25 | * This component is equivalent to the WidgetQuestionListener | ||||
26 | * @see KNewStuff::WidgetQuestionListener | ||||
27 | * @see KNewStuffCore::Question | ||||
28 | */ | ||||
29 | | ||||
30 | import QtQuick 2.11 | ||||
31 | import QtQuick.Controls 2.11 as QtControls | ||||
32 | import QtQuick.Layouts 1.11 as QtLayouts | ||||
33 | | ||||
34 | import org.kde.kirigami 2.7 as Kirigami | ||||
35 | | ||||
36 | import org.kde.newstuff.core 1.1 as NewStuffCore | ||||
37 | import org.kde.newstuff 1.1 as NewStuff | ||||
38 | | ||||
39 | QtControls.Dialog { | ||||
40 | id: dialog | ||||
41 | modal: true | ||||
42 | focus: true | ||||
43 | property int questionType | ||||
44 | anchors.centerIn: QtControls.Overlay.overlay | ||||
45 | margins: Kirigami.Units.largeSpacing | ||||
46 | padding: Kirigami.Units.largeSpacing | ||||
47 | standardButtons: { | ||||
48 | switch (questionType) { | ||||
49 | case NewStuffCore.Question.SelectFromListQuestion: | ||||
50 | case NewStuffCore.Question.InputTextQuestion: | ||||
51 | case NewStuffCore.Question.PasswordQuestion: | ||||
52 | case NewStuffCore.Question.ContinueCancelQuestion: | ||||
53 | // QtControls Dialog standardButtons does not have a Continue button... | ||||
54 | return QtControls.Dialog.Ok | QtControls.Dialog.Cancel; | ||||
55 | break; | ||||
56 | case NewStuffCore.Question.YesNoQuestion: | ||||
57 | return QtControls.Dialog.Yes | QtControls.Dialog.No; | ||||
58 | break; | ||||
59 | default: | ||||
60 | break; | ||||
61 | } | ||||
62 | } | ||||
63 | | ||||
64 | Connections { | ||||
65 | target: NewStuff.QuickQuestionListener | ||||
66 | onAskListQuestion: { | ||||
67 | dialog.questionType = NewStuffCore.Question.SelectFromListQuestion; | ||||
68 | dialog.title = title; | ||||
69 | questionLabel.text = question; | ||||
70 | for (var i = 0; i < list.length; i++) { | ||||
71 | listView.model.append({ text: list[i] }); | ||||
72 | } | ||||
73 | listView.currentIndex = 0; | ||||
74 | listView.visible = true; | ||||
75 | dialog.open(); | ||||
76 | } | ||||
77 | onAskContinueCancelQuestion: { | ||||
78 | dialog.questionType = NewStuffCore.Question.ContinueCancelQuestion; | ||||
79 | dialog.title = title; | ||||
80 | questionLabel.text = question; | ||||
81 | dialog.open(); | ||||
82 | } | ||||
83 | onAskTextInputQuestion: { | ||||
84 | dialog.questionType = NewStuffCore.Question.InputTextQuestion; | ||||
85 | dialog.title = title; | ||||
86 | questionLabel.text = question; | ||||
87 | textInput.visible = true; | ||||
88 | dialog.open(); | ||||
89 | } | ||||
90 | onAskPasswordQuestion: { | ||||
91 | dialog.questionType = NewStuffCore.Question.PasswordQuestion; | ||||
92 | dialog.title = title; | ||||
93 | questionLabel.text = question; | ||||
94 | textInput.echoMode = QtControls.TextInput.PasswordEchoOnEdit; | ||||
95 | textInput.visible = true; | ||||
96 | dialog.open(); | ||||
97 | } | ||||
98 | onAskYesNoQuestion: { | ||||
99 | dialog.questionType = NewStuffCore.Question.YesNoQuestion; | ||||
100 | dialog.title = title; | ||||
101 | questionLabel.text = question; | ||||
102 | dialog.open(); | ||||
103 | } | ||||
104 | } | ||||
105 | Connections { | ||||
106 | target: applicationWindow() | ||||
107 | // Since dialogs in QML don't automatically reject when the application is closed, | ||||
108 | // we just do that little job for it (and then we don't end up blocking everything | ||||
109 | // when the application is shut without the question being answered) | ||||
110 | onClosing: { | ||||
111 | if (dialog.opened === true) { | ||||
112 | passResponse(false); | ||||
113 | } | ||||
114 | } | ||||
115 | } | ||||
116 | function passResponse(responseIsContinue) { | ||||
117 | var input = ""; | ||||
118 | switch(dialog.questionType) { | ||||
119 | case NewStuffCore.Question.SelectFromListQuestion: | ||||
120 | input = listView.currentItem.text; | ||||
121 | listView.model.clear(); | ||||
122 | listView.visible = false; | ||||
123 | break; | ||||
124 | case NewStuffCore.Question.InputTextQuestion: | ||||
125 | input = textInput.text; | ||||
126 | textInput.text = ""; | ||||
127 | textInput.visible = false; | ||||
128 | break; | ||||
129 | case NewStuffCore.Question.PasswordQuestion: | ||||
130 | input = textInput.text; | ||||
131 | textInput.text = ""; | ||||
132 | textInput.visible = false; | ||||
133 | textInput.echoMode = QtControls.TextInput.Normal; | ||||
134 | break; | ||||
135 | case NewStuffCore.Question.ContinueCancelQuestion: | ||||
136 | case NewStuffCore.Question.YesNoQuestion: | ||||
137 | default: | ||||
138 | // Nothing special to do for these types of question, we just pass along the positive or negative response | ||||
139 | break; | ||||
140 | } | ||||
141 | NewStuff.QuickQuestionListener.passResponse(responseIsContinue, input); | ||||
142 | } | ||||
143 | | ||||
144 | QtLayouts.ColumnLayout { | ||||
145 | anchors.fill: parent | ||||
146 | property int maxWidth: applicationWindow().width - (dialog.leftPadding + dialog.leftMargin + dialog.rightMargin + dialog.rightPadding) | ||||
147 | QtControls.Label { | ||||
148 | id: questionLabel | ||||
149 | QtLayouts.Layout.maximumWidth: parent.maxWidth | ||||
150 | wrapMode: Text.Wrap | ||||
151 | } | ||||
152 | ListView { | ||||
153 | id: listView | ||||
154 | visible: false | ||||
155 | QtLayouts.Layout.maximumWidth: parent.maxWidth | ||||
156 | QtLayouts.Layout.fillWidth: true | ||||
157 | QtLayouts.Layout.minimumHeight: Kirigami.Units.gridUnit * 6 | ||||
158 | model: ListModel { } | ||||
159 | delegate: Kirigami.BasicListItem { | ||||
160 | reserveSpaceForIcon: false | ||||
161 | text: model.text | ||||
162 | } | ||||
163 | } | ||||
164 | QtControls.TextField { | ||||
165 | id: textInput | ||||
166 | visible: false | ||||
167 | QtLayouts.Layout.maximumWidth: parent.maxWidth | ||||
168 | QtLayouts.Layout.fillWidth: true | ||||
169 | } | ||||
170 | } | ||||
171 | onAccepted: { | ||||
172 | passResponse(true); | ||||
173 | } | ||||
174 | onRejected: { | ||||
175 | passResponse(false); | ||||
176 | } | ||||
177 | } |