KNewStuff

widgetquestionlistener.cpp
1 /*
2  This file is part of KNewStuffCore.
3  SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 #include "widgetquestionlistener.h"
9 
10 #include "core/question.h"
11 
12 #include <KMessageBox>
13 #include <KPasswordDialog>
14 #include <QInputDialog>
15 
16 using namespace KNS3;
17 
18 class WidgetQuestionListenerHelper {
19 public:
20  WidgetQuestionListenerHelper() : q(nullptr) {}
21  ~WidgetQuestionListenerHelper() { delete q; }
22  WidgetQuestionListenerHelper(const WidgetQuestionListenerHelper &) = delete;
23  WidgetQuestionListenerHelper& operator=(const WidgetQuestionListenerHelper &) = delete;
24  WidgetQuestionListener *q;
25 };
26 Q_GLOBAL_STATIC(WidgetQuestionListenerHelper, s_kns3_widgetQuestionListener)
27 
28 WidgetQuestionListener* WidgetQuestionListener::instance()
29 {
30  if(!s_kns3_widgetQuestionListener()->q) {
31  new WidgetQuestionListener;
32  }
33  return s_kns3_widgetQuestionListener()->q;
34 }
35 
36 WidgetQuestionListener::WidgetQuestionListener()
37  : KNSCore::QuestionListener(nullptr)
38 {
39  s_kns3_widgetQuestionListener()->q = this;
40 }
41 
42 WidgetQuestionListener::~WidgetQuestionListener()
43 {
44 }
45 
46 void KNS3::WidgetQuestionListener::askQuestion(KNSCore::Question* question)
47 {
48  switch(question->questionType())
49  {
50  case KNSCore::Question::SelectFromListQuestion:
51  {
52  bool ok = false;
53  question->setResponse(QInputDialog::getItem(nullptr, question->title(), question->question(), question->list(), 0, false, &ok));
54  if(ok) {
55  question->setResponse(KNSCore::Question::OKResponse);
56  }
57  else {
58  question->setResponse(KNSCore::Question::CancelResponse);
59  }
60  }
61  break;
62  case KNSCore::Question::ContinueCancelQuestion:
63  {
64  KMessageBox::ButtonCode response = KMessageBox::warningContinueCancel(nullptr, question->question(), question->title());
65  if(response == KMessageBox::Continue) {
66  question->setResponse(KNSCore::Question::ContinueResponse);
67  }
68  else {
69  question->setResponse(KNSCore::Question::CancelResponse);
70  }
71  }
72  break;
73  case KNSCore::Question::PasswordQuestion:
74  {
75  KPasswordDialog dlg;
76  dlg.setPrompt(question->question());
77  if(dlg.exec()) {
78  question->setResponse(dlg.password());
79  question->setResponse(KNSCore::Question::ContinueResponse);
80  }
81  else {
82  question->setResponse(KNSCore::Question::CancelResponse);
83  }
84  }
85  break;
86  case KNSCore::Question::YesNoQuestion:
87  default:
88  {
89  KMessageBox::ButtonCode response = KMessageBox::questionYesNo(nullptr, question->question(), question->title());
90  if(response == KMessageBox::Yes) {
91  question->setResponse(KNSCore::Question::YesResponse);
92  }
93  else {
94  question->setResponse(KNSCore::Question::NoResponse);
95  }
96  }
97  break;
98  }
99 }
void setResponse(Response response)
When the user makes a choice on a question, that is a response.
Definition: question.cpp:97
A way to ask a user a question from inside a GUI-less library (like KNewStuffCore) ...
Definition: question.h:41
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)
The namespace for the KNewStuff classes.
Definition: button.cpp:19
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 18 2021 22:43:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.