KNewStuff

QuestionAsker.qml
1/*
2 SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <admin@leinir.dk>
3 SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8/**
9 * @brief A component used to forward questions from KNewStuff's engine to the UI
10 *
11 * This component is equivalent to the WidgetQuestionListener
12 * @see KNewStuff::WidgetQuestionListener
13 * @see KNewStuffCore::Question
14 * @since 5.63
15 */
16
17import QtQuick
18import QtQuick.Controls as QQC2
19import QtQuick.Layouts
20import org.kde.kirigami as Kirigami
21import org.kde.newstuff as NewStuff
22import org.kde.newstuff.core as NewStuffCore
23
24QQC2.Dialog {
25 id: dialog
26
27 property int questionType
28
29 anchors.centerIn: parent
30
31 modal: true
32 focus: true
33
34 margins: Kirigami.Units.largeSpacing
35 padding: Kirigami.Units.largeSpacing
36
37 standardButtons: {
38 switch (questionType) {
39 case NewStuffCore.Question.SelectFromListQuestion:
40 case NewStuffCore.Question.InputTextQuestion:
41 case NewStuffCore.Question.PasswordQuestion:
42 case NewStuffCore.Question.ContinueCancelQuestion:
43 // QQC2 Dialog standardButtons does not have a Continue button...
44 return QQC2.Dialog.Ok | QQC2.Dialog.Cancel;
45 case NewStuffCore.Question.YesNoQuestion:
46 return QQC2.Dialog.Yes | QQC2.Dialog.No;
47 default:
48 return QQC2.Dialog.NoButton;
49 }
50 }
51
52 Connections {
53 target: NewStuff.QuickQuestionListener
54
55 function onAskListQuestion(title, question, list) {
56 dialog.questionType = NewStuffCore.Question.SelectFromListQuestion;
57 dialog.title = title;
58 questionLabel.text = question;
59 for (var i = 0; i < list.length; i++) {
60 listView.model.append({ text: list[i] });
61 }
62 listView.currentIndex = 0;
63 listView.visible = true;
64 dialog.open();
65 }
66
67 function onAskContinueCancelQuestion(title, question) {
68 dialog.questionType = NewStuffCore.Question.ContinueCancelQuestion;
69 dialog.title = title;
70 questionLabel.text = question;
71 dialog.open();
72 }
73
74 function onAskTextInputQuestion(title, question) {
75 dialog.questionType = NewStuffCore.Question.InputTextQuestion;
76 dialog.title = title;
77 questionLabel.text = question;
78 textInput.visible = true;
79 dialog.open();
80 }
81
82 function onAskPasswordQuestion(title, question) {
83 dialog.questionType = NewStuffCore.Question.PasswordQuestion;
84 dialog.title = title;
85 questionLabel.text = question;
86 textInput.echoMode = QQC2.TextInput.PasswordEchoOnEdit;
87 textInput.visible = true;
88 dialog.open();
89 }
90
91 function onAskYesNoQuestion(title, question) {
92 dialog.questionType = NewStuffCore.Question.YesNoQuestion;
93 dialog.title = title;
94 questionLabel.text = question;
95 dialog.open();
96 }
97 }
98
99 function passResponse(responseIsContinue) {
100 let input = "";
101 switch (dialog.questionType) {
102 case NewStuffCore.Question.SelectFromListQuestion:
103 input = listView.currentItem.text;
104 listView.model.clear();
105 listView.visible = false;
106 break;
107 case NewStuffCore.Question.InputTextQuestion:
108 input = textInput.text;
109 textInput.text = "";
110 textInput.visible = false;
111 break;
112 case NewStuffCore.Question.PasswordQuestion:
113 input = textInput.text;
114 textInput.text = "";
115 textInput.visible = false;
116 textInput.echoMode = QQC2.TextInput.Normal;
117 break;
118 case NewStuffCore.Question.ContinueCancelQuestion:
119 case NewStuffCore.Question.YesNoQuestion:
120 default:
121 // Nothing special to do for these types of question, we just pass along the positive or negative response
122 break;
123 }
124 NewStuff.QuickQuestionListener.passResponse(responseIsContinue, input);
125 }
126
127 ColumnLayout {
128 id: layout
129
130 readonly property real maxWidth: {
131 const bounds = dialog.parent;
132 if (!bounds) {
133 return 0;
134 }
135 return bounds.width - (dialog.leftPadding + dialog.leftMargin + dialog.rightMargin + dialog.rightPadding);
136 }
137
138 anchors.fill: parent
139 spacing: Kirigami.Units.smallSpacing
140
141 QQC2.Label {
142 id: questionLabel
143 Layout.maximumWidth: layout.maxWidth
144 wrapMode: Text.Wrap
145 }
146
147 ListView {
148 id: listView
149 spacing: Kirigami.Units.smallSpacing
150 Layout.maximumWidth: layout.maxWidth
151 Layout.fillWidth: true
152
153 visible: false
154
155 model: ListModel { }
156
157 delegate: QQC2.ItemDelegate {
158 width: listView.width
159 text: model.text
160 }
161 }
162
163 QQC2.TextField {
164 id: textInput
165
166 Layout.maximumWidth: layout.maxWidth
167 Layout.fillWidth: true
168
169 visible: false
170 }
171 }
172
173 onAccepted: {
174 passResponse(true);
175 }
176
177 onRejected: {
178 passResponse(false);
179 }
180}
KIOCORE_EXPORT QStringList list(const QString &fileClass)
qsizetype length() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:56:35 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.