File indexing completed on 2024-04-21 07:43:22

0001 /*
0002     This file is part of KNewStuffCore.
0003     SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <admin@leinir.dk>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "question.h"
0009 
0010 #include "entry.h"
0011 #include "questionmanager.h"
0012 
0013 #include <QCoreApplication>
0014 #include <QEventLoop>
0015 #include <optional>
0016 
0017 using namespace KNSCore;
0018 
0019 class KNSCore::QuestionPrivate
0020 {
0021 public:
0022     QuestionPrivate()
0023         : questionType(Question::YesNoQuestion)
0024         , response()
0025     {
0026     }
0027     QString question;
0028     QString title;
0029     QStringList list;
0030     Entry entry;
0031 
0032     QEventLoop loop;
0033     Question::QuestionType questionType;
0034     std::optional<Question::Response> response;
0035     QString textResponse;
0036 };
0037 
0038 Question::Question(QuestionType questionType, QObject *parent)
0039     : QObject(parent)
0040     , d(new QuestionPrivate)
0041 {
0042     d->questionType = questionType;
0043 }
0044 
0045 Question::~Question() = default;
0046 
0047 Question::Response Question::ask()
0048 {
0049     Q_EMIT QuestionManager::instance()->askQuestion(this);
0050     if (!d->response.has_value()) {
0051         d->loop.exec(); // Wait for the setResponse method to quit the event loop
0052     }
0053     return *d->response;
0054 }
0055 
0056 Question::QuestionType Question::questionType() const
0057 {
0058     return d->questionType;
0059 }
0060 
0061 void Question::setQuestionType(Question::QuestionType newType)
0062 {
0063     d->questionType = newType;
0064 }
0065 
0066 void Question::setQuestion(const QString &newQuestion)
0067 {
0068     d->question = newQuestion;
0069 }
0070 
0071 QString Question::question() const
0072 {
0073     return d->question;
0074 }
0075 
0076 void Question::setTitle(const QString &newTitle)
0077 {
0078     d->title = newTitle;
0079 }
0080 
0081 QString Question::title() const
0082 {
0083     return d->title;
0084 }
0085 
0086 void Question::setList(const QStringList &newList)
0087 {
0088     d->list = newList;
0089 }
0090 
0091 QStringList Question::list() const
0092 {
0093     return d->list;
0094 }
0095 
0096 void Question::setResponse(Response response)
0097 {
0098     d->response = response;
0099     d->loop.quit();
0100 }
0101 
0102 void Question::setResponse(const QString &response)
0103 {
0104     d->textResponse = response;
0105 }
0106 
0107 QString Question::response() const
0108 {
0109     return d->textResponse;
0110 }
0111 
0112 void Question::setEntry(const Entry &entry)
0113 {
0114     d->entry = entry;
0115 }
0116 
0117 Entry Question::entry() const
0118 {
0119     return d->entry;
0120 }
0121 
0122 #include "moc_question.cpp"