7#include <QtGui/qwindow.h>
8#include <QtGui/private/qguiapplication_p.h>
9#include <qpa/qplatformtheme.h>
11#include <QtCore/private/qcore_mac_p.h>
18using namespace Qt::StringLiterals;
20QIOSMessageDialog::QIOSMessageDialog()
21 : m_alertController(
nullptr)
25QIOSMessageDialog::~QIOSMessageDialog()
30inline QString QIOSMessageDialog::messageTextPlain()
33 const QSharedPointer<QMessageDialogOptions> &opt = options();
34 constexpr auto lineShift =
"\n\n"_L1;
35 const QString &informativeText = opt->informativeText();
36 const QString &detailedText = opt->detailedText();
38 QString text = opt->text();
39 if (!informativeText.isEmpty())
40 text += lineShift + informativeText;
41 if (!detailedText.isEmpty())
42 text += lineShift + detailedText;
44 text.replace(
"<p>"_L1,
"\n"_L1, Qt::CaseInsensitive);
50inline UIAlertAction *QIOSMessageDialog::createAction(
51 const QMessageDialogOptions::CustomButton &customButton)
53 const QString label = QPlatformTheme::removeMnemonics(customButton.label);
54 const UIAlertActionStyle style = UIAlertActionStyleDefault;
56 return [UIAlertAction actionWithTitle:label.toNSString() style:style handler:^(UIAlertAction *) {
58 emit clicked(
static_cast<QPlatformDialogHelper::StandardButton>(customButton.id), customButton.role);
62inline UIAlertAction *QIOSMessageDialog::createAction(StandardButton button)
64 const StandardButton labelButton = button == NoButton ? Ok : button;
65 const QString &standardLabel = QGuiApplicationPrivate::platformTheme()->standardButtonText(labelButton);
66 const QString &label = QPlatformTheme::removeMnemonics(standardLabel);
68 UIAlertActionStyle style = UIAlertActionStyleDefault;
70 style = UIAlertActionStyleCancel;
71 else if (button == Discard)
72 style = UIAlertActionStyleDestructive;
74 return [UIAlertAction actionWithTitle:label.toNSString() style:style handler:^(UIAlertAction *) {
76 if (button == NoButton)
79 emit clicked(button, buttonRole(button));
83void QIOSMessageDialog::exec()
85 m_eventLoop.exec(QEventLoop::DialogExec);
88bool QIOSMessageDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent)
90 Q_UNUSED(windowFlags);
93 || windowModality == Qt::NonModal)
96 if (!options()->checkBoxLabel().isNull())
99 m_alertController = [[UIAlertController
100 alertControllerWithTitle:options()->windowTitle().toNSString()
101 message:messageTextPlain().toNSString()
102 preferredStyle:UIAlertControllerStyleAlert] retain];
104 const QVector<QMessageDialogOptions::CustomButton> customButtons = options()->customButtons();
105 for (
const QMessageDialogOptions::CustomButton &button : customButtons) {
106 UIAlertAction *act = createAction(button);
107 [m_alertController addAction:act];
110 if (StandardButtons buttons = options()->standardButtons()) {
111 for (
int i = FirstButton; i < LastButton; i<<=1) {
113 [m_alertController addAction:createAction(StandardButton(i))];
115 }
else if (customButtons.isEmpty()) {
117 [m_alertController addAction:createAction(NoButton)];
120 UIWindow *window = presentationWindow(parent);
132 [window.rootViewController presentViewController:m_alertController animated:YES completion:nil];
136void QIOSMessageDialog::hide()
139 [m_alertController dismissViewControllerAnimated:YES completion:nil];
140 [m_alertController release];
141 m_alertController =
nullptr;
#define QStringLiteral(str)