Changeset View
Changeset View
Standalone View
Standalone View
src/core/question.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | This file is part of KNewStuffCore. | ||||
3 | | ||||
4 | Copyright (c) 2016 Dan Leinir Turthra Jensen <admin@leinir.dk> | ||||
5 | | ||||
6 | This library is free software; you can redistribute it and/or | ||||
7 | modify it under the terms of the GNU Lesser General Public | ||||
8 | License as published by the Free Software Foundation; either | ||||
9 | version 2.1 of the License, or (at your option) any later version. | ||||
10 | | ||||
11 | This library is distributed in the hope that it will be useful, | ||||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
14 | Lesser General Public License for more details. | ||||
15 | | ||||
16 | You should have received a copy of the GNU Lesser General Public | ||||
17 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
18 | */ | ||||
19 | | ||||
20 | #ifndef KNS3_QUESTION_H | ||||
21 | #define KNS3_QUESTION_H | ||||
22 | | ||||
23 | #include <QObject> | ||||
24 | | ||||
25 | #include "knewstuffcore_export.h" | ||||
26 | | ||||
27 | namespace KNSCore | ||||
28 | { | ||||
29 | /** | ||||
30 | * @short A way to ask a user a question from insude a GUI-less library (like KNewStuffCore) | ||||
31 | * | ||||
32 | * Rather than using a message box (which is a UI thing), when you want to ask your user | ||||
33 | * a question, create an instance of this class and use that instead. The consuming library | ||||
34 | * (in most cases KNewStuff or KNewStuffQuick) will listen to any question being asked, | ||||
35 | * and act appropriately (that is, KNewStuff will show a dialog with an appropriate dialog | ||||
36 | * box, and KNewStuffQuick will either request a question be asked if the developer is using | ||||
37 | * the plugin directly, or ask the question using an appropriate method for Qt Quick based | ||||
38 | * applications) | ||||
39 | * | ||||
40 | * The following is an example of a question asking the user to select an item from a list. | ||||
41 | * | ||||
42 | * @code | ||||
43 | QStringList choices() << "foo" << "bar"; | ||||
44 | Question question(Question::SelectFromListQuestion); | ||||
45 | question.setTitle("Pick your option"); | ||||
46 | question.setQuestion("Please select which option you would like"); | ||||
47 | question.setList(choices); | ||||
48 | if(question.ask() == Question::OKResponse) { | ||||
49 | QString theChoice = question.response(); | ||||
50 | } | ||||
51 | @endcode | ||||
52 | */ | ||||
53 | class KNEWSTUFFCORE_EXPORT Question : public QObject | ||||
54 | { | ||||
55 | Q_OBJECT | ||||
56 | public: | ||||
57 | enum Response { | ||||
58 | InvalidResponse = 0, | ||||
59 | YesResponse = 1, | ||||
60 | NoResponse = 2, | ||||
61 | ContinueResponse = 3, | ||||
62 | CancelResponse = 4, | ||||
63 | OKResponse = YesResponse | ||||
64 | }; | ||||
65 | | ||||
66 | enum QuestionType { | ||||
67 | YesNoQuestion = 0, | ||||
68 | ContinueCancelQuestion = 1, | ||||
69 | InputTextQuestion = 2, | ||||
70 | SelectFromListQuestion = 3, | ||||
71 | PasswordQuestion = 4 | ||||
72 | }; | ||||
73 | | ||||
74 | explicit Question(QuestionType = YesNoQuestion, QObject* parent = 0); | ||||
75 | virtual ~Question(); | ||||
76 | | ||||
77 | Response ask(); | ||||
78 | | ||||
79 | void setQuestionType(QuestionType newType = YesNoQuestion); | ||||
80 | QuestionType questionType() const; | ||||
81 | | ||||
82 | void setQuestion(QString newQuestion); | ||||
83 | QString question() const; | ||||
84 | void setTitle(QString newTitle); | ||||
85 | QString title() const; | ||||
86 | void setList(QStringList newList); | ||||
87 | QStringList list() const; | ||||
88 | | ||||
89 | /** | ||||
90 | * When the user makes a choice on a question, that is a response. This is the return value in ask(). | ||||
91 | * @param response This will set the response, and mark the question as answered | ||||
92 | */ | ||||
93 | void setResponse(Response response); | ||||
94 | /** | ||||
95 | * If the user has any way of inputting data to go along with the response above, consider this a part | ||||
96 | * of the response. As such, you can set, and later get, that response as well. This does NOT mark the | ||||
97 | * question as answered ( @see setResponse(Response) ). | ||||
98 | * @param response This sets the string response for the question | ||||
99 | */ | ||||
100 | void setResponse(QString response); | ||||
101 | QString response(); | ||||
102 | private: | ||||
103 | class Private; | ||||
104 | Private* d; | ||||
105 | }; | ||||
106 | } | ||||
107 | | ||||
108 | #endif//KNS3_QUESTION_H |