File indexing completed on 2024-04-14 03:40:55

0001 /*  KStars Asynchronous Message Box Implementation for Desktop/Android and EkosLive
0002     Based on QMessageBox.
0003 
0004     SPDX-FileCopyrightText: 2019 Jasem Mutlaq <mutlaqja@ikarustech.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #pragma once
0010 
0011 #include <KLocalizedString>
0012 
0013 #include <QMessageBox>
0014 #include <QPointer>
0015 #include <QTimer>
0016 #include <QRoundProgressBar.h>
0017 #include <QJsonObject>
0018 #include <QJsonArray>
0019 #include <QHBoxLayout>
0020 
0021 /**
0022  * @class KSMessageBox
0023  * KStars Message Box implementation.
0024  *
0025  * @author Jasem Mutlaq
0026  */
0027 class KSMessageBox: public QMessageBox
0028 {
0029         Q_OBJECT
0030         Q_PROPERTY(quint32 timeout MEMBER m_Timeout)
0031 
0032     public:
0033         static KSMessageBox *Instance();
0034 
0035         void questionYesNo(const QString &message, const QString &title = i18n("Question"), quint32 timeout = 0,
0036                            bool defaultToYes = true, const QString &yesText = i18n("Yes"), const QString &noText = i18n("No"));
0037         void warningContinueCancel(const QString &message, const QString &title = i18n("Warning"), quint32 timeout = 0,
0038                                    bool defaultToContinue = true, const QString &continueText = i18n("Continue"), const QString &cancelText = i18n("Cancel"));
0039         void error(const QString &message, const QString &title = i18n("Error"), quint32 timeout = 0);
0040         void sorry(const QString &message, const QString &title = i18n("Sorry"), quint32 timeout = 0);
0041         void info(const QString &message, const QString &title = i18n("Info"), quint32 timeout = 0);
0042         /**
0043          * @brief transient Non modal message box that gets deleted on close.
0044          * @param message message content
0045          * @param title message title
0046          */
0047         void transient(const QString &message, const QString &title);
0048 
0049         /**
0050          * @brief selectResponse Programatically select one the buttons in the dialog.
0051          * @param button text of button to click
0052          * @return True if button is found and clicked, false otherwise.
0053          */
0054         bool selectResponse(const QString &button);
0055 
0056     signals:
0057         void newMessage(const QJsonObject &message);
0058 
0059     protected:
0060         void timerTick();
0061 
0062     private:
0063         // Dialog timeout in seconds
0064         quint32 m_Timeout {60};
0065 
0066         static KSMessageBox *m_Instance;
0067 
0068         void reset();
0069         void addOKButton();
0070         void setupTimeout(quint32 timeout);
0071         void resetTimeout()
0072         {
0073             m_Timeout = 0;
0074         }
0075         QJsonObject createMessageObject();
0076         KSMessageBox();
0077 
0078         QPointer<QRoundProgressBar> m_ProgressIndicator;
0079         QPointer<QLabel> m_ProgressLabel;
0080         QPointer<QHBoxLayout> m_ProgressLayout;
0081 
0082         QTimer m_ProgressTimer;
0083 };
0084