From d663ec6aeb02be3368e18b839a9889a523e3e950 Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 17 Mar 2018 12:39:15 +0100 Subject: [PATCH] Got entirely rid of global variables --- src/Lib/client.cpp | 12 ++++++------ src/Lib/client.h | 3 +++ src/Lib/clienthelpnotificationserver.cpp | 2 -- src/Lib/lablib.h | 3 +-- src/Lib/receipts_handler.cpp | 2 -- src/Lib/session.cpp | 2 -- src/Lib/settings.cpp | 12 +++++++----- src/Lib/settings.h | 8 ++++---- src/Lib/ztree.cpp | 2 -- src/localzleafstarter.cpp | 2 -- src/main.cpp | 9 ++------- src/manualprintingsetup.cpp | 2 -- 12 files changed, 23 insertions(+), 36 deletions(-) diff --git a/src/Lib/client.cpp b/src/Lib/client.cpp index bc83671..2adf1b6 100644 --- a/src/Lib/client.cpp +++ b/src/Lib/client.cpp @@ -18,14 +18,13 @@ */ #include "clientpinger.h" -#include "lablib.h" +#include "settings.h" -#include - -extern std::unique_ptr settings; -extern std::unique_ptr lablib; +#include +#include lc::Client::Client(const QString &argIP, const QString &argMAC, const QString &argName, + const Settings *const argSettings, const unsigned short int argXPosition, const unsigned short int argYPosition, const QString &argPingCmd): @@ -34,7 +33,8 @@ lc::Client::Client(const QString &argIP, const QString &argMAC, const QString &a name{argName}, xPosition{argXPosition}, yPosition{argYPosition}, - protectedCycles{0} + protectedCycles{0}, + settings{argSettings} { qRegisterMetaType("state_t"); diff --git a/src/Lib/client.h b/src/Lib/client.h index d4b0278..2cc9228 100644 --- a/src/Lib/client.h +++ b/src/Lib/client.h @@ -29,6 +29,7 @@ class QTimer; namespace lc { class ClientPinger; +class Settings; //! Class which represents the clients in the lab /*! @@ -58,6 +59,7 @@ public: * \param argYPosition The client's y coordinate in the lab's grid */ Client(const QString &argIP, const QString &argMAC, const QString &argName, + const Settings *const argSettings, const unsigned short int argXPosition, const unsigned short int argYPosition, const QString &argPingCmd); @@ -148,6 +150,7 @@ private: unsigned short int protectedCycles; ClientPinger *pinger = nullptr; QThread pingerThread; + const Settings *const settings = nullptr; state_t state = state_t::UNINITIALIZED; //! QTimer used to trigger pings by pinger's ClientPinger instance QTimer *pingTimer = nullptr; diff --git a/src/Lib/clienthelpnotificationserver.cpp b/src/Lib/clienthelpnotificationserver.cpp index d18b15e..b6d5eed 100644 --- a/src/Lib/clienthelpnotificationserver.cpp +++ b/src/Lib/clienthelpnotificationserver.cpp @@ -26,8 +26,6 @@ #include #include -#include - lc::ClientHelpNotificationServer::ClientHelpNotificationServer(const Settings *const argSettings, QObject *argParent) : QObject{argParent}, diff --git a/src/Lib/lablib.h b/src/Lib/lablib.h index fd0070f..1f6efe2 100644 --- a/src/Lib/lablib.h +++ b/src/Lib/lablib.h @@ -21,7 +21,6 @@ #define LABLIB_H #include -#include #include #include @@ -130,7 +129,7 @@ private: QThread netstatThread; //! A timer for regular execution of the NetstatAgent instance's request mechanism QTimer *netstatTimer = nullptr; - QVector< quint16 > occupiedPorts; + QVector occupiedPorts; //! A derivation from QAbstractTableModel used to store the single Session instances SessionsModel *sessionsModel = nullptr; Settings *const settings = nullptr; diff --git a/src/Lib/receipts_handler.cpp b/src/Lib/receipts_handler.cpp index 0a7daf2..ebd5676 100644 --- a/src/Lib/receipts_handler.cpp +++ b/src/Lib/receipts_handler.cpp @@ -17,8 +17,6 @@ * along with Labcontrol. If not, see . */ -#include - #include #include "receipts_handler.h" diff --git a/src/Lib/session.cpp b/src/Lib/session.cpp index 4006ccf..2838b02 100644 --- a/src/Lib/session.cpp +++ b/src/Lib/session.cpp @@ -22,8 +22,6 @@ #include -#include - lc::Session::Session(QVector &&argAssocClients, const QString &argZTreeDataTargetPath, const quint16 argZTreePort, const QString &argZTreeVersionPath, bool argPrintReceiptsForLocalClients, diff --git a/src/Lib/settings.cpp b/src/Lib/settings.cpp index 12fdd64..4e0a9e0 100644 --- a/src/Lib/settings.cpp +++ b/src/Lib/settings.cpp @@ -134,12 +134,12 @@ lc::Settings::Settings(const QSettings &argSettings, QObject *argParent) : installedZTreeVersions{DetectInstalledzTreeVersions()}, clientHelpNotificationServerPort{GetClientHelpNotificationServerPort(argSettings)}, chosenzTreePort{GetInitialPort(argSettings)}, - clients{CreateClients(argSettings, pingCmd)}, localzLeafName{ReadSettingsItem("local_zLeaf_name", "The local zLeaf default name will default to 'local'.", - argSettings, false)}, - clIPsToClMap{CreateClIPsToClMap(clients)} + argSettings, false)} { + clients = CreateClients(argSettings, pingCmd); + clIPsToClMap = CreateClIPsToClMap(clients); // Let the local zLeaf name default to 'local' if none was given in the settings if (localzLeafName.isEmpty()) { qDebug() << "'local_zLeaf_name' was not set, defaulting to 'local'"; @@ -241,9 +241,11 @@ QVector lc::Settings::CreateClients(const QSettings &argSettings, qDebug() << "clientYPositions:" << clientYPositions.join(" / "); for (int i = 0; i < clientQuantity; i++) { - tempClientVec.append(new Client{clientIPs[i], clientMACs[i], clientNames[i], + tempClientVec.append(new Client{clientIPs[i], clientMACs[i], + clientNames[i], this, clientXPositions[i].toUShort(), - clientYPositions[i].toUShort(), argPingCmd}); + clientYPositions[i].toUShort(), + argPingCmd}); } return tempClientVec; diff --git a/src/Lib/settings.h b/src/Lib/settings.h index f34f697..2688d84 100644 --- a/src/Lib/settings.h +++ b/src/Lib/settings.h @@ -103,8 +103,8 @@ private: static bool CheckPathAndComplain(const QString &argPath, const QString &argVariableName, const QString &argMessage); - static QVector CreateClients(const QSettings &argSettings, - const QString &argPingCmd); + QVector CreateClients(const QSettings &argSettings, + const QString &argPingCmd); static QMap CreateClIPsToClMap(const QVector &argClients); QStringList DetectInstalledLaTeXHeaders() const; QStringList DetectInstalledzTreeVersions() const; @@ -119,11 +119,11 @@ private: bool argItemIsFile); int chosenzTreePort = 0; - QVector< Client * > clients; + QVector clients; QString localzLeafName; public: - const QMap clIPsToClMap; + QMap clIPsToClMap; }; } // namespace lc diff --git a/src/Lib/ztree.cpp b/src/Lib/ztree.cpp index 9369e99..5581a34 100644 --- a/src/Lib/ztree.cpp +++ b/src/Lib/ztree.cpp @@ -23,8 +23,6 @@ #include #include -#include - lc::ZTree::ZTree(const Settings *const argSettings, const QString &argZTreeDataTargetPath, const int argZTreePort, const QString &argZTreeVersionPath, QObject *argParent) : diff --git a/src/localzleafstarter.cpp b/src/localzleafstarter.cpp index bd8dc6c..cf8fc57 100644 --- a/src/localzleafstarter.cpp +++ b/src/localzleafstarter.cpp @@ -23,8 +23,6 @@ #include -#include - lc::LocalzLeafStarter::LocalzLeafStarter(Settings *const argSettings, QWidget *argParent) : QWidget{argParent}, diff --git a/src/main.cpp b/src/main.cpp index e00d6db..e9f9fa9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,16 +22,11 @@ #include -#include - -std::unique_ptr settings; - int main(int argc, char *argv[]) { QApplication a{argc, argv}; - settings = std::make_unique(QSettings{"Labcontrol", - "Labcontrol"}); - lc::MainWindow w{settings.get()}; + lc::Settings settings{QSettings{"Labcontrol", "Labcontrol"}}; + lc::MainWindow w{&settings}; w.show(); return a.exec(); diff --git a/src/manualprintingsetup.cpp b/src/manualprintingsetup.cpp index f143199..a560a72 100644 --- a/src/manualprintingsetup.cpp +++ b/src/manualprintingsetup.cpp @@ -24,8 +24,6 @@ #include #include -#include - lc::ManualPrintingSetup::ManualPrintingSetup(const Settings *const argSettings, QWidget *argParent) : QWidget{argParent},