Kstars

ksmessagebox.cpp
1 /* KStars Asynchronous Message Box Implementation for Desktop/Android and EkosLive
2  Based on QMessageBox.
3 
4  SPDX-FileCopyrightText: 2019 Jasem Mutlaq <[email protected]>
5 
6  SPDX-License-Identifier: GPL-2.0-or-later
7 */
8 
9 #include "ksmessagebox.h"
10 #include "config-kstars.h"
11 #include "Options.h"
12 
13 #ifdef KSTARS_LITE
14 #include "kstarslite.h"
15 #else
16 #include "kstars.h"
17 #endif
18 
19 #include <QPushButton>
20 #include <QCheckBox>
21 #include <QLayout>
22 #include <QSpacerItem>
23 #include <QLabel>
24 
25 KSMessageBox * KSMessageBox::m_Instance = nullptr;
26 
27 KSMessageBox * KSMessageBox::Instance()
28 {
29  if (!m_Instance)
30  m_Instance = new KSMessageBox();
31 
32  return m_Instance;
33 }
34 
35 KSMessageBox::KSMessageBox() : QMessageBox()
36 {
37  setDefaultButton(StandardButton::Ok);
38  setIcon(QMessageBox::Information);
39  setObjectName("KSMessageBox");
40 
42 
43  m_ProgressTimer.setInterval(1000);
44  m_ProgressTimer.setSingleShot(false);
45  connect(&m_ProgressTimer, &QTimer::timeout, this, &KSMessageBox::timerTick);
46 
47  connect(this, &KSMessageBox::rejected, [this]()
48  {
49  m_ProgressTimer.stop();
50  emit newMessage(QJsonObject());
51  });
52 
53  connect(this, &KSMessageBox::accepted, [this]()
54  {
55  m_ProgressTimer.stop();
56  emit newMessage(QJsonObject());
57  });
58 }
59 
60 void KSMessageBox::error(const QString &message, const QString &title, quint32 timeout)
61 {
62 #ifdef KSTARS_LITE
63  Q_UNUSED(title);
65 #else
66  //KMessageBox::error(nullptr, message, title);
67  reset();
68 
69  addOKButton();
72  setWindowTitle(title);
73 
74  setupTimeout(timeout);
75  open();
76 
77  emit newMessage(createMessageObject());
78 #endif
79 }
80 
81 void KSMessageBox::sorry(const QString &message, const QString &title, quint32 timeout)
82 {
83 #ifdef KSTARS_LITE
84  Q_UNUSED(title);
86 #else
87  //KSNotification::sorry(message, title);
88  reset();
89 
90  addOKButton();
93  setWindowTitle(title);
94 
95  setupTimeout(timeout);
96  open();
97 
98  emit newMessage(createMessageObject());
99 #endif
100 }
101 
102 void KSMessageBox::info(const QString &message, const QString &title, quint32 timeout)
103 {
104 #ifdef KSTARS_LITE
105  Q_UNUSED(title);
107 #else
108  //KMessageBox::information(nullptr, message, title);
109  reset();
110 
111  addOKButton();
113  setText(message);
114  setWindowTitle(title);
115 
116  setupTimeout(timeout);
117  open();
118 
119  emit newMessage(createMessageObject());
120 
121 
122 #endif
123 }
124 
125 void KSMessageBox::setupTimeout(quint32 timeout)
126 {
127  m_Timeout = timeout;
128  if (m_Timeout == 0)
129  return;
130 
131  m_ProgressLabel = new QLabel(this);
132  m_ProgressLabel->setText(i18n("Auto close in ..."));
133 
134  m_ProgressIndicator = new QRoundProgressBar(this);
135  m_ProgressIndicator->setDecimals(0);
136  m_ProgressIndicator->setFormat("%v");
137  m_ProgressIndicator->setBarStyle(QRoundProgressBar::StyleLine);
138  m_ProgressIndicator->setFixedSize(QSize(32, 32));
139 
140  m_ProgressLayout = new QHBoxLayout();
141  m_ProgressLayout->setObjectName("ProgressLayout");
142  m_ProgressLayout->addWidget(m_ProgressLabel);
143  m_ProgressLayout->addWidget(m_ProgressIndicator);
144 
145  QGridLayout *gridLayout = qobject_cast<QGridLayout*>(layout());
146  if (gridLayout)
147  {
148  //gridLayout->addWidget(m_ProgressLabel.get(), 1, 2, 1, 1, Qt::AlignHCenter | Qt::AlignVCenter);
149  //gridLayout->addWidget(m_ProgressIndicator.get(), 1, 2, 1, 1, Qt::AlignHCenter | Qt::AlignVCenter);
150  gridLayout->addItem(m_ProgressLayout, 1, 2, 1, 2, Qt::AlignHCenter | Qt::AlignVCenter);
151  }
152 
153  m_ProgressTimer.start();
154  m_ProgressIndicator->setRange(0, m_Timeout);
155  m_ProgressIndicator->setValue(static_cast<int>(m_Timeout));
156 }
157 
158 void KSMessageBox::reset()
159 {
160  m_ProgressTimer.stop();
161  resetTimeout();
162 
163  QList<QPushButton *> allButtons = findChildren<QPushButton*>();
164  qDeleteAll(allButtons);
165 
166  delete (m_ProgressIndicator);
167  delete (m_ProgressLabel);
168  if (m_ProgressLayout)
169  {
170  layout()->removeItem(m_ProgressLayout);
171  delete (m_ProgressLayout);
172  }
173 
174 }
175 
176 void KSMessageBox::questionYesNo(const QString &message, const QString &title, quint32 timeout, bool defaultToYes,
177  const QString &yesText, const QString &noText)
178 {
179  reset();
180 
182  setText(message);
183  setWindowTitle(title);
184 
185  QPushButton *yesButton = new QPushButton(yesText, this);
186  QPushButton *noButton = new QPushButton(noText, this);
187 
190 
193 
194 
195  setDefaultButton(defaultToYes ? yesButton : noButton);
196  yesButton->setDefault(defaultToYes);
197  noButton->setDefault(!defaultToYes);
198 
199  setupTimeout(timeout);
200 
201  open();
202 
203  emit newMessage(createMessageObject());
204 }
205 
206 void KSMessageBox::warningContinueCancel(const QString &message, const QString &title, quint32 timeout,
207  bool defaultToContinue,
208  const QString &continueText, const QString &cancelText)
209 {
210  reset();
211 
213  setText(message);
214  setWindowTitle(title);
215 
216  QPushButton *continueButton = new QPushButton(continueText, this);
217  QPushButton *cancelButton = new QPushButton(cancelText, this);
218 
219  connect(continueButton, &QPushButton::clicked, this, &KSMessageBox::accepted);
220  connect(cancelButton, &QPushButton::clicked, this, &KSMessageBox::rejected);
221 
222  addButton(continueButton, QMessageBox::ActionRole);
223  addButton(cancelButton, QMessageBox::ActionRole);
224 
225  setDefaultButton(defaultToContinue ? continueButton : cancelButton);
226  continueButton->setDefault(defaultToContinue);
227  cancelButton->setDefault(!defaultToContinue);
228 
229  setupTimeout(timeout);
230 
231  open();
232 
233  emit newMessage(createMessageObject());
234 }
235 
236 void KSMessageBox::transient(const QString &message, const QString &title)
237 {
238 #ifdef KSTARS_LITE
239  Q_UNUSED(title);
241 #else
242  QPointer<QMessageBox> msgBox = new QMessageBox();
243  msgBox->setAttribute(Qt::WA_DeleteOnClose);
244  msgBox->setWindowTitle(title);
245  msgBox->setText(message);
246  msgBox->setModal(false);
247  msgBox->setIcon(QMessageBox::Warning);
248  msgBox->show();
249 #endif
250 }
251 
252 void KSMessageBox::addOKButton()
253 {
254  QPushButton *okButton = new QPushButton(i18n("Ok"), this);
257  setDefaultButton(okButton);
258 }
259 
260 void KSMessageBox::timerTick()
261 {
262  m_Timeout--;
263  m_ProgressIndicator->setValue(static_cast<int>(m_Timeout));
264 
265  if (m_Timeout == 0)
266  {
267  m_ProgressTimer.stop();
268  if (defaultButton())
270  else
271  reject();
272  }
273 }
274 
275 QJsonObject KSMessageBox::createMessageObject()
276 {
279 
280  message.insert("title", windowTitle());
281  message.insert("message", text());
282  message.insert("icon", icon());
283  message.insert("timeout", static_cast<int32_t>(m_Timeout));
284 
285  for (const auto oneButton : findChildren<QPushButton*>())
286  {
287  buttons.append(oneButton->text());
288  if (oneButton == defaultButton())
289  message.insert("default", oneButton->text());
290  }
291 
292  message.insert("buttons", buttons);
293 
294  return message;
295 }
296 
298 {
299  for (const auto oneButton : findChildren<QPushButton*>())
300  {
301  const QString buttonText = oneButton->text().remove("&");
302 
303  if (button == buttonText)
304  {
305  oneButton->animateClick();
306  return true;
307  }
308  }
309 
310  return false;
311 }
AlignHCenter
void setDefaultButton(QPushButton *button)
virtual void reject()
@ StyleLine
Line style (thin round line around the text)
QLayout * layout() const const
void clicked(bool checked)
static KStarsLite * Instance()
Definition: kstarslite.h:77
void transient(const QString &message, const QString &title)
transient Non modal message box that gets deleted on close.
QPushButton * defaultButton() const const
void addItem(QLayoutItem *item, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment)
void notificationMessage(QString msg)
Once this signal is emitted, notification with text msg will appear on the screen.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setIcon(QMessageBox::Icon)
virtual void open()
WindowStaysOnTopHint
void setWindowTitle(const QString &title)
void start(int msec)
void rejected()
QString i18n(const char *text, const TYPE &arg...)
void setWindowFlags(Qt::WindowFlags type)
QString buttonText(int button) const const
void timeout()
void animateClick(int msec)
QAbstractButton * button(QMessageBox::StandardButton which) const const
QString & remove(int position, int n)
void addButton(QAbstractButton *button, QMessageBox::ButtonRole role)
The QRoundProgressBar class represents a circular progress bar and maintains its API similar to the Q...
void setText(const QString &text)
QString & insert(int position, QChar ch)
void stop()
void setObjectName(const QString &name)
QList< QAbstractButton * > buttons() const const
void setDefault(bool)
void removeItem(QLayoutItem *item)
QString message
WA_DeleteOnClose
bool selectResponse(const QString &button)
selectResponse Programatically select one the buttons in the dialog.
QMessageBox(QWidget *parent)
void accepted()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 15 2022 04:04:02 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.