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 2 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 implicitWidth: Math.max(Kirigami.Units.gridUnit * 20, view.width - (Kirigami.Units.gridUnit * 6))
30 implicitHeight: dialog.height + footer.height + (Kirigami.Units.gridUnit * 3)
31
32 anchors.centerIn: QQC2.Overlay.overlay
33
34 modal: true
35 focus: true
36
37 margins: Kirigami.Units.largeSpacing
38 padding: Kirigami.Units.largeSpacing
39
40 standardButtons: {
41 switch (questionType) {
42 case NewStuffCore.Question.SelectFromListQuestion:
43 case NewStuffCore.Question.InputTextQuestion:
44 case NewStuffCore.Question.PasswordQuestion:
45 case NewStuffCore.Question.ContinueCancelQuestion:
46 // QQC2 Dialog standardButtons does not have a Continue button...
47 return QQC2.Dialog.Ok | QQC2.Dialog.Cancel;
48 case NewStuffCore.Question.YesNoQuestion:
49 return QQC2.Dialog.Yes | QQC2.Dialog.No;
50 default:
51 return QQC2.Dialog.NoButton;
52 }
53 }
54
55 Connections {
56 target: NewStuff.QuickQuestionListener
57
58 function onAskListQuestion(title, question, list) {
59 dialog.questionType = NewStuffCore.Question.SelectFromListQuestion;
60 dialog.title = title;
61 questionLabel.text = question;
62 for (var i = 0; i < list.length; i++) {
63 listView.model.append({ text: list[i] });
64 }
65 listView.currentIndex = 0;
66 listView.visible = true;
67 dialog.open();
68 }
69
70 function onAskContinueCancelQuestion(title, question) {
71 dialog.questionType = NewStuffCore.Question.ContinueCancelQuestion;
72 dialog.title = title;
73 questionLabel.text = question;
74 dialog.open();
75 }
76
77 function onAskTextInputQuestion(title, question) {
78 dialog.questionType = NewStuffCore.Question.InputTextQuestion;
79 dialog.title = title;
80 questionLabel.text = question;
81 textInput.visible = true;
82 dialog.open();
83 }
84
85 function onAskPasswordQuestion(title, question) {
86 dialog.questionType = NewStuffCore.Question.PasswordQuestion;
87 dialog.title = title;
88 questionLabel.text = question;
89 textInput.echoMode = QQC2.TextInput.PasswordEchoOnEdit;
90 textInput.visible = true;
91 dialog.open();
92 }
93
94 function onAskYesNoQuestion(title, question) {
95 dialog.questionType = NewStuffCore.Question.YesNoQuestion;
96 dialog.title = title;
97 questionLabel.text = question;
98 dialog.open();
99 }
100 }
101
102 function passResponse(responseIsContinue) {
103 let input = "";
104 switch(dialog.questionType) {
105 case NewStuffCore.Question.SelectFromListQuestion:
106 input = listView.currentItem.text;
107 listView.model.clear();
108 listView.visible = false;
109 break;
110 case NewStuffCore.Question.InputTextQuestion:
111 input = textInput.text;
112 textInput.text = "";
113 textInput.visible = false;
114 break;
115 case NewStuffCore.Question.PasswordQuestion:
116 input = textInput.text;
117 textInput.text = "";
118 textInput.visible = false;
119 textInput.echoMode = QQC2.TextInput.Normal;
120 break;
121 case NewStuffCore.Question.ContinueCancelQuestion:
122 case NewStuffCore.Question.YesNoQuestion:
123 default:
124 // Nothing special to do for these types of question, we just pass along the positive or negative response
125 break;
126 }
127 NewStuff.QuickQuestionListener.passResponse(responseIsContinue, input);
128 }
129
130 ColumnLayout {
131 id: layout
132
133 property int maxWidth: dialog.width - (dialog.leftPadding + dialog.leftMargin + dialog.rightMargin + dialog.rightPadding)
134
135 anchors.fill: parent
136 spacing: Kirigami.Units.smallSpacing
137
138 QQC2.Label {
139 id: questionLabel
140 Layout.maximumWidth: layout.maxWidth
141 wrapMode: Text.Wrap
142 }
143
144 ListView {
145 id: listView
146 spacing: Kirigami.Units.smallSpacing
147 Layout.maximumWidth: layout.maxWidth
148 Layout.fillWidth: true
149
150 visible: false
151
152 model: ListModel { }
153
154 delegate: QQC2.ItemDelegate {
155 width: listView.width
156 text: model.text
157 }
158 }
159
160 QQC2.TextField {
161 id: textInput
162
163 Layout.maximumWidth: layout.maxWidth
164 Layout.fillWidth: true
165
166 visible: false
167 }
168 }
169
170 onAccepted: {
171 passResponse(true);
172 }
173
174 onRejected: {
175 passResponse(false);
176 }
177}
KIOCORE_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:21:35 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.