File indexing completed on 2024-04-28 03:59:18

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.setApplicationName(QStringLiteral("kmessagedialogtest"));
0025 
0026     const auto types = {KMessageDialog::QuestionTwoActions,
0027                         KMessageDialog::QuestionTwoActionsCancel,
0028                         KMessageDialog::WarningTwoActions,
0029                         KMessageDialog::WarningTwoActionsCancel,
0030                         KMessageDialog::WarningContinueCancel,
0031                         KMessageDialog::Information,
0032                         KMessageDialog::Error};
0033 
0034     for (auto type : types) {
0035         auto dlg = std::unique_ptr<KMessageDialog>(new KMessageDialog(type, QStringLiteral("Message in a box."), nullptr));
0036         dlg->setCaption(QString{});
0037         dlg->setIcon(QIcon{});
0038         if ((type != KMessageDialog::Information) && (type != KMessageDialog::Error)) {
0039             dlg->setButtons(KGuiItem(QStringLiteral("Primary Action")), KGuiItem(QStringLiteral("Secondary Action")), KStandardGuiItem::cancel());
0040         }
0041 
0042         auto getResult = [&]() {
0043             const auto result = static_cast<KMessageDialog::ButtonType>(dlg->exec());
0044             switch (result) {
0045             case KMessageDialog::Ok:
0046             case KMessageDialog::PrimaryAction:
0047                 qDebug() << "Button OK/Primary clicked."
0048                          << "Don't ask again status:" << (dlg->isDontAskAgainChecked() ? "Checked" : "Unchecked");
0049                 break;
0050             case KMessageDialog::SecondaryAction:
0051                 qDebug() << "Button Secondary clicked"
0052                          << "Don't ask again status:" << (dlg->isDontAskAgainChecked() ? "Checked" : "Unchecked");
0053                 break;
0054             case KMessageDialog::Cancel:
0055                 qDebug() << "Button Cancel clicked";
0056                 break;
0057             default:
0058                 break;
0059             }
0060         };
0061 
0062         getResult();
0063 
0064         dlg->setDontAskAgainText(QStringLiteral("Do not show this again"));
0065         dlg->setDontAskAgainChecked(false);
0066         getResult();
0067 
0068         dlg->setListWidgetItems(QStringList{QStringLiteral("file1"), QStringLiteral("file2")});
0069         getResult();
0070 
0071         dlg->setDetails(QStringLiteral("Some more details."));
0072         getResult();
0073     }
0074     return app.exec();
0075 }