| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/messagebox.h" |
| 4 | |
| 5 | #include "miscellaneous/application.h" |
| 6 | #include "miscellaneous/iconfactory.h" |
| 7 | |
| 8 | #include <QCheckBox> |
| 9 | #include <QDialogButtonBox> |
| 10 | #include <QPushButton> |
| 11 | #include <QStyle> |
| 12 | #include <QtGlobal> |
| 13 | |
| 14 | MsgBox::MsgBox(QWidget* parent) : QMessageBox(parent) { |
| 15 | // setAttribute(Qt::WA_WindowPropagation, true); |
| 16 | } |
| 17 | |
| 18 | void MsgBox::setIcon(QMessageBox::Icon icon) { |
| 19 | // Determine correct status icon size. |
| 20 | const int icon_size = qApp->style()->pixelMetric(QStyle::PixelMetric::PM_MessageBoxIconSize, nullptr, this); |
| 21 | |
| 22 | // Setup status icon. |
| 23 | setIconPixmap(iconForStatus(icon).pixmap(icon_size, icon_size)); |
| 24 | } |
| 25 | |
| 26 | void MsgBox::setCheckBox(QMessageBox* msg_box, const QString& text, bool* data) { |
| 27 | // Add "don't show this again checkbox. |
| 28 | auto* check_box = new QCheckBox(msg_box); |
| 29 | |
| 30 | check_box->setChecked(*data); |
| 31 | check_box->setText(text); |
| 32 | connect(check_box, &QCheckBox::toggled, [=](bool checked) { |
| 33 | *data = checked; |
| 34 | }); |
| 35 | msg_box->setCheckBox(check_box); |
| 36 | } |
| 37 | |
| 38 | QIcon MsgBox::iconForStatus(QMessageBox::Icon status) { |
| 39 | switch (status) { |
| 40 | case QMessageBox::Icon::Information: |
| 41 | return qApp->icons()->fromTheme(QSL("dialog-information")); |
| 42 | |
| 43 | case QMessageBox::Icon::Warning: |
| 44 | return qApp->icons()->fromTheme(QSL("dialog-warning")); |
| 45 | |
| 46 | case QMessageBox::Icon::Critical: |
| 47 | return qApp->icons()->fromTheme(QSL("dialog-error")); |
| 48 | |
| 49 | case QMessageBox::Icon::Question: |
| 50 | return qApp->icons()->fromTheme(QSL("dialog-question")); |
| 51 | |
| 52 | default: |
| 53 | return QIcon(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | QMessageBox::StandardButton MsgBox::show(QWidget* parent, |
| 58 | QMessageBox::Icon icon, |
| 59 | const QString& title, |
| 60 | const QString& text, |
| 61 | const QString& informative_text, |
| 62 | const QString& detailed_text, |
| 63 | QMessageBox::StandardButtons buttons, |
| 64 | QMessageBox::StandardButton default_button, |
| 65 | bool* dont_show_again, |
| 66 | const QString& functor_heading, |
| 67 | const std::function<void()>& functor) { |
| 68 | if (parent == nullptr) { |
| 69 | parent = qApp->mainFormWidget(); |
| 70 | } |
| 71 | |
| 72 | // Create and find needed components. |
| 73 | MsgBox msg_box(parent); |
| 74 | |
| 75 | // Initialize message box properties. |
| 76 | msg_box.setWindowTitle(title); |
| 77 | msg_box.setText(text); |
| 78 | msg_box.setInformativeText(informative_text); |
| 79 | msg_box.setDetailedText(detailed_text); |
| 80 | msg_box.setIcon(icon); |
| 81 | msg_box.setStandardButtons(buttons); |
| 82 | msg_box.setDefaultButton(default_button); |
| 83 | |
| 84 | if (dont_show_again != nullptr) { |
| 85 | MsgBox::setCheckBox(&msg_box, tr("Do not show this dialog again."), dont_show_again); |
| 86 | } |
| 87 | |
| 88 | if (functor) { |
| 89 | connect(msg_box.addButton(functor_heading, QMessageBox::ButtonRole::HelpRole), |
| 90 | &QPushButton::clicked, |
| 91 | &msg_box, |
| 92 | functor); |
| 93 | } |
| 94 | |
| 95 | // Display it. |
| 96 | if (msg_box.exec() == -1) { |
| 97 | return QMessageBox::StandardButton::Cancel; |
| 98 | } |
| 99 | else { |
| 100 | return msg_box.standardButton(msg_box.clickedButton()); |
| 101 | } |
| 102 | } |