From b3812989817a3db9ea22eb6edcffa7a786c1f2a1 Mon Sep 17 00:00:00 2001 From: Antony Woods Date: Wed, 27 Aug 2014 21:25:11 +0100 Subject: [PATCH] Added 'auto connect' checkbox to connect dialog. When the main window becomes active for the first time and auto connect is set to true, it will call connectToServer at that point. --- cockatrice/src/dlg_connect.cpp | 40 +++++++++++++++++++++++++++++++- cockatrice/src/dlg_connect.h | 4 +++- cockatrice/src/settingscache.cpp | 8 +++++++ cockatrice/src/settingscache.h | 3 +++ cockatrice/src/window_main.cpp | 13 ++++++++++- cockatrice/src/window_main.h | 1 + 6 files changed, 66 insertions(+), 3 deletions(-) diff --git a/cockatrice/src/dlg_connect.cpp b/cockatrice/src/dlg_connect.cpp index d1955d14..f13b2f8f 100644 --- a/cockatrice/src/dlg_connect.cpp +++ b/cockatrice/src/dlg_connect.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "dlg_connect.h" DlgConnect::DlgConnect(QWidget *parent) @@ -32,6 +34,20 @@ DlgConnect::DlgConnect(QWidget *parent) savePasswordCheckBox = new QCheckBox(tr("&Save password")); savePasswordCheckBox->setChecked(settings.value("save_password", 1).toInt()); + //autoConnectCheckBox = new QCheckBox(tr("A&uto connect at start")); + autoConnectCheckBox = new QCheckBox("A&uto connect at start"); // TODO needs tr() + if(savePasswordCheckBox->isChecked()) + { + autoConnectCheckBox->setChecked(settings.value("auto_connect", 0).toInt()); + autoConnectCheckBox->setEnabled(true); + } else { + settings.setValue("auto_connect", 0); + autoConnectCheckBox->setChecked(0); + autoConnectCheckBox->setEnabled(false); + } + + connect(savePasswordCheckBox, SIGNAL(stateChanged(int)), this, SLOT(passwordSaved(int))); + QGridLayout *grid = new QGridLayout; grid->addWidget(hostLabel, 0, 0); grid->addWidget(hostEdit, 0, 1); @@ -42,10 +58,11 @@ DlgConnect::DlgConnect(QWidget *parent) grid->addWidget(passwordLabel, 3, 0); grid->addWidget(passwordEdit, 3, 1); grid->addWidget(savePasswordCheckBox, 4, 0, 1, 2); + grid->addWidget(autoConnectCheckBox, 5, 0, 1, 2); QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(buttonBox, SIGNAL(accepted()), this, SLOT(actOk())); - connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); + connect(buttonBox, SIGNAL(rejected()), this, SLOT(actCancel())); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addLayout(grid); @@ -57,6 +74,17 @@ DlgConnect::DlgConnect(QWidget *parent) setMinimumWidth(300); } +void DlgConnect::passwordSaved(int state) +{ + if(!savePasswordCheckBox->isChecked()) + { + autoConnectCheckBox->setChecked(0); + autoConnectCheckBox->setEnabled(false); + } else { + autoConnectCheckBox->setEnabled(true); + } +} + void DlgConnect::actOk() { QSettings settings; @@ -66,7 +94,17 @@ void DlgConnect::actOk() settings.setValue("playername", playernameEdit->text()); settings.setValue("password", savePasswordCheckBox->isChecked() ? passwordEdit->text() : QString()); settings.setValue("save_password", savePasswordCheckBox->isChecked() ? 1 : 0); + settings.setValue("auto_connect", autoConnectCheckBox->isChecked() ? 1 : 0); settings.endGroup(); accept(); } + +void DlgConnect::actCancel() +{ + QSettings settings; + settings.beginGroup("server"); + settings.setValue("auto_connect", autoConnectCheckBox->isChecked() ? 1 : 0); + settings.endGroup(); + reject(); +} diff --git a/cockatrice/src/dlg_connect.h b/cockatrice/src/dlg_connect.h index 87fe4ed9..6f135aa0 100644 --- a/cockatrice/src/dlg_connect.h +++ b/cockatrice/src/dlg_connect.h @@ -18,10 +18,12 @@ public: QString getPassword() const { return passwordEdit->text(); } private slots: void actOk(); + void actCancel(); + void passwordSaved(int state); private: QLabel *hostLabel, *portLabel, *playernameLabel, *passwordLabel; QLineEdit *hostEdit, *portEdit, *playernameEdit, *passwordEdit; - QCheckBox *savePasswordCheckBox; + QCheckBox *savePasswordCheckBox, *autoConnectCheckBox; }; #endif diff --git a/cockatrice/src/settingscache.cpp b/cockatrice/src/settingscache.cpp index 5f504aa3..92c06041 100644 --- a/cockatrice/src/settingscache.cpp +++ b/cockatrice/src/settingscache.cpp @@ -48,6 +48,8 @@ SettingsCache::SettingsCache() priceTagSource = settings->value("deckeditor/pricetagsource", 0).toInt(); ignoreUnregisteredUsers = settings->value("chat/ignore_unregistered", false).toBool(); + + attemptAutoConnect = settings->value("server/auto_connect", 0).toInt() == 0 ? false : true; } void SettingsCache::setLang(const QString &_lang) @@ -267,6 +269,12 @@ void SettingsCache::setMainWindowGeometry(const QByteArray &_mainWindowGeometry) settings->setValue("interface/main_window_geometry", mainWindowGeometry); } +void SettingsCache::setAutoConnect(const bool &_autoConnect) +{ + attemptAutoConnect = _autoConnect; + settings->value("server/auto_connect", attemptAutoConnect ? 1 : 0); +} + void SettingsCache::copyPath(const QString &src, const QString &dst) { // test source && return if inexistent diff --git a/cockatrice/src/settingscache.h b/cockatrice/src/settingscache.h index 3b4d612d..b222a58b 100644 --- a/cockatrice/src/settingscache.h +++ b/cockatrice/src/settingscache.h @@ -57,6 +57,7 @@ private: bool ignoreUnregisteredUsers; QString picUrl; QString picUrlHq; + bool attemptAutoConnect; public: SettingsCache(); const QByteArray &getMainWindowGeometry() const { return mainWindowGeometry; } @@ -93,6 +94,7 @@ public: QString getPicUrl() const { return picUrl; } QString getPicUrlHq() const { return picUrlHq; } void copyPath(const QString &src, const QString &dst); + bool getAutoConnect() const { return attemptAutoConnect; } public slots: void setMainWindowGeometry(const QByteArray &_mainWindowGeometry); void setLang(const QString &_lang); @@ -127,6 +129,7 @@ public slots: void setIgnoreUnregisteredUsers(bool _ignoreUnregisteredUsers); void setPicUrl(const QString &_picUrl); void setPicUrlHq(const QString &_picUrlHq); + void setAutoConnect(const bool &_autoConnect); }; extern SettingsCache *settingsCache; diff --git a/cockatrice/src/window_main.cpp b/cockatrice/src/window_main.cpp index 43b0624e..51a09418 100644 --- a/cockatrice/src/window_main.cpp +++ b/cockatrice/src/window_main.cpp @@ -359,7 +359,7 @@ void MainWindow::createMenus() } MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent), localServer(0) + : QMainWindow(parent), localServer(0), bHasActivated(false) { QPixmapCache::setCacheLimit(200000); @@ -417,5 +417,16 @@ void MainWindow::changeEvent(QEvent *event) { if (event->type() == QEvent::LanguageChange) retranslateUi(); + else if(event->type() == QEvent::ActivationChange) { + if(isActiveWindow() && !bHasActivated){ + bHasActivated = true; + if(settingsCache->getAutoConnect()) { + qDebug() << "Attempting auto-connect..."; + DlgConnect dlg(this); + client->connectToServer(dlg.getHost(), dlg.getPort(), dlg.getPlayerName(), dlg.getPassword()); + } + } + } + QMainWindow::changeEvent(event); } diff --git a/cockatrice/src/window_main.h b/cockatrice/src/window_main.h index 07ad1d71..e804bbe6 100644 --- a/cockatrice/src/window_main.h +++ b/cockatrice/src/window_main.h @@ -71,6 +71,7 @@ private: QThread *clientThread; LocalServer *localServer; + bool bHasActivated; public: MainWindow(QWidget *parent = 0); ~MainWindow();