| 1 | /* |
|---|---|
| 2 | This file is part of KNewStuffCore. |
| 3 | SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <admin@leinir.dk> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "question.h" |
| 9 | |
| 10 | #include "entry.h" |
| 11 | #include "questionmanager.h" |
| 12 | |
| 13 | #include <QCoreApplication> |
| 14 | #include <QEventLoop> |
| 15 | #include <optional> |
| 16 | |
| 17 | using namespace KNSCore; |
| 18 | |
| 19 | class KNSCore::QuestionPrivate |
| 20 | { |
| 21 | public: |
| 22 | QuestionPrivate() |
| 23 | : questionType(Question::YesNoQuestion) |
| 24 | , response() |
| 25 | { |
| 26 | } |
| 27 | QString question; |
| 28 | QString title; |
| 29 | QStringList list; |
| 30 | Entry entry; |
| 31 | |
| 32 | QEventLoop loop; |
| 33 | Question::QuestionType questionType; |
| 34 | std::optional<Question::Response> response; |
| 35 | QString textResponse; |
| 36 | }; |
| 37 | |
| 38 | Question::Question(QuestionType questionType, QObject *parent) |
| 39 | : QObject(parent) |
| 40 | , d(new QuestionPrivate) |
| 41 | { |
| 42 | d->questionType = questionType; |
| 43 | } |
| 44 | |
| 45 | Question::~Question() = default; |
| 46 | |
| 47 | Question::Response Question::ask() |
| 48 | { |
| 49 | Q_EMIT QuestionManager::instance() -> askQuestion(question: this); |
| 50 | if (!d->response.has_value()) { |
| 51 | d->loop.exec(); // Wait for the setResponse method to quit the event loop |
| 52 | } |
| 53 | return *d->response; |
| 54 | } |
| 55 | |
| 56 | Question::QuestionType Question::questionType() const |
| 57 | { |
| 58 | return d->questionType; |
| 59 | } |
| 60 | |
| 61 | void Question::setQuestionType(Question::QuestionType newType) |
| 62 | { |
| 63 | d->questionType = newType; |
| 64 | } |
| 65 | |
| 66 | void Question::setQuestion(const QString &newQuestion) |
| 67 | { |
| 68 | d->question = newQuestion; |
| 69 | } |
| 70 | |
| 71 | QString Question::question() const |
| 72 | { |
| 73 | return d->question; |
| 74 | } |
| 75 | |
| 76 | void Question::setTitle(const QString &newTitle) |
| 77 | { |
| 78 | d->title = newTitle; |
| 79 | } |
| 80 | |
| 81 | QString Question::title() const |
| 82 | { |
| 83 | return d->title; |
| 84 | } |
| 85 | |
| 86 | void Question::setList(const QStringList &newList) |
| 87 | { |
| 88 | d->list = newList; |
| 89 | } |
| 90 | |
| 91 | QStringList Question::list() const |
| 92 | { |
| 93 | return d->list; |
| 94 | } |
| 95 | |
| 96 | void Question::setResponse(Response response) |
| 97 | { |
| 98 | d->response = response; |
| 99 | d->loop.quit(); |
| 100 | } |
| 101 | |
| 102 | void Question::setResponse(const QString &response) |
| 103 | { |
| 104 | d->textResponse = response; |
| 105 | } |
| 106 | |
| 107 | QString Question::response() const |
| 108 | { |
| 109 | return d->textResponse; |
| 110 | } |
| 111 | |
| 112 | void Question::setEntry(const Entry &entry) |
| 113 | { |
| 114 | d->entry = entry; |
| 115 | } |
| 116 | |
| 117 | Entry Question::entry() const |
| 118 | { |
| 119 | return d->entry; |
| 120 | } |
| 121 | |
| 122 | #include "moc_question.cpp" |
| 123 |
