Skip to content

Commit d3fb466

Browse files
committed
Refactor
Add helper function to initialize shutdown message. Group similar functions together. Merge shutdown() function into its only caller. Add override keyword
1 parent 1099a50 commit d3fb466

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

src/gui/shutdownconfirm.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,10 @@ ShutdownConfirmDlg::ShutdownConfirmDlg(const ShutdownAction &action)
4949
{
5050
ui->setupUi(this);
5151

52+
initText();
5253
QIcon warningIcon(style()->standardIcon(QStyle::SP_MessageBoxWarning));
5354
ui->warningLabel->setPixmap(warningIcon.pixmap(32));
5455

55-
updateText();
56-
5756
if (m_action == ShutdownAction::None)
5857
ui->neverShowAgainCheckbox->setVisible(true);
5958
else
@@ -63,12 +62,13 @@ ShutdownConfirmDlg::ShutdownConfirmDlg(const ShutdownAction &action)
6362
QPushButton *cancelButton = ui->buttonBox->button(QDialogButtonBox::Cancel);
6463
cancelButton->setFocus();
6564
cancelButton->setDefault(true);
65+
6666
// Always on top
6767
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
68+
move(Utils::Misc::screenCenter(this));
69+
6870
m_timer.setInterval(1000); // 1sec
6971
connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateSeconds()));
70-
// Move to center
71-
move(Utils::Misc::screenCenter(this));
7272
}
7373

7474
ShutdownConfirmDlg::~ShutdownConfirmDlg()
@@ -85,8 +85,7 @@ void ShutdownConfirmDlg::showEvent(QShowEvent *event)
8585
bool ShutdownConfirmDlg::askForConfirmation(const ShutdownAction &action)
8686
{
8787
ShutdownConfirmDlg dlg(action);
88-
dlg.exec();
89-
return dlg.shutdown();
88+
return (dlg.exec() == QDialog::Accepted);
9089
}
9190

9291
void ShutdownConfirmDlg::updateSeconds()
@@ -105,43 +104,43 @@ void ShutdownConfirmDlg::accept()
105104
QDialog::accept();
106105
}
107106

108-
bool ShutdownConfirmDlg::shutdown() const
107+
void ShutdownConfirmDlg::initText()
109108
{
110-
return (result() == QDialog::Accepted);
111-
}
112-
113-
void ShutdownConfirmDlg::updateText()
114-
{
115-
QString text;
116109
QPushButton *okButton = ui->buttonBox->button(QDialogButtonBox::Ok);
117110

118111
switch (m_action) {
119112
case ShutdownAction::None:
120-
text = tr("qBittorrent will now exit.");
113+
m_msg = tr("qBittorrent will now exit.");
121114

122115
okButton->setText(tr("E&xit Now"));
123116
setWindowTitle(tr("Exit confirmation"));
124117
break;
125118
case ShutdownAction::Shutdown:
126-
text = tr("The computer is going to shutdown.");
119+
m_msg = tr("The computer is going to shutdown.");
127120

128121
okButton->setText(tr("&Shutdown Now"));
129122
setWindowTitle(tr("Shutdown confirmation"));
130123
break;
131124
case ShutdownAction::Suspend:
132-
text = tr("The computer is going to enter suspend mode.");
125+
m_msg = tr("The computer is going to enter suspend mode.");
133126

134127
okButton->setText(tr("&Suspend Now"));
135128
setWindowTitle(tr("Suspend confirmation"));
136129
break;
137130
case ShutdownAction::Hibernate:
138-
text = tr("The computer is going to enter hibernation mode.");
131+
m_msg = tr("The computer is going to enter hibernation mode.");
139132

140133
okButton->setText(tr("&Hibernate Now"));
141134
setWindowTitle(tr("Hibernate confirmation"));
142135
break;
143136
}
144137

145-
text += "\n" + tr("You can cancel the action within %1 seconds.").arg(QString::number(m_timeout)) + "\n";
146-
ui->shutdownText->setText(text);
138+
m_msg += "\n";
139+
updateText();
140+
}
141+
142+
void ShutdownConfirmDlg::updateText()
143+
{
144+
QString t = tr("You can cancel the action within %1 seconds.").arg(QString::number(m_timeout)) + "\n";
145+
ui->shutdownText->setText(m_msg + t);
147146
}

src/gui/shutdownconfirm.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,27 @@ class ShutdownConfirmDlg: public QDialog
4747
public:
4848
ShutdownConfirmDlg(const ShutdownAction &action);
4949
~ShutdownConfirmDlg();
50-
bool shutdown() const;
5150

5251
static bool askForConfirmation(const ShutdownAction &action);
5352

5453
protected:
55-
void showEvent(QShowEvent *event);
54+
void showEvent(QShowEvent *event) override;
5655

5756
private slots:
5857
void updateSeconds();
5958
void accept() override;
6059

6160
private:
6261
// Methods
62+
void initText();
6363
void updateText();
6464

6565
// Vars
6666
Ui::confirmShutdownDlg *ui;
6767
QTimer m_timer;
6868
int m_timeout;
6969
ShutdownAction m_action;
70+
QString m_msg;
7071
};
7172

7273
#endif // SHUTDOWNCONFIRM_H

0 commit comments

Comments
 (0)