KNewStuff

QuestionAsker.qml
1 /*
2  SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
16 import QtQuick 2.11
17 import QtQuick.Controls 2.11 as QtControls
18 import QtQuick.Layouts 1.11 as QtLayouts
19 
20 import org.kde.kirigami 2.7 as Kirigami
21 
22 import org.kde.newstuff.core 1.62 as NewStuffCore
23 import org.kde.newstuff 1.62 as NewStuff
24 
25 QtControls.Dialog {
26  id: dialog
27  modal: true
28  focus: true
29  property int questionType
30  anchors.centerIn: QtControls.Overlay.overlay
31  margins: Kirigami.Units.largeSpacing
32  padding: Kirigami.Units.largeSpacing
33  standardButtons: {
34  switch (questionType) {
35  case NewStuffCore.Question.SelectFromListQuestion:
36  case NewStuffCore.Question.InputTextQuestion:
37  case NewStuffCore.Question.PasswordQuestion:
38  case NewStuffCore.Question.ContinueCancelQuestion:
39  // QtControls Dialog standardButtons does not have a Continue button...
40  return QtControls.Dialog.Ok | QtControls.Dialog.Cancel;
41  break;
42  case NewStuffCore.Question.YesNoQuestion:
43  return QtControls.Dialog.Yes | QtControls.Dialog.No;
44  break;
45  default:
46  break;
47  }
48  }
49 
50  Connections {
51  target: NewStuff.QuickQuestionListener
52  onAskListQuestion: {
53  dialog.questionType = NewStuffCore.Question.SelectFromListQuestion;
54  dialog.title = title;
55  questionLabel.text = question;
56  for (var i = 0; i < list.length; i++) {
57  listView.model.append({ text: list[i] });
58  }
59  listView.currentIndex = 0;
60  listView.visible = true;
61  dialog.open();
62  }
63  onAskContinueCancelQuestion: {
64  dialog.questionType = NewStuffCore.Question.ContinueCancelQuestion;
65  dialog.title = title;
66  questionLabel.text = question;
67  dialog.open();
68  }
69  onAskTextInputQuestion: {
70  dialog.questionType = NewStuffCore.Question.InputTextQuestion;
71  dialog.title = title;
72  questionLabel.text = question;
73  textInput.visible = true;
74  dialog.open();
75  }
76  onAskPasswordQuestion: {
77  dialog.questionType = NewStuffCore.Question.PasswordQuestion;
78  dialog.title = title;
79  questionLabel.text = question;
80  textInput.echoMode = QtControls.TextInput.PasswordEchoOnEdit;
81  textInput.visible = true;
82  dialog.open();
83  }
84  onAskYesNoQuestion: {
85  dialog.questionType = NewStuffCore.Question.YesNoQuestion;
86  dialog.title = title;
87  questionLabel.text = question;
88  dialog.open();
89  }
90  }
91  Connections {
92  target: applicationWindow()
93  // Since dialogs in QML don't automatically reject when the application is closed,
94  // we just do that little job for it (and then we don't end up blocking everything
95  // when the application is shut without the question being answered)
96  onClosing: {
97  if (dialog.opened === true) {
98  passResponse(false);
99  }
100  }
101  }
102  function passResponse(responseIsContinue) {
103  var 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 = QtControls.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  QtLayouts.ColumnLayout {
131  anchors.fill: parent
132  property int maxWidth: applicationWindow().width - (dialog.leftPadding + dialog.leftMargin + dialog.rightMargin + dialog.rightPadding)
133  QtControls.Label {
134  id: questionLabel
135  QtLayouts.Layout.maximumWidth: parent.maxWidth
136  wrapMode: Text.Wrap
137  }
138  ListView {
139  id: listView
140  visible: false
141  QtLayouts.Layout.maximumWidth: parent.maxWidth
142  QtLayouts.Layout.fillWidth: true
143  QtLayouts.Layout.minimumHeight: Kirigami.Units.gridUnit * 6
144  model: ListModel { }
145  delegate: Kirigami.BasicListItem {
146  reserveSpaceForIcon: false
147  text: model.text
148  }
149  }
150  QtControls.TextField {
151  id: textInput
152  visible: false
153  QtLayouts.Layout.maximumWidth: parent.maxWidth
154  QtLayouts.Layout.fillWidth: true
155  }
156  }
157  onAccepted: {
158  passResponse(true);
159  }
160  onRejected: {
161  passResponse(false);
162  }
163 }
int length() const const
A dialog which has a NewStuff.Page at the base.
Definition: Dialog.qml:23
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 18 2021 22:43:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.