73 Q_UNUSED(windowFlags);
75 qCDebug(lcQpaDialogs) <<
"Asked to show" << windowModality <<
"dialog with parent" << parent;
77 if (m_alert.window.visible) {
78 qCDebug(lcQpaDialogs) <<
"Dialog already visible, ignoring request to show";
83 if (windowModality == Qt::NonModal)
87 if (windowModality == Qt::WindowModal && (!parent || !parent->handle())) {
88 qCWarning(lcQpaDialogs,
"Cannot run window modal dialog without parent window");
93 if (windowModality == Qt::WindowModal
94 && QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSTahoe)
102 if (!options()->detailedText().isEmpty()) {
103 qCWarning(lcQpaDialogs,
"Message box contains detailed text");
107 if (Qt::mightBeRichText(options()->text()) ||
108 Qt::mightBeRichText(options()->informativeText())) {
111 qCDebug(lcQpaDialogs,
"Message box contains text in rich text format");
116 m_alert = [NSAlert
new];
117 m_alert.window.title = options()->windowTitle().toNSString();
119 const QString text = toPlainText(options()->text());
120 m_alert.messageText = text.toNSString();
121 m_alert.informativeText = toPlainText(options()->informativeText()).toNSString();
123 switch (options()->standardIcon()) {
124 case QMessageDialogOptions::NoIcon: {
130 QPixmap iconPixmap = options()->iconPixmap();
131 if (!iconPixmap.isNull())
132 m_alert.icon = [NSImage imageFromQImage:iconPixmap.toImage()];
135 case QMessageDialogOptions::Information:
136 case QMessageDialogOptions::Question:
137 [m_alert setAlertStyle:NSAlertStyleInformational];
139 case QMessageDialogOptions::Warning:
140 [m_alert setAlertStyle:NSAlertStyleWarning];
142 case QMessageDialogOptions::Critical:
143 [m_alert setAlertStyle:NSAlertStyleCritical];
147 auto defaultButton = options()->defaultButton();
148 auto escapeButton = options()->escapeButton();
150 const auto addButton = [&](
auto title,
auto tag,
auto role) {
151 title = QPlatformTheme::removeMnemonics(title);
152 NSButton *button = [m_alert addButtonWithTitle:title.toNSString()];
161 qCDebug(lcQpaDialogs).verbosity(0) <<
"Adding button" << title <<
"with" << role;
163 if (!defaultButton && role == AcceptRole)
166 if (tag == defaultButton)
167 button.keyEquivalent = @
"\r";
168 else if ([button.keyEquivalent isEqualToString:@
"\r"])
169 button.keyEquivalent = @
"";
171 if (!escapeButton && role == RejectRole)
175 if (tag == escapeButton && ![button.keyEquivalent isEqualToString:@
"\r"])
176 button.keyEquivalent = @
"\e";
177 else if ([button.keyEquivalent isEqualToString:@
"\e"])
178 button.keyEquivalent = @
"";
180 button.hasDestructiveAction = role == DestructiveRole;
193 Q_ASSERT(tag >= NSAlertFirstButtonReturn);
199 struct Button { QString title;
int identifier; ButtonRole role; };
200 std::vector<Button> buttons;
202 const auto *platformTheme = QGuiApplicationPrivate::platformTheme();
203 if (
auto standardButtons = options()->standardButtons()) {
204 for (
int standardButton = FirstButton; standardButton <= LastButton; standardButton <<= 1) {
205 if (standardButtons & standardButton) {
206 auto title = platformTheme->standardButtonText(standardButton);
208 title, standardButton, buttonRole(StandardButton(standardButton))
213 const auto customButtons = options()->customButtons();
214 for (
auto customButton : customButtons)
215 buttons.push_back({customButton.label, customButton.id, customButton.role});
222 bool seenAccept =
false;
223 for (
auto &button : buttons) {
224 if (button.role == AcceptRole) {
228 button.role = AlternateRole;
232 std::vector<Button> orderedButtons;
233 const int *layoutEntry = buttonLayout(Qt::Horizontal, ButtonLayout::MacLayout);
234 while (*layoutEntry != QPlatformDialogHelper::EOL) {
235 const auto role = ButtonRole(*layoutEntry & ~ButtonRole::Reverse);
236 const bool reverse = *layoutEntry & ButtonRole::Reverse;
238 auto addButton = [&](
const Button &button) {
239 if (button.role == role)
240 orderedButtons.push_back(button);
244 std::for_each(std::crbegin(buttons), std::crend(buttons), addButton);
246 std::for_each(std::cbegin(buttons), std::cend(buttons), addButton);
252 for (
auto button = orderedButtons.crbegin(); button != orderedButtons.crend(); ++button)
253 addButton(button->title, button->identifier, button->role);
258 m_alert.buttons.firstObject.keyEquivalent = @
"\r";
260 if (
auto checkBoxLabel = options()->checkBoxLabel(); !checkBoxLabel.isNull()) {
261 checkBoxLabel = QPlatformTheme::removeMnemonics(checkBoxLabel);
262 m_alert.suppressionButton.title = checkBoxLabel.toNSString();
263 auto state = options()->checkBoxState();
264 m_alert.suppressionButton.allowsMixedState = state == Qt::PartiallyChecked;
265 m_alert.suppressionButton.state = controlStateFor(state);
266 m_alert.showsSuppressionButton = YES;
269 qCDebug(lcQpaDialogs) <<
"Showing" << m_alert;
271 if (windowModality == Qt::WindowModal) {
272 auto *cocoaWindow =
static_cast<
QCocoaWindow*>(parent->handle());
273 [m_alert beginSheetModalForWindow:cocoaWindow->nativeWindow()
274 completionHandler:^(NSModalResponse response) {
275 processResponse(response);
286 QTimer::singleShot(0,
this, [
this]{
287 if (m_alert && !m_alert.window.visible) {
288 qCDebug(lcQpaDialogs) <<
"Running deferred modal" << m_alert;
289 runModalAndProcessResponse();