Reformat HelpMessageSender

remotes/origin/HEAD
markuspg 8 years ago
parent f423daf50c
commit bc345ff4d3

@ -2,7 +2,9 @@
#include <iostream> #include <iostream>
lcHelpMessageWindow::lcHelpMessageWindow( const QString &argServerIP, const unsigned short int &argServerPort, QWidget *argParent ) : lcHelpMessageWindow::lcHelpMessageWindow(const QString &argServerIP,
const unsigned short int &argServerPort,
QWidget *argParent) :
QMainWindow{argParent}, QMainWindow{argParent},
helpMessageSocket {new QTcpSocket{this}}, helpMessageSocket {new QTcpSocket{this}},
serverPort{argServerPort}, serverPort{argServerPort},
@ -10,8 +12,10 @@ lcHelpMessageWindow::lcHelpMessageWindow( const QString &argServerIP, const unsi
ui{new Ui::HelpMessageWindow} ui{new Ui::HelpMessageWindow}
{ {
ui->setupUi(this); ui->setupUi(this);
connect( ui->PBAskForHelp, &QPushButton::clicked, this, &lcHelpMessageWindow::RequestHelp ); connect(ui->PBAskForHelp, &QPushButton::clicked,
connect( helpMessageSocket, &QTcpSocket::readyRead, this, &lcHelpMessageWindow::ReadHelpReply ); this, &lcHelpMessageWindow::RequestHelp);
connect(helpMessageSocket, &QTcpSocket::readyRead,
this, &lcHelpMessageWindow::ReadHelpReply);
connect(helpMessageSocket, SIGNAL(error(QAbstractSocket::SocketError)), connect(helpMessageSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(DisplayError(QAbstractSocket::SocketError))); this, SLOT(DisplayError(QAbstractSocket::SocketError)));
@ -20,17 +24,20 @@ lcHelpMessageWindow::lcHelpMessageWindow( const QString &argServerIP, const unsi
// Get saved network configuration // Get saved network configuration
QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}}; QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}};
settings.beginGroup(QLatin1String{"QtNetwork"}); settings.beginGroup(QLatin1String{"QtNetwork"});
const QString id = settings.value( QLatin1String{ "DefaultNetworkConfiguration" } ).toString(); const QString id
= settings.value(QLatin1String{"DefaultNetworkConfiguration"}).toString();
settings.endGroup(); settings.endGroup();
// If the saved network configuration is not currently discovered use the system default // If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier( id ); QNetworkConfiguration config = manager.configurationFromIdentifier( id );
if ( ( config.state() & QNetworkConfiguration::Discovered ) != QNetworkConfiguration::Discovered ) { if ((config.state() & QNetworkConfiguration::Discovered)
!= QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration(); config = manager.defaultConfiguration();
} }
networkSession = new QNetworkSession{config, this}; networkSession = new QNetworkSession{config, this};
connect( networkSession, &QNetworkSession::opened, this, &lcHelpMessageWindow::OpenedSession ); connect(networkSession, &QNetworkSession::opened,
this, &lcHelpMessageWindow::OpenedSession);
networkSession->open(); networkSession->open();
} }
@ -46,10 +53,12 @@ void lcHelpMessageWindow::DisplayError( QAbstractSocket::SocketError socketError
case QAbstractSocket::RemoteHostClosedError: case QAbstractSocket::RemoteHostClosedError:
return; return;
case QAbstractSocket::HostNotFoundError: case QAbstractSocket::HostNotFoundError:
errorMessage = tr( "An error occurred: The server could not be found for error reporting:\n" ); errorMessage = tr("An error occurred: The server could not be found for"
" error reporting:\n");
break; break;
case QAbstractSocket::ConnectionRefusedError: case QAbstractSocket::ConnectionRefusedError:
errorMessage = tr( "An error occurred: The connection was refused by the laboratory server:\n" ); errorMessage = tr("An error occurred: The connection was refused by the"
" laboratory server:\n");
break; break;
default: default:
errorMessage = tr("The following error occured:\n"); errorMessage = tr("The following error occured:\n");
@ -63,10 +72,12 @@ void lcHelpMessageWindow::OpenedSession() {
// Save the used configuration // Save the used configuration
QNetworkConfiguration config = networkSession->configuration(); QNetworkConfiguration config = networkSession->configuration();
QString id; QString id;
if ( config.type() == QNetworkConfiguration::UserChoice ) if (config.type() == QNetworkConfiguration::UserChoice) {
id = networkSession->sessionProperty( QLatin1String{ "UserChoiceConfiguration" } ).toString(); id = networkSession->sessionProperty(
else QLatin1String{"UserChoiceConfiguration"}).toString();
} else {
id = config.identifier(); id = config.identifier();
}
QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}}; QSettings settings{QSettings::UserScope, QLatin1String{"QtProject"}};
settings.beginGroup(QLatin1String{"QtNetwork"}); settings.beginGroup(QLatin1String{"QtNetwork"});
@ -79,8 +90,9 @@ void lcHelpMessageWindow::ReadHelpReply() {
in.setVersion(QDataStream::Qt_5_2); in.setVersion(QDataStream::Qt_5_2);
if (blockSize == 0) { if (blockSize == 0) {
if ( helpMessageSocket->bytesAvailable() < ( int )sizeof( quint16 ) ) if (helpMessageSocket->bytesAvailable() < (int)sizeof(quint16)) {
return; return;
}
in >> blockSize; in >> blockSize;
} }
@ -93,10 +105,14 @@ void lcHelpMessageWindow::ReadHelpReply() {
in >> serverAnswer; in >> serverAnswer;
if (serverAnswer == "Help demand retrieved.") { if (serverAnswer == "Help demand retrieved.") {
ui->LSendingSuccess->setText( tr( "Help message successfully sent.\nPlease wait for the experimenter to show up at your booth." ) ); ui->LSendingSuccess->setText(tr("Help message successfully"
" sent.\nPlease wait for the"
" experimenter to show up at your booth."));
ui->PBAskForHelp->setEnabled(false); ui->PBAskForHelp->setEnabled(false);
} else { } else {
ui->LSendingSuccess->setText( tr( "An error occurred sending the help message. Please raise your arm.\n\n'%1'" ).arg( serverAnswer ) ); ui->LSendingSuccess->setText(tr("An error occurred sending the help"
" message. Please raise your"
" arm.\n\n'%1'").arg(serverAnswer));
} }
} }

@ -9,14 +9,16 @@
namespace Ui { namespace Ui {
class HelpMessageWindow; class HelpMessageWindow;
} } // namespace Ui
class lcHelpMessageWindow : public QMainWindow class lcHelpMessageWindow : public QMainWindow
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit lcHelpMessageWindow( const QString &argServerIP, const unsigned short int &argServerPort, QWidget *argParent = nullptr ); explicit lcHelpMessageWindow(const QString &argServerIP,
const unsigned short int &argServerPort,
QWidget *argParent = nullptr);
~lcHelpMessageWindow(); ~lcHelpMessageWindow();
private: private:

@ -1,11 +1,13 @@
#include "helpmessagewindow.h" #include "helpmessagewindow.h"
#include <QApplication> #include <QApplication>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
QSettings labSettings{"Economic Laboratory", "Labcontrol"}; QSettings labSettings{"Economic Laboratory", "Labcontrol"};
lcHelpMessageWindow w{ labSettings.value( "server_ip", "127.0.0.1" ).toString(), labSettings.value( "client_help_server_port", "0" ).toUInt() }; lcHelpMessageWindow w{labSettings.value("server_ip", "127.0.0.1").toString(),
labSettings.value("client_help_server_port", "0").toUInt()};
w.show(); w.show();
return a.exec(); return a.exec();

Loading…
Cancel
Save