/*! * * Copyright (c) 2020-2024 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file GuiController.cpp * \author (last) Behrouz NematiPour * \date (last) 08-Aug-2023 * \author (original) Behrouz NematiPour * \date (original) 26-Aug-2020 * */ #include "GuiController.h" // Qt #include #include // Project #include "Logger.h" #include "ApplicationController.h" #include "AlarmGenerator.h" #include "MessageAcknowModel.h" // namespace using namespace Gui; /*! * \brief GuiController::GuiController * \details Constructor * \param parent - QObject parent owner object. * Qt handles the children destruction by their parent objects life-cycle. */ GuiController::GuiController(QObject *parent) : QObject(parent) {} /*! * \brief GuiController::init * \details Initialized the Class by calling the init() method first * And initializes the thread vThread by calling initThread * on success init(). * \param vThread - the thread * \return returns the return value of the init() method */ bool GuiController::init(QThread &vThread) { if ( ! init() ) return false; initThread(vThread); return true; } /*! * \brief GuiController::quit * \details quits the class * Calls quitThread */ void GuiController::quit() { quitThread(); // validated } /*! * \brief GuiController::initConnections * \details Initializes the required signal/slot connection between this class and other objects * to be able to communicate. */ void GuiController::initConnections() { // From HD/DG connect(&_ApplicationController, SIGNAL(didActionReceive (GuiActionType, const QVariantList &)), this , SLOT( onActionReceive (GuiActionType, const QVariantList &))); // From OS : USB Drive has been removed physically. // USB drive connect(&_ApplicationController, SIGNAL(didUSBDriveMount ()), this , SLOT( onUSBDriveMount ())); connect(&_ApplicationController, SIGNAL(didUSBDriveRemove()), this , SLOT( onUSBDriveRemove())); connect(&_ApplicationController, SIGNAL(didUSBSpaceChange(bool, qint64, qint64, quint8)), this , SLOT( onUSBSpaceChange(bool, qint64, qint64, quint8))); // SD card connect(&_ApplicationController, SIGNAL(didSDCardStateChange(bool,bool)), this , SLOT( onSDCardStateChange(bool,bool))); connect(&_ApplicationController, SIGNAL(didSDCardSpaceChange(bool, qint64, qint64, quint8)), this , SLOT( onSDCardSpaceChange(bool, qint64, qint64, quint8))); connect(&_ApplicationController, SIGNAL(didSDCardSpaceTooLow(quint8)), this , SLOT( onSDCardSpaceTooLow(quint8))); // Export connect(&_ApplicationController, SIGNAL(didExport()), this , SLOT( onExport())); connect(&_ApplicationController, SIGNAL(didExportStat (quint32, const QString &, quint8)), this , SLOT( onExportStat (quint32, const QString &, quint8))); // transmission fail connect(&_ApplicationController, SIGNAL(didFailedTransmit(Sequence)), this , SLOT( onFailedTransmit(Sequence))); // POST result connect(&_ApplicationController, SIGNAL(didPOSTPass(bool)), this , SLOT( onPOSTPass(bool))); // Device Signal/Slots DEVICE_GUI_INIT_CONNECTIONS_LIST // ---- Signal/Slots ACTION_RECEIVE_MODEL_BRIDGE_CONNECTIONS(_ApplicationController) } /*! * \brief ApplicationController::initThread * \details Moves this object into the thread vThread. * And checks that this method is called from main thread. * Also connects quitThread to application aboutToQuit. * \param vThread - the thread */ void GuiController::initThread(QThread &vThread) { // runs in main thread Q_ASSERT_X(QThread::currentThread() == qApp->thread() , __func__, "The Class initialization must be done in Main Thread" ); _thread = &vThread; _thread->setObjectName(QString("%1_Thread").arg(metaObject()->className())); connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(quit())); moveToThread(_thread); _thread->start(); } /*! * \brief GuiController::quitThread * \details Moves this object to main thread to be handled by QApplication * And to be destroyed there. */ void GuiController::quitThread() { if ( ! _thread ) return; // runs in thread moveToThread(qApp->thread()); // validated } /*! * \brief GuiController initializer */ bool GuiController::init() { if ( _init ) return false; _init = true; initConnections(); LOG_DEBUG(QString("%1 Initialized").arg(metaObject()->className())); return true; } /*! * \brief An Action has been requested * \details This method Confirmed that if the action is accepted or not, * Regarding the current state and the action. * These actions are only user actions and there is only one user interaction, * So no need to capture from which screen this action comes since we have the current state. * Sometimes GuiController requires to investigate with the ApplicationController to get approval from HD device. * \param vAction - User requested Action * \param vData - Message data to be transmitted. */ void GuiController::doActionTransmit(GuiActionType vAction, const QVariantList &vData) { // This is a sample code and currently does nothing // The handleTransmit is a place holder and currently has not been used. if (! handleTransmit(vAction, vData)) { emit didActionTransmit(vAction, vData); } } /*! * \brief GuiController::handleTransmit * \details If an action request from Gui can be handled in Gui Controller * without passing to HD, then can be handled here. * \param vAction - the Requested action * \param vData - Data of the action * \return if handled returns true to not to pass to the lower level (Application Controller) * to not to send to HD then. */ bool GuiController::handleTransmit(GuiActionType vAction, const QVariantList &vData) { // This is a sample code and currently does nothing Q_UNUSED(vAction) Q_UNUSED(vData) // This is an example implementation of how to handle actions // which does not require HD approval in GuiController // Process the GuiView Request. // It can be processed in GuiController take action and notify GuiView // AUTOGEN ISSUE /* switch (vAction) { case GuiActionType::ID_PowerOff: //qApp->quit(); // GUI Controller decides (loop back) //if (vData == GuiActionData::NoData){ // // PowerOff noData is a request // emit didActionReceive (vAction, GuiActionData::Accepted); // return true; //} break; //case Another_Command_Which_does not_Require_HD_Approval: //return true; //break; default: break; } */ return false; } /*! * \brief Action commanded by HD * \details An action has been commanded by HD, * Gui requires to be notified to perform the action. * \param vAction - Message Action * \param vData - The data to be translated. */ void GuiController::onActionReceive (GuiActionType vAction, const QVariantList &vData) { // Process the command and notify GuiView // Process ... emit didActionReceive (vAction, vData); } /*! * \brief GuiController::onUSBDriveMount * \details emits didUSBDriveMount signal to notify other classes (GuiView) * , the USB drive has been mounted. */ void GuiController::onUSBDriveMount() { // This needs user interaction to plug-in USB device emit didUSBDriveMount(); } /*! * \brief GuiController::doUSBDriveUmount * \details emits didUSBDriveUmount signal to notify other classes (GuiView) * , the USB drive has been unmounted. */ void GuiController::doUSBDriveUmount() { // This needs user interaction to plug-out the USB device emit didUSBDriveUmount(); } /*! * \brief GuiController::onUSBDriveRemove * \details emits didUSBDriveRemove signal to notify other classes (GuiView) * , the USB drive has been removed. */ void GuiController::onUSBDriveRemove() { emit didUSBDriveRemove(); } void GuiController::onUSBSpaceChange(bool vReady, qint64 vTotal, qint64 vAvailable, quint8 vPercent) { //DEBUG:0: qDebug() << "GuiController::onUSBSpaceChange" << vReady << vTotal << vAvailable << vPercent; emit didUSBSpaceChange(vReady, vTotal, vAvailable, vPercent); } /*! * \brief GuiController::onSDCardStateChange * \details emits didSDCardStateChange signal to notify other classes (GuiView) * , the SD Card state has been changed. * \param vIsReady - SdCard is Ready * \param vIsReadOnly - SdCard is ReadOnly */ void GuiController::onSDCardStateChange(bool vIsReady, bool vIsReadOnly) { //DEBUG:0: qDebug() << " ***** GuiController " << Storage::SDCard_Base_Path_Name << vIsReady << vIsReadOnly << gDisableSDCFailLogStop; emit didSDCardStateChange(vIsReady, vIsReadOnly); } void GuiController::onSDCardSpaceChange(bool vReady, qint64 vTotal, qint64 vAvailable, quint8 vPercent) { emit didSDCardSpaceChange(vReady, vTotal, vAvailable, vPercent); } /*! * \brief GuiController::onSDCardSpaceTooLow * \details The handler slot for the didSDCardSpaceTooLow signal comes form ApplicationController. * \param vAvailablePercent - the minimum limit of available storage space */ void GuiController::onSDCardSpaceTooLow(quint8 vAvailablePercent) { // This needs to fill up the SD-Card and test with human interactions. emit didSDCardSpaceTooLow(vAvailablePercent); } /*! * \brief GuiController::onExport * \details The slot which will be called to notify the export is done * by emitting the didExport signal. */ void GuiController::onExport() { emit didExport(); } void GuiController::onExportStat(quint32 vIndex, const QString &vFileName, quint8 vPercent) { //DEBUG: qDebug() << "2" << vIndex << vFileName << vPercent; emit didExportStat(vIndex, vFileName, vPercent); } /*! * \brief GuiController::doExportLog * \details emits didExportLog signal to notify other classes (ApplicationController) * , the User requested to export the log. */ void GuiController::doExportLog(const GuiStringIndexMap &vExportList) { emit didExportLog(vExportList); } /*! * \brief GuiController::doExportService * \details emits didExportService signal to notify other classes (ApplicationController) * , the User requested to export the log. */ void GuiController::doExportService(const GuiStringIndexMap &vExportList) { emit didExportService(vExportList); } /*! * \brief GuiController::doExportTreatment * \details emits didExportTreatment signal to notify other classes (ApplicationController) * , the User requested to export the log. */ void GuiController::doExportTreatment(const GuiStringIndexMap &vExportList) { emit didExportTreatment(vExportList); } /*! * \brief GuiController::didFailedTransmit * \details Raises an HD communication timeout alarm if communication with HD is lost. * \param seq - Sequence that failed to transmit */ void GuiController::onFailedTransmit(Sequence) { AlarmStatusData data = AlarmGenerator::ALARM_ID_TD_COMM_TIMEOUT(); LOG_APPED_MSG(GuiActions::ID_AlarmStatusData, AlarmGenerator::toString()); //DEBUG qDebug() << " A" << _MessageAcknowModel.count(); emit didActionReceive(data); } /*! * \brief GuiController::onPOSTPass * \details Passes on the Post status * \param vPass - True if passed */ void GuiController::onPOSTPass(bool vPass) { emit didPOSTPass(vPass); } /*! * \brief GuiController::doQuitApplication * \details emit the didQuitApplication signal to ask ApplicationController to Quit the application gracefully. */ void GuiController::doQuitApplication() { emit didQuitApplication(); } /*! * \brief GuiController::doTreatmentRangesDone * \details emit the didTreatmentRangesDone signal to ask ApplicationController to emit alarm that settings are bad */ void GuiController::doTreatmentRangesDone(bool vPass) { emit didTreatmentRangesDone(vPass); }