BibleTime
messagedialog.cpp
Go to the documentation of this file.
1/*********
2*
3* In the name of the Father, and of the Son, and of the Holy Spirit.
4*
5* This file is part of BibleTime's source code, https://bibletime.info/
6*
7* Copyright 1999-2025 by the BibleTime developers.
8* The BibleTime source code is licensed under the GNU General Public License
9* version 2.0.
10*
11**********/
12
13#include "messagedialog.h"
14
15#include <QAbstractButton>
16#include <QAction>
17#include <QDialogButtonBox>
18#include <QMessageBox>
19#include <QObject>
20#include <QPushButton>
21#include <Qt>
22#include "../util/btassert.h"
23
24
25namespace message {
26
27namespace {
28
29void replaceText(QDialogButtonBox *box, QDialogButtonBox::StandardButton flag,
30 const QString &text) {
31 QPushButton *button(box->button(flag));
32 if (button != nullptr) {
33 button->setText(text);
34 }
35}
36
37QMessageBox::StandardButton bt_messageBox(QMessageBox::Icon icon,
38 QWidget * parent,
39 const QString &title,
40 const QString &text,
41 QMessageBox::StandardButtons buttons,
42 QMessageBox::StandardButton defaultButton)
43{
44 QMessageBox messageBox(icon, title, text, QMessageBox::Ok, parent);
45 messageBox.setTextFormat(Qt::RichText);
46 //We need the button box to translate the strings (the idea of this whole function)
47 QDialogButtonBox* box = dynamic_cast<QDialogButtonBox*>(messageBox.button(QMessageBox::Ok)->parent());
48 BT_ASSERT(box);
49 messageBox.setStandardButtons(buttons);
50 messageBox.setDefaultButton(defaultButton);
52 return static_cast<QMessageBox::StandardButton>(messageBox.exec());
53}
54
55} // anonymous namespace
56
57void setQActionCheckedNoTrigger(QAction * const action, const bool checked) {
58 BT_ASSERT(action);
59 const bool signalsWereBlocked = action->blockSignals(true);
60 action->setChecked(checked);
61 action->blockSignals(signalsWereBlocked);
62}
63
64void prepareDialogBox(QDialogButtonBox *box) {
65 replaceText(box, QDialogButtonBox::Ok , QPushButton::tr("OK" , "Dialog Button"));
66 replaceText(box, QDialogButtonBox::Open , QPushButton::tr("Open" , "Dialog Button"));
67 replaceText(box, QDialogButtonBox::Save , QPushButton::tr("Save" , "Dialog Button"));
68 replaceText(box, QDialogButtonBox::Cancel , QPushButton::tr("Cancel" , "Dialog Button"));
69 replaceText(box, QDialogButtonBox::Close , QPushButton::tr("Close" , "Dialog Button"));
70 replaceText(box, QDialogButtonBox::Discard , QPushButton::tr("Discard" , "Dialog Button"));
71 replaceText(box, QDialogButtonBox::Apply , QPushButton::tr("Apply" , "Dialog Button"));
72 replaceText(box, QDialogButtonBox::Reset , QPushButton::tr("Reset" , "Dialog Button"));
73 replaceText(box, QDialogButtonBox::RestoreDefaults, QPushButton::tr("Restore defaults", "Dialog Button"));
74 replaceText(box, QDialogButtonBox::Help , QPushButton::tr("Help" , "Dialog Button"));
75 replaceText(box, QDialogButtonBox::SaveAll , QPushButton::tr("Save All" , "Dialog Button"));
76 replaceText(box, QDialogButtonBox::Yes , QPushButton::tr("Yes" , "Dialog Button"));
77 replaceText(box, QDialogButtonBox::YesToAll, QPushButton::tr("Yes to all", "Dialog Button"));
78 replaceText(box, QDialogButtonBox::No , QPushButton::tr("No" , "Dialog Button"));
79 replaceText(box, QDialogButtonBox::NoToAll , QPushButton::tr("No to all" , "Dialog Button"));
80}
81
82QMessageBox::StandardButton showWarning(QWidget * parent, const QString & title, const QString & text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) {
83 return bt_messageBox(QMessageBox::Warning, parent, title, text, buttons, defaultButton);
84}
85
86QMessageBox::StandardButton showInformation(QWidget * parent, const QString & title, const QString & text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) {
87 return bt_messageBox(QMessageBox::Information, parent, title, text, buttons, defaultButton);
88}
89
90QMessageBox::StandardButton showCritical(QWidget * parent, const QString & title, const QString & text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) {
91 return bt_messageBox(QMessageBox::Critical, parent, title, text, buttons, defaultButton);
92}
93
94QMessageBox::StandardButton showQuestion(QWidget * parent, const QString & title, const QString & text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) {
95 return bt_messageBox(QMessageBox::Question, parent, title, text, buttons, defaultButton);
96}
97
98} // namespace message
#define BT_ASSERT(...)
Definition btassert.h:17
QMessageBox::StandardButton bt_messageBox(QMessageBox::Icon icon, QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
void replaceText(QDialogButtonBox *box, QDialogButtonBox::StandardButton flag, const QString &text)
void prepareDialogBox(QDialogButtonBox *box)
void setQActionCheckedNoTrigger(QAction *const action, const bool checked)
QMessageBox::StandardButton showQuestion(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
QMessageBox::StandardButton showInformation(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
QMessageBox::StandardButton showCritical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
QMessageBox::StandardButton showWarning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)