Changeset View
Changeset View
Standalone View
Standalone View
src/qtquick/qml/NewStuffQuestionAsker.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.7 | ||||
31 | import QtQuick.Controls 2.5 as QtControls | ||||
32 | import QtQuick.Layouts 1.12 as QtLayouts | ||||
33 | import org.kde.kirigami 2.7 as Kirigami | ||||
34 | import org.kde.newstuff 1.1 as NewStuff | ||||
35 | import org.kde.newstuff.core 1.1 as NewStuffCore | ||||
36 | | ||||
37 | QtControls.Dialog { | ||||
38 | id: dialog | ||||
39 | modal: true | ||||
40 | focus: true | ||||
41 | property int questionType | ||||
42 | anchors.centerIn: QtControls.Overlay.overlay | ||||
43 | margins: Kirigami.Units.largeSpacing | ||||
44 | padding: Kirigami.Units.largeSpacing | ||||
45 | standardButtons: { | ||||
46 | switch (questionType) { | ||||
47 | case NewStuffCore.Question.SelectFromListQuestion: | ||||
48 | case NewStuffCore.Question.InputTextQuestion: | ||||
49 | case NewStuffCore.Question.PasswordQuestion: | ||||
50 | case NewStuffCore.Question.ContinueCancelQuestion: | ||||
51 | // QtControls Dialog standardButtons does not have a Continue button... | ||||
52 | return QtControls.Dialog.Ok | QtControls.Dialog.Cancel; | ||||
53 | break; | ||||
54 | case NewStuffCore.Question.YesNoQuestion: | ||||
55 | return QtControls.Dialog.Yes | QtControls.Dialog.No; | ||||
56 | break; | ||||
57 | default: | ||||
58 | break; | ||||
59 | } | ||||
60 | } | ||||
61 | | ||||
62 | Connections { | ||||
63 | target: NewStuff.QuickQuestionListener | ||||
64 | onAskListQuestion: { | ||||
65 | dialog.questionType = NewStuffCore.Question.SelectFromListQuestion; | ||||
66 | dialog.title = title; | ||||
67 | questionLabel.text = question; | ||||
68 | for (var i = 0; i < list.length; i++) { | ||||
69 | listView.model.append({ text: list[i] }); | ||||
70 | } | ||||
71 | listView.currentIndex = 0; | ||||
72 | listView.visible = true; | ||||
73 | dialog.open(); | ||||
74 | } | ||||
75 | onAskContinueCancelQuestion: { | ||||
76 | dialog.questionType = NewStuffCore.Question.ContinueCancelQuestion; | ||||
77 | dialog.title = title; | ||||
78 | questionLabel.text = question; | ||||
79 | dialog.open(); | ||||
80 | } | ||||
81 | onAskTextInputQuestion: { | ||||
82 | dialog.questionType = NewStuffCore.Question.InputTextQuestion; | ||||
83 | dialog.title = title; | ||||
84 | questionLabel.text = question; | ||||
85 | textInput.visible = true; | ||||
86 | dialog.open(); | ||||
87 | } | ||||
88 | onAskPasswordQuestion: { | ||||
89 | dialog.questionType = NewStuffCore.Question.PasswordQuestion; | ||||
90 | dialog.title = title; | ||||
91 | questionLabel.text = question; | ||||
92 | textInput.echoMode = QtControls.TextInput.PasswordEchoOnEdit; | ||||
93 | textInput.visible = true; | ||||
94 | dialog.open(); | ||||
95 | } | ||||
96 | onAskYesNoQuestion: { | ||||
97 | dialog.questionType = NewStuffCore.Question.YesNoQuestion; | ||||
98 | dialog.title = title; | ||||
99 | questionLabel.text = question; | ||||
100 | dialog.open(); | ||||
101 | } | ||||
102 | } | ||||
103 | Connections { | ||||
104 | target: applicationWindow() | ||||
105 | // Since dialogs in QML don't automatically reject when the application is closed, | ||||
106 | // we just do that little job for it (and then we don't end up blocking everything | ||||
107 | // when the application is shut without the question being answered) | ||||
108 | onClosing: { | ||||
109 | if (dialog.opened === true) { | ||||
110 | passResponse(false); | ||||
111 | } | ||||
112 | } | ||||
113 | } | ||||
114 | function passResponse(responseIsContinue) { | ||||
115 | var input = ""; | ||||
116 | switch(dialog.questionType) { | ||||
117 | case NewStuffCore.Question.SelectFromListQuestion: | ||||
118 | input = listView.currentItem.text; | ||||
119 | listView.model.clear(); | ||||
120 | listView.visible = false; | ||||
121 | break; | ||||
122 | case NewStuffCore.Question.InputTextQuestion: | ||||
123 | input = textInput.text; | ||||
124 | textInput.text = ""; | ||||
125 | textInput.visible = false; | ||||
126 | break; | ||||
127 | case NewStuffCore.Question.PasswordQuestion: | ||||
128 | input = textInput.text; | ||||
129 | textInput.text = ""; | ||||
130 | textInput.visible = false; | ||||
131 | textInput.echoMode = QtControls.TextInput.Normal; | ||||
132 | break; | ||||
133 | case NewStuffCore.Question.ContinueCancelQuestion: | ||||
134 | case NewStuffCore.Question.YesNoQuestion: | ||||
135 | default: | ||||
136 | // Nothing special to do for these types of question, we just pass along the positive or negative response | ||||
137 | break; | ||||
138 | } | ||||
139 | NewStuff.QuickQuestionListener.passResponse(responseIsContinue, input); | ||||
140 | } | ||||
141 | | ||||
142 | QtLayouts.ColumnLayout { | ||||
143 | anchors.fill: parent | ||||
144 | property int maxWidth: applicationWindow().width - (dialog.leftPadding + dialog.leftMargin + dialog.rightMargin + dialog.rightPadding) | ||||
145 | QtControls.Label { | ||||
146 | id: questionLabel | ||||
147 | QtLayouts.Layout.maximumWidth: parent.maxWidth | ||||
148 | wrapMode: Text.Wrap | ||||
149 | } | ||||
150 | ListView { | ||||
151 | id: listView | ||||
152 | visible: false | ||||
153 | QtLayouts.Layout.maximumWidth: parent.maxWidth | ||||
154 | QtLayouts.Layout.fillWidth: true | ||||
155 | QtLayouts.Layout.minimumHeight: Kirigami.Units.gridUnit * 6 | ||||
156 | model: ListModel { } | ||||
157 | delegate: Kirigami.BasicListItem { | ||||
158 | reserveSpaceForIcon: false | ||||
159 | text: model.text | ||||
160 | } | ||||
161 | } | ||||
162 | QtControls.TextField { | ||||
163 | id: textInput | ||||
164 | visible: false | ||||
165 | QtLayouts.Layout.maximumWidth: parent.maxWidth | ||||
166 | QtLayouts.Layout.fillWidth: true | ||||
167 | } | ||||
168 | } | ||||
169 | onAccepted: { | ||||
170 | passResponse(true); | ||||
171 | } | ||||
172 | onRejected: { | ||||
173 | passResponse(false); | ||||
174 | } | ||||
175 | } |