KNewStuff

question.cpp
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
17using namespace KNSCore;
18
19class KNSCore::QuestionPrivate
20{
21public:
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
38Question::Question(QuestionType questionType, QObject *parent)
39 : QObject(parent)
40 , d(new QuestionPrivate)
41{
42 d->questionType = questionType;
43}
44
45Question::~Question() = default;
46
47Question::Response Question::ask()
48{
49 Q_EMIT QuestionManager::instance()->askQuestion(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
56Question::QuestionType Question::questionType() const
57{
58 return d->questionType;
59}
60
61void Question::setQuestionType(Question::QuestionType newType)
62{
63 d->questionType = newType;
64}
65
66void Question::setQuestion(const QString &newQuestion)
67{
68 d->question = newQuestion;
69}
70
71QString Question::question() const
72{
73 return d->question;
74}
75
76void Question::setTitle(const QString &newTitle)
77{
78 d->title = newTitle;
79}
80
81QString Question::title() const
82{
83 return d->title;
84}
85
86void Question::setList(const QStringList &newList)
87{
88 d->list = newList;
89}
90
91QStringList Question::list() const
92{
93 return d->list;
94}
95
96void Question::setResponse(Response response)
97{
98 d->response = response;
99 d->loop.quit();
100}
101
102void Question::setResponse(const QString &response)
103{
104 d->textResponse = response;
105}
106
107QString Question::response() const
108{
109 return d->textResponse;
110}
111
112void Question::setEntry(const Entry &entry)
113{
114 d->entry = entry;
115}
116
117Entry Question::entry() const
118{
119 return d->entry;
120}
121
122#include "moc_question.cpp"
KNewStuff data entry container.
Definition entry.h:48
A way to ask a user a question from inside a GUI-less library (like KNewStuffCore)
Definition question.h:47
void setResponse(Response response)
When the user makes a choice on a question, that is a response.
Definition question.cpp:96
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:54:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.