Warning, /frameworks/knewstuff/src/qtquick/qml/QuestionAsker.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003     SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 /**
0009  * @brief A component used to forward questions from KNewStuff's engine to the UI
0010  *
0011  * This component is equivalent to the WidgetQuestionListener
0012  * @see KNewStuff::WidgetQuestionListener
0013  * @see KNewStuffCore::Question
0014  * @since 5.63
0015  */
0016 
0017 import QtQuick
0018 import QtQuick.Controls as QQC2
0019 import QtQuick.Layouts
0020 import org.kde.kirigami 2 as Kirigami
0021 import org.kde.newstuff as NewStuff
0022 import org.kde.newstuff.core as NewStuffCore
0023 
0024 QQC2.Dialog {
0025     id: dialog
0026 
0027     property int questionType
0028 
0029     anchors.centerIn: QQC2.Overlay.overlay
0030 
0031     modal: true
0032     focus: true
0033 
0034     margins: Kirigami.Units.largeSpacing
0035     padding: Kirigami.Units.largeSpacing
0036 
0037     standardButtons: {
0038         switch (questionType) {
0039             case NewStuffCore.Question.SelectFromListQuestion:
0040             case NewStuffCore.Question.InputTextQuestion:
0041             case NewStuffCore.Question.PasswordQuestion:
0042             case NewStuffCore.Question.ContinueCancelQuestion:
0043                 // QQC2 Dialog standardButtons does not have a Continue button...
0044                 return QQC2.Dialog.Ok | QQC2.Dialog.Cancel;
0045             case NewStuffCore.Question.YesNoQuestion:
0046                 return QQC2.Dialog.Yes | QQC2.Dialog.No;
0047             default:
0048                 return QQC2.Dialog.NoButton;
0049         }
0050     }
0051 
0052     Connections {
0053         target: NewStuff.QuickQuestionListener
0054 
0055         function onAskListQuestion(title, question, list) {
0056             dialog.questionType = NewStuffCore.Question.SelectFromListQuestion;
0057             dialog.title = title;
0058             questionLabel.text = question;
0059             for (var i = 0; i < list.length; i++) {
0060                 listView.model.append({ text: list[i] });
0061             }
0062             listView.currentIndex = 0;
0063             listView.visible = true;
0064             dialog.open();
0065         }
0066 
0067         function onAskContinueCancelQuestion(title, question) {
0068             dialog.questionType = NewStuffCore.Question.ContinueCancelQuestion;
0069             dialog.title = title;
0070             questionLabel.text = question;
0071             dialog.open();
0072         }
0073 
0074         function onAskTextInputQuestion(title, question) {
0075             dialog.questionType = NewStuffCore.Question.InputTextQuestion;
0076             dialog.title = title;
0077             questionLabel.text = question;
0078             textInput.visible = true;
0079             dialog.open();
0080         }
0081 
0082         function onAskPasswordQuestion(title, question) {
0083             dialog.questionType = NewStuffCore.Question.PasswordQuestion;
0084             dialog.title = title;
0085             questionLabel.text = question;
0086             textInput.echoMode = QQC2.TextInput.PasswordEchoOnEdit;
0087             textInput.visible = true;
0088             dialog.open();
0089         }
0090 
0091         function onAskYesNoQuestion(title, question) {
0092             dialog.questionType = NewStuffCore.Question.YesNoQuestion;
0093             dialog.title = title;
0094             questionLabel.text = question;
0095             dialog.open();
0096         }
0097     }
0098 
0099     function passResponse(responseIsContinue) {
0100         let input = "";
0101         switch(dialog.questionType) {
0102         case NewStuffCore.Question.SelectFromListQuestion:
0103             input = listView.currentItem.text;
0104             listView.model.clear();
0105             listView.visible = false;
0106             break;
0107         case NewStuffCore.Question.InputTextQuestion:
0108             input = textInput.text;
0109             textInput.text = "";
0110             textInput.visible = false;
0111             break;
0112         case NewStuffCore.Question.PasswordQuestion:
0113             input = textInput.text;
0114             textInput.text = "";
0115             textInput.visible = false;
0116             textInput.echoMode = QQC2.TextInput.Normal;
0117             break;
0118         case NewStuffCore.Question.ContinueCancelQuestion:
0119         case NewStuffCore.Question.YesNoQuestion:
0120         default:
0121             // Nothing special to do for these types of question, we just pass along the positive or negative response
0122             break;
0123         }
0124         NewStuff.QuickQuestionListener.passResponse(responseIsContinue, input);
0125     }
0126 
0127     ColumnLayout {
0128         id: layout
0129 
0130         property int maxWidth: applicationWindow().width - (dialog.leftPadding + dialog.leftMargin + dialog.rightMargin + dialog.rightPadding)
0131 
0132         anchors.fill: parent
0133         spacing: Kirigami.Units.smallSpacing
0134 
0135         QQC2.Label {
0136             id: questionLabel
0137             Layout.maximumWidth: layout.maxWidth
0138             wrapMode: Text.Wrap
0139         }
0140 
0141         ListView {
0142             id: listView
0143 
0144             Layout.maximumWidth: layout.maxWidth
0145             Layout.fillWidth: true
0146             Layout.minimumHeight: Kirigami.Units.gridUnit * 6
0147 
0148             visible: false
0149 
0150             model: ListModel { }
0151 
0152             delegate: QQC2.ItemDelegate {
0153                 width: listView.width
0154                 text: model.text
0155             }
0156         }
0157 
0158         QQC2.TextField {
0159             id: textInput
0160 
0161             Layout.maximumWidth: layout.maxWidth
0162             Layout.fillWidth: true
0163 
0164             visible: false
0165         }
0166     }
0167 
0168     onAccepted: {
0169         passResponse(true);
0170     }
0171 
0172     onRejected: {
0173         passResponse(false);
0174     }
0175 }