Debian Codegraph @

kysdk-qtwidgets/src/kmessagebox.cpp

raw
1 /*
2 * libkysdk-qtwidgets's Library
3 *
4 * Copyright (C) 2023, KylinSoft Co., Ltd.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this library. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Authors: Zhen Sun <[email protected]>
20 *
21 */
22
23 #include "kmessagebox.h"
24
25 #include "accessinfohelper.h"
26 #include "themeController.h"
27 #include <QApplication>
28 #include <QCheckBox>
29 #include <QDialogButtonBox>
30 #include <QLabel>
31 #include <QMessageBox>
32 #include <QPainter>
33 #include <QScreen>
34 #include <QStyle>
35 #include <QStyleOption>
36 #include <QTextDocument>
37 #include <QVBoxLayout>
38
39 namespace kdk
40 {
41
42 enum Button { Old_Ok = 1,
43 Old_Cancel = 2,
44 Old_Yes = 3,
45 Old_No = 4,
46 Old_Abort = 5,
47 Old_Retry = 6,
48 Old_Ignore = 7,
49 Old_YesAll = 8,
50 Old_NoAll = 9,
51 Old_ButtonMask = 0xFF,
52 NewButtonMask = 0xFFFFFC00 };
53
54 static QString iconName = "";
55
56 class KMessageBoxPrivate : public QObject, public ThemeController
57 {
58 Q_OBJECT
59 Q_DECLARE_PUBLIC(KMessageBox)
60
61 public:
62 KMessageBoxPrivate(KMessageBox *parent);
63
64 void init(const QString &title = QString(), const QString &text = QString());
65 static QPixmap standardIcon(KMessageBox::Icon icon, KMessageBox *mb);
66 void setupLayout();
67 void updateSize();
68 void setClickedButton(QAbstractButton *button);
69 int execReturnCode(QAbstractButton *button);
70 int dialogCodeForButton(QAbstractButton *button) const;
71 void addOldButtons(int button0, int button1, int button2);
72 QAbstractButton *findButton(int button0, int button1, int button2, int flags);
73 void setAccessInfo();
74
75 public Q_SLOTS:
76 void _q_buttonClicked(QAbstractButton *);
77
78 protected:
79 void changeTheme();
80
81 private:
82 KMessageBox *q_ptr;
83 QLabel *iconLabel;
84 QLabel *label;
85 QLabel *informativeLabel;
86 QDialogButtonBox *buttonBox;
87 QList<QAbstractButton *> customButtonList;
88 KMessageBox::Icon icon;
89 QAbstractButton *clickedButton;
90 QCheckBox *checkbox;
91 QPushButton *defaultButton;
92 bool autoAddOkButton;
93 bool compatMode;
94 QString m_iconName;
95 };
96
97 KMessageBox::KMessageBox(QWidget *parent)
98 : KDialog(parent)
99 , d_ptr(new KMessageBoxPrivate(this))
100 {
101 Q_D(KMessageBox);
102 d->init();
103 d->changeTheme();
104 connect(m_gsetting,&QGSettings::changed,this,[=](QString key){
105 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
106 {
107 changeTheme();
108 }
109 });
110 }
111
112 KMessageBox::~KMessageBox()
113 {
114 }
115
116 void KMessageBox::setCustomIcon(const QIcon &icon)
117 {
118 Q_D(KMessageBox);
119 d->m_iconName = icon.name();
120 this->setIconPixmap(icon.pixmap(24, 24));
121 }
122
123 void KMessageBox::addButton(QAbstractButton *button, ButtonRole role)
124 {
125 Q_D(KMessageBox);
126 if (!button)
127 return;
128 removeButton(button);
129 d->buttonBox->addButton(button, (QDialogButtonBox::ButtonRole)role);
130 d->customButtonList.append(button);
131 d->autoAddOkButton = false;
132 }
133
134 QPushButton *KMessageBox::addButton(const QString &text, ButtonRole role)
135 {
136 Q_D(KMessageBox);
137 QPushButton *pushButton = new QPushButton(text);
138 addButton(pushButton, role);
139 return pushButton;
140 }
141
142 QPushButton *KMessageBox::addButton(StandardButton button)
143 {
144 Q_D(KMessageBox);
145 QPushButton *pushButton = d->buttonBox->addButton((QDialogButtonBox::StandardButton)button);
146 pushButton->setIcon(QIcon());
147 if (pushButton)
148 d->autoAddOkButton = false;
149 return pushButton;
150 }
151
152 void KMessageBox::removeButton(QAbstractButton *button)
153 {
154 Q_D(KMessageBox);
155 d->customButtonList.removeAll(button);
156 if (d->defaultButton == button)
157 d->defaultButton = 0;
158 d->buttonBox->removeButton(button);
159 }
160
161 QAbstractButton *KMessageBox::button(KMessageBox::StandardButton which) const
162 {
163 Q_D(const KMessageBox);
164 return d->buttonBox->button(QDialogButtonBox::StandardButton(which));
165 }
166
167 QList<QAbstractButton *> KMessageBox::buttons() const
168 {
169 Q_D(const KMessageBox);
170 return d->buttonBox->buttons();
171 }
172
173 KMessageBox::ButtonRole KMessageBox::buttonRole(QAbstractButton *button) const
174 {
175 Q_D(const KMessageBox);
176 return KMessageBox::ButtonRole(d->buttonBox->buttonRole(button));
177 }
178
179 QCheckBox *KMessageBox::checkBox() const
180 {
181 Q_D(const KMessageBox);
182 return d->checkbox;
183 }
184
185 void KMessageBox::setCheckBox(QCheckBox *cb)
186 {
187 Q_D(KMessageBox);
188
189 if (cb == d->checkbox)
190 return;
191
192 if (d->checkbox) {
193 d->checkbox->hide();
194 layout()->removeWidget(d->checkbox);
195 if (d->checkbox->parentWidget() == this) {
196 d->checkbox->setParent(0);
197 d->checkbox->deleteLater();
198 }
199 }
200 d->checkbox = cb;
201 if (d->checkbox) {
202 QSizePolicy sp = d->checkbox->sizePolicy();
203 sp.setHorizontalPolicy(QSizePolicy::MinimumExpanding);
204 d->checkbox->setSizePolicy(sp);
205 }
206 d->setupLayout();
207 }
208
209 QString KMessageBox::text() const
210 {
211 Q_D(const KMessageBox);
212 return d->label->text();
213 }
214
215 void KMessageBox::setText(const QString &text)
216 {
217 Q_D(KMessageBox);
218 d->label->setText(text);
219 d->label->setWordWrap(d->label->textFormat() == Qt::RichText
220 || (d->label->textFormat() == Qt::AutoText && Qt::mightBeRichText(text)));
221 d->updateSize();
222 }
223
224 QString KMessageBox::informativeText() const
225 {
226 Q_D(const KMessageBox);
227 return d->informativeLabel ? d->informativeLabel->text() : QString();
228 }
229
230 void KMessageBox::setInformativeText(const QString &text)
231 {
232 Q_D(KMessageBox);
233 if (text.isEmpty()) {
234 if (d->informativeLabel) {
235 d->informativeLabel->hide();
236 d->informativeLabel->deleteLater();
237 }
238 d->informativeLabel = 0;
239 } else {
240 if (!d->informativeLabel) {
241 QLabel *label = new QLabel;
242 label->setObjectName(QLatin1String("qt_msgbox_informativelabel"));
243 label->setTextInteractionFlags(Qt::TextInteractionFlags(style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags, 0, this)));
244 label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
245 label->setOpenExternalLinks(true);
246 label->setWordWrap(true);
247 d->informativeLabel = label;
248
249 d->setAccessInfo();
250 }
251 d->informativeLabel->setText(text);
252 }
253 d->setupLayout();
254 }
255
256 KMessageBox::Icon KMessageBox::icon() const
257 {
258 Q_D(const KMessageBox);
259 return d->icon;
260 }
261
262 void KMessageBox::setIcon(Icon icon)
263 {
264 Q_D(KMessageBox);
265 setIconPixmap(KMessageBoxPrivate::standardIcon((KMessageBox::Icon)icon, this));
266 d->icon = icon;
267 }
268
269 QPixmap KMessageBox::iconPixmap() const
270 {
271 Q_D(const KMessageBox);
272 if (d->iconLabel && d->iconLabel->pixmap().isNull())
273 return d->iconLabel->pixmap();
274 return QPixmap();
275 }
276
277 void KMessageBox::setIconPixmap(const QPixmap &pixmap)
278 {
279 Q_D(KMessageBox);
280 d->iconLabel->setPixmap(pixmap);
281 d->icon = NoIcon;
282 d->setupLayout();
283 }
284
285 KMessageBox::StandardButtons KMessageBox::standardButtons() const
286 {
287 Q_D(const KMessageBox);
288 return KMessageBox::StandardButtons(int(d->buttonBox->standardButtons()));
289 }
290
291 void KMessageBox::setStandardButtons(KMessageBox::StandardButtons buttons)
292 {
293 Q_D(KMessageBox);
294 d->buttonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(buttons)));
295
296 QList<QAbstractButton *> buttonList = d->buttonBox->buttons();
297 if (!buttonList.contains(d->defaultButton))
298 d->defaultButton = 0;
299 d->autoAddOkButton = false;
300 }
301
302 KMessageBox::StandardButton KMessageBox::standardButton(QAbstractButton *button) const
303 {
304 Q_D(const KMessageBox);
305 return (KMessageBox::StandardButton)d->buttonBox->standardButton(button);
306 }
307
308 QPushButton *KMessageBox::defaultButton() const
309 {
310 Q_D(const KMessageBox);
311 return d->defaultButton;
312 }
313
314 void KMessageBox::setDefaultButton(QPushButton *button)
315 {
316 Q_D(KMessageBox);
317 if (!d->buttonBox->buttons().contains(button)) {
318 return;
319 }
320
321 d->defaultButton = button;
322 button->setProperty("isImportant", true);
323 button->setDefault(true);
324 button->setFocus();
325 }
326
327 void KMessageBox::setDefaultButton(KMessageBox::StandardButton button)
328 {
329 Q_D(KMessageBox);
330 setDefaultButton(d->buttonBox->button(QDialogButtonBox::StandardButton(button)));
331 }
332
333 QAbstractButton *KMessageBox::clickedButton() const
334 {
335 Q_D(const KMessageBox);
336 return d->clickedButton;
337 }
338
339 QPixmap KMessageBox::standardIcon(KMessageBox::Icon icon)
340 {
341 return KMessageBoxPrivate::standardIcon(icon, 0);
342 }
343
344 KMessageBox::StandardButton KMessageBox::information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons, KMessageBox::StandardButton defaultButton)
345 {
346 KMessageBox msgBox(parent);
347 msgBox.setIcon(KMessageBox::Icon::Information);
348 msgBox.setWindowTitle(title);
349 msgBox.setText(text);
350 msgBox.setStandardButtons(buttons);
351 msgBox.setDefaultButton(defaultButton);
352 msgBox.setParent(parent);
353 QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox *>();
354 Q_ASSERT(buttonBox != 0);
355
356 if (msgBox.exec() == -1)
357 return KMessageBox::Cancel;
358 return msgBox.standardButton(msgBox.clickedButton());
359 }
360
361 KMessageBox::StandardButton KMessageBox::question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons, KMessageBox::StandardButton defaultButton)
362 {
363 KMessageBox msgBox(parent);
364 msgBox.setIcon(KMessageBox::Icon::Question);
365 msgBox.setWindowTitle(title);
366 msgBox.setText(text);
367 msgBox.setStandardButtons(buttons);
368 msgBox.setDefaultButton(defaultButton);
369 msgBox.setParent(parent);
370 QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox *>();
371 Q_ASSERT(buttonBox != 0);
372
373 if (msgBox.exec() == -1)
374 return KMessageBox::Cancel;
375 return msgBox.standardButton(msgBox.clickedButton());
376 }
377
378 KMessageBox::StandardButton KMessageBox::warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons, KMessageBox::StandardButton defaultButton)
379 {
380 KMessageBox msgBox(parent);
381 msgBox.setIcon(KMessageBox::Icon::Warning);
382 msgBox.setWindowTitle(title);
383 msgBox.setText(text);
384 msgBox.setStandardButtons(buttons);
385 msgBox.setDefaultButton(defaultButton);
386 msgBox.setParent(parent);
387 QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox *>();
388 Q_ASSERT(buttonBox != 0);
389
390 if (msgBox.exec() == -1)
391 return KMessageBox::Cancel;
392 return msgBox.standardButton(msgBox.clickedButton());
393 }
394
395 KMessageBox::StandardButton KMessageBox::critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons, KMessageBox::StandardButton defaultButton)
396 {
397 KMessageBox msgBox(parent);
398 msgBox.setIcon(KMessageBox::Icon::Critical);
399 msgBox.setWindowTitle(title);
400 msgBox.setText(text);
401 msgBox.setStandardButtons(buttons);
402 msgBox.setDefaultButton(defaultButton);
403 msgBox.setParent(parent);
404 QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox *>();
405 Q_ASSERT(buttonBox != 0);
406
407 if (msgBox.exec() == -1)
408 return KMessageBox::Cancel;
409 return msgBox.standardButton(msgBox.clickedButton());
410 }
411
412 KMessageBox::StandardButton KMessageBox::success(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons, KMessageBox::StandardButton defaultButton)
413 {
414 KMessageBox msgBox(parent);
415 msgBox.setCustomIcon(QIcon::fromTheme("ukui-dialog-success"));
416 msgBox.setWindowTitle(title);
417 msgBox.setText(text);
418 msgBox.setStandardButtons(buttons);
419 msgBox.setDefaultButton(defaultButton);
420 msgBox.setParent(parent);
421 QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox *>();
422 Q_ASSERT(buttonBox != 0);
423
424 if (msgBox.exec() == -1)
425 return KMessageBox::Cancel;
426 return msgBox.standardButton(msgBox.clickedButton());
427 }
428
429 QLabel *KMessageBox::informativeLabel()
430 {
431 Q_D(KMessageBox);
432 return d->informativeLabel;
433 }
434
435 bool KMessageBox::event(QEvent *e)
436 {
437 Q_D(KMessageBox);
438 bool result = KDialog::event(e);
439 switch (e->type()) {
440 case QEvent::LayoutRequest:
441 d->updateSize();
442 break;
443 case QEvent::FontChange:
444 d->updateSize();
445 break;
446 case QEvent::Show: {
447 d->updateSize();
448 QWidget *parentWindow = qobject_cast<QWidget *>(this->parent());
449
450 if (parentWindow && parentWindow->isActiveWindow()) {
451 const QPoint parentWindowCenter = parentWindow->geometry().center();
452 const QPoint messageBoxCenter = this->geometry().center();
453 const QPoint moveOffset = parentWindowCenter - messageBoxCenter;
454 move(pos() + moveOffset);
455 } else {
456 QScreen *screen = QGuiApplication::primaryScreen();
457 int screenWidth = screen->geometry().width();
458 int screenHeight = screen->geometry().height();
459
460 int messageBox_X = (screenWidth - this->width()) / 2;
461 int messageBox_Y = (screenHeight - this->height()) / 2;
462
463 this->move(messageBox_X, messageBox_Y);
464 }
465 break;
466 }
467 default:
468 break;
469 }
470 return result;
471 }
472
473 KMessageBoxPrivate::KMessageBoxPrivate(KMessageBox *parent)
474 : q_ptr(parent)
475 , informativeLabel(0)
476 , checkbox(0)
477 , compatMode(false)
478 , clickedButton(0)
479 , defaultButton(0)
480
481 {
482 Q_Q(KMessageBox);
483 setParent(parent);
484 }
485
486 void KMessageBoxPrivate::init(const QString &title, const QString &text)
487 {
488 Q_Q(KMessageBox);
489
490 label = new QLabel;
491 label->setObjectName(QLatin1String("qt_kmsgbox_label"));
492 label->setTextInteractionFlags(Qt::TextInteractionFlags(q->style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags, 0, q)));
493 label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
494 label->setOpenExternalLinks(true);
495 iconLabel = new QLabel(q);
496 iconLabel->setObjectName(QLatin1String("qt_kmsgbox_icon_label"));
497 iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
498
499 buttonBox = new QDialogButtonBox;
500 buttonBox->setObjectName(QLatin1String("qt_kmsgbox_buttonbox"));
501 buttonBox->setCenterButtons(q->style()->styleHint(QStyle::SH_MessageBox_CenterButtons, nullptr, q));
502 QObject::connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
503 this, SLOT(_q_buttonClicked(QAbstractButton *)));
504 setupLayout();
505 if (!title.isEmpty() || !text.isEmpty()) {
506 q->setWindowTitle(title);
507 q->setText(text);
508 }
509 q->setModal(true);
510 icon = KMessageBox::NoIcon;
511
512 KDK_ALL_INFO_FORMAT(iconLabel,"");
513 KDK_ALL_INFO_FORMAT(label,"");
514 KDK_ALL_INFO_FORMAT(buttonBox,"");
515 }
516
517 QPixmap KMessageBoxPrivate::standardIcon(KMessageBox::Icon icon, KMessageBox *mb)
518 {
519 QStyle *style = mb ? mb->style() : QApplication::style();
520 int iconSize = style->pixelMetric(QStyle::PM_MessageBoxIconSize, nullptr, mb);
521 QIcon tmpIcon;
522 switch (icon) {
523 case KMessageBox::Information:
524 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxInformation, nullptr, mb);
525 iconName = tmpIcon.name();
526 break;
527 case KMessageBox::Warning:
528 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxWarning, nullptr, mb);
529 iconName = tmpIcon.name();
530 break;
531 case KMessageBox::Critical:
532 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxCritical, nullptr, mb);
533 iconName = tmpIcon.name();
534 break;
535 case KMessageBox::Question:
536 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxQuestion, nullptr, mb);
537 iconName = tmpIcon.name();
538 default:
539 break;
540 }
541 if (!tmpIcon.isNull()) {
542 QWindow *window = nullptr;
543 if (mb) {
544 window = mb->windowHandle();
545 if (!window) {
546 if (const QWidget *nativeParent = mb->nativeParentWidget())
547 window = nativeParent->windowHandle();
548 }
549 }
550 return tmpIcon.pixmap(window, QSize(iconSize, iconSize));
551 }
552
553 return QPixmap();
554 }
555
556 void KMessageBoxPrivate::setupLayout()
557 {
558 Q_Q(KMessageBox);
559 if (q->mainWidget()->layout())
560 delete q->mainWidget()->layout();
561
562 QGridLayout *grid = new QGridLayout;
563 QHBoxLayout *buttonLayout = new QHBoxLayout;
564 buttonLayout->setContentsMargins(0, 0, 0, 0);
565 grid->setHorizontalSpacing(8);
566 if (informativeLabel)
567 grid->setVerticalSpacing(8);
568 else
569 grid->setVerticalSpacing(0);
570 grid->setContentsMargins(0, 0, 0, 0);
571
572 bool hasIcon = !iconLabel->pixmap().isNull();
573 if (hasIcon)
574 grid->addWidget(iconLabel, 0, 0, 2, 1, Qt::AlignTop);
575 iconLabel->setVisible(hasIcon);
576 grid->addWidget(label, 0, hasIcon ? 2 : 1, 1, 1);
577
578 if (informativeLabel) {
579 grid->addWidget(informativeLabel, 1, hasIcon ? 2 : 1, 1, 1);
580 }
581 grid->setSizeConstraint(QLayout::SetNoConstraint);
582
583 QVBoxLayout *layout = new QVBoxLayout;
584 layout->setContentsMargins(24, 0, 24, 24);
585
586 buttonLayout->setSizeConstraint(QLayout::SetNoConstraint);
587 if (checkbox)
588 buttonLayout->addWidget(checkbox, 0, Qt::AlignLeft | Qt::AlignVCenter);
589 buttonLayout->addWidget(buttonBox, 0, Qt::AlignRight | Qt::AlignVCenter);
590
591 layout->setSpacing(0);
592 layout->addLayout(grid);
593 layout->addSpacing(32);
594 layout->addLayout(buttonLayout);
595
596 q->mainWidget()->setLayout(layout);
597 updateSize();
598 }
599
600 void KMessageBoxPrivate::updateSize()
601 {
602 Q_Q(KMessageBox);
603
604 if (!q->isVisible())
605 return;
606
607 while (buttonBox->layout()->count() < buttonBox->buttons().count() + 1) {
608 QEvent event(QEvent::StyleChange);
609 QGuiApplication::sendEvent(buttonBox, &event);
610 }
611
612 QSize minSize(424, 156);
613 QSize size;
614 QSize screenSize = QGuiApplication::screenAt(QCursor::pos())->availableGeometry().size();
615 QSize maxSize(screenSize.width() * 0.8, screenSize.height() * 0.8);
616
617 label->setWordWrap(false);
618 if (informativeLabel) {
619 informativeLabel->setWordWrap(false);
620 }
621
622 q->mainWidget()->layout()->activate();
623
624 if (q->sizeHint().width() > qMax(buttonBox->sizeHint().width() + 24 + 24, 452)) {
625 label->setWordWrap(true);
626 if (informativeLabel) {
627 informativeLabel->setWordWrap(true);
628 }
629 }
630
631 q->mainWidget()->layout()->activate();
632
633 q->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
634 size.setWidth(qMax(qMin(maxSize.width(), q->sizeHint().width()), minSize.width()));
635 size.setHeight(qMax(qMin(maxSize.height(), q->layout()->hasHeightForWidth() ? q->layout()->totalHeightForWidth(size.width()) : q->layout()->totalMinimumSize().height()), minSize.height()));
636 q->setFixedSize(size);
637
638 QCoreApplication::removePostedEvents(q, QEvent::LayoutRequest);
639 }
640
641 void KMessageBoxPrivate::_q_buttonClicked(QAbstractButton *button)
642 {
643 Q_Q(KMessageBox);
644 setClickedButton(button);
645 }
646
647 static int oldButton(int button)
648 {
649 switch (button & KMessageBox::ButtonMask) {
650 case KMessageBox::Ok:
651 return Old_Ok;
652 case KMessageBox::Cancel:
653 return Old_Cancel;
654 case KMessageBox::Yes:
655 return Old_Yes;
656 case KMessageBox::No:
657 return Old_No;
658 case KMessageBox::Abort:
659 return Old_Abort;
660 case KMessageBox::Retry:
661 return Old_Retry;
662 case KMessageBox::Ignore:
663 return Old_Ignore;
664 case KMessageBox::YesToAll:
665 return Old_YesAll;
666 case KMessageBox::NoToAll:
667 return Old_NoAll;
668 default:
669 return 0;
670 }
671 }
672
673 void KMessageBoxPrivate::setClickedButton(QAbstractButton *button)
674 {
675 Q_Q(KMessageBox);
676
677 clickedButton = button;
678 emit q->buttonClicked(clickedButton);
679
680 int resultCode = execReturnCode(button);
681 q->setResult(resultCode);
682 q->hide();
683 q->close();
684 int dialogCode = dialogCodeForButton(button);
685 if (dialogCode == QDialog::Accepted)
686 emit q->accepted();
687 else if (dialogCode == QDialog::Rejected)
688 emit q->rejected();
689 emit q->finished(resultCode);
690 }
691
692 int KMessageBoxPrivate::execReturnCode(QAbstractButton *button)
693 {
694 int ret = buttonBox->standardButton(button);
695 if (ret == KMessageBox::NoButton) {
696 ret = customButtonList.indexOf(button); // if button == 0, correctly sets ret = -1
697 } else if (compatMode) {
698 ret = oldButton(ret);
699 }
700 return ret;
701 }
702
703 int KMessageBoxPrivate::dialogCodeForButton(QAbstractButton *button) const
704 {
705 Q_Q(const KMessageBox);
706
707 switch (q->buttonRole(button)) {
708 case KMessageBox::AcceptRole:
709 case KMessageBox::YesRole:
710 return KDialog::Accepted;
711 case KMessageBox::RejectRole:
712 case KMessageBox::NoRole:
713 return KDialog::Rejected;
714 default:
715 return -1;
716 }
717 }
718
719 static KMessageBox::StandardButton newButton(int button)
720 {
721 // this is needed for source compatibility with Qt 4.0 and 4.1
722 if (button == KMessageBox::NoButton || (button & NewButtonMask))
723 return KMessageBox::StandardButton(button & KMessageBox::ButtonMask);
724
725 #if QT_VERSION < 0x050000
726 // this is needed for binary compatibility with Qt 4.0 and 4.1
727 switch (button & Old_ButtonMask) {
728 case Old_Ok:
729 return KMessageBox::Ok;
730 case Old_Cancel:
731 return KMessageBox::Cancel;
732 case Old_Yes:
733 return KMessageBox::Yes;
734 case Old_No:
735 return KMessageBox::No;
736 case Old_Abort:
737 return KMessageBox::Abort;
738 case Old_Retry:
739 return KMessageBox::Retry;
740 case Old_Ignore:
741 return KMessageBox::Ignore;
742 case Old_YesAll:
743 return KMessageBox::YesToAll;
744 case Old_NoAll:
745 return KMessageBox::NoToAll;
746 default:
747 return KMessageBox::NoButton;
748 }
749 #else
750 return KMessageBox::NoButton;
751 #endif
752 }
753
754 static bool detectedCompat(int button0, int button1, int button2)
755 {
756 if (button0 != 0 && !(button0 & NewButtonMask))
757 return true;
758 if (button1 != 0 && !(button1 & NewButtonMask))
759 return true;
760 if (button2 != 0 && !(button2 & NewButtonMask))
761 return true;
762 return false;
763 }
764
765 void KMessageBoxPrivate::addOldButtons(int button0, int button1, int button2)
766 {
767 Q_Q(KMessageBox);
768 q->addButton(newButton(button0));
769 q->addButton(newButton(button1));
770 q->addButton(newButton(button2));
771 q->setDefaultButton(
772 static_cast<QPushButton *>(findButton(button0, button1, button2, KMessageBox::Default)));
773 compatMode = detectedCompat(button0, button1, button2);
774 }
775
776 QAbstractButton *KMessageBoxPrivate::findButton(int button0, int button1, int button2, int flags)
777 {
778 Q_Q(KMessageBox);
779 int button = 0;
780
781 if (button0 & flags) {
782 button = button0;
783 } else if (button1 & flags) {
784 button = button1;
785 } else if (button2 & flags) {
786 button = button2;
787 }
788 return q->button(newButton(button));
789 }
790
791 void KMessageBoxPrivate::setAccessInfo()
792 {
793 Q_Q(KMessageBox);
794 KDK_ALL_INFO_FORMAT(informativeLabel,"");
795 }
796
797 void KMessageBoxPrivate::changeTheme()
798 {
799 Q_Q(KMessageBox);
800 if (iconName != "")
801 m_iconName = iconName;
802 q->setIconPixmap(QIcon::fromTheme(m_iconName).pixmap(24, 24));
803 }
804 }
805
806 #include "kmessagebox.moc"
807 #include "moc_kmessagebox.cpp"