File indexing completed on 2024-04-21 15:05:25

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2020 Ahmad Samir <a.samirh78@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "kmessagedialog.h"
0009 
0010 #include <KGuiItem>
0011 #include <KStandardGuiItem>
0012 
0013 #include <QApplication>
0014 #include <QDebug>
0015 #include <QDialogButtonBox>
0016 #include <QMessageBox>
0017 #include <QPushButton>
0018 
0019 #include <memory>
0020 
0021 int main(int argc, char *argv[])
0022 {
0023     QApplication app(argc, argv);
0024     app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
0025     app.setApplicationName(QStringLiteral("kmessagedialogtest"));
0026 
0027     const auto types = {
0028         KMessageDialog::QuestionTwoActions,
0029         KMessageDialog::QuestionTwoActionsCancel,
0030         KMessageDialog::WarningTwoActions,
0031         KMessageDialog::WarningTwoActionsCancel,
0032         KMessageDialog::WarningContinueCancel,
0033         KMessageDialog::Information,
0034 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 97)
0035         KMessageDialog::Sorry,
0036 #endif
0037         KMessageDialog::Error
0038     };
0039 
0040     for (auto type : types) {
0041         auto dlg = std::unique_ptr<KMessageDialog>(new KMessageDialog(type, QStringLiteral("Message in a box."), nullptr));
0042         dlg->setCaption(QString{});
0043         dlg->setIcon(QIcon{});
0044         if ((type != KMessageDialog::Information) &&
0045 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 97)
0046             (type != KMessageDialog::Sorry) &&
0047 #endif
0048             (type != KMessageDialog::Error)) {
0049             dlg->setButtons(KGuiItem(QStringLiteral("Primary Action")), KGuiItem(QStringLiteral("Secondary Action")), KStandardGuiItem::cancel());
0050         }
0051 
0052         auto getResult = [&]() {
0053             const auto result = static_cast<KMessageDialog::ButtonType>(dlg->exec());
0054             switch (result) {
0055             case KMessageDialog::Ok:
0056             case KMessageDialog::PrimaryAction:
0057                 qDebug() << "Button OK/Primary clicked."
0058                          << "Don't ask again status:" << (dlg->isDontAskAgainChecked() ? "Checked" : "Unchecked");
0059                 break;
0060             case KMessageDialog::SecondaryAction:
0061                 qDebug() << "Button Secondary clicked"
0062                          << "Don't ask again status:" << (dlg->isDontAskAgainChecked() ? "Checked" : "Unchecked");
0063                 break;
0064             case KMessageDialog::Cancel:
0065                 qDebug() << "Button Cancel clicked";
0066                 break;
0067             default:
0068                 break;
0069             }
0070         };
0071 
0072         getResult();
0073 
0074         dlg->setDontAskAgainText(QStringLiteral("Do not show this again"));
0075         dlg->setDontAskAgainChecked(false);
0076         getResult();
0077 
0078         dlg->setListWidgetItems(QStringList{QStringLiteral("file1"), QStringLiteral("file2")});
0079         getResult();
0080 
0081         dlg->setDetails(QStringLiteral("Some more details."));
0082         getResult();
0083     }
0084 }