KNewStuff

widgetquestionlistener.cpp
1 /*
2  This file is part of KNewStuffCore.
3 
4  Copyright (c) 2016 Dan Leinir Turthra Jensen <[email protected]>
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 #include "widgetquestionlistener.h"
21 
22 #include "core/question.h"
23 
24 #include <KMessageBox>
25 #include <KPasswordDialog>
26 #include <QInputDialog>
27 
28 using namespace KNS3;
29 
30 class WidgetQuestionListenerHelper {
31 public:
32  WidgetQuestionListenerHelper() : q(nullptr) {}
33  ~WidgetQuestionListenerHelper() { delete q; }
34  WidgetQuestionListenerHelper(const WidgetQuestionListenerHelper &) = delete;
35  WidgetQuestionListenerHelper& operator=(const WidgetQuestionListenerHelper &) = delete;
36  WidgetQuestionListener *q;
37 };
38 Q_GLOBAL_STATIC(WidgetQuestionListenerHelper, s_kns3_widgetQuestionListener)
39 
40 WidgetQuestionListener* WidgetQuestionListener::instance()
41 {
42  if(!s_kns3_widgetQuestionListener()->q) {
43  new WidgetQuestionListener;
44  }
45  return s_kns3_widgetQuestionListener()->q;
46 }
47 
48 WidgetQuestionListener::WidgetQuestionListener()
49  : KNSCore::QuestionListener(nullptr)
50 {
51  s_kns3_widgetQuestionListener()->q = this;
52 }
53 
54 WidgetQuestionListener::~WidgetQuestionListener()
55 {
56 }
57 
58 void KNS3::WidgetQuestionListener::askQuestion(KNSCore::Question* question)
59 {
60  switch(question->questionType())
61  {
62  case KNSCore::Question::SelectFromListQuestion:
63  {
64  bool ok = false;
65  question->setResponse(QInputDialog::getItem(nullptr, question->title(), question->question(), question->list(), 0, false, &ok));
66  if(ok) {
67  question->setResponse(KNSCore::Question::OKResponse);
68  }
69  else {
70  question->setResponse(KNSCore::Question::CancelResponse);
71  }
72  }
73  break;
74  case KNSCore::Question::ContinueCancelQuestion:
75  {
76  KMessageBox::ButtonCode response = KMessageBox::warningContinueCancel(nullptr, question->question(), question->title());
77  if(response == KMessageBox::Continue) {
78  question->setResponse(KNSCore::Question::ContinueResponse);
79  }
80  else {
81  question->setResponse(KNSCore::Question::CancelResponse);
82  }
83  }
84  break;
85  case KNSCore::Question::PasswordQuestion:
86  {
87  KPasswordDialog dlg;
88  dlg.setPrompt(question->question());
89  if(dlg.exec()) {
90  question->setResponse(dlg.password());
91  question->setResponse(KNSCore::Question::ContinueResponse);
92  }
93  else {
94  question->setResponse(KNSCore::Question::CancelResponse);
95  }
96  }
97  break;
98  case KNSCore::Question::YesNoQuestion:
99  default:
100  {
101  KMessageBox::ButtonCode response = KMessageBox::questionYesNo(nullptr, question->question(), question->title());
102  if(response == KMessageBox::Yes) {
103  question->setResponse(KNSCore::Question::YesResponse);
104  }
105  else {
106  question->setResponse(KNSCore::Question::NoResponse);
107  }
108  }
109  break;
110  }
111 }
void setResponse(Response response)
When the user makes a choice on a question, that is a response.
Definition: question.cpp:110
A way to ask a user a question from inside a GUI-less library (like KNewStuffCore) ...
Definition: question.h:53
QString getItem(QWidget *parent, const QString &title, const QString &label, const QStringList &items, int current, bool editable, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
Contains the core functionality for handling interaction with NewStuff providers. ...
virtual int exec()
QString password() const
ButtonCode warningContinueCancel(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonContinue=KStandardGuiItem::cont(), const KGuiItem &buttonCancel=KStandardGuiItem::cancel(), const QString &dontAskAgainName=QString(), Options options=Notify)
ButtonCode questionYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Notify)
void setPrompt(const QString &prompt)
Definition: button.cpp:29
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 22:43:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.