1- #include < QCloseEvent>
2- #include < QKeyEvent>
1+ #include < QRadioButton>
32#include " ShieldSystemDialog.h"
43#include " ui/util/SignalBlockers.h"
54#include " ui_ShieldSystemDialog.h"
6- #include " mission/util.h"
75
86namespace fso ::fred::dialogs {
97
8+ namespace {
9+
10+ QString statusLabelText (GlobalShieldStatus status)
11+ {
12+ switch (status) {
13+ case GlobalShieldStatus::HasShields:
14+ return QObject::tr (" Currently: Has shields" );
15+ case GlobalShieldStatus::NoShields:
16+ return QObject::tr (" Currently: No shields" );
17+ case GlobalShieldStatus::MixedShields:
18+ default :
19+ return QObject::tr (" Currently: Mixed" );
20+ }
21+ }
22+
23+ // Clear the selection in a Qt exclusive button group. Without toggling
24+ // autoExclusive off, neither radio in an exclusive pair can be unchecked
25+ // programmatically.
26+ void clearRadioPair (QRadioButton* a, QRadioButton* b)
27+ {
28+ a->setAutoExclusive (false );
29+ b->setAutoExclusive (false );
30+ a->setChecked (false );
31+ b->setChecked (false );
32+ a->setAutoExclusive (true );
33+ b->setAutoExclusive (true );
34+ }
35+
36+ } // namespace
37+
1038ShieldSystemDialog::ShieldSystemDialog (FredView* parent, EditorViewport* viewport) :
1139 QDialog (parent),
1240 _viewport (viewport),
@@ -23,32 +51,6 @@ ShieldSystemDialog::ShieldSystemDialog(FredView* parent, EditorViewport* viewpor
2351
2452ShieldSystemDialog::~ShieldSystemDialog () = default ;
2553
26- void ShieldSystemDialog::accept ()
27- {
28- // If apply() returns true, close the dialog
29- if (_model->apply ()) {
30- QDialog::accept ();
31- }
32- // else: validation failed, don't close
33- }
34-
35- void ShieldSystemDialog::reject ()
36- {
37- // Asks the user if they want to save changes, if any
38- // If they do, it runs _model->apply() and returns the success value
39- // If they don't, it runs _model->reject() and returns true
40- if (rejectOrCloseHandler (this , _model.get (), _viewport)) {
41- QDialog::reject (); // actually close
42- }
43- // else: do nothing, don't close
44- }
45-
46- void ShieldSystemDialog::closeEvent (QCloseEvent* e)
47- {
48- reject ();
49- e->ignore (); // Don't let the base class close the window
50- }
51-
5254void ShieldSystemDialog::initializeUi () {
5355 util::SignalBlockers blockers (this );
5456
@@ -73,58 +75,74 @@ void ShieldSystemDialog::updateUi() {
7375 util::SignalBlockers blockers (this );
7476
7577 auto typeShieldSys = _model->getCurrentTypeShieldSys ();
76- ui->typeHasShieldRadio ->setChecked (typeShieldSys == GlobalShieldStatus::HasShields);
77- ui->typeNoShieldRadio ->setChecked (typeShieldSys == GlobalShieldStatus::NoShields);
78+ ui->typeCurrentStatusLabel ->setText (statusLabelText (typeShieldSys));
79+ if (typeShieldSys == GlobalShieldStatus::MixedShields) {
80+ clearRadioPair (ui->typeHasShieldRadio , ui->typeNoShieldRadio );
81+ ui->applyTypeButton ->setEnabled (false );
82+ } else {
83+ ui->typeHasShieldRadio ->setChecked (typeShieldSys == GlobalShieldStatus::HasShields);
84+ ui->typeNoShieldRadio ->setChecked (typeShieldSys == GlobalShieldStatus::NoShields);
85+ ui->applyTypeButton ->setEnabled (true );
86+ }
7887
7988 auto teamShieldSys = _model->getCurrentTeamShieldSys ();
80- ui->teamHasShieldRadio ->setChecked (teamShieldSys == GlobalShieldStatus::HasShields);
81- ui->teamNoShieldRadio ->setChecked (teamShieldSys == GlobalShieldStatus::NoShields);
82- }
83-
84- void ShieldSystemDialog::on_okAndCancelButtons_accepted () {
85- accept ();
86- }
87-
88- void ShieldSystemDialog::on_okAndCancelButtons_rejected () {
89- reject ();
89+ ui->teamCurrentStatusLabel ->setText (statusLabelText (teamShieldSys));
90+ if (teamShieldSys == GlobalShieldStatus::MixedShields) {
91+ clearRadioPair (ui->teamHasShieldRadio , ui->teamNoShieldRadio );
92+ ui->applyTeamButton ->setEnabled (false );
93+ } else {
94+ ui->teamHasShieldRadio ->setChecked (teamShieldSys == GlobalShieldStatus::HasShields);
95+ ui->teamNoShieldRadio ->setChecked (teamShieldSys == GlobalShieldStatus::NoShields);
96+ ui->applyTeamButton ->setEnabled (true );
97+ }
9098}
9199
92100void ShieldSystemDialog::on_shipTypeCombo_currentIndexChanged (int index) {
93101 if (index >= 0 ) {
94- _model->setCurrentTeam (index);
102+ _model->setCurrentShipType (index);
95103 }
96104 updateUi ();
97105}
98106
99107void ShieldSystemDialog::on_shipTeamCombo_currentIndexChanged (int index) {
100108 if (index >= 0 ) {
101- _model->setCurrentShipType (index);
109+ _model->setCurrentTeam (index);
102110 }
103111 updateUi ();
104112}
105113
106114void ShieldSystemDialog::on_typeHasShieldRadio_toggled (bool checked) {
107115 if (checked) {
108- _model-> setCurrentTypeShieldSys (checked );
116+ ui-> applyTypeButton -> setEnabled ( true );
109117 }
110118}
111119
112120void ShieldSystemDialog::on_typeNoShieldRadio_toggled (bool checked) {
113121 if (checked) {
114- _model-> setCurrentTypeShieldSys (!checked );
122+ ui-> applyTypeButton -> setEnabled ( true );
115123 }
116124}
117125
118126void ShieldSystemDialog::on_teamHasShieldRadio_toggled (bool checked) {
119127 if (checked) {
120- _model-> setCurrentTeamShieldSys (checked );
128+ ui-> applyTeamButton -> setEnabled ( true );
121129 }
122130}
123131
124132void ShieldSystemDialog::on_teamNoShieldRadio_toggled (bool checked) {
125133 if (checked) {
126- _model-> setCurrentTeamShieldSys (!checked );
134+ ui-> applyTeamButton -> setEnabled ( true );
127135 }
128136}
129137
138+ void ShieldSystemDialog::on_applyTypeButton_clicked () {
139+ _model->applyType (ui->typeHasShieldRadio ->isChecked ());
140+ updateUi ();
141+ }
142+
143+ void ShieldSystemDialog::on_applyTeamButton_clicked () {
144+ _model->applyTeam (ui->teamHasShieldRadio ->isChecked ());
145+ updateUi ();
146+ }
147+
130148} // namespace fso::fred::dialogs
0 commit comments