2121#include " ui_WarningDialog.h"
2222
2323#include " Application.h"
24- #include " Styles.h"
2524
25+ #include < QPushButton>
2626#include < QStyle>
2727
2828WarningDialog::WarningDialog (const QString &text, const QString &details, QWidget *parent)
@@ -36,16 +36,15 @@ WarningDialog::WarningDialog(const QString &text, const QString &details, QWidge
3636 setParent (parent, Qt::Sheet);
3737#endif
3838
39- connect ( ui->cancel , &QPushButton::clicked, this , &WarningDialog::reject );
40- connect ( this , &WarningDialog::finished, this , &WarningDialog::close );
41-
42- ui->cancel ->setFont (Styles::font (Styles::Condensed, 14 ));
43- ui->text ->setFont (Styles::font (Styles::Regular, 14 ));
39+ ui->buttonBox ->layout ()->setSpacing (40 );
4440 ui->text ->setText (text);
45- ui->details ->setFont (ui->text ->font ());
4641 ui->details ->setText (details);
4742 ui->details ->setHidden (details.isEmpty ());
4843 ui->showDetails ->setHidden (details.isEmpty ());
44+ cancel = ui->buttonBox ->button (QDialogButtonBox::Close);
45+ cancel->setCursor (Qt::PointingHandCursor);
46+ connect (cancel, &QPushButton::clicked, this , &QDialog::reject);
47+ resetCancelStyle (true );
4948
5049 if (!details.isEmpty ())
5150 {
@@ -72,63 +71,86 @@ void WarningDialog::addButton(ButtonText label, int ret, bool red)
7271
7372void WarningDialog::addButton (const QString& label, int ret, bool red)
7473{
75- auto *button = new QPushButton (label, this );
76- button->setAccessibleName (label.toLower ());
77- button->setCursor (ui->cancel ->cursor ());
78- button->setFont (ui->cancel ->font ());
79- button->setMinimumSize (
80- std::max<int >(ui->cancel ->minimumWidth (), ui->cancel ->fontMetrics ().boundingRect (label).width () + 16 ),
81- ui->cancel ->minimumHeight ());
82-
83- if (red) {
74+ if (ui->buttonBox ->buttons ().size () > 3 )
75+ ui->buttonBox ->layout ()->setSpacing (5 );
76+
77+ QDialogButtonBox::ButtonRole role = red ? QDialogButtonBox::RejectRole : [ret] {
78+ switch (ret) {
79+ case QDialogButtonBox::Ok:
80+ case QDialogButtonBox::Save:
81+ case QDialogButtonBox::Open:
82+ case QDialogButtonBox::SaveAll:
83+ case QDialogButtonBox::Retry:
84+ case QDialogButtonBox::Ignore:
85+ return QDialogButtonBox::AcceptRole;
86+ case QDialogButtonBox::Cancel:
87+ case QDialogButtonBox::Close:
88+ case QDialogButtonBox::Abort:
89+ return QDialogButtonBox::RejectRole;
90+ case QDialogButtonBox::Discard:
91+ return QDialogButtonBox::DestructiveRole;
92+ case QDialogButtonBox::Help:
93+ return QDialogButtonBox::HelpRole;
94+ case QDialogButtonBox::Apply:
95+ return QDialogButtonBox::ApplyRole;
96+ case QDialogButtonBox::Yes:
97+ case QDialogButtonBox::YesToAll:
98+ return QDialogButtonBox::YesRole;
99+ case QDialogButtonBox::No:
100+ case QDialogButtonBox::NoToAll:
101+ return QDialogButtonBox::NoRole;
102+ case QDialogButtonBox::RestoreDefaults:
103+ case QDialogButtonBox::Reset:
104+ return QDialogButtonBox::ResetRole;
105+ default :
106+ return QDialogButtonBox::ActionRole;
107+ }
108+ }();
109+
110+ QPushButton *button = ui->buttonBox ->addButton (label, role);
111+ button->setCursor (Qt::PointingHandCursor);
112+ switch (role) {
113+ case QDialogButtonBox::NoRole:
114+ case QDialogButtonBox::RejectRole:
115+ case QDialogButtonBox::ResetRole:
116+ case QDialogButtonBox::DestructiveRole:
117+ style ()->unpolish (button);
84118 button->setProperty (" warning" , true );
85- #ifdef Q_OS_WIN // For Windows this button should be on the left side of the dialog window
86- setLayoutDirection (Qt::RightToLeft);
87- #endif
88- } else {
89- #ifdef Q_OS_UNIX // For macOS, Linux and BSD all positive buttons should be on the right side of the dialog window
90- setLayoutDirection (Qt::RightToLeft);
91- #endif
119+ style ()->polish (button);
120+ break ;
121+ default :
122+ break ;
92123 }
93-
94124 connect (button, &QPushButton::clicked, this , [this , ret] { done (ret); });
95- ui->buttonBarLayout ->insertWidget (ui->buttonBarLayout ->findChildren <QPushButton>().size () + 1 , button);
96125}
97126
98127QString WarningDialog::buttonLabel (ButtonText label)
99128{
100129 switch (label) {
101- case NO: return tr (" NO " );
130+ case NO: return tr (" No " );
102131 case OK: return QStringLiteral (" OK" );
103- case Cancel: return tr (" CANCEL" );
104- case YES: return tr (" YES" );
132+ case Cancel: return tr (" Cancel" );
133+ case YES: return tr (" Yes" );
134+ case Remove: return tr (" Remove" );
105135 default : return {};
106136 }
107137}
108138
109- void WarningDialog::resetCancelStyle ()
110- {
111- style ()->unpolish (ui->cancel );
112- ui->cancel ->setProperty (" warning" , false );
113- style ()->polish (ui->cancel );
114- }
115-
116- void WarningDialog::setButtonSize (int width, int margin)
139+ void WarningDialog::resetCancelStyle (bool warning)
117140{
118- ui-> buttonBarLayout -> setSpacing (margin );
119- ui-> cancel ->setMinimumSize (width, ui-> cancel -> minimumHeight () );
120- ui-> cancel -> setMaximumSize (width, ui-> cancel -> minimumHeight () );
141+ style ()-> unpolish (cancel );
142+ cancel->setProperty ( " warning " , warning );
143+ style ()-> polish (cancel );
121144}
122145
123146void WarningDialog::setCancelText (ButtonText label)
124147{
125148 setCancelText (buttonLabel (label));
126149}
127150
128- void WarningDialog::setCancelText (const QString& label)
151+ void WarningDialog::setCancelText (const QString & label)
129152{
130- ui->cancel ->setText (label);
131- ui->cancel ->setAccessibleName (label.toLower ());
153+ cancel->setText (label);
132154}
133155
134156WarningDialog* WarningDialog::show (const QString &text, const QString &details)
0 commit comments