Refactor config loading/saving for the runtime config dialog to avoid code duplication.

This commit is contained in:
Dave Page 2020-07-11 16:37:31 +05:30 committed by Akshay Joshi
parent 6855ac1d5e
commit 5ac656c638
4 changed files with 44 additions and 114 deletions

View File

@ -9,6 +9,8 @@
//
//////////////////////////////////////////////////////////////////////////
#include "pgAdmin4.h"
#include "ConfigWindow.h"
#include "ui_ConfigWindow.h"
@ -42,45 +44,15 @@ void ConfigWindow::on_chkFixedPort_stateChanged(int state)
ui->spinPortNumber->setEnabled(false);
}
QString ConfigWindow::getBrowserCommand()
void ConfigWindow::LoadSettings()
{
return ui->browserCommandLineEdit->text();
}
QSettings settings;
bool ConfigWindow::getFixedPort()
{
return ui->chkFixedPort->isChecked();
}
setWindowTitle(QString(tr("%1 Configuration")).arg(PGA_APP_NAME));
int ConfigWindow::getPortNumber()
{
return ui->spinPortNumber->value();
}
ui->browserCommandLineEdit->setText(settings.value("BrowserCommand").toString());
bool ConfigWindow::getOpenTabAtStartup()
{
return ui->chkOpenTabAtStartup->isChecked();
}
QString ConfigWindow::getPythonPath()
{
return ui->pythonPathLineEdit->text();
}
QString ConfigWindow::getApplicationPath()
{
return ui->applicationPathLineEdit->text();
}
void ConfigWindow::setBrowserCommand(QString command)
{
ui->browserCommandLineEdit->setText(command);
}
void ConfigWindow::setFixedPort(bool fixedPort)
{
if (fixedPort)
if(settings.value("FixedPort").toBool())
{
ui->chkFixedPort->setCheckState(Qt::Checked);
ui->spinPortNumber->setEnabled(true);
@ -90,16 +62,10 @@ void ConfigWindow::setFixedPort(bool fixedPort)
ui->chkFixedPort->setCheckState(Qt::Unchecked);
ui->spinPortNumber->setEnabled(false);
}
}
void ConfigWindow::setPortNumber(int port)
{
ui->spinPortNumber->setValue(port);
}
ui->spinPortNumber->setValue(settings.value("PortNumber").toInt());
void ConfigWindow::setOpenTabAtStartup(bool openTabAtStartup)
{
if (openTabAtStartup)
if (settings.value("OpenTabAtStartup", true).toBool())
{
ui->chkOpenTabAtStartup->setCheckState(Qt::Checked);
}
@ -107,15 +73,37 @@ void ConfigWindow::setOpenTabAtStartup(bool openTabAtStartup)
{
ui->chkOpenTabAtStartup->setCheckState(Qt::Unchecked);
}
ui->pythonPathLineEdit->setText(settings.value("PythonPath").toString());
ui->applicationPathLineEdit->setText(settings.value("ApplicationPath").toString());
}
void ConfigWindow::setPythonPath(QString path)
bool ConfigWindow::SaveSettings()
{
ui->pythonPathLineEdit->setText(path);
}
void ConfigWindow::setApplicationPath(QString path)
{
ui->applicationPathLineEdit->setText(path);
QSettings settings;
// Save the settings, and return true if a restart is required, otherwise false.
QString browsercommand = ui->browserCommandLineEdit->text();
bool fixedport = ui->chkFixedPort->isChecked();
int portnumber = ui->spinPortNumber->value();
bool opentabatstartup = ui->chkOpenTabAtStartup->isChecked();
QString pythonpath = ui->pythonPathLineEdit->text();
QString applicationpath = ui->applicationPathLineEdit->text();
bool needRestart = (settings.value("FixedPort").toBool() != fixedport ||
settings.value("PortNumber").toInt() != portnumber ||
settings.value("PythonPath").toString() != pythonpath ||
settings.value("ApplicationPath").toString() != applicationpath);
settings.setValue("BrowserCommand", browsercommand);
settings.setValue("FixedPort", fixedport);
settings.setValue("PortNumber", portnumber);
settings.setValue("OpenTabAtStartup", opentabatstartup);
settings.setValue("PythonPath", pythonpath);
settings.setValue("ApplicationPath", applicationpath);
settings.sync();
return needRestart;
}

View File

@ -25,19 +25,8 @@ class ConfigWindow : public QDialog
public:
explicit ConfigWindow(QWidget *parent = Q_NULLPTR);
QString getBrowserCommand();
bool getFixedPort();
int getPortNumber();
bool getOpenTabAtStartup();
QString getPythonPath();
QString getApplicationPath();
void setBrowserCommand(QString command);
void setFixedPort(bool fixedPort);
void setPortNumber(int port);
void setOpenTabAtStartup(bool openTabAtStartup);
void setPythonPath(QString path);
void setApplicationPath(QString path);
void LoadSettings();
bool SaveSettings();
private slots:
void on_buttonBox_accepted();

View File

@ -64,45 +64,19 @@ void MenuActions::onCopyUrl()
// Show the config dialogue
void MenuActions::onConfig()
{
QSettings settings;
bool ok;
ConfigWindow *dlg = new ConfigWindow();
dlg->setWindowTitle(QString(tr("%1 Configuration")).arg(PGA_APP_NAME));
dlg->setBrowserCommand(settings.value("BrowserCommand").toString());
dlg->setFixedPort(settings.value("FixedPort").toBool());
dlg->setPortNumber(settings.value("PortNumber").toInt());
dlg->setOpenTabAtStartup(settings.value("OpenTabAtStartup", true).toBool());
dlg->setPythonPath(settings.value("PythonPath").toString());
dlg->setApplicationPath(settings.value("ApplicationPath").toString());
dlg->LoadSettings();
dlg->setModal(true);
ok = dlg->exec();
QString browsercommand = dlg->getBrowserCommand();
bool fixedport = dlg->getFixedPort();
int portnumber = dlg->getPortNumber();
bool opentabatstartup = dlg->getOpenTabAtStartup();
QString pythonpath = dlg->getPythonPath();
QString applicationpath = dlg->getApplicationPath();
if (ok)
{
bool needRestart = (settings.value("FixedPort").toBool() != fixedport ||
settings.value("PortNumber").toInt() != portnumber ||
settings.value("PythonPath").toString() != pythonpath ||
settings.value("ApplicationPath").toString() != applicationpath);
settings.setValue("BrowserCommand", browsercommand);
settings.setValue("FixedPort", fixedport);
settings.setValue("PortNumber", portnumber);
settings.setValue("OpenTabAtStartup", opentabatstartup);
settings.setValue("PythonPath", pythonpath);
settings.setValue("ApplicationPath", applicationpath);
bool needRestart = dlg->SaveSettings();
if (needRestart && QMessageBox::Yes == QMessageBox::question(Q_NULLPTR, tr("Shut down server?"), QString(tr("The %1 server must be restarted for changes to take effect. Do you want to shut down the server now?")).arg(PGA_APP_NAME), QMessageBox::Yes | QMessageBox::No))
{
exit(0);
}
}
}

View File

@ -350,33 +350,12 @@ int main(int argc, char * argv[])
bool ok;
ConfigWindow *dlg = new ConfigWindow();
dlg->setWindowTitle(QWidget::tr("Configuration"));
dlg->setBrowserCommand(settings.value("BrowserCommand").toString());
dlg->setFixedPort(settings.value("FixedPort").toBool());
dlg->setPortNumber(settings.value("PortNumber").toInt());
dlg->setOpenTabAtStartup(settings.value("OpenTabAtStartup", true).toBool());
dlg->setPythonPath(settings.value("PythonPath").toString());
dlg->setApplicationPath(settings.value("ApplicationPath").toString());
dlg->LoadSettings();
dlg->setModal(true);
ok = dlg->exec();
QString browsercommand = dlg->getBrowserCommand();
bool fixedport = dlg->getFixedPort();
int portnumber = dlg->getPortNumber();
bool opentabatstartup = dlg->getOpenTabAtStartup();
QString pythonpath = dlg->getPythonPath();
QString applicationpath = dlg->getApplicationPath();
if (ok)
{
settings.setValue("BrowserCommand", browsercommand);
settings.setValue("FixedPort", fixedport);
settings.setValue("PortNumber", portnumber);
settings.setValue("OpenTabAtStartup", opentabatstartup);
settings.setValue("PythonPath", pythonpath);
settings.setValue("ApplicationPath", applicationpath);
settings.sync();
}
dlg->SaveSettings();
else
{
Logger::ReleaseLogger();